geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject incubator-geode git commit: Complete implementation of OptionJFormatter.
Date Wed, 15 Jun 2016 00:28:08 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-835 e6e68e5b8 -> 33ed9d647


Complete implementation of OptionJFormatter.

* add more tests for OptionJFormatter
* use OptionJFormatter from JoptOptionParser
* add integration tests for --J= option


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/33ed9d64
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/33ed9d64
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/33ed9d64

Branch: refs/heads/feature/GEODE-835
Commit: 33ed9d6473e0cbfa66409899b15e4ebb7fdeb818
Parents: e6e68e5
Author: Kirk Lund <klund@apache.org>
Authored: Tue Jun 14 17:26:23 2016 -0700
Committer: Kirk Lund <klund@apache.org>
Committed: Tue Jun 14 17:26:23 2016 -0700

----------------------------------------------------------------------
 .../management/internal/cli/GfshParser.java     |   2 +
 .../cli/parser/jopt/JoptOptionParser.java       |   3 +-
 .../internal/cli/remote/CommandProcessor.java   |   3 +-
 .../internal/cli/util/OptionJFormatter.java     |  98 ++++++++------
 .../internal/cli/GfshParserIntegrationTest.java |  87 ++++++++----
 .../internal/cli/util/OptionJFormatterTest.java | 131 +++++++++++++++----
 6 files changed, 231 insertions(+), 93 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/33ed9d64/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/GfshParser.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/GfshParser.java
