@Test public void add_accepts_property_equal_to_mandatory_option_and_does_not_add_it_twice() { JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue)); underTest.add(randomPrefix + randomValue); assertThat(underTest.getAll()).containsOnly(randomPrefix + randomValue); }
public T addFromMandatoryProperty(Props props, String propertyName) { String value = props.nonNullValue(propertyName); if (!value.isEmpty()) { List<String> jvmOptions = Arrays.stream(value.split(" (?=-)")).map(String::trim).collect(Collectors.toList()); checkOptionFormat(propertyName, jvmOptions); checkMandatoryOptionOverwrite(propertyName, jvmOptions); options.addAll(jvmOptions); } return castThis(); }
/** * Add an option. * Argument is trimmed before being added. * * @throws IllegalArgumentException if argument is empty or does not start with {@code -}. */ public T add(String str) { requireNonNull(str, JVM_OPTION_NOT_NULL_ERROR_MESSAGE); String value = str.trim(); if (isInvalidOption(value)) { throw new IllegalArgumentException("a JVM option can't be empty and must start with '-'"); } checkMandatoryOptionOverwrite(value); options.add(value); return castThis(); }
public JvmOptions(Map<String, String> mandatoryJvmOptions) { requireNonNull(mandatoryJvmOptions, JVM_OPTION_NOT_NULL_ERROR_MESSAGE) .entrySet() .stream() .filter(e -> { requireNonNull(e.getKey(), "JVM option prefix can't be null"); if (e.getKey().trim().isEmpty()) { throw new IllegalArgumentException("JVM option prefix can't be empty"); } requireNonNull(e.getValue(), "JVM option value can't be null"); return true; }).forEach(e -> { String key = e.getKey().trim(); String value = e.getValue().trim(); mandatoryOptions.put(key, value); add(key + value); }); }
@Test public void addFromMandatoryProperty_supports_spaces_inside_options() { properties.put(randomPropertyName, "-foo bar -duck"); underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName); assertThat(underTest.getAll()).containsOnly("-foo bar", "-duck"); }
@Test public void add_checks_against_mandatory_options_is_case_sensitive() { String[] optionOverrides = { randomPrefix, randomPrefix + randomAlphanumeric(1), randomPrefix + randomAlphanumeric(2), randomPrefix + randomAlphanumeric(3), randomPrefix + randomAlphanumeric(4), randomPrefix + randomValue.substring(1), randomPrefix + randomValue.substring(2), randomPrefix + randomValue.substring(3) }; JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue)); for (String optionOverride : optionOverrides) { underTest.add(optionOverride.toUpperCase(Locale.ENGLISH)); } }
private <T extends JvmOptions> ProcessBuilder create(JavaCommand<T> javaCommand) { List<String> commands = new ArrayList<>(); commands.add(buildJavaPath()); commands.addAll(javaCommand.getJvmOptions().getAll()); commands.addAll(buildClasspath(javaCommand)); commands.add(javaCommand.getClassName()); if (javaCommand.getReadsArgumentsFromFile()) { commands.add(buildPropertiesFile(javaCommand).getAbsolutePath()); } else { javaCommand.getArguments().forEach((key, value) -> { if (value != null && !value.isEmpty()) { commands.add("-E" + key + "=" + value); } }); } return create(javaCommand, commands); }
@Test public void addFromMandatoryProperty_accepts_property_equal_to_mandatory_option_and_does_not_add_it_twice() { JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue)); properties.put(randomPropertyName, randomPrefix + randomValue); underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName); assertThat(underTest.getAll()).containsOnly(randomPrefix + randomValue); }
@Test public void constructor_without_arguments_creates_empty_JvmOptions() { JvmOptions<JvmOptions> testJvmOptions = new JvmOptions<>(); assertThat(testJvmOptions.getAll()).isEmpty(); }
private void checkMandatoryOptionOverwrite(String value) { Map.Entry<String, String> overriddenMandatoryOption = mandatoryOptionFor(value); if (overriddenMandatoryOption != null) { throw new MessageException(String.format( "a JVM option can't overwrite mandatory JVM options. %s overwrites %s", value, overriddenMandatoryOption.getKey() + overriddenMandatoryOption.getValue())); } }
@Test public void addFromMandatoryProperty_reports_all_overriding_options_in_single_exception() { String overriding1 = randomPrefix; String overriding2 = randomPrefix + randomValue + randomAlphanumeric(1); properties.setProperty(randomPropertyName, "-foo " + overriding1 + " -bar " + overriding2); JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue)); expectedException.expect(MessageException.class); expectedException.expectMessage("a JVM option can't overwrite mandatory JVM options. " + "The following JVM options defined by property '" + randomPropertyName + "' are invalid: " + overriding1 + " overwrites " + randomPrefix + randomValue + ", " + overriding2 + " overwrites " + randomPrefix + randomValue); underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName); }
@Test @UseDataProvider("variousEmptyStrings") public void constructor_accepts_any_empty_option_value(String emptyString) { Map<String, String> mandatoryJvmOptions = shuffleThenToMap( Stream.of( IntStream.range(0, random.nextInt(10)).mapToObj(i -> new Option("-B", valueOf(i))), Stream.of(new Option("-prefix", emptyString))) .flatMap(s -> s)); new JvmOptions(mandatoryJvmOptions); }
@Test public void addFromMandatoryProperty_fails_with_IAE_if_property_contains_an_empty_value() { expectMissingPropertyIAE(this.randomPropertyName); underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName); }
private void setSystemPropertyToDefaultIfNotSet(JvmOptions jvmOptions, String httpsProperty, String httpProperty) { Optional<String> httpValue = getPropsValue(httpProperty); Optional<String> httpsValue = getPropsValue(httpsProperty); if (!httpsValue.isPresent() && httpValue.isPresent()) { jvmOptions.add("-D" + httpsProperty + "=" + httpValue.get()); } }
@Test @UseDataProvider("variousEmptyStrings") public void addFromMandatoryProperty_adds_single_option_of_property_with_trimming(String emptyString) { properties.put(randomPropertyName, emptyString + "-foo" + emptyString); underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName); assertThat(underTest.getAll()).containsOnly("-foo"); }
@Test public void add_throws_MessageException_if_option_starts_with_prefix_of_mandatory_option_but_has_different_value() { String[] optionOverrides = { randomPrefix, randomPrefix + randomAlphanumeric(1), randomPrefix + randomAlphanumeric(2), randomPrefix + randomAlphanumeric(3), randomPrefix + randomAlphanumeric(4), randomPrefix + randomValue.substring(1), randomPrefix + randomValue.substring(2), randomPrefix + randomValue.substring(3) }; JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue)); for (String optionOverride : optionOverrides) { try { underTest.add(optionOverride); fail("an MessageException should have been thrown"); } catch (MessageException e) { assertThat(e.getMessage()).isEqualTo("a JVM option can't overwrite mandatory JVM options. " + optionOverride + " overwrites " + randomPrefix + randomValue); } } }
@Test public void createWebCommand_returns_command_for_default_settings() { JavaCommand command = newFactory(new Properties()).createWebCommand(true); assertThat(command.getClassName()).isEqualTo("org.sonar.server.app.WebServer"); assertThat(command.getWorkDir().getAbsolutePath()).isEqualTo(homeDir.getAbsolutePath()); assertThat(command.getClasspath()) .containsExactly("./lib/common/*"); 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.WEB_SERVER); 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"); }
private void checkMandatoryOptionOverwrite(String propertyName, List<String> jvmOptionsFromProperty) { List<Match> matches = jvmOptionsFromProperty.stream() .map(jvmOption -> new Match(jvmOption, mandatoryOptionFor(jvmOption))) .filter(match -> match.getMandatoryOption() != null) .collect(Collectors.toList()); if (!matches.isEmpty()) { throw new MessageException(format( "a JVM option can't overwrite mandatory JVM options. The following JVM options defined by property '%s' are invalid: %s", propertyName, matches.stream() .map(m -> m.getOption() + " overwrites " + m.mandatoryOption.getKey() + m.mandatoryOption.getValue()) .collect(joining(", ")))); } }