Builder addArguments(List<String> args) {
for (String arg : args) {
- if (arg!=null && !"".equals(arg.trim())) {
+ if (arg != null && !"".equals(arg.trim())) {
arguments.add(arg);
}
}
});
}
- 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());
}
}
- private void waitUntilFinish(StreamGobbler thread) {
+ private static void waitUntilFinish(StreamGobbler thread) {
if (thread != null) {
try {
thread.join();
public class EmbeddedRunner extends Runner<EmbeddedRunner> {
private final IsolatedLauncherFactory launcherFactory;
private IsolatedLauncher launcher;
- private String sqVersion;
private final List<Object> extensions = new ArrayList<Object>();
private static final String MASK_RULES_PROP = "sonarRunner.maskRules";
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);
}
}
}
+
+ 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;
/**
* @since 2.2
*/
-public abstract class Runner<T extends Runner> {
+public abstract class Runner<T extends Runner<?>> {
private final Properties globalProperties = new Properties();
protected Runner() {
class SourceEncoding {
private SourceEncoding() {
-
+
}
+
static void init(Properties p) {
boolean onProject = Utils.taskRequiresProject(p);
if (onProject) {
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;
}
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<Path> {
+ private static class DeleteQuietlyFileVisitor extends SimpleFileVisitor<Path> {
@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;
}
}
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;
ArgumentCaptor<StreamConsumer> arg1 = ArgumentCaptor.forClass(StreamConsumer.class);
ArgumentCaptor<StreamConsumer> 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<String> printedLines = new LinkedList<>();
runner.execute();
verify(commandExecutor).execute(any(Command.class), eq(consumer), eq(consumer), anyLong(),
- any(ProcessMonitor.class));
+ any(ProcessMonitor.class));
}
@Test
assertThat(properties.getProperty("-Xmx512m")).isNull();
assertThat(properties.getProperty("SONAR_HOME")).isNull();
}
-
+
@Test
public void should_merge_properties() throws IOException {
JarExtractor jarExtractor = createMockExtractor();
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);
import java.util.Properties;
-class SimpleRunner extends Runner {
+class SimpleRunner extends Runner<SimpleRunner> {
@Override
protected void doExecute(Properties props) {
}
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();
}
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);
}