From: simonbrandhof Date: Wed, 22 Sep 2010 21:02:17 +0000 (+0000) Subject: sonar-plugin-api: use parallel junit tests X-Git-Tag: 2.6~975 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=89c8dcc3b8db2fb2b044b093113a81aced0dabbd;p=sonarqube.git sonar-plugin-api: use parallel junit tests --- diff --git a/sonar-plugin-api/pom.xml b/sonar-plugin-api/pom.xml index 712f58e424f..f4a4bdbed97 100644 --- a/sonar-plugin-api/pom.xml +++ b/sonar-plugin-api/pom.xml @@ -41,6 +41,16 @@ false + + + org.apache.maven.plugins + maven-surefire-plugin + + methods + 3 + true + + diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/ManifestUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/ManifestUtilsTest.java index 3ddbfb8d876..c7a5870e602 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/ManifestUtilsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/ManifestUtilsTest.java @@ -21,13 +21,12 @@ package org.sonar.api.utils; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; -import org.junit.After; -import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.io.OutputStream; import java.net.URLClassLoader; import java.util.List; @@ -42,24 +41,10 @@ import static org.junit.matchers.JUnitMatchers.hasItems; public class ManifestUtilsTest { - private File tempDir; + @Rule + public TemporaryFolder tempDir = new TemporaryFolder(); - @Before - public void before() throws IOException { - tempDir = new File("target/test-tmp/ManifestUtilsTest/"); - FileUtils.forceMkdir(tempDir); - } - - @After - public void tryToCleanDirectory() { - try { - FileUtils.cleanDirectory(tempDir); - } catch (Exception e) { - // fails on windows because URLClassLoader locks jar files - } - } - - @Test + @Test public void emptyManifest() throws Exception { Manifest mf = new Manifest(); File jar = createJar(mf, "emptyManifest.jar"); @@ -99,7 +84,7 @@ public class ManifestUtilsTest { private File createJar(Manifest mf, String name) throws Exception { mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); - File file = new File(tempDir, name); + File file = tempDir.newFile(name); OutputStream out = new JarOutputStream(new FileOutputStream(file), mf); out.flush(); IOUtils.closeQuietly(out); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeProfilerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeProfilerTest.java index fc4c695cf75..a46e941549c 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeProfilerTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeProfilerTest.java @@ -19,75 +19,55 @@ */ package org.sonar.api.utils; -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.core.WriterAppender; -import ch.qos.logback.core.layout.EchoLayout; -import org.apache.commons.lang.StringUtils; +import org.junit.Before; import org.junit.Test; -import org.slf4j.LoggerFactory; +import org.slf4j.Logger; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; -import static org.junit.internal.matchers.StringContains.containsString; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; public class TimeProfilerTest { + private Logger logger; + + @Before + public void before() { + logger = mock(Logger.class); + } + @Test public void testBasicProfiling() { - StringWriter writer = new StringWriter(); - TimeProfiler profiler = new TimeProfiler(mockLogger(writer)); - + TimeProfiler profiler = new TimeProfiler(logger); profiler.start("Cycle analysis"); - assertThat(writer.toString(), containsString("[INFO] Cycle analysis...")); + verify(logger).info("Cycle analysis..."); profiler.stop(); - assertThat(writer.toString(), containsString("[INFO] Cycle analysis done:")); + verify(logger).info(eq("{} done: {} ms"), eq("Cycle analysis"), anyInt()); } @Test - public void stopOnce() throws IOException { - StringWriter writer = new StringWriter(); - TimeProfiler profiler = new TimeProfiler(mockLogger(writer)); + public void stopOnce() { + TimeProfiler profiler = new TimeProfiler(logger); profiler.start("Cycle analysis"); profiler.stop(); profiler.stop(); profiler.stop(); - assertThat(StringUtils.countMatches(writer.toString(), "Cycle analysis done"), is(1)); + verify(logger, times(1)).info(anyString()); // start() executes log() with 1 parameter + verify(logger, times(1)).info(anyString(), anyString(), anyInt()); // stop() executes log() with 3 parameters } @Test - public void doNotLogNeverEndedTask() throws IOException { - StringWriter writer = new StringWriter(); - TimeProfiler profiler = new TimeProfiler(mockLogger(writer)); + public void doNotLogNeverEndedTask() { + TimeProfiler profiler = new TimeProfiler(logger); profiler.start("Cycle analysis"); profiler.start("New task"); profiler.stop(); profiler.stop(); - assertThat(writer.toString(), not(containsString("Cycle analysis done"))); - } - - - private static Logger mockLogger(Writer writer) { - WriterAppender writerAppender = new WriterAppender(); - writerAppender.setLayout(new EchoLayout()); - writerAppender.setWriter(writer); - writerAppender.setImmediateFlush(true); - writerAppender.start(); - - Logger logger = ((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger(TimeProfilerTest.class); - logger.addAppender(writerAppender); - logger.setLevel(Level.INFO); - logger.setAdditive(true); - return logger; - + verify(logger, never()).info(eq("{} done: {} ms"), eq("Cycle analysis"), anyInt()); + verify(logger, times(1)).info(eq("{} done: {} ms"), eq("New task"), anyInt()); } } diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/sample/src/test/java/foo/BarTest.java b/sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/sample/src/test/java/foo/BarTest.java index ecd39bd391f..5d99561dd1c 100644 --- a/sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/sample/src/test/java/foo/BarTest.java +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/sample/src/test/java/foo/BarTest.java @@ -1,4 +1,6 @@ package foo; -public class BarTest { +import org.junit.Ignore; + +public abstract class BarTest { } \ No newline at end of file