From af6943b490207ef3677c136fe11fa18a28d7342d Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 13 Aug 2015 15:12:44 +0200 Subject: [PATCH] Fix ITs --- it/pom.xml | 7 +- .../java-bytecode/sonar-project.properties | 1 - .../java/com/sonar/runner/it/CacheTest.java | 109 ++++++++++++++---- .../java/com/sonar/runner/it/JavaTest.java | 21 +++- .../com/sonar/runner/it/RunnerTestCase.java | 1 + 5 files changed, 110 insertions(+), 29 deletions(-) diff --git a/it/pom.xml b/it/pom.xml index 1f2bbdd..26eb073 100644 --- a/it/pom.xml +++ b/it/pom.xml @@ -22,7 +22,7 @@ - 3.7 + 5.0 -server @@ -51,6 +51,11 @@ 1.4 test + + org.assertj + assertj-core + 2.1.0 + diff --git a/it/projects/java-bytecode/sonar-project.properties b/it/projects/java-bytecode/sonar-project.properties index cf6eb18..3d60e92 100644 --- a/it/projects/java-bytecode/sonar-project.properties +++ b/it/projects/java-bytecode/sonar-project.properties @@ -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 diff --git a/it/src/test/java/com/sonar/runner/it/CacheTest.java b/it/src/test/java/com/sonar/runner/it/CacheTest.java index 788b82c..d4c82ec 100644 --- a/it/src/test/java/com/sonar/runner/it/CacheTest.java +++ b/it/src/test/java/com/sonar/runner/it/CacheTest.java @@ -19,49 +19,116 @@ */ 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 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; } diff --git a/it/src/test/java/com/sonar/runner/it/JavaTest.java b/it/src/test/java/com/sonar/runner/it/JavaTest.java index 23cc3a4..f5ec9e0 100644 --- a/it/src/test/java/com/sonar/runner/it/JavaTest.java +++ b/it/src/test/java/com/sonar/runner/it/JavaTest.java @@ -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")); diff --git a/it/src/test/java/com/sonar/runner/it/RunnerTestCase.java b/it/src/test/java/com/sonar/runner/it/RunnerTestCase.java index bd308c4..7323a55 100644 --- a/it/src/test/java/com/sonar/runner/it/RunnerTestCase.java +++ b/it/src/test/java/com/sonar/runner/it/RunnerTestCase.java @@ -57,6 +57,7 @@ public abstract class RunnerTestCase { public static void stopServer() { if (orchestrator != null) { orchestrator.stop(); + orchestrator = null; } } -- 2.39.5