]> source.dussan.org Git - sonarqube.git/commitdiff
sonar-plugin-api: use parallel junit tests
authorsimonbrandhof <simon.brandhof@gmail.com>
Wed, 22 Sep 2010 21:02:17 +0000 (21:02 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Wed, 22 Sep 2010 21:02:17 +0000 (21:02 +0000)
sonar-plugin-api/pom.xml
sonar-plugin-api/src/test/java/org/sonar/api/utils/ManifestUtilsTest.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeProfilerTest.java
sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/sample/src/test/java/foo/BarTest.java

index 712f58e424f962093bb84eca1798cbec14d29d3e..f4a4bdbed97779b2941dbbdf278b4bbb47af3a9b 100644 (file)
           <skip>false</skip>
         </configuration>
       </plugin>
+      <!-- Running JUnit tests in parallel -->
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <parallel>methods</parallel>
+          <threadCount>3</threadCount>
+          <perCoreThreadCount>true</perCoreThreadCount>
+        </configuration>
+      </plugin>
     </plugins>
   </build>
   <dependencies>
index 3ddbfb8d876c181c44987e5610a2f015b6284bd0..c7a5870e6021738a32c56f19c9c00cb1d6255b97 100644 (file)
@@ -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);
index fc4c695cf758a4d9e03307441cdbc859a2f9197a..a46e941549ce2bb8c39c5836281626f105348de3 100644 (file)
  */
 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());
   }
 }