diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-08-18 16:11:54 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-09-05 14:24:12 +0200 |
commit | ae5637262461062c7e5fac17c1d6ac5792b5284d (patch) | |
tree | a819ae65cbabe3d25fbc8897e9337ad8b66f56a5 /server/sonar-process/src/test/java | |
parent | 4b8987715d5a476d3293a4a3e9d8e3f8d7a32e33 (diff) | |
download | sonarqube-ae5637262461062c7e5fac17c1d6ac5792b5284d.tar.gz sonarqube-ae5637262461062c7e5fac17c1d6ac5792b5284d.zip |
SONAR-9590 only heap and heap dump options are now in search.javaOpts
Diffstat (limited to 'server/sonar-process/src/test/java')
6 files changed, 457 insertions, 19 deletions
diff --git a/server/sonar-process/src/test/java/org/sonar/process/command/AbstractCommandTest.java b/server/sonar-process/src/test/java/org/sonar/process/command/AbstractCommandTest.java index 7ddb9c54089..62f59f11a48 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/command/AbstractCommandTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/command/AbstractCommandTest.java @@ -20,9 +20,11 @@ package org.sonar.process.command; import java.io.File; +import java.io.IOException; import java.util.Properties; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.process.ProcessId; @@ -32,10 +34,33 @@ public class AbstractCommandTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void constructor_throws_NPE_of_ProcessId_is_null() throws IOException { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("ProcessId can't be null"); + + new AbstractCommand<AbstractCommand>(null, temp.newFolder()) { + + }; + } + + @Test + public void constructor_throws_NPE_of_workDir_is_null() throws IOException { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("workDir can't be null"); + + new AbstractCommand<AbstractCommand>(ProcessId.WEB_SERVER, null) { + + }; + } @Test public void test_command_with_complete_information() throws Exception { - AbstractCommand command = new AbstractCommand(ProcessId.ELASTICSEARCH) { + File workDir = temp.newFolder(); + AbstractCommand command = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir) { }; @@ -45,8 +70,6 @@ public class AbstractCommandTest { command.setArguments(args); command.setEnvVariable("JAVA_COMMAND_TEST", "1000"); - File workDir = temp.newFolder(); - command.setWorkDir(workDir); assertThat(command.toString()).isNotNull(); assertThat(command.getWorkDir()).isSameAs(workDir); diff --git a/server/sonar-process/src/test/java/org/sonar/process/command/JavaCommandTest.java b/server/sonar-process/src/test/java/org/sonar/process/command/JavaCommandTest.java index 96d266a7d28..dd0f71a677c 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/command/JavaCommandTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/command/JavaCommandTest.java @@ -25,6 +25,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.process.ProcessId; +import org.sonar.process.jmvoptions.JvmOptions; import static org.assertj.core.api.Assertions.assertThat; @@ -35,7 +36,8 @@ public class JavaCommandTest { @Test public void test_command_with_complete_information() throws Exception { - JavaCommand command = new JavaCommand(ProcessId.ELASTICSEARCH); + File workDir = temp.newFolder(); + JavaCommand<JvmOptions> command = new JavaCommand<>(ProcessId.ELASTICSEARCH, workDir); command.setArgument("first_arg", "val1"); Properties args = new Properties(); @@ -44,15 +46,14 @@ public class JavaCommandTest { command.setClassName("org.sonar.ElasticSearch"); command.setEnvVariable("JAVA_COMMAND_TEST", "1000"); - File workDir = temp.newFolder(); - command.setWorkDir(workDir); command.addClasspath("lib/*.jar"); command.addClasspath("conf/*.xml"); - command.addJavaOption("-Xmx128m"); + JvmOptions<JvmOptions> jvmOptions = new JvmOptions<JvmOptions>() {}; + command.setJvmOptions(jvmOptions); assertThat(command.toString()).isNotNull(); assertThat(command.getClasspath()).containsOnly("lib/*.jar", "conf/*.xml"); - assertThat(command.getJavaOptions()).containsOnly("-Xmx128m"); + assertThat(command.getJvmOptions()).isSameAs(jvmOptions); assertThat(command.getWorkDir()).isSameAs(workDir); assertThat(command.getClassName()).isEqualTo("org.sonar.ElasticSearch"); @@ -61,15 +62,4 @@ public class JavaCommandTest { assertThat(command.getEnvVariables().size()).isEqualTo(System.getenv().size() + 1); } - @Test - public void addJavaOptions_adds_jvm_options() { - JavaCommand command = new JavaCommand(ProcessId.ELASTICSEARCH); - assertThat(command.getJavaOptions()).isEmpty(); - - command.addJavaOptions(""); - assertThat(command.getJavaOptions()).isEmpty(); - - command.addJavaOptions("-Xmx512m -Xms256m -Dfoo"); - assertThat(command.getJavaOptions()).containsOnly("-Xmx512m", "-Xms256m", "-Dfoo"); - } } diff --git a/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/CeJvmOptionsTest.java b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/CeJvmOptionsTest.java new file mode 100644 index 00000000000..3bd8bf37d91 --- /dev/null +++ b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/CeJvmOptionsTest.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.process.jmvoptions; + +import java.io.File; +import java.io.IOException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CeJvmOptionsTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void constructor_sets_mandatory_JVM_options() throws IOException { + File tmpDir = temporaryFolder.newFolder(); + CeJvmOptions underTest = new CeJvmOptions(tmpDir); + + assertThat(underTest.getAll()).containsExactly( + "-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath()); + } +} diff --git a/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/EsJvmOptionsTest.java b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/EsJvmOptionsTest.java new file mode 100644 index 00000000000..5f4ef878875 --- /dev/null +++ b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/EsJvmOptionsTest.java @@ -0,0 +1,108 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.process.jmvoptions; + +import java.io.File; +import java.io.IOException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.test.ExceptionCauseMatcher.hasType; + +public class EsJvmOptionsTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void constructor_sets_mandatory_JVM_options() { + EsJvmOptions underTest = new EsJvmOptions(); + + assertThat(underTest.getAll()).containsExactly( + "-XX:+UseConcMarkSweepGC", + "-XX:CMSInitiatingOccupancyFraction=75", + "-XX:+UseCMSInitiatingOccupancyOnly", + "-XX:+AlwaysPreTouch", + "-server", + "-Xss1m", + "-Djava.awt.headless=true", + "-Dfile.encoding=UTF-8", + "-Djna.nosys=true", + "-Djdk.io.permissionsUseCanonicalPath=true", + "-Dio.netty.noUnsafe=true", + "-Dio.netty.noKeySetOptimization=true", + "-Dio.netty.recycler.maxCapacityPerThread=0", + "-Dlog4j.shutdownHookEnabled=false", + "-Dlog4j2.disable.jmx=true", + "-Dlog4j.skipJansi=true"); + } + + @Test + public void writeToJvmOptionFile_writes_all_JVM_options_to_file_with_warning_header() throws IOException { + File file = temporaryFolder.newFile(); + EsJvmOptions underTest = new EsJvmOptions() + .add("-foo") + .add("-bar"); + + underTest.writeToJvmOptionFile(file); + + assertThat(file).hasContent( + "# This file has been automatically generated by SonarQube during startup.\n" + + "# Please use the sonar.search.javaOpts in sonar.properties to specify jvm options for Elasticsearch\n" + + "\n" + + "# DO NOT EDIT THIS FILE\n" + + "\n" + + "-XX:+UseConcMarkSweepGC\n" + + "-XX:CMSInitiatingOccupancyFraction=75\n" + + "-XX:+UseCMSInitiatingOccupancyOnly\n" + + "-XX:+AlwaysPreTouch\n" + + "-server\n" + + "-Xss1m\n" + + "-Djava.awt.headless=true\n" + + "-Dfile.encoding=UTF-8\n" + + "-Djna.nosys=true\n" + + "-Djdk.io.permissionsUseCanonicalPath=true\n" + + "-Dio.netty.noUnsafe=true\n" + + "-Dio.netty.noKeySetOptimization=true\n" + + "-Dio.netty.recycler.maxCapacityPerThread=0\n" + + "-Dlog4j.shutdownHookEnabled=false\n" + + "-Dlog4j2.disable.jmx=true\n" + + "-Dlog4j.skipJansi=true\n" + + "-foo\n" + + "-bar"); + + } + + @Test + public void writeToJvmOptionFile_throws_ISE_in_case_of_IOException() throws IOException { + File notAFile = temporaryFolder.newFolder(); + EsJvmOptions underTest = new EsJvmOptions(); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Cannot write Elasticsearch jvm options file"); + expectedException.expectCause(hasType(IOException.class)); + + underTest.writeToJvmOptionFile(notAFile); + } +} diff --git a/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/JvmOptionsTest.java b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/JvmOptionsTest.java new file mode 100644 index 00000000000..7e42d307d68 --- /dev/null +++ b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/JvmOptionsTest.java @@ -0,0 +1,232 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.process.jmvoptions; + +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.util.ArrayList; +import java.util.Properties; +import java.util.Random; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import org.apache.commons.lang.RandomStringUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.sonar.process.Props; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(DataProviderRunner.class) +public class JvmOptionsTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private final Random random = new Random(); + private final String randomPropertyName = RandomStringUtils.randomAlphanumeric(3); + private final Properties properties = new Properties(); + private final Props props = new Props(properties); + private final JvmOptions underTest = new JvmOptions(); + + @Test + public void constructor_without_arguments_creates_empty_JvmOptions() { + JvmOptions<JvmOptions> testJvmOptions = new JvmOptions<>(); + + assertThat(testJvmOptions.getAll()).isEmpty(); + } + + @Test + public void constructor_throws_NPE_if_argument_is_null() { + expectJvmOptionNotNullNPE(); + + new JvmOptions(null); + } + + @Test + public void constructor_throws_NPE_if_any_argument_is_null() { + ArrayList<String> nullList = new ArrayList<>(); + nullList.add(null); + String[] arguments = Stream.of( + Stream.of("-S1"), + IntStream.range(0, random.nextInt(2)).mapToObj(i -> "-B" + i), + nullList.stream(), + IntStream.range(0, random.nextInt(2)).mapToObj(i -> "-A" + i)).flatMap(s -> s) + .toArray(String[]::new); + + expectJvmOptionNotNullNPE(); + + new JvmOptions(arguments); + } + + @Test + @UseDataProvider("variousEmptyStrings") + public void constructor_throws_IAE_if_argument_is_empty(String emptyString) { + expectJvmOptionNotEmptyAndStartByDashIAE(); + + new JvmOptions(emptyString); + } + + @Test + @UseDataProvider("variousEmptyStrings") + public void constructor_throws_IAE_if_any_argument_is_empty(String emptyString) { + String[] arguments = Stream.of( + Stream.of("-S1"), + IntStream.range(0, random.nextInt(2)).mapToObj(i -> "-B" + i), + Stream.of(emptyString), + IntStream.range(0, random.nextInt(2)).mapToObj(i -> "-A" + i)) + .flatMap(s -> s) + .toArray(String[]::new); + + expectJvmOptionNotEmptyAndStartByDashIAE(); + + new JvmOptions(arguments); + } + + @Test + public void constructor_throws_IAE_if_argument_does_not_start_with_dash() { + expectJvmOptionNotEmptyAndStartByDashIAE(); + + new JvmOptions(RandomStringUtils.randomAlphanumeric(3)); + } + + @Test + public void constructor_throws_IAE_if_any_argument_does_not_start_with_dash() { + String[] arguments = Stream.of( + Stream.of("-S1"), + IntStream.range(0, random.nextInt(2)).mapToObj(i -> "-B" + i), + Stream.of(RandomStringUtils.randomAlphanumeric(3)), + IntStream.range(0, random.nextInt(2)).mapToObj(i -> "-A" + i)) + .flatMap(s -> s) + .toArray(String[]::new); + + expectJvmOptionNotEmptyAndStartByDashIAE(); + + new JvmOptions(arguments); + } + + @Test + public void add_throws_NPE_if_argument_is_null() { + expectJvmOptionNotNullNPE(); + + underTest.add(null); + } + + @Test + @UseDataProvider("variousEmptyStrings") + public void add_throws_IAE_if_argument_is_empty(String emptyString) { + expectJvmOptionNotEmptyAndStartByDashIAE(); + + underTest.add(emptyString); + } + + @Test + public void add_throws_IAE_if_argument_does_not_start_with_dash() { + expectJvmOptionNotEmptyAndStartByDashIAE(); + + underTest.add(RandomStringUtils.randomAlphanumeric(3)); + } + + @Test + public void addFromMandatoryProperty_fails_with_IAE_if_property_does_not_exist() { + expectMissingPropertyIAE(this.randomPropertyName); + + underTest.addFromMandatoryProperty(props, this.randomPropertyName); + } + + @Test + @UseDataProvider("variousEmptyStrings") + public void addFromMandatoryProperty_fails_with_IAE_if_property_contains_an_empty_value(String emptyString) { + expectMissingPropertyIAE(this.randomPropertyName); + + underTest.addFromMandatoryProperty(props, randomPropertyName); + } + + @Test + @UseDataProvider("variousEmptyStrings") + public void addFromMandatoryProperty_adds_single_option_of_property_with_trimming(String emptyString) { + properties.put(randomPropertyName, emptyString + "-foo" + emptyString); + + underTest.addFromMandatoryProperty(props, randomPropertyName); + + assertThat(underTest.getAll()).containsOnly("-foo"); + } + + @Test + @UseDataProvider("variousEmptyStrings") + public void addFromMandatoryProperty_fails_with_IAE_if_property_does_not_start_with_dash_after_trimmed(String emptyString) { + properties.put(randomPropertyName, emptyString + "foo -bar"); + + expectJvmOptionNotEmptyAndStartByDashIAE(); + + underTest.addFromMandatoryProperty(props, randomPropertyName); + } + + @Test + @UseDataProvider("variousEmptyStrings") + public void addFromMandatoryProperty_adds_options_of_property_with_trimming(String emptyString) { + properties.put(randomPropertyName, emptyString + "-foo" + emptyString + " -bar" + emptyString + " -duck" + emptyString); + + underTest.addFromMandatoryProperty(props, randomPropertyName); + + assertThat(underTest.getAll()).containsOnly("-foo", "-bar", "-duck"); + } + + @Test + public void addFromMandatoryProperty_supports_spaces_inside_options() { + properties.put(randomPropertyName, "-foo bar -duck"); + + underTest.addFromMandatoryProperty(props, randomPropertyName); + + assertThat(underTest.getAll()).containsOnly("-foo bar", "-duck"); + } + + @Test + public void toString_prints_all_jvm_options() { + underTest.add("-foo").add("-bar"); + + assertThat(underTest.toString()).isEqualTo("[-foo, -bar]"); + } + + private void expectJvmOptionNotNullNPE() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("a JVM option can't be null"); + } + + private void expectJvmOptionNotEmptyAndStartByDashIAE() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("a JVM option can't be empty and must start with '-'"); + } + + public void expectMissingPropertyIAE(String randomPropertyName) { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Missing property: " + randomPropertyName); + } + + @DataProvider() + public static Object[][] variousEmptyStrings() { + return new Object[][] { + {""}, + {" "}, + {" "} + }; + } +} diff --git a/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/WebJvmOptionsTest.java b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/WebJvmOptionsTest.java new file mode 100644 index 00000000000..9387a15b808 --- /dev/null +++ b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/WebJvmOptionsTest.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.process.jmvoptions; + +import java.io.File; +import java.io.IOException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static org.assertj.core.api.Assertions.assertThat; + +public class WebJvmOptionsTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void constructor_sets_mandatory_JVM_options() throws IOException { + File tmpDir = temporaryFolder.newFolder(); + WebJvmOptions underTest = new WebJvmOptions(tmpDir); + + assertThat(underTest.getAll()).containsExactly( + "-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath()); + } + +} |