From ee770f146bac137f3d8ab1fd68b4988603568e31 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 20 Aug 2015 17:55:35 +0200 Subject: [PATCH] improve quality --- .../runner/impl/IsolatedClassloader.java | 2 +- sonar-runner-cli/pom.xml | 6 +++ .../main/java/org/sonar/runner/cli/Main.java | 2 +- .../java/org/sonar/runner/cli/SystemInfo.java | 30 +++++++++--- .../org/sonar/runner/cli/ShutdownTest.java | 23 ++++++--- .../org/sonar/runner/cli/SystemInfoTest.java | 49 +++++++++++++++++-- 6 files changed, 92 insertions(+), 20 deletions(-) diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedClassloader.java b/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedClassloader.java index 958b1b0..c469fd1 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedClassloader.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedClassloader.java @@ -86,7 +86,7 @@ class IsolatedClassloader extends URLClassLoader { return c; } - private boolean fromSonarBatchPackage(String name) { + private static boolean fromSonarBatchPackage(String name) { return name.startsWith("org.sonar.runner.batch"); } diff --git a/sonar-runner-cli/pom.xml b/sonar-runner-cli/pom.xml index 179b49f..57cf0cd 100644 --- a/sonar-runner-cli/pom.xml +++ b/sonar-runner-cli/pom.xml @@ -38,6 +38,12 @@ mockito-all test + + com.jayway.awaitility + awaitility + 1.6.3 + test + diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java index 6316122..88b4510 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java @@ -148,7 +148,7 @@ public class Main { Logs.info("------------------------------------------------------------------------"); } - public void showError(String message, Throwable e, boolean showStackTrace) { + private void showError(String message, Throwable e, boolean showStackTrace) { if (showStackTrace) { Logs.error(message, e); if (!cli.isDebugMode()) { diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java index 1ab1458..43b27ab 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java @@ -22,16 +22,20 @@ package org.sonar.runner.cli; import org.sonar.runner.api.RunnerVersion; class SystemInfo { + private static System2 system = new System2(); private SystemInfo() { - // only static methods + } + + static void setSystem(System2 system) { + SystemInfo.system = system; } static void print() { Logs.info("SonarQube Runner " + RunnerVersion.version()); Logs.info(java()); Logs.info(os()); - String runnerOpts = System.getenv("SONAR_RUNNER_OPTS"); + String runnerOpts = system.getenv("SONAR_RUNNER_OPTS"); if (runnerOpts != null) { Logs.info("SONAR_RUNNER_OPTS=" + runnerOpts); } @@ -41,10 +45,10 @@ class SystemInfo { StringBuilder sb = new StringBuilder(); sb .append("Java ") - .append(System.getProperty("java.version")) + .append(system.getProperty("java.version")) .append(" ") - .append(System.getProperty("java.vendor")); - String bits = System.getProperty("sun.arch.data.model"); + .append(system.getProperty("java.vendor")); + String bits = system.getProperty("sun.arch.data.model"); if ("32".equals(bits) || "64".equals(bits)) { sb.append(" (").append(bits).append("-bit)"); } @@ -54,11 +58,21 @@ class SystemInfo { static String os() { StringBuilder sb = new StringBuilder(); sb - .append(System.getProperty("os.name")) + .append(system.getProperty("os.name")) .append(" ") - .append(System.getProperty("os.version")) + .append(system.getProperty("os.version")) .append(" ") - .append(System.getProperty("os.arch")); + .append(system.getProperty("os.arch")); return sb.toString(); } + + static class System2 { + String getProperty(String key) { + return System.getProperty(key); + } + + String getenv(String key) { + return System.getenv(key); + } + } } diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java index 98a5b85..ec401db 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java @@ -21,6 +21,12 @@ package org.sonar.runner.cli; import static org.mockito.Mockito.verify; import static org.fest.assertions.Assertions.assertThat; +import static com.jayway.awaitility.Awaitility.await; + +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; + +import com.jayway.awaitility.Duration; import org.mockito.MockitoAnnotations; import org.sonar.runner.cli.Exit; import org.sonar.runner.cli.Shutdown; @@ -50,12 +56,17 @@ public class ShutdownTest { shutdown = new Shutdown(exit, 100_000); shutdown.signalReady(false); assertThat(shutdown.shouldExit()).isFalse(); - - Thread t = new HookCaller(); + + final Thread t = new HookCaller(); t.start(); - Thread.sleep(1000); - - assertThat(t.isAlive()).isTrue(); + + await().atMost(Duration.TWO_SECONDS).pollDelay(50, TimeUnit.MILLISECONDS).until(new Callable() { + @Override + public Boolean call() throws Exception { + return t.isAlive(); + } + }); + assertThat(shutdown.shouldExit()).isTrue(); shutdown.signalReady(true); @@ -65,7 +76,7 @@ public class ShutdownTest { @Test(timeout = 60_000) public void testTimeout() throws InterruptedException { shutdown = new Shutdown(exit, 0); - + Thread t = new HookCaller(); t.start(); t.join(); diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/SystemInfoTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/SystemInfoTest.java index 8f16d34..cfedfe7 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/SystemInfoTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/SystemInfoTest.java @@ -19,25 +19,66 @@ */ package org.sonar.runner.cli; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import static org.mockito.Mockito.verify; + +import org.junit.Before; +import org.sonar.runner.cli.SystemInfo.System2; import org.junit.Test; import org.sonar.runner.cli.SystemInfo; - import static org.fest.assertions.Assertions.assertThat; public class SystemInfoTest { + System2 mockSystem = mock(System2.class); + + @Before + public void setUp() { + SystemInfo.setSystem(mockSystem); + } + @Test public void test_java() { - assertThat(SystemInfo.java()).matches("Java .* \\((32|64)-bit\\)"); + mockJava(); + assertThat(SystemInfo.java()).isEqualTo("Java 1.9 oracle (64-bit)"); + + when(mockSystem.getProperty("sun.arch.data.model")).thenReturn("32"); + assertThat(SystemInfo.java()).isEqualTo("Java 1.9 oracle (32-bit)"); + + when(mockSystem.getProperty("sun.arch.data.model")).thenReturn(null); + assertThat(SystemInfo.java()).isEqualTo("Java 1.9 oracle"); } @Test public void test_os() { - assertThat(SystemInfo.os()).isNotEmpty(); + mockOs(); + + assertThat(SystemInfo.os()).isEqualTo("linux 2.5 x64"); + } + + private void mockJava() { + when(mockSystem.getProperty("java.version")).thenReturn("1.9"); + when(mockSystem.getProperty("java.vendor")).thenReturn("oracle"); + when(mockSystem.getProperty("sun.arch.data.model")).thenReturn("64"); + } + + private void mockOs() { + when(mockSystem.getProperty("os.version")).thenReturn("2.5"); + when(mockSystem.getProperty("os.arch")).thenReturn("x64"); + when(mockSystem.getProperty("os.name")).thenReturn("linux"); } @Test public void should_print() { + mockOs(); + mockJava(); + when(mockSystem.getenv("SONAR_RUNNER_OPTS")).thenReturn("arg"); + SystemInfo.print(); - // should mock output + + verify(mockSystem).getProperty("java.version"); + verify(mockSystem).getProperty("os.version"); + verify(mockSystem).getenv("SONAR_RUNNER_OPTS"); } } -- 2.39.5