From e7d218f5d4bae169f3a66d31170a670254efbf10 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Wed, 9 Sep 2015 13:33:24 +0200 Subject: [PATCH] Improvie tests and quality --- .../java-bytecode/.sonar/findbugs-include.xml | 7 -- .../java-bytecode/.sonar/findbugs-result.xml | 69 ------------------- .../sonar-project.properties | 7 ++ .../src/basic/Hello.java | 9 +++ .../src/basic/World.java | 8 +++ .../java/com/sonar/runner/it/CacheTest.java | 40 +++++++---- .../java/org/sonar/runner/api/UtilsTest.java | 35 +++++++++- 7 files changed, 85 insertions(+), 90 deletions(-) delete mode 100644 it/projects/java-bytecode/.sonar/findbugs-include.xml delete mode 100644 it/projects/java-bytecode/.sonar/findbugs-result.xml create mode 100644 it/projects/java-sample-non-associated/sonar-project.properties create mode 100644 it/projects/java-sample-non-associated/src/basic/Hello.java create mode 100644 it/projects/java-sample-non-associated/src/basic/World.java diff --git a/it/projects/java-bytecode/.sonar/findbugs-include.xml b/it/projects/java-bytecode/.sonar/findbugs-include.xml deleted file mode 100644 index d4846f0..0000000 --- a/it/projects/java-bytecode/.sonar/findbugs-include.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/it/projects/java-bytecode/.sonar/findbugs-result.xml b/it/projects/java-bytecode/.sonar/findbugs-result.xml deleted file mode 100644 index 0ae2961..0000000 --- a/it/projects/java-bytecode/.sonar/findbugs-result.xml +++ /dev/null @@ -1,69 +0,0 @@ - - - - - /home/duartem/git/sonar-tests-core/it-sonar-runner/projects/java-bytecode/src/HasFindbugsViolation.java - /home/duartem/git/sonar-tests-core/it-sonar-runner/projects/java-bytecode/build/classes/HasFindbugsViolation.class - /home/duartem/git/sonar-tests-core/it-sonar-runner/projects/java-bytecode/build/classes - /home/duartem/git/sonar-tests-core/it-sonar-runner/projects/java-bytecode/lib/deprecated.jar - /home/duartem/git/sonar-tests-core/it-sonar-runner/projects/java-bytecode/.sonar/findbugs/annotations.jar - /home/duartem/git/sonar-tests-core/it-sonar-runner/projects/java-bytecode/.sonar/findbugs/jsr305.jar - /home/duartem/git/sonar-tests-core/it-sonar-runner/projects/java-bytecode/.sonar - - - Method invokes System.exit(...) - HasFindbugsViolation.use() invokes System.exit(...), which shuts down the entire virtual machine - - - At HasFindbugsViolation.java:[lines 1-12] - - In class HasFindbugsViolation - - - - In method HasFindbugsViolation.use() - - - At HasFindbugsViolation.java:[line 7] - - - - Bad practice - - - Method invokes System.exit(...) -
Invoking System.exit shuts down the entire Java virtual machine. This - should only been done when it is appropriate. Such calls make it - hard or impossible for your code to be invoked by other code. - Consider throwing a RuntimeException instead.

