]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8798 move confusing inner class JavaProcessLauncher#SystemProcessBuilder
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 18 Jul 2017 13:47:57 +0000 (15:47 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Wed, 9 Aug 2017 13:09:54 +0000 (15:09 +0200)
server/sonar-process-monitor/src/main/java/org/sonar/application/process/JavaProcessLauncher.java
server/sonar-process-monitor/src/main/java/org/sonar/application/process/JavaProcessLauncherImpl.java
server/sonar-process-monitor/src/test/java/org/sonar/application/process/JavaProcessLauncherImplTest.java

index 93e7c677424835aff2f178d4e32776bb2af16742..e9cb85b2cea07c71453a4e370b3469038a73ec68 100644 (file)
 package org.sonar.application.process;
 
 import java.io.Closeable;
-import java.io.File;
-import java.io.IOException;
-import java.util.List;
-import java.util.Map;
 
 public interface JavaProcessLauncher extends Closeable {
 
-  class SystemProcessBuilder {
-    private final ProcessBuilder builder = new ProcessBuilder();
-
-    /**
-     * @see ProcessBuilder#command()
-     */
-    public List<String> command() {
-      return builder.command();
-    }
-
-    /**
-     * @see ProcessBuilder#command(List)
-     */
-    public SystemProcessBuilder command(List<String> commands) {
-      builder.command(commands);
-      return this;
-    }
-
-    /**
-     * @see ProcessBuilder#directory(File)
-     */
-    public SystemProcessBuilder directory(File dir) {
-      builder.directory(dir);
-      return this;
-    }
-
-    /**
-     * @see ProcessBuilder#environment()
-     */
-    public Map<String, String> environment() {
-      return builder.environment();
-    }
-
-    /**
-     * @see ProcessBuilder#redirectErrorStream(boolean)
-     */
-    public SystemProcessBuilder redirectErrorStream(boolean b) {
-      builder.redirectErrorStream(b);
-      return this;
-    }
-
-    /**
-     * @see ProcessBuilder#start()
-     */
-    public Process start() throws IOException {
-      return builder.start();
-    }
-  }
-
   @Override
   void close();
 
index c02aedd00461df25e971c7cb6342151530251f6e..aff3a8ff3efefe1828ed6c90d1f77f5c56a7207f 100644 (file)
@@ -21,10 +21,12 @@ package org.sonar.application.process;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 import java.util.function.Supplier;
 import org.slf4j.Logger;
@@ -43,13 +45,13 @@ public class JavaProcessLauncherImpl implements JavaProcessLauncher {
 
   private final File tempDir;
   private final AllProcessesCommands allProcessesCommands;
-  private final Supplier<SystemProcessBuilder> processBuilderSupplier;
+  private final Supplier<ProcessBuilder> processBuilderSupplier;
 
   public JavaProcessLauncherImpl(File tempDir) {
-    this(tempDir, new AllProcessesCommands(tempDir), SystemProcessBuilder::new);
+    this(tempDir, new AllProcessesCommands(tempDir), JavaLangProcessBuilder::new);
   }
 
-  JavaProcessLauncherImpl(File tempDir, AllProcessesCommands allProcessesCommands, Supplier<SystemProcessBuilder> processBuilderSupplier) {
+  JavaProcessLauncherImpl(File tempDir, AllProcessesCommands allProcessesCommands, Supplier<ProcessBuilder> processBuilderSupplier) {
     this.tempDir = tempDir;
     this.allProcessesCommands = allProcessesCommands;
     this.processBuilderSupplier = processBuilderSupplier;
@@ -67,7 +69,7 @@ public class JavaProcessLauncherImpl implements JavaProcessLauncher {
     try {
       commands = allProcessesCommands.createAfterClean(javaCommand.getProcessId().getIpcIndex());
 
-      SystemProcessBuilder processBuilder = create(javaCommand);
+      ProcessBuilder processBuilder = create(javaCommand);
       LOG.info("Launch process[{}]: {}", javaCommand.getProcessId().getKey(), String.join(" ", processBuilder.command()));
       process = processBuilder.start();
       return new ProcessMonitorImpl(process, commands);
@@ -81,7 +83,7 @@ public class JavaProcessLauncherImpl implements JavaProcessLauncher {
     }
   }
 
-  private SystemProcessBuilder create(JavaCommand javaCommand) {
+  private ProcessBuilder create(JavaCommand javaCommand) {
     List<String> commands = new ArrayList<>();
     commands.add(buildJavaPath());
     commands.addAll(javaCommand.getJavaOptions());
@@ -92,7 +94,7 @@ public class JavaProcessLauncherImpl implements JavaProcessLauncher {
     commands.add(javaCommand.getClassName());
     commands.add(buildPropertiesFile(javaCommand).getAbsolutePath());
 
-    SystemProcessBuilder processBuilder = processBuilderSupplier.get();
+    ProcessBuilder processBuilder = processBuilderSupplier.get();
     processBuilder.command(commands);
     processBuilder.directory(javaCommand.getWorkDir());
     processBuilder.environment().putAll(javaCommand.getEnvVariables());
@@ -129,4 +131,77 @@ public class JavaProcessLauncherImpl implements JavaProcessLauncher {
       throw new IllegalStateException("Cannot write temporary settings to " + propertiesFile, e);
     }
   }
+
+  /**
+   * An interface of the methods of {@link java.lang.ProcessBuilder} that we use in {@link JavaProcessLauncherImpl}.
+   * <p>Allows testing creating processes without actualling creating them at OS level</p>
+   */
+  public interface ProcessBuilder {
+    List<String> command();
+
+    ProcessBuilder command(List<String> commands);
+
+    ProcessBuilder directory(File dir);
+
+    Map<String, String> environment();
+
+    ProcessBuilder redirectErrorStream(boolean b);
+
+    Process start() throws IOException;
+  }
+
+  private static class JavaLangProcessBuilder implements ProcessBuilder {
+    private final java.lang.ProcessBuilder builder = new java.lang.ProcessBuilder();
+
+    /**
+     * @see java.lang.ProcessBuilder#command()
+     */
+    @Override
+    public List<String> command() {
+      return builder.command();
+    }
+
+    /**
+     * @see java.lang.ProcessBuilder#command(List)
+     */
+    @Override
+    public ProcessBuilder command(List<String> commands) {
+      builder.command(commands);
+      return this;
+    }
+
+    /**
+     * @see java.lang.ProcessBuilder#directory(File)
+     */
+    @Override
+    public ProcessBuilder directory(File dir) {
+      builder.directory(dir);
+      return this;
+    }
+
+    /**
+     * @see java.lang.ProcessBuilder#environment()
+     */
+    @Override
+    public Map<String, String> environment() {
+      return builder.environment();
+    }
+
+    /**
+     * @see java.lang.ProcessBuilder#redirectErrorStream(boolean)
+     */
+    @Override
+    public ProcessBuilder redirectErrorStream(boolean b) {
+      builder.redirectErrorStream(b);
+      return this;
+    }
+
+    /**
+     * @see java.lang.ProcessBuilder#start()
+     */
+    @Override
+    public Process start() throws IOException {
+      return builder.start();
+    }
+  }
 }
index 2d00d230b26a894a5d30896a0c6c752264467862..c1fc1f2b030ea3ac070d9c451ace9f54c528e6a5 100644 (file)
@@ -108,7 +108,7 @@ public class JavaProcessLauncherImplTest {
   @Test
   public void throw_ISE_if_command_fails() throws IOException {
     File tempDir = temp.newFolder();
-    JavaProcessLauncher.SystemProcessBuilder processBuilder = mock(JavaProcessLauncher.SystemProcessBuilder.class, RETURNS_MOCKS);
+    JavaProcessLauncherImpl.ProcessBuilder processBuilder = mock(JavaProcessLauncherImpl.ProcessBuilder.class, RETURNS_MOCKS);
     when(processBuilder.start()).thenThrow(new IOException("error"));
     JavaProcessLauncher underTest = new JavaProcessLauncherImpl(tempDir, commands, () -> processBuilder);
 
@@ -118,7 +118,7 @@ public class JavaProcessLauncherImplTest {
     underTest.launch(new JavaCommand(ProcessId.ELASTICSEARCH));
   }
 
-  private static class TestProcessBuilder extends JavaProcessLauncher.SystemProcessBuilder {
+  private static class TestProcessBuilder implements JavaProcessLauncherImpl.ProcessBuilder {
     private List<String> commands = null;
     private File dir = null;
     private Boolean redirectErrorStream = null;