]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
Fix ITs
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 13 Aug 2015 13:12:44 +0000 (15:12 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 13 Aug 2015 13:21:42 +0000 (15:21 +0200)
it/pom.xml
it/projects/java-bytecode/sonar-project.properties
it/src/test/java/com/sonar/runner/it/CacheTest.java
it/src/test/java/com/sonar/runner/it/JavaTest.java
it/src/test/java/com/sonar/runner/it/RunnerTestCase.java

index 1f2bbdd4046de856068d66e13ea1ee591ab614b7..26eb073b26ceed9c049b2020ad791f07503c820b 100644 (file)
@@ -22,7 +22,7 @@
   </organization>
 
   <properties>
-    <sonar.buildVersion>3.7</sonar.buildVersion>
+    <sonar.buildVersion>5.0</sonar.buildVersion>
     <surefire.argLine>-server</surefire.argLine>
     <!-- following properties must be set in command-line : sonar.runtimeVersion and sonarRunner.version -->
   </properties>
       <version>1.4</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+               <groupId>org.assertj</groupId>
+               <artifactId>assertj-core</artifactId>
+               <version>2.1.0</version>
+       </dependency>
   </dependencies>
 
   <build>
index cf6eb18ae7aa357197a7dd665f47c8e6bda4496f..3d60e924979938bfcbed0151afcc9861815a50b1 100644 (file)
@@ -2,7 +2,6 @@
 sonar.projectKey=java:bytecode
 sonar.projectName=Java Bytecode Sample
 sonar.projectVersion=1.0
-sonar.profile=With Findbugs
 
 sonar.sources=src
 sonar.binaries=build/classes
index 788b82c151f389fb123e272495169055ad9fa33f..d4c82ecd63bc21bcc00b92296253dcff4de5423b 100644 (file)
  */
 package com.sonar.runner.it;
 
+import org.sonar.wsclient.issue.Issue;
+import org.sonar.wsclient.issue.IssueQuery;
+import org.junit.BeforeClass;
+import org.junit.rules.TemporaryFolder;
+import org.junit.Rule;
 import com.sonar.orchestrator.build.BuildFailureException;
-
 import com.sonar.orchestrator.locator.ResourceLocation;
 import com.sonar.orchestrator.build.BuildResult;
 import com.sonar.orchestrator.build.SonarRunner;
 import org.junit.Test;
 
 import java.io.File;
+import java.io.IOException;
+import java.util.List;
 
-import static org.junit.Assert.*;
-import static org.junit.Assume.assumeTrue;
+import static org.assertj.core.api.Assertions.assertThat;
 
 public class CacheTest extends RunnerTestCase {
-  @Test
-  public void testOffline() {
-    assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.2"));
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  private File currentTemp = null;
+  private static boolean serverRunning = false;
+
+  @BeforeClass
+  public static void setUpClass() {
+    System.out.println("SETTING UP");
     orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml"));
+    orchestrator.getServer().provisionProject("java:sample", "Java Sample, with comma");
+    orchestrator.getServer().associateProjectToQualityProfile("java:sample", "java", "sonar-way");
+    serverRunning = true;
+  }
+
+  private static void ensureStarted() {
+    if (!serverRunning) {
+      orchestrator.start();
+      serverRunning = true;
+    }
+  }
+
+  private static void ensureStopped() {
+    if (serverRunning) {
+      orchestrator.stop();
+      serverRunning = false;
+    }
+  }
+
+  @Test
+  public void testIssuesMode() throws IOException {
+    // online, without cache -> should sync
+    ensureStarted();
+    SonarRunner build = createRunner("issues", true);
+    BuildResult result = orchestrator.executeBuild(build, false);
+    assertThat(result.isSuccess()).isTrue();
+
+    // offline, with cache -> should run from cache
+    ensureStopped();
+    build = createRunner("issues", false);
+    result = orchestrator.executeBuild(build, false);
+    assertThat(result.isSuccess()).isTrue();
 
-    SonarRunner build = createRunner(true);
-    BuildResult result = orchestrator.executeBuild(build);
-    stopServer();
+    // offline, without cache -> should fail
+    build = createRunner("issues", true);
+    try {
+      result = orchestrator.executeBuild(build);
+    } catch (BuildFailureException e) {
+      assertThat(e.getResult().getLogs()).contains("Server is not accessible and data is not cached");
+    }
+  }
 
-    build = createRunner(false);
+  @Test
+  public void testPublishModeOffline() throws IOException {
+    // online (cache not used)
+    ensureStarted();
+    SonarRunner build = createRunner("publish");
+    BuildResult result = orchestrator.executeBuild(build, false);
+    assertThat(result.isSuccess()).isTrue();
+    getIssues();
+    
+    // offline (cache not used) -> should fail
+    ensureStopped();
+    build = createRunner("publish", false);
     try {
-      result = orchestrator.executeBuild(build, false);
+      result = orchestrator.executeBuild(build);
     } catch (BuildFailureException e) {
-      // expected
+      assertThat(e.getResult().getLogs()).contains("Fail to download libraries from server");
     }
 
-    build = createRunner(true);
-    result = orchestrator.executeBuild(build, false);
-    assertTrue(result.isSuccess());
   }
 
-  private SonarRunner createRunner(boolean enableOffline) {
-    SonarRunner runner = newRunner(new File("projects/java-sample"))
-      .setProperty("sonar.analysis.mode", "preview")
-      .setProfile("sonar-way");
+  private void getIssues() {
+    List<Issue> issues = orchestrator.getServer().wsClient().issueClient()
+      .find(IssueQuery.create()).list();
+    System.out.println(issues.size());
+
+  }
 
-    if (enableOffline) {
-      runner.setProperty("sonar.enableOffline", "true");
+  private SonarRunner createRunner(String mode) throws IOException {
+    return createRunner(mode, false);
+  }
+
+  private SonarRunner createRunner(String mode, boolean refreshCache) throws IOException {
+    if (refreshCache || currentTemp == null) {
+      currentTemp = temp.newFolder();
     }
 
+    SonarRunner runner = newRunner(new File("projects/java-sample"))
+      .setProperty("sonar.analysis.mode", mode)
+      .setProperty("sonar.userHome", currentTemp.getAbsolutePath());
+
     return runner;
   }
 
index 23cc3a4b4ef126c5ade6f57c6c922e5a10a9d964..f5ec9e01f03cbbc2237e27102a7cebc738b99c27 100644 (file)
@@ -48,11 +48,12 @@ public class JavaTest extends RunnerTestCase {
   @Test
   public void scan_java_sources() {
     orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml"));
+    orchestrator.getServer().provisionProject("java:sample", "Java Sample, with comma");
+    orchestrator.getServer().associateProjectToQualityProfile("java:sample", "java", "sonar-way");
 
     SonarRunner build = newRunner(new File("projects/java-sample"))
       .setProperty("sonar.verbose", "true")
-      .addArguments("-e", "-X")
-      .setProfile("sonar-way");
+      .addArguments("-e", "-X");
     // SONARPLUGINS-3061
     // Add a trailing slash
     build.setProperty("sonar.host.url", orchestrator.getServer().getUrl() + "/");
@@ -93,7 +94,10 @@ public class JavaTest extends RunnerTestCase {
   @Test
   public void scan_java_sources_and_bytecode() {
     orchestrator.getServer().restoreProfile(ResourceLocation.create("/requires-bytecode-profile.xml"));
-    SonarRunner build = newRunner(new File("projects/java-bytecode")).setProfile("requires-bytecode");
+    orchestrator.getServer().provisionProject("java:bytecode", "Java Bytecode Sample");
+    orchestrator.getServer().associateProjectToQualityProfile("java:bytecode", "java", "requires-bytecode");
+
+    SonarRunner build = newRunner(new File("projects/java-bytecode"));
     orchestrator.executeBuild(build);
 
     Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:bytecode").setMetrics("lcom4", "violations"));
@@ -122,7 +126,10 @@ public class JavaTest extends RunnerTestCase {
   @Test
   public void basedir_contains_java_sources() {
     orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml"));
-    SonarRunner build = newRunner(new File("projects/basedir-with-source")).setProfile("sonar-way");
+    orchestrator.getServer().provisionProject("java:basedir-with-source", "Basedir with source");
+    orchestrator.getServer().associateProjectToQualityProfile("java:basedir-with-source", "java", "sonar-way");
+
+    SonarRunner build = newRunner(new File("projects/basedir-with-source"));
     orchestrator.executeBuild(build);
 
     Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:basedir-with-source").setMetrics("files", "ncloc"));
@@ -136,9 +143,11 @@ public class JavaTest extends RunnerTestCase {
   @Test
   public void should_support_simple_project_keys() {
     orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml"));
+    orchestrator.getServer().provisionProject("SAMPLE", "Java Sample, with comma");
+    orchestrator.getServer().associateProjectToQualityProfile("SAMPLE", "java", "sonar-way");
+
     SonarRunner build = newRunner(new File("projects/java-sample"))
-      .setProjectKey("SAMPLE")
-      .setProfile("sonar-way");
+      .setProjectKey("SAMPLE");
     orchestrator.executeBuild(build);
 
     Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("SAMPLE").setMetrics("files", "ncloc"));
index bd308c4932c542e8630142d4d7d216fdd7e5ed12..7323a55b801d022ecaa74c72b92b84cfd66aa927 100644 (file)
@@ -57,6 +57,7 @@ public abstract class RunnerTestCase {
   public static void stopServer() {
     if (orchestrator != null) {
       orchestrator.stop();
+      orchestrator = null;
     }
   }