- - ]]>
-
- - Dubious method used - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/it/projects/java-sample-non-associated/sonar-project.properties b/it/projects/java-sample-non-associated/sonar-project.properties new file mode 100644 index 0000000..bd17a41 --- /dev/null +++ b/it/projects/java-sample-non-associated/sonar-project.properties @@ -0,0 +1,7 @@ +# Note that the format of project key is still groupId:artifactId in order to support test with sonar 2.6. +#sonar.projectKey=java:sample +sonar.projectName=Java Sample, with comma +sonar.projectDescription=This is a Java sample +sonar.projectVersion=1.2.3 + +sonar.sources=src diff --git a/it/projects/java-sample-non-associated/src/basic/Hello.java b/it/projects/java-sample-non-associated/src/basic/Hello.java new file mode 100644 index 0000000..b9db5a0 --- /dev/null +++ b/it/projects/java-sample-non-associated/src/basic/Hello.java @@ -0,0 +1,9 @@ +package basic; + +public class Hello { + + public void hello() { + int i=356; + if (true) i=5658; + } +} diff --git a/it/projects/java-sample-non-associated/src/basic/World.java b/it/projects/java-sample-non-associated/src/basic/World.java new file mode 100644 index 0000000..c65d91c --- /dev/null +++ b/it/projects/java-sample-non-associated/src/basic/World.java @@ -0,0 +1,8 @@ +package basic; + +public final class World { + + public void world() { + System.out.println("hello world"); + } +} 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 1b2f6ca..09f8d20 100644 --- a/it/src/test/java/com/sonar/runner/it/CacheTest.java +++ b/it/src/test/java/com/sonar/runner/it/CacheTest.java @@ -20,7 +20,6 @@ package com.sonar.runner.it; import org.junit.Assume; - import org.junit.BeforeClass; import org.junit.rules.TemporaryFolder; import org.junit.Rule; @@ -67,21 +66,21 @@ public class CacheTest extends RunnerTestCase { @Test public void testIssuesMode() throws IOException { Assume.assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.2")); - + // online, without cache -> should sync ensureStarted(); - SonarRunner build = createRunner("issues", true); + SonarRunner build = createRunner("issues", true, "java-sample"); BuildResult result = orchestrator.executeBuild(build, false); assertThat(result.isSuccess()).isTrue(); // offline, with cache -> should run from cache ensureStopped(); - build = createRunner("issues", false); + build = createRunner("issues", false, "java-sample"); result = orchestrator.executeBuild(build, false); assertThat(result.isSuccess()).isTrue(); // offline, without cache -> should fail - build = createRunner("issues", true); + build = createRunner("issues", true, "java-sample"); try { result = orchestrator.executeBuild(build); } catch (BuildFailureException e) { @@ -89,19 +88,36 @@ public class CacheTest extends RunnerTestCase { } } + @Test + public void testNonAssociatedMode() throws IOException { + Assume.assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.2")); + + // online, without cache -> should sync + ensureStarted(); + SonarRunner build = createRunner("issues", true, "java-sample-non-associated"); + BuildResult result = orchestrator.executeBuild(build, false); + assertThat(result.isSuccess()).isTrue(); + + // offline, with cache -> should run from cache + ensureStopped(); + build = createRunner("issues", false, "java-sample-non-associated"); + result = orchestrator.executeBuild(build, false); + assertThat(result.isSuccess()).isTrue(); + } + @Test public void testPublishModeOffline() throws IOException { Assume.assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.2")); - + // online (cache not used) ensureStarted(); - SonarRunner build = createRunner("publish"); + SonarRunner build = createRunner("publish", "java-sample"); BuildResult result = orchestrator.executeBuild(build, false); assertThat(result.isSuccess()).isTrue(); // offline (cache not used) -> should fail ensureStopped(); - build = createRunner("publish", false); + build = createRunner("publish", false, "java-sample"); try { result = orchestrator.executeBuild(build); } catch (BuildFailureException e) { @@ -110,16 +126,16 @@ public class CacheTest extends RunnerTestCase { } - private SonarRunner createRunner(String mode) throws IOException { - return createRunner(mode, false); + private SonarRunner createRunner(String mode, String project) throws IOException { + return createRunner(mode, false, project); } - private SonarRunner createRunner(String mode, boolean refreshCache) throws IOException { + private SonarRunner createRunner(String mode, boolean refreshCache, String project) throws IOException { if (refreshCache || currentTemp == null) { currentTemp = temp.newFolder(); } - SonarRunner runner = newRunner(new File("projects/java-sample")) + SonarRunner runner = newRunner(new File("projects/" + project)) .setProperty("sonar.analysis.mode", mode) .setProperty("sonar.userHome", currentTemp.getAbsolutePath()); diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java index 653f61f..3b1239f 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java @@ -19,19 +19,27 @@ */ package org.sonar.runner.api; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; + import java.io.Closeable; +import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Properties; -import org.junit.Test; +import org.junit.Test; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; public class UtilsTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test public void should_join_strings() { assertThat(Utils.join(new String[] {}, ",")).isEqualTo(""); @@ -48,6 +56,15 @@ public class UtilsTest { assertThat(Utils.taskRequiresProject(props)).isTrue(); } + @Test + public void write_properties() throws IOException { + File f = temp.newFile(); + Properties p = new Properties(); + p.put("key", "value"); + Utils.writeProperties(f, p); + assertThat(Files.readAllLines(f.toPath(), StandardCharsets.UTF_8)).contains("key=value"); + } + @Test public void task_should_not_require_project() { Properties props = new Properties(); @@ -56,7 +73,7 @@ public class UtilsTest { } @Test - public void close_quietly() throws IOException { + public void close_quietly_error() throws IOException { Closeable c = mock(Closeable.class); doThrow(IOException.class).when(c).close(); Utils.closeQuietly(c); @@ -68,6 +85,20 @@ public class UtilsTest { Utils.closeQuietly(null); } + @Test + public void close_quietly() throws IOException { + Closeable c = mock(Closeable.class); + Utils.closeQuietly(c); + verify(c).close(); + } + + @Test + public void delete_quietly() { + File f = mock(File.class); + doThrow(IOException.class).when(f).toPath(); + Utils.deleteQuietly(f); + } + @Test public void delete_non_empty_directory() throws IOException { /*- -- 2.39.5