b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/GfshParser.java
index ce00d6c..0bf2f66 100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/GfshParser.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/GfshParser.java
@@ -893,6 +893,7 @@ public class GfshParser implements Parser {
           preConfigureConverters(commandTarget);
 
           try {
+            // TODO: next call invokes OptionJFormatter
             parse = commandTarget.getOptionParser().parse(gfshMethodTarget.getRemainingBuffer());
           } catch (CliException ce) {
             if (ce instanceof CliCommandOptionException) {
@@ -911,6 +912,7 @@ public class GfshParser implements Parser {
           }
 
           error = processArguments(parse, commandTarget, paramValMap, parameters, error);
+          // TODO: next call throws when space before closing "
           error = processOptions(parse, commandTarget, paramValMap, parameters, error);
 
           if (!error) {

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/33ed9d64/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/parser/jopt/JoptOptionParser.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/parser/jopt/JoptOptionParser.java
b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/parser/jopt/JoptOptionParser.java
index 5522d1e..777e705 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/parser/jopt/JoptOptionParser.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/parser/jopt/JoptOptionParser.java
@@ -39,6 +39,7 @@ import com.gemstone.gemfire.management.internal.cli.parser.SyntaxConstants;
 import com.gemstone.gemfire.management.internal.cli.parser.preprocessor.Preprocessor;
 import com.gemstone.gemfire.management.internal.cli.parser.preprocessor.PreprocessorUtils;
 import com.gemstone.gemfire.management.internal.cli.parser.preprocessor.TrimmedInput;
+import com.gemstone.gemfire.management.internal.cli.util.OptionJFormatter;
 
 /**
  * Implementation of {@link GfshOptionParser} which internally makes use of
@@ -114,7 +115,7 @@ public class JoptOptionParser implements GfshOptionParser {
     optionSet.setUserInput(userInput!=null?userInput.trim():"");
     if (userInput != null) {
       TrimmedInput input = PreprocessorUtils.trim(userInput);
-      String[] preProcessedInput = preProcess(input.getString());
+      String[] preProcessedInput = preProcess(new OptionJFormatter().formatCommand(input.getString()));
       joptsimple.OptionSet joptOptionSet = null;
       CliCommandOptionException ce = null;
       // int factor = 0;

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/33ed9d64/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/remote/CommandProcessor.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/remote/CommandProcessor.java
b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/remote/CommandProcessor.java
index 68da8a4..b62f922 100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/remote/CommandProcessor.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/remote/CommandProcessor.java
@@ -30,6 +30,7 @@ import com.gemstone.gemfire.management.internal.cli.GfshParser;
 import com.gemstone.gemfire.management.internal.cli.LogWrapper;
 import com.gemstone.gemfire.management.internal.cli.result.ResultBuilder;
 import com.gemstone.gemfire.management.internal.cli.util.CommentSkipHelper;
+import com.gemstone.gemfire.management.internal.cli.util.OptionJFormatter;
 import com.gemstone.gemfire.management.internal.security.ResourceOperation;
 import com.gemstone.gemfire.security.NotAuthorizedException;
 
@@ -90,7 +91,7 @@ public class CommandProcessor {
 
     CommentSkipHelper commentSkipper = new CommentSkipHelper();
     String commentLessLine = commentSkipper.skipComments(cmdStmt.getCommandString());
-    // TODO:
+
     if (commentLessLine != null && !commentLessLine.isEmpty()) {
       CommandExecutionContext.setShellEnv(cmdStmt.getEnv());
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/33ed9d64/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatter.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatter.java
b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatter.java
index 1aba057..4534f19 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatter.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatter.java
@@ -19,62 +19,84 @@ package com.gemstone.gemfire.management.internal.cli.util;
 public class OptionJFormatter {
 
   private static final String J_OPTION = "--J=";
-  private static final String QUOTE = "\"";
-  private static final String SPACE = " ";
+  private static final char QUOTE = '\"';
+  private static final char SPACE = ' ';
 
+  private boolean quotesOpened;
+  private int previousSpace;
   private String command;
-  private int index;
-  private StringBuilder formatted = new StringBuilder();
-
-  private void everythingBeforeJ() {
-    int indexOfJ = this.command.indexOf(J_OPTION);
-    this.formatted.append(this.command.substring(0, indexOfJ));
-  }
-
-  private int nextJ(int start) {
-    int startOfJ = this.command.indexOf(J_OPTION, start);
-    int nextSpace = this.command.indexOf(SPACE, startOfJ);
-    if (start+1 != startOfJ){
-      this.formatted.append(command.substring(start, startOfJ));
-    }
-    this.formatted.append(J_OPTION);
-    this.formatted.append(QUOTE);
-    if (nextSpace == -1) {
-      this.formatted.append(this.command.substring(startOfJ + 4));
-    } else {
-      this.formatted.append(this.command.substring(startOfJ + 4, nextSpace));
-    }
-    this.formatted.append(QUOTE);
-    return nextSpace;
-  }
+  private StringBuilder formatted;
 
   public String formatCommand(String command){
     if (!containsJopt(command)) {
       return command;
     }
     this.command = command;
+    this.formatted = new StringBuilder();
+    quotesOpened = false;
+
+    int nextJ = this.command.indexOf(J_OPTION);
 
-    int start = 0;
-   // everythingBeforeJ();
+    while (nextJ > -1) {
+      String stringBeforeJ = this.command.substring(0, nextJ+4);
+      if (quotesOpened && stringBeforeJ.contains("--")){
+        previousSpace = stringBeforeJ.indexOf("--") - 1;
+        while (stringBeforeJ.charAt(previousSpace) == SPACE){
+          previousSpace--;
+        }
+        stringBeforeJ = stringBeforeJ.substring(0,previousSpace + 1) + QUOTE + stringBeforeJ.substring(previousSpace
+ 1);
+        quotesOpened = false;
+      }
 
-    while (start > -1) {
-      start = nextJ(start);
+      this.command = this.command.substring(nextJ+4);
 
-      boolean noMoreJs = command.indexOf(J_OPTION, start) == -1;
-      boolean hasMoreOptions = start > -1;
+      this.formatted.append(stringBeforeJ);
+      if (!this.command.startsWith(""+QUOTE)){
+        this.formatted.append(QUOTE);
+        quotesOpened = true;
+      }
+      quotesOpened = true;
 
-      if (noMoreJs){
-        formatted.append(command.substring(start));
-        break;
-      } else if (hasMoreOptions) {
-        this.formatted.append(SPACE);
+      int nextSpace = this.command.indexOf(SPACE);
+      String stringAfterJ = null;
+      if (nextSpace > -1) {
+        stringAfterJ = this.command.substring(0, nextSpace);
+        this.command = this.command.substring(nextSpace);
+      } else {
+        stringAfterJ = this.command.substring(0);
+        this.command = "";
       }
+
+      this.formatted.append(stringAfterJ);
+      if (stringAfterJ.endsWith("\"")){
+        quotesOpened = false;
+      }
+
+      nextSpace = this.command.indexOf(SPACE);
+
+      if (nextSpace == -1) {
+        if (!stringAfterJ.endsWith("" + QUOTE)) {
+          this.formatted.append(QUOTE);
+          quotesOpened = false;
+        }
+      } else if (!this.formatted.toString().endsWith(""+QUOTE)) {
+        if(this.command.startsWith(" --")){
+          this.formatted.append(QUOTE);
+          quotesOpened = false;
+        }
+      }
+
+      if (!containsJopt(this.command)){
+        this.formatted.append(this.command);
+      }
+
+      nextJ = this.command.indexOf(J_OPTION);
     }
 
     return formatted.toString();
   }
 
-  boolean containsJopt(String cmd){
+  public boolean containsJopt(String cmd){
     if (cmd.contains("--J")){
       return true;
     }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/33ed9d64/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/GfshParserIntegrationTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/GfshParserIntegrationTest.java
b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/GfshParserIntegrationTest.java
index dd42028..17e78a5 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/GfshParserIntegrationTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/GfshParserIntegrationTest.java
@@ -18,6 +18,7 @@ package com.gemstone.gemfire.management.internal.cli;
 
 import static org.assertj.core.api.Assertions.*;
 
+import java.util.Arrays;
 import java.util.Map;
 
 import org.junit.After;
@@ -38,10 +39,7 @@ public class GfshParserIntegrationTest {
   public void setUp() throws Exception {
     CommandManager.clearInstance();
     this.commandManager = CommandManager.getInstance(true);
-
     this.parser = new GfshParser(commandManager);
-
-    //CliUtil.isGfshVM = false;
   }
 
   @After
@@ -49,34 +47,73 @@ public class GfshParserIntegrationTest {
     CommandManager.clearInstance();
   }
 
-  @Test
-  public void testCommandWithoutOptions() throws Exception {
-    String input = "start locator --name=loc1 --J=\"-Dgemfire.http-service-port=8080\"";
-    //String input = "start locator --name=loc1";
+  private Map<String, String> params(String input, String commandName, String commandMethod)
{
     ParseResult parseResult = parser.parse(input);
-    assertThat(parseResult).isNotNull().isExactlyInstanceOf(GfshParseResult.class);
-
     GfshParseResult gfshParseResult = (GfshParseResult) parseResult;
+    Map<String, String> params = gfshParseResult.getParamValueStrings();
+    for (String param : params.keySet()) {
+      System.out.println(param + "=" + params.get(param));
+    }
 
-    assertThat(gfshParseResult.getMethod().getName()).isEqualTo("startLocator");
-    assertThat(gfshParseResult.getUserInput()).isEqualTo(input);
+    assertThat(gfshParseResult.getMethod().getName()).isEqualTo(commandMethod);
+    assertThat(gfshParseResult.getUserInput()).isEqualTo(input.trim());
+    assertThat(gfshParseResult.getCommandName()).isEqualTo(commandName);
 
-    assertThat(gfshParseResult.getArguments()).isNotNull();
-    boolean foundName = false;
-    for (Object arg : gfshParseResult.getArguments()) {
-      if ("loc1".equals(arg)) {
-        foundName = true;
-      }
-    }
-    assertThat(foundName).isTrue();
+    return params;
+  }
 
-    assertThat(gfshParseResult.getCommandName()).isEqualTo("start locator");
+  @Test
+  public void oneJOptionWithQuotes() throws Exception {
+    String input = "start locator  --J=\"-Dgemfire.http-service-port=8080\" --name=loc1";
+    Map<String, String> params = params(input, "start locator", "startLocator");
 
-    Map<String, String> params = gfshParseResult.getParamValueStrings();
-    assertThat(params).isNotNull().isNotEmpty();
+    assertThat(params.get("name")).isEqualTo("loc1");
+    assertThat(params.get("J")).isEqualTo("\"-Dgemfire.http-service-port=8080\"");
+  }
 
-    for (String param : params.keySet()) {
-      System.out.println(param + "=" + params.get(param));
-    }
+  @Test
+  public void oneJOptionWithSpaceInQuotes() throws Exception {
+    String input = "start locator  --J=\"-Dgemfire.http-service-port= 8080\" --name=loc1";
+    Map<String, String> params = params(input, "start locator", "startLocator");
+
+    assertThat(params.get("name")).isEqualTo("loc1");
+    assertThat(params.get("J")).isEqualTo("\"-Dgemfire.http-service-port= 8080\"");
   }
+
+  @Test
+  public void oneJOption() throws Exception {
+    String input = "start locator --J=-Dgemfire.http-service-port=8080 --name=loc1";
+    Map<String, String> params = params(input, "start locator", "startLocator");
+
+    assertThat(params.get("name")).isEqualTo("loc1");
+    assertThat(params.get("J")).isEqualTo("\"-Dgemfire.http-service-port=8080\"");
+  }
+
+  @Test
+  public void twoJOptions() throws Exception {
+    String input = "start locator --J=-Dgemfire.http-service-port=8080 --name=loc1 --J=-Ddummythinghere";
+    Map<String, String> params = params(input, "start locator", "startLocator");
+
+    assertThat(params.get("name")).isEqualTo("loc1");
+    assertThat(params.get("J")).isEqualTo("\"-Dgemfire.http-service-port=8080\",\"-Ddummythinghere\"");
+  }
+
+  @Test
+  public void twoJOptionsOneWithQuotesOneWithout() throws Exception {
+    String input = "start locator --J=\"-Dgemfire.http-service-port=8080\" --name=loc1 --J=-Ddummythinghere";
+    Map<String, String> params = params(input, "start locator", "startLocator");
+
+    assertThat(params.get("name")).isEqualTo("loc1");
+    assertThat(params.get("J")).isEqualTo("\"-Dgemfire.http-service-port=8080\",\"-Ddummythinghere\"");
+  }
+
+  @Test
+  public void oneJOptionWithQuotesAndLotsOfSpaces() throws Exception {
+    String input = "start locator       --J=\"-Dgemfire.http-service-port=8080\"      --name=loc1
        ";
+    Map<String, String> params = params(input, "start locator", "startLocator");
+
+    assertThat(params.get("name")).isEqualTo("loc1");
+    assertThat(params.get("J")).isEqualTo("\"-Dgemfire.http-service-port=8080\"");
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/33ed9d64/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatterTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatterTest.java
b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatterTest.java
index b73ccbe..12dcd5e 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatterTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/util/OptionJFormatterTest.java
@@ -19,7 +19,7 @@ package com.gemstone.gemfire.management.internal.cli.util;
 import static org.assertj.core.api.Assertions.*;
 import static org.junit.Assert.*;
 
-import org.junit.Ignore;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
@@ -28,87 +28,162 @@ import com.gemstone.gemfire.test.junit.categories.UnitTest;
 @Category(UnitTest.class)
 public class OptionJFormatterTest {
 
-  private static final String quote = "\"";
+  private OptionJFormatter formatter;
+  
+  @Before
+  public void setUp() {
+    this.formatter = new OptionJFormatter();
+  }
 
   @Test
   public void containsJoptShouldReturnTrueIfCmdHasJ() {
     String cmd = "start locator --name=loc1 --J=-Dfoo=bar";
-    OptionJFormatter ojf = new OptionJFormatter();
-    assertTrue(ojf.containsJopt(cmd));
+    assertTrue(this.formatter.containsJopt(cmd));
   }
 
   @Test
   public void containsJoptShouldReturnFalseIfCmdDoesntHaveJ() {
     String cmd = "start locator --name=loc1 ";
-    OptionJFormatter ojf = new OptionJFormatter();
-    assertFalse(ojf.containsJopt(cmd));
+    assertFalse(this.formatter.containsJopt(cmd));
   }
 
   @Test
   public void containsJoptShouldReturnTrueIfCmdHasMultipleJ() {
     String cmd = "start locator --name=loc1 --J=-Dfoo=bar --J=-Dbar=foo";
-    OptionJFormatter ojf = new OptionJFormatter();
-    assertTrue(ojf.containsJopt(cmd));
+    assertTrue(this.formatter.containsJopt(cmd));
   }
 
   @Test
   public void valueWithoutQuotesReturnsWithQuotes() {
     String cmd = "start locator --name=loc1 --J=-Dfoo=bar";
-    OptionJFormatter ojf = new OptionJFormatter();
-    String formattedCmd = ojf.formatCommand(cmd);
+    String formattedCmd = this.formatter.formatCommand(cmd);
 
-    String expected = "start locator --name=loc1 --J=" + quote + "-Dfoo=bar" + quote;
+    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";
-    OptionJFormatter ojf = new OptionJFormatter();
-    String formattedCmd = ojf.formatCommand(cmd);
+    String formattedCmd = this.formatter.formatCommand(cmd);
 
-    String expected = "start locator --J=" + quote + "-Dfoo=bar" + quote + " --name=loc1";
+    String expected = "start locator --J=\"-Dfoo=bar\" --name=loc1";
     assertThat(formattedCmd).isEqualTo(expected);
   }
 
   @Test
   public void nullShouldThrowNullPointerException() {
-    assertThatThrownBy(() -> new OptionJFormatter().formatCommand(null)).isExactlyInstanceOf(NullPointerException.class);
+    assertThatThrownBy(() -> this.formatter.formatCommand(null)).isExactlyInstanceOf(NullPointerException.class);
   }
 
   @Test
   public void emptyShouldThrowNullPointerException() {
-    assertThat(new OptionJFormatter().formatCommand("")).isEqualTo("");
+    assertThat(this.formatter.formatCommand("")).isEqualTo("");
   }
 
   @Test
-  public void multipleJOptionsShould_something() {
+  public void multipleJOptions() {
     String cmd = "start locator --name=loc1 --J=-Dfoo=bar --J=-Dbar=foo";
-    OptionJFormatter ojf = new OptionJFormatter();
-    String formattedCmd = ojf.formatCommand(cmd);
+    String formattedCmd = this.formatter.formatCommand(cmd);
 
-    String expected = "start locator --name=loc1 --J=" + quote + "-Dfoo=bar" + quote + "
--J=" + quote + "-Dbar=foo" + quote;
+    String expected = "start locator --name=loc1 --J=\"-Dfoo=bar\" --J=\"-Dbar=foo\"";
     assertThat(formattedCmd).isEqualTo(expected);
   }
 
   @Test
-  public void multipleJOptionsWithSomethingAfterShould_something() {
+  public void multipleJOptionsWithSomethingAfter() {
     String cmd = "start locator --name=loc1 --J=-Dfoo=bar --J=-Dbar=foo --group=locators";
-    OptionJFormatter ojf = new OptionJFormatter();
-    String formattedCmd = ojf.formatCommand(cmd);
+    String formattedCmd = this.formatter.formatCommand(cmd);
 
-    String expected = "start locator --name=loc1 --J=" + quote + "-Dfoo=bar" + quote + "
--J=" + quote + "-Dbar=foo" + quote + " --group=locators";
+    String expected = "start locator --name=loc1 --J=\"-Dfoo=bar\" --J=\"-Dbar=foo\" --group=locators";
     assertThat(formattedCmd).isEqualTo(expected);
   }
 
   @Test
-  public void multipleJOptionsWithSomethingBetweenShould_something() {
+  public void multipleJOptionsWithSomethingBetween() {
     String cmd = "start locator --name=loc1 --J=-Dfoo=bar --group=locators --J=-Dbar=foo";
-    OptionJFormatter ojf = new OptionJFormatter();
-    String formattedCmd = ojf.formatCommand(cmd);
+    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 valueWithMissingEndQuote() {
+    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 valueWithMissingStartQuote() {
+    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 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);
+  }
 
-    String expected = "start locator --name=loc1 --J=" + quote + "-Dfoo=bar" + quote + "
--group=locators --J=" + quote + "-Dbar=foo" + quote;
+  @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);
+  }
+
 }


Mime
View raw message