]> source.dussan.org Git - sonarqube.git/commitdiff
fix quality flaws
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Tue, 10 Oct 2017 16:10:28 +0000 (18:10 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Tue, 10 Oct 2017 16:49:34 +0000 (18:49 +0200)
server/sonar-main/src/main/java/org/sonar/application/command/CommandFactoryImpl.java
server/sonar-main/src/test/java/org/sonar/application/command/CommandFactoryImplTest.java
server/sonar-process/src/main/java/org/sonar/process/System2.java

index ef09996c8cc937bd9cb0c6a1902ef0fad5f410f5..3f840eb0131b205229009e08544bf07eb260048a 100644 (file)
@@ -32,7 +32,6 @@ import org.sonar.process.ProcessProperties;
 import org.sonar.process.Props;
 import org.sonar.process.System2;
 
-import static org.apache.commons.lang.SystemUtils.IS_OS_WINDOWS;
 import static org.sonar.process.ProcessProperties.HTTPS_PROXY_HOST;
 import static org.sonar.process.ProcessProperties.HTTPS_PROXY_PORT;
 import static org.sonar.process.ProcessProperties.HTTP_PROXY_HOST;
@@ -55,10 +54,12 @@ public class CommandFactoryImpl implements CommandFactory {
 
   private final Props props;
   private final File tempDir;
+  private final System2 system2;
 
   public CommandFactoryImpl(Props props, File tempDir, System2 system2) {
     this.props = props;
     this.tempDir = tempDir;
+    this.system2 = system2;
     String javaToolOptions = system2.getenv(ENV_VAR_JAVA_TOOL_OPTIONS);
     if (javaToolOptions != null && !javaToolOptions.trim().isEmpty()) {
       LoggerFactory.getLogger(CommandFactoryImpl.class)
@@ -69,29 +70,14 @@ public class CommandFactoryImpl implements CommandFactory {
 
   @Override
   public AbstractCommand<?> createEsCommand() {
-    if (IS_OS_WINDOWS) {
+    if (system2.isOsWindows()) {
       return createEsCommandForWindows();
     }
     return createEsCommandForUnix();
   }
 
   private EsScriptCommand createEsCommandForUnix() {
-    EsInstallation esInstallation = new EsInstallation(props);
-    if (!esInstallation.getExecutable().exists()) {
-      throw new IllegalStateException("Cannot find elasticsearch binary");
-    }
-    Map<String, String> settingsMap = new EsSettings(props, esInstallation, System2.INSTANCE).build();
-
-    esInstallation
-      .setLog4j2Properties(new EsLogging().createProperties(props, esInstallation.getLogDirectory()))
-      .setEsJvmOptions(new EsJvmOptions()
-        .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_OPTS)
-        .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
-      .setEsYmlSettings(new EsYmlSettings(settingsMap))
-      .setClusterName(settingsMap.get("cluster.name"))
-      .setHost(settingsMap.get("network.host"))
-      .setPort(Integer.valueOf(settingsMap.get("transport.tcp.port")));
-
+    EsInstallation esInstallation = createEsInstallation();
     return new EsScriptCommand(ProcessId.ELASTICSEARCH, esInstallation.getHomeDirectory())
       .setEsInstallation(esInstallation)
       .addOption("-Epath.conf=" + esInstallation.getConfDirectory().getAbsolutePath())
@@ -101,22 +87,7 @@ public class CommandFactoryImpl implements CommandFactory {
   }
 
   private JavaCommand createEsCommandForWindows() {
-    EsInstallation esInstallation = new EsInstallation(props);
-    if (!esInstallation.getExecutable().exists()) {
-      throw new IllegalStateException("Cannot find elasticsearch binary");
-    }
-    Map<String, String> settingsMap = new EsSettings(props, esInstallation, System2.INSTANCE).build();
-
-    esInstallation
-      .setLog4j2Properties(new EsLogging().createProperties(props, esInstallation.getLogDirectory()))
-      .setEsJvmOptions(new EsJvmOptions()
-        .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_OPTS)
-        .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
-      .setEsYmlSettings(new EsYmlSettings(settingsMap))
-      .setClusterName(settingsMap.get("cluster.name"))
-      .setHost(settingsMap.get("network.host"))
-      .setPort(Integer.valueOf(settingsMap.get("transport.tcp.port")));
-
+    EsInstallation esInstallation = createEsInstallation();
     return new JavaCommand<EsJvmOptions>(ProcessId.ELASTICSEARCH, esInstallation.getHomeDirectory())
       .setEsInstallation(esInstallation)
       .setReadsArgumentsFromFile(false)
@@ -134,6 +105,25 @@ public class CommandFactoryImpl implements CommandFactory {
       .suppressEnvVariable(ENV_VAR_JAVA_TOOL_OPTIONS);
   }
 
+  private EsInstallation createEsInstallation() {
+    EsInstallation esInstallation = new EsInstallation(props);
+    if (!esInstallation.getExecutable().exists()) {
+      throw new IllegalStateException("Cannot find elasticsearch binary");
+    }
+    Map<String, String> settingsMap = new EsSettings(props, esInstallation, System2.INSTANCE).build();
+
+    esInstallation
+      .setLog4j2Properties(new EsLogging().createProperties(props, esInstallation.getLogDirectory()))
+      .setEsJvmOptions(new EsJvmOptions()
+        .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_OPTS)
+        .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
+      .setEsYmlSettings(new EsYmlSettings(settingsMap))
+      .setClusterName(settingsMap.get("cluster.name"))
+      .setHost(settingsMap.get("network.host"))
+      .setPort(Integer.valueOf(settingsMap.get("transport.tcp.port")));
+    return esInstallation;
+  }
+
   @Override
   public JavaCommand createWebCommand(boolean leader) {
     File homeDir = props.nonNullValueAsFile(ProcessProperties.PATH_HOME);
index 6058590a20d42d289dff5c493c13b49590f9752c..32d29fd82eb3ccde30fa816e36c750c393bb848c 100644 (file)
@@ -104,15 +104,51 @@ public class CommandFactoryImplTest {
   }
 
   @Test
-  public void createEsCommand_returns_command_for_default_settings() throws Exception {
+  public void createEsCommand_for_unix_returns_command_for_default_settings() throws Exception {
+    System2 system2 = Mockito.mock(System2.class);
+    when(system2.isOsWindows()).thenReturn(false);
     prepareEsFileSystem();
 
     Properties props = new Properties();
     props.setProperty("sonar.search.host", "localhost");
 
-    AbstractCommand esCommand = newFactory(props).createEsCommand();
+    AbstractCommand esCommand = newFactory(props, system2).createEsCommand();
     EsInstallation esConfig = esCommand.getEsInstallation();
 
+    assertThat(esCommand).isInstanceOf(EsScriptCommand.class);
+    assertThat(esConfig.getClusterName()).isEqualTo("sonarqube");
+    assertThat(esConfig.getHost()).isNotEmpty();
+    assertThat(esConfig.getPort()).isEqualTo(9001);
+    assertThat(esConfig.getEsJvmOptions().getAll())
+      // enforced values
+      .contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8")
+      // default settings
+      .contains("-Xms512m", "-Xmx512m", "-XX:+HeapDumpOnOutOfMemoryError");
+    File esConfDir = new File(tempDir, "conf/es");
+    assertThat(esCommand.getEnvVariables())
+      .contains(entry("ES_JVM_OPTIONS", new File(esConfDir, "jvm.options").getAbsolutePath()))
+      .containsKey("JAVA_HOME");
+    assertThat(esConfig.getEsYmlSettings()).isNotNull();
+
+    assertThat(esConfig.getLog4j2Properties())
+      .contains(entry("appender.file_es.fileName", new File(logsDir, "es.log").getAbsolutePath()));
+
+    assertThat(esCommand.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS");
+  }
+
+  @Test
+  public void createEsCommand_for_windows_returns_command_for_default_settings() throws Exception {
+    System2 system2 = Mockito.mock(System2.class);
+    when(system2.isOsWindows()).thenReturn(true);
+    prepareEsFileSystem();
+
+    Properties props = new Properties();
+    props.setProperty("sonar.search.host", "localhost");
+
+    AbstractCommand esCommand = newFactory(props, system2).createEsCommand();
+    EsInstallation esConfig = esCommand.getEsInstallation();
+
+    assertThat(esCommand).isInstanceOf(JavaCommand.class);
     assertThat(esConfig.getClusterName()).isEqualTo("sonarqube");
     assertThat(esConfig.getHost()).isNotEmpty();
     assertThat(esConfig.getPort()).isEqualTo(9001);
@@ -182,6 +218,31 @@ public class CommandFactoryImplTest {
     assertThat(command.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS");
   }
 
+  @Test
+  public void createCeCommand_returns_command_for_default_settings() throws Exception {
+    JavaCommand command = newFactory(new Properties()).createCeCommand();
+
+    assertThat(command.getClassName()).isEqualTo("org.sonar.ce.app.CeServer");
+    assertThat(command.getWorkDir().getAbsolutePath()).isEqualTo(homeDir.getAbsolutePath());
+    assertThat(command.getClasspath())
+      .containsExactlyInAnyOrder("./lib/common/*", "./lib/server/*", "./lib/ce/*");
+    assertThat(command.getJvmOptions().getAll())
+      // enforced values
+      .contains("-Djava.awt.headless=true", "-Dfile.encoding=UTF-8")
+      // default settings
+      .contains("-Djava.io.tmpdir=" + tempDir.getAbsolutePath(), "-Dfile.encoding=UTF-8")
+      .contains("-Xmx512m", "-Xms128m", "-XX:+HeapDumpOnOutOfMemoryError");
+    assertThat(command.getProcessId()).isEqualTo(ProcessId.COMPUTE_ENGINE);
+    assertThat(command.getEnvVariables())
+      .isNotEmpty();
+    assertThat(command.getArguments())
+      // default settings
+      .contains(entry("sonar.web.javaOpts", "-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError"))
+      .contains(entry("sonar.cluster.enabled", "false"));
+
+    assertThat(command.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS");
+  }
+
   @Test
   public void createWebCommand_configures_command_with_overridden_settings() throws Exception {
     Properties props = new Properties();
@@ -222,7 +283,11 @@ public class CommandFactoryImplTest {
     FileUtils.touch(new File(homeDir, "elasticsearch/bin/elasticsearch.bat"));
   }
 
-  private CommandFactory newFactory(Properties userProps) throws IOException {
+  private CommandFactoryImpl newFactory(Properties userProps) throws IOException {
+    return newFactory(userProps, System2.INSTANCE);
+  }
+
+  private CommandFactoryImpl newFactory(Properties userProps, System2 system2) throws IOException {
     Properties p = new Properties();
     p.setProperty("sonar.path.home", homeDir.getAbsolutePath());
     p.setProperty("sonar.path.temp", tempDir.getAbsolutePath());
@@ -231,7 +296,7 @@ public class CommandFactoryImplTest {
 
     Props props = new Props(p);
     ProcessProperties.completeDefaults(props);
-    return new CommandFactoryImpl(props, tempDir, System2.INSTANCE);
+    return new CommandFactoryImpl(props, tempDir, system2);
   }
 
   private <T> void attachMemoryAppenderToLoggerOf(Class<T> loggerClass) {
index 1c9ca5c51c69799011513fbefc864d44bb6cd493..0e32165046ce119b61ded3de8bee12ecdfc5dc80 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.process;
 
 import java.util.Map;
+import org.apache.commons.lang.SystemUtils;
 
 /**
  * An interface allowing to wrap around static call to {@link System} class.
@@ -35,6 +36,11 @@ public interface System2 {
     public String getenv(String name) {
       return System.getenv(name);
     }
+
+    @Override
+    public boolean isOsWindows() {
+      return SystemUtils.IS_OS_WINDOWS;
+    }
   };
 
   /**
@@ -46,4 +52,9 @@ public interface System2 {
    * Proxy to {@link System#getenv(String)}.
    */
   String getenv(String name);
+
+  /**
+   * True if this is MS Windows.
+   */
+  boolean isOsWindows();
 }