From 25a566485179819c127aea3fbf7d3c4a2d3e36a9 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 23 Jun 2015 14:25:51 +0200 Subject: [PATCH] Fix quality flaws and formatting --- .../java/org/sonar/runner/api/Command.java | 2 +- .../org/sonar/runner/api/CommandExecutor.java | 6 +-- .../org/sonar/runner/api/EmbeddedRunner.java | 1 - .../org/sonar/runner/api/ForkedRunner.java | 27 +++++++------ .../java/org/sonar/runner/api/Runner.java | 2 +- .../org/sonar/runner/api/SourceEncoding.java | 3 +- .../main/java/org/sonar/runner/api/Utils.java | 39 +++++++++++-------- .../sonar/runner/api/ForkedRunnerTest.java | 14 +++---- .../org/sonar/runner/api/SimpleRunner.java | 2 +- .../java/org/sonar/runner/api/UtilsTest.java | 2 + .../sonar/runner/impl/BatchLauncherMain.java | 2 +- 11 files changed, 55 insertions(+), 45 deletions(-) diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Command.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Command.java index 01048ee..b427c14 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Command.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Command.java @@ -94,7 +94,7 @@ class Command { Builder addArguments(List args) { for (String arg : args) { - if (arg!=null && !"".equals(arg.trim())) { + if (arg != null && !"".equals(arg.trim())) { arguments.add(arg); } } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java index fd5b6d8..27140b3 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java @@ -131,13 +131,13 @@ class CommandExecutor { }); } - private void verifyGobbler(Command command, StreamGobbler gobbler, String type) { + private static void verifyGobbler(Command command, StreamGobbler gobbler, String type) { if (gobbler.getException() != null) { throw new CommandException("Error inside " + type + " stream", command, gobbler.getException()); } } - private void closeStreams(Process process) { + private static void closeStreams(Process process) { if (process != null) { Utils.closeQuietly(process.getInputStream()); Utils.closeQuietly(process.getInputStream()); @@ -146,7 +146,7 @@ class CommandExecutor { } } - private void waitUntilFinish(StreamGobbler thread) { + private static void waitUntilFinish(StreamGobbler thread) { if (thread != null) { try { thread.join(); diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java index e1c04b7..4883ea0 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java @@ -40,7 +40,6 @@ import java.util.Properties; public class EmbeddedRunner extends Runner { private final IsolatedLauncherFactory launcherFactory; private IsolatedLauncher launcher; - private String sqVersion; private final List extensions = new ArrayList(); private static final String MASK_RULES_PROP = "sonarRunner.maskRules"; diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java index 31a932f..565e330 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java @@ -200,20 +200,10 @@ public class ForkedRunner extends Runner { private void fork(ForkCommand forkCommand) { if (stdOut == null) { - stdOut = new StreamConsumer() { - @Override - public void consumeLine(String line) { - Logs.info(line); - } - }; + stdOut = new OutConsumer(); } if (stdErr == null) { - stdErr = new StreamConsumer() { - @Override - public void consumeLine(String line) { - Logs.error(line); - } - }; + stdErr = new ErrConsumer(); } int status = commandExecutor.execute(forkCommand.command, stdOut, stdErr, ONE_DAY_IN_MILLISECONDS, processMonitor); @@ -225,6 +215,19 @@ public class ForkedRunner extends Runner { } } } + + private static class OutConsumer implements StreamConsumer { + @Override + public void consumeLine(String line) { + Logs.info(line); + } + }; + private static class ErrConsumer implements StreamConsumer { + @Override + public void consumeLine(String line) { + Logs.error(line); + } + }; static class ForkCommand { Command command; diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java index 890ce46..863b08c 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java @@ -32,7 +32,7 @@ import java.util.Properties; /** * @since 2.2 */ -public abstract class Runner { +public abstract class Runner> { private final Properties globalProperties = new Properties(); protected Runner() { diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java index 0029308..d21f9f3 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java @@ -27,8 +27,9 @@ import java.util.Properties; class SourceEncoding { private SourceEncoding() { - + } + static void init(Properties p) { boolean onProject = Utils.taskRequiresProject(p); if (onProject) { diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java index 4d4907a..efe39c4 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java @@ -39,13 +39,13 @@ class Utils { private Utils() { // only util static methods } - + static boolean isAtLeast52(String version) { - //it can be snapshot (5.2-SNAPSHOT) - if(version == null) { + // it can be snapshot (5.2-SNAPSHOT) + if (version == null) { return false; } - + int endIndex = Math.min(3, version.length()); return Double.parseDouble(version.substring(0, endIndex)) >= 5.2; } @@ -81,34 +81,41 @@ class Utils { static void deleteQuietly(File f) { try { - Files.walkFileTree(f.toPath(), new DeleteFileVisitor()); + Files.walkFileTree(f.toPath(), new DeleteQuietlyFileVisitor()); } catch (IOException e) { // ignore } } - private static class DeleteFileVisitor extends SimpleFileVisitor { + private static class DeleteQuietlyFileVisitor extends SimpleFileVisitor { @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + try { + Files.delete(file); + } catch (IOException e) { + // ignore + } return FileVisitResult.CONTINUE; } @Override - public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { - Files.delete(file); + public FileVisitResult visitFileFailed(Path file, IOException exc) { + try { + Files.delete(file); + } catch (IOException e) { + // ignore + } return FileVisitResult.CONTINUE; } @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - if (exc == null) { + public FileVisitResult postVisitDirectory(Path dir, IOException exc) { + try { Files.delete(dir); - return FileVisitResult.CONTINUE; - } else { - // directory iteration failed; propagate exception - throw exc; + } catch (IOException e) { + // ignore } + return FileVisitResult.CONTINUE; } } diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java index e7c3a56..a15cb86 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java @@ -31,11 +31,9 @@ import org.junit.rules.TemporaryFolder; import org.mockito.ArgumentMatcher; import org.sonar.runner.impl.JarExtractor; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.PrintStream; import java.util.LinkedList; import java.util.List; import java.util.Properties; @@ -91,16 +89,16 @@ public class ForkedRunnerTest { ArgumentCaptor arg1 = ArgumentCaptor.forClass(StreamConsumer.class); ArgumentCaptor arg2 = ArgumentCaptor.forClass(StreamConsumer.class); - + verify(commandExecutor).execute(any(Command.class), arg1.capture(), arg2.capture(), anyLong(), any(ProcessMonitor.class)); arg1.getValue().consumeLine("test1"); arg2.getValue().consumeLine("test2"); - + verify(listener).log("test1", Level.INFO); verify(listener).log("test2", Level.ERROR); verifyNoMoreInteractions(listener); } - + @Test public void should_print_to_consumers_by_default() throws IOException { final List printedLines = new LinkedList<>(); @@ -119,7 +117,7 @@ public class ForkedRunnerTest { runner.execute(); verify(commandExecutor).execute(any(Command.class), eq(consumer), eq(consumer), anyLong(), - any(ProcessMonitor.class)); + any(ProcessMonitor.class)); } @Test @@ -143,7 +141,7 @@ public class ForkedRunnerTest { assertThat(properties.getProperty("-Xmx512m")).isNull(); assertThat(properties.getProperty("SONAR_HOME")).isNull(); } - + @Test public void should_merge_properties() throws IOException { JarExtractor jarExtractor = createMockExtractor(); @@ -157,7 +155,7 @@ public class ForkedRunnerTest { verify(spy).writeProperties(properties.capture()); assertThat(properties.getValue().keySet()).contains("sonar.working.directory", "sonar.host.url", "sonar.sourceEncoding", "sonar.login"); } - + @Test public void test_java_command() throws IOException { JarExtractor jarExtractor = mock(JarExtractor.class); diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/SimpleRunner.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/SimpleRunner.java index 5d3f0c2..6c356fe 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/SimpleRunner.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/SimpleRunner.java @@ -21,7 +21,7 @@ package org.sonar.runner.api; import java.util.Properties; -class SimpleRunner extends Runner { +class SimpleRunner extends Runner { @Override protected void doExecute(Properties props) { } 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 a268ec3..b8dfc3f 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 @@ -47,6 +47,8 @@ public class UtilsTest { assertThat(Utils.isAtLeast52(null)).isFalse(); assertThat(Utils.isAtLeast52("52")).isTrue(); assertThat(Utils.isAtLeast52("5.0")).isFalse(); + assertThat(Utils.isAtLeast52("")).isFalse(); + assertThat(Utils.isAtLeast52("trash")).isFalse(); assertThat(Utils.isAtLeast52("6.0.0")).isTrue(); assertThat(Utils.isAtLeast52("5.2-SNAPSHOT")).isTrue(); } diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java index f41d855..14a57b4 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java @@ -63,7 +63,7 @@ public class BatchLauncherMain { try { new BatchLauncherMain(new IsolatedLauncherFactory()).execute(args); } catch (Exception e) { - e.printStackTrace(System.err); + Logs.error(e.getMessage(), e); // make sure non-daemon threads don't hang app System.exit(1); } -- 2.39.5