diff options
Diffstat (limited to 'sonar-batch/src/test')
8 files changed, 386 insertions, 127 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/LifecycleProviderAdapterTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/LifecycleProviderAdapterTest.java new file mode 100644 index 00000000000..5c3bb85a109 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/LifecycleProviderAdapterTest.java @@ -0,0 +1,79 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.bootstrap; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Before; +import org.picocontainer.Startable; +import org.junit.Test; + +public class LifecycleProviderAdapterTest { + private DummyProvider provider; + + @Before + public void setUp() { + provider = new DummyProvider(); + provider.provide(); + } + + @Test + public void testStart() { + // ComponentLifecycle's start gets called on the provider + provider.start(null); + assertThat(provider.inst.started).isEqualTo(true); + assertThat(provider.isStarted()).isEqualTo(true); + assertThat(provider.inst.stopped).isEqualTo(false); + } + + @Test + public void testSop() { + // ComponentLifecycle's stop gets called on the provider + provider.stop(null); + assertThat(provider.inst.stopped).isEqualTo(true); + assertThat(provider.isStarted()).isEqualTo(false); + assertThat(provider.inst.started).isEqualTo(false); + } + + public class DummyProvided implements Startable { + boolean started = false; + boolean stopped = false; + + @Override + public void start() { + started = true; + } + + @Override + public void stop() { + stopped = true; + } + } + + public class DummyProvider extends LifecycleProviderAdapter { + DummyProvided inst; + + public DummyProvided provide() { + inst = new DummyProvided(); + super.instance = inst; + return inst; + } + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PersistentCacheProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PersistentCacheProviderTest.java index 5b6db572df5..24bdf6047d5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PersistentCacheProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PersistentCacheProviderTest.java @@ -19,24 +19,20 @@ */ package org.sonar.batch.bootstrap; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.junit.Before; - -import static org.mockito.Mockito.when; +import java.util.Collections; +import org.junit.Before; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; public class PersistentCacheProviderTest { private PersistentCacheProvider provider = null; - @Mock private BootstrapProperties props = null; @Before public void prepare() { - MockitoAnnotations.initMocks(this); + props = new BootstrapProperties(Collections.<String, String>emptyMap()); provider = new PersistentCacheProvider(); } @@ -55,7 +51,7 @@ public class PersistentCacheProviderTest { // normally force update (cache disabled) assertThat(provider.provide(props).isForceUpdate()).isTrue(); - when(props.property("sonar.enableHttpCache")).thenReturn("true"); + props.properties().put("sonar.enableHttpCache", "true"); provider = new PersistentCacheProvider(); assertThat(provider.provide(props).isForceUpdate()).isFalse(); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectTempFolderProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectTempFolderProviderTest.java index e79f0516159..2b7a0435240 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectTempFolderProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectTempFolderProviderTest.java @@ -19,9 +19,9 @@ */ package org.sonar.batch.bootstrap; -import org.apache.commons.io.FileUtils; +import org.sonar.api.utils.TempFolder; -import org.sonar.api.utils.ProjectTempFolder; +import org.apache.commons.io.FileUtils; import com.google.common.collect.ImmutableMap; import org.junit.Rule; import org.junit.Test; @@ -46,7 +46,7 @@ public class ProjectTempFolderProviderTest { File workingDir = temp.newFolder(); File tmpDir = new File(workingDir, ProjectTempFolderProvider.TMP_NAME); - ProjectTempFolder tempFolder = tempFolderProvider.provide(new BootstrapProperties(ImmutableMap.of(CoreProperties.WORKING_DIRECTORY, workingDir.getAbsolutePath()))); + TempFolder tempFolder = tempFolderProvider.provide(new BootstrapProperties(ImmutableMap.of(CoreProperties.WORKING_DIRECTORY, workingDir.getAbsolutePath()))); tempFolder.newDir(); tempFolder.newFile(); assertThat(tmpDir).exists(); @@ -58,7 +58,7 @@ public class ProjectTempFolderProviderTest { File defaultDir = new File(CoreProperties.WORKING_DIRECTORY_DEFAULT_VALUE, ProjectTempFolderProvider.TMP_NAME); try { - ProjectTempFolder tempFolder = tempFolderProvider.provide(new BootstrapProperties(Collections.<String, String>emptyMap())); + TempFolder tempFolder = tempFolderProvider.provide(new BootstrapProperties(Collections.<String, String>emptyMap())); tempFolder.newDir(); tempFolder.newFile(); assertThat(defaultDir).exists(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java index ce64074d9a6..8c1e23b7b56 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java @@ -25,7 +25,12 @@ import com.google.common.collect.ImmutableMap; import org.sonar.api.CoreProperties; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.attribute.BasicFileAttributeView; +import java.nio.file.attribute.FileTime; import java.util.Collections; +import java.util.concurrent.TimeUnit; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Rule; @@ -40,42 +45,72 @@ public class TempFolderProviderTest { @Test public void createTempFolderProps() throws Exception { - File workingDir = temp.newFolder(); + File workingDir = temp.getRoot(); TempFolder tempFolder = tempFolderProvider.provide(new BootstrapProperties(ImmutableMap.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, workingDir.getAbsolutePath()))); tempFolder.newDir(); tempFolder.newFile(); - assertThat(new File(workingDir, TempFolderProvider.TMP_NAME)).exists(); - assertThat(new File(workingDir, ".sonartmp").list()).hasSize(2); + assertThat(getCreatedTempDir(workingDir)).exists(); + assertThat(getCreatedTempDir(workingDir).list()).hasSize(2); + } + + @Test + public void cleanUpOld() throws IOException { + long creationTime = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(100); + File workingDir = temp.getRoot(); + + for (int i = 0; i < 3; i++) { + File tmp = new File(workingDir, ".sonartmp_" + i); + tmp.mkdirs(); + setFileCreationDate(tmp, creationTime); + } + + tempFolderProvider.provide(new BootstrapProperties(ImmutableMap.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, workingDir.getAbsolutePath()))); + // this also checks that all other temps were deleted + assertThat(getCreatedTempDir(workingDir)).exists(); } @Test public void createTempFolderSonarHome() throws Exception { // with sonar home, it will be in {sonar.home}/.sonartmp - File sonarHome = temp.newFolder(); - File tmpDir = new File(new File(sonarHome, CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE), TempFolderProvider.TMP_NAME); + File sonarHome = temp.getRoot(); + File workingDir = new File(sonarHome, CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE); TempFolder tempFolder = tempFolderProvider.provide(new BootstrapProperties(ImmutableMap.of("sonar.userHome", sonarHome.getAbsolutePath()))); tempFolder.newDir(); tempFolder.newFile(); - assertThat(tmpDir).exists(); - assertThat(tmpDir.list()).hasSize(2); + assertThat(getCreatedTempDir(workingDir)).exists(); + assertThat(getCreatedTempDir(workingDir).list()).hasSize(2); } @Test public void createTempFolderDefault() throws Exception { + File userHome = temp.getRoot(); + System.setProperty("user.home", userHome.getAbsolutePath()); + // if nothing is defined, it will be in {user.home}/.sonar/.sonartmp File defaultSonarHome = new File(System.getProperty("user.home"), ".sonar"); - File tmpDir = new File(new File(defaultSonarHome, CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE), TempFolderProvider.TMP_NAME); + File workingDir = new File(defaultSonarHome, CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE).getAbsoluteFile(); try { TempFolder tempFolder = tempFolderProvider.provide(new BootstrapProperties(Collections.<String, String>emptyMap())); tempFolder.newDir(); tempFolder.newFile(); - assertThat(tmpDir).exists(); - assertThat(tmpDir.list()).hasSize(2); + assertThat(getCreatedTempDir(workingDir)).exists(); + assertThat(getCreatedTempDir(workingDir).list()).hasSize(2); } finally { - FileUtils.deleteDirectory(tmpDir); + FileUtils.deleteDirectory(getCreatedTempDir(workingDir)); } } + + private File getCreatedTempDir(File workingDir) { + assertThat(workingDir.listFiles()).hasSize(1); + return workingDir.listFiles()[0]; + } + + private void setFileCreationDate(File f, long time) throws IOException { + BasicFileAttributeView attributes = Files.getFileAttributeView(f.toPath(), BasicFileAttributeView.class); + FileTime creationTime = FileTime.fromMillis(time); + attributes.setTimes(creationTime, creationTime, creationTime); + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/LogCallbackAppenderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/LogCallbackAppenderTest.java new file mode 100644 index 00000000000..ea1fd3a470e --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/LogCallbackAppenderTest.java @@ -0,0 +1,66 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.bootstrapper; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import org.junit.Test; +import org.sonar.home.log.LogListener; +import org.junit.Before; + +public class LogCallbackAppenderTest { + private LogListener listener; + private LogCallbackAppender appender; + private ILoggingEvent event; + + @Before + public void setUp() { + listener = mock(LogListener.class); + appender = new LogCallbackAppender(listener); + event = mock(ILoggingEvent.class); + when(event.getMessage()).thenReturn("test"); + when(event.getLevel()).thenReturn(Level.INFO); + } + + @Test + public void testAppendLog() { + + appender.append(event); + + verify(event).getMessage(); + verify(event).getLevel(); + + verify(listener).log("test", LogListener.Level.INFO); + + verifyNoMoreInteractions(event, listener); + } + + @Test + public void testChangeTarget() { + listener = mock(LogListener.class); + appender.setTarget(listener); + testAppendLog(); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/PrintStreamTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/PrintStreamTest.java deleted file mode 100644 index da833eae508..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/PrintStreamTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrapper; - -import ch.qos.logback.core.encoder.EchoEncoder; -import ch.qos.logback.classic.spi.ILoggingEvent; -import org.mockito.Matchers; -import ch.qos.logback.core.encoder.Encoder; -import ch.qos.logback.core.Context; -import org.junit.Test; -import org.junit.Before; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; - -import static org.assertj.core.api.Assertions.assertThat; - -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.mock; - -public class PrintStreamTest { - private static final String TEST_STR = "foo"; - - private ByteArrayOutputStream os; - private PrintStream stream; - private PrintStreamAppender<ILoggingEvent> appender; - private Context context = mock(Context.class); - - private Encoder<ILoggingEvent> encoder = mock(Encoder.class); - private ILoggingEvent event = mock(ILoggingEvent.class); - - @Before - public void setUp() { - os = new ByteArrayOutputStream(); - stream = new PrintStream(os); - - appender = new PrintStreamAppender<ILoggingEvent>(stream); - when(event.getMessage()).thenReturn(TEST_STR); - when(event.toString()).thenReturn(TEST_STR); - } - - @Test - public void testNullStream() { - appender.setContext(mock(Context.class)); - appender.setEncoder(encoder); - appender.setTarget(null); - appender.start(); - appender.doAppend(event); - - verifyNoMoreInteractions(encoder); - } - - @Test - public void testEncoder() throws IOException { - appender.setContext(mock(Context.class)); - appender.setEncoder(encoder); - appender.start(); - appender.doAppend(event); - - verify(encoder, times(1)).init(Matchers.notNull(OutputStream.class)); - verify(encoder, times(1)).doEncode(event); - - } - - @Test - public void testWrite() { - encoder = new EchoEncoder<>(); - encoder.setContext(context); - encoder.start(); - - appender.setContext(mock(Context.class)); - appender.setEncoder(encoder); - appender.setTarget(stream); - appender.start(); - - appender.doAppend(event); - - assertThat(os.toString()).isEqualTo(TEST_STR + System.lineSeparator()); - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/CachesTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/CachesTest.java index 801d0f3d5ed..411435aebec 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/CachesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/CachesTest.java @@ -65,8 +65,6 @@ public class CachesTest extends AbstractCachesTest { public void leak_test() throws PersistitException { caches.stop(); - System.out.println(cachesManager.tempDir()); - int len = 1 * 1024 * 1024; StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/log/LogListenerTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/log/LogListenerTest.java new file mode 100644 index 00000000000..c50f9e20adf --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/log/LogListenerTest.java @@ -0,0 +1,187 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.mediumtest.log; + +import org.junit.BeforeClass; +import org.junit.AfterClass; +import org.sonar.home.log.LogListener; +import org.sonar.home.log.LogListener.Level; +import org.apache.commons.io.FileUtils; +import org.junit.Test; +import com.google.common.collect.ImmutableMap; +import org.junit.After; +import org.junit.Before; +import org.sonar.batch.mediumtest.BatchMediumTester; +import org.sonar.xoo.XooPlugin; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.util.LinkedList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Rule; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; + +public class LogListenerTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private Pattern simpleTimePattern = Pattern.compile("\\d{2}:\\d{2}:\\d{2}"); + private List<LogEvent> logOutput; + private ByteArrayOutputStream stdOutTarget = new ByteArrayOutputStream(); + private ByteArrayOutputStream stdErrTarget = new ByteArrayOutputStream(); + private static PrintStream savedStdOut; + private static PrintStream savedStdErr; + + public BatchMediumTester tester = BatchMediumTester.builder() + .registerPlugin("xoo", new XooPlugin()) + .addDefaultQProfile("xoo", "Sonar Way") + .setLogListener(new SimpleLogListener()) + .build(); + + private File baseDir; + + private ImmutableMap.Builder<String, String> builder; + + @BeforeClass + public static void backupStdStreams() { + savedStdOut = System.out; + savedStdErr = System.err; + } + + @AfterClass + public static void resumeStdStreams() { + if (savedStdOut != null) { + System.setOut(savedStdOut); + } + if (savedStdErr != null) { + System.setErr(savedStdErr); + } + } + + @Before + public void prepare() throws IOException { + System.setOut(new PrintStream(stdOutTarget)); + System.setErr(new PrintStream(stdErrTarget)); + logOutput = new LinkedList<>(); + tester.start(); + + baseDir = temp.newFolder(); + + builder = ImmutableMap.<String, String>builder() + .put("sonar.task", "scan") + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.projectKey", "com.foo.project") + .put("sonar.projectName", "Foo Project") + .put("sonar.projectVersion", "1.0-SNAPSHOT") + .put("sonar.projectDescription", "Description of Foo Project"); + } + + private void assertNoStdOutput() { + assertThat(stdOutTarget.toByteArray()).isEmpty(); + assertThat(stdErrTarget.toByteArray()).isEmpty(); + } + + /** + * Check that log message is not formatted, i.e. has no log level and timestamp. + */ + private void assertMsgClean(String msg) { + for (Level l : Level.values()) { + assertThat(msg).doesNotContain(l.toString()); + } + + Matcher matcher = simpleTimePattern.matcher(msg); + assertThat(matcher.find()).isFalse(); + } + + @After + public void stop() { + tester.stop(); + } + + @Test + public void testNoStdLog() throws IOException { + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + + tester.newTask() + .properties(builder + .put("sonar.sources", "src") + .build()) + .start(); + + assertNoStdOutput(); + assertThat(logOutput).isNotEmpty(); + for (LogEvent e : logOutput) { + savedStdOut.println("[captured]" + e.level + " " + e.msg); + } + } + + @Test + public void testNoFormattedMsgs() throws IOException { + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + + tester.newTask() + .properties(builder + .put("sonar.sources", "src") + .build()) + .start(); + + assertNoStdOutput(); + + for (LogEvent e : logOutput) { + assertMsgClean(e.msg); + savedStdOut.println("[captured]" + e.level + " " + e.msg); + } + } + + private class SimpleLogListener implements LogListener { + @Override + public void log(String msg, Level level) { + logOutput.add(new LogEvent(msg, level)); + } + } + + private static class LogEvent { + String msg; + Level level; + + LogEvent(String msg, Level level) { + this.msg = msg; + this.level = level; + } + } +} |