HyphenFormatter { public String formatCommand(String command) { if (!containsOption(command)) { return command; } this.formatted = new StringBuilder(); List<String> strings = split(command); for (String string : strings) { if (string.contains(EQUAL_HYPHEN)) { int indexOfEquals = string.indexOf(EQUAL); formatted.append(string.substring(0, indexOfEquals + 1)); formatted.append(QUOTE); formatted.append(string.substring(indexOfEquals + 1)); formatted.append(QUOTE); } else { formatted.append(string); } formatted.append(SPACE); } return formatted.toString().trim(); }  String formatCommand(String command);  }
@Test public void valueWithoutQuotesReturnsWithQuotes() { String cmd = "start locator --name=loc1 --J=-Dfoo=bar"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=bar\""; assertThat(formattedCmd).isEqualTo(expected); }
@Test public void valueWithoutQuotesReturnsWithQuotes_2() { String cmd = "start locator --J=-Dfoo=bar --name=loc1"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --J=\"-Dfoo=bar\" --name=loc1"; assertThat(formattedCmd).isEqualTo(expected); }
@Test public void valueWithHyphenWithoutQuotesFails() { String cmd = "rebalance --exclude-region=" + SEPARATOR + "GemfireDataCommandsDUnitTestRegion2 --simulate=true --time-out=-1"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "rebalance --exclude-region=" + SEPARATOR + "GemfireDataCommandsDUnitTestRegion2 --simulate=true --time-out=\"-1\""; assertThat(formattedCmd).isEqualTo(expected); }
@Test public void valueWithHyphenWithoutQuotes() { String cmd = "rebalance --exclude-region=" + SEPARATOR + "GemfireDataCommandsDUnitTestRegion2 --simulate=true --time-out=-1"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "rebalance --exclude-region=" + SEPARATOR + "GemfireDataCommandsDUnitTestRegion2 --simulate=true --time-out=\"-1\""; assertThat(formattedCmd).isEqualTo(expected); }
@Test public void nullShouldThrowNullPointerException() { assertThatThrownBy(() -> this.formatter.formatCommand(null)) .isExactlyInstanceOf(NullPointerException.class); }
@Test public void emptyShouldThrowNullPointerException() { assertThat(this.formatter.formatCommand("")).isEqualTo(""); }
@Test public void multipleJOptions() { String cmd = "start locator --name=loc1 --J=-Dfoo=bar --J=-Dbar=foo"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=bar\" --J=\"-Dbar=foo\""; assertThat(formattedCmd).isEqualTo(expected); }
@Test public void multipleJOptionsWithSomethingAfter() { String cmd = "start locator --name=loc1 --J=-Dfoo=bar --J=-Dbar=foo --group=locators"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=bar\" --J=\"-Dbar=foo\" --group=locators"; assertThat(formattedCmd).isEqualTo(expected); }
@Test public void multipleJOptionsWithSomethingBetween() { String cmd = "start locator --name=loc1 --J=-Dfoo=bar --group=locators --J=-Dbar=foo"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=bar\" --group=locators --J=\"-Dbar=foo\""; assertThat(formattedCmd).isEqualTo(expected); }
@Test public void valueWithQuotes() { String cmd = "start locator --name=loc1 --J=\"-Dfoo=bar\""; String formattedCmd = this.formatter.formatCommand(cmd); assertThat(formattedCmd).isEqualTo(cmd); }
@Test public void oneValueWithQuotesOneWithout() { String cmd = "start locator --name=loc1 --J=\"-Dfoo=bar\" --J=-Dfoo=bar"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=bar\" --J=\"-Dfoo=bar\""; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void oneValueWithoutQuotesOneWith() { String cmd = "start locator --name=loc1 --J=-Dfoo=bar --J=\"-Dfoo=bar\""; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=bar\" --J=\"-Dfoo=bar\""; assertThat(formattedCmd).isEqualTo(expected); }
@Test public void twoValuesWithQuotes() { String cmd = "start locator --name=loc1 --J=\"-Dfoo=bar\" --J=\"-Dfoo=bar\""; String formattedCmd = this.formatter.formatCommand(cmd); assertThat(formattedCmd).as(cmd).isEqualTo(cmd); }
@Test public void valueContainingQuotes() { String cmd = "start locator --name=loc1 --J=\"-Dfoo=region\""; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=region\""; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void valueContainingQuotesAndSpace() { String cmd = "start locator --name=loc1 --J=\"-Dfoo=my phrase\""; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=my phrase\""; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void valueContainingQuotesAndMultipleSpaces() { String cmd = "start locator --name=loc1 --J=\"-Dfoo=this is a phrase\""; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=this is a phrase\""; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void valueContainingMultipleJWithSpaces() { String cmd = "start locator --name=loc1 --J=-Dfoo=this is a phrase --J=\"-Dfoo=a short sentence\""; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=this is a phrase\" --J=\"-Dfoo=a short sentence\""; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void valueContainingMultipleJWithSpaces2() { String cmd = "start locator --name=loc1 --J=\"-Dfoo=this is a phrase \" --J=\"-Dfoo=a short sentence\""; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=this is a phrase \" --J=\"-Dfoo=a short sentence\""; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void optionAfterOneJOption() { String cmd = "start locator --name=loc1 --J=-Dfoo=bar --http-service=8080"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --J=\"-Dfoo=bar\" --http-service=8080"; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void optionWithMoreThanOneHyphen() { String cmd = "start locator --name=loc1 --http-service-port=8080"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start locator --name=loc1 --http-service-port=8080"; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void optionWithOneHyphenAfterOneJOption() { String cmd = "start server --name=me3 --J=-Dgemfire.jmx-manager=true --redis-port=8080"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start server --name=me3 --J=\"-Dgemfire.jmx-manager=true\" --redis-port=8080"; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void optionWithMoreThanOneHyphenAfterOneJOption() { String cmd = "start server --name=me3 --J=-Dgemfire.jmx-manager=true --http-service-port=8080"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start server --name=me3 --J=\"-Dgemfire.jmx-manager=true\" --http-service-port=8080"; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void optionWithOneHyphenAfterTwoJOptions() { String cmd = "start server --name=me3 --J=-Dgemfire.jmx-manager=true --J=-Dgemfire.jmx-manager-start=true --redis-port=8080"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start server --name=me3 --J=\"-Dgemfire.jmx-manager=true\" --J=\"-Dgemfire.jmx-manager-start=true\" --redis-port=8080"; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void optionWithMoreThanOneHyphenAfterTwoJOptions() { String cmd = "start server --name=me3 --J=-Dgemfire.jmx-manager=true --J=-Dgemfire.jmx-manager-start=true --http-service-port=8080"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start server --name=me3 --J=\"-Dgemfire.jmx-manager=true\" --J=\"-Dgemfire.jmx-manager-start=true\" --http-service-port=8080"; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }
@Test public void optionWithMoreThanOneHyphenWithoutValueAfterJOptions() { String cmd = "start server --name=Server2 --log-level=config --J=-Dgemfire.locators=localhost[10334] --disable-default-server"; String formattedCmd = this.formatter.formatCommand(cmd); String expected = "start server --name=Server2 --log-level=config --J=\"-Dgemfire.locators=localhost[10334]\" --disable-default-server"; assertThat(formattedCmd).as(cmd).isEqualTo(expected); }