Browse Source

improve quality

tags/2.5-rc1
Duarte Meneses 8 years ago
parent
commit
ee770f146b

+ 1
- 1
sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedClassloader.java View File

@@ -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");
}


+ 6
- 0
sonar-runner-cli/pom.xml View File

@@ -38,6 +38,12 @@
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>1.6.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>

+ 1
- 1
sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java View File

@@ -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()) {

+ 22
- 8
sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java View File

@@ -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);
}
}
}

+ 17
- 6
sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java View File

@@ -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<Boolean>() {
@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();

+ 45
- 4
sonar-runner-cli/src/test/java/org/sonar/runner/cli/SystemInfoTest.java View File

@@ -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");
}
}

Loading…
Cancel
Save