geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zho...@apache.org
Subject [18/51] [abbrv] geode git commit: Add GfshParserRule to facilitate command unit tests
Date Wed, 14 Jun 2017 22:07:05 GMT
Add GfshParserRule to facilitate command unit tests


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

Branch: refs/heads/feature/GEM-1483
Commit: 0ef7f56bd7a82afbd2af1e6968aaaed4303905c7
Parents: 8a10c1a
Author: Jinmei Liao <jiliao@pivotal.io>
Authored: Sun Jun 4 23:51:42 2017 -0700
Committer: Jared Stewart <jstewart@pivotal.io>
Committed: Fri Jun 9 15:17:11 2017 -0700

----------------------------------------------------------------------
 .../management/internal/cli/GfshParser.java     |  33 +--
 .../internal/cli/remote/CommandProcessor.java   |   3 +-
 .../management/internal/cli/shell/Gfsh.java     |   3 +-
 .../cli/GfshParserAutoCompletionTest.java       | 222 +++++++++----------
 .../internal/cli/GfshParserConverterTest.java   |  95 +++-----
 .../internal/cli/GfshParserParsingTest.java     |  10 +-
 .../commands/LauncherLifecycleCommandsTest.java |  51 +++++
 .../geode/test/dunit/rules/GfshParserRule.java  | 126 +++++++++++
 8 files changed, 321 insertions(+), 222 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/0ef7f56b/geode-core/src/main/java/org/apache/geode/management/internal/cli/GfshParser.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/GfshParser.java
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/GfshParser.java
index b5c24cb..c31c50d 100755
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/GfshParser.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/GfshParser.java
@@ -53,34 +53,20 @@ public class GfshParser extends SimpleParser {
   private static Pattern PATTERN =
       Pattern.compile("\\s*([^\\s']*)'([^']*)'\\s+|\\s*([^\\s\"]*)\"([^\"]*)\"\\s+|\\S+");
 
-
-  private CommandManager commandManager = null;
-
-  public GfshParser() {
-    this(null);
-  }
-
-  public GfshParser(Properties cacheProperties) {
-    this.commandManager = new CommandManager(cacheProperties);
-
+  public GfshParser(CommandManager commandManager) {
     for (CommandMarker command : commandManager.getCommandMarkers()) {
       add(command);
     }
 
-    List<Converter<?>> converters = commandManager.getConverters();
-    for (Converter<?> converter : converters) {
+    for (Converter<?> converter : commandManager.getConverters()) {
       if (converter.getClass().isAssignableFrom(ArrayConverter.class)) {
         ArrayConverter arrayConverter = (ArrayConverter) converter;
-        arrayConverter.setConverters(new HashSet<>(converters));
+        arrayConverter.setConverters(new HashSet<>(commandManager.getConverters()));
       }
       add(converter);
     }
   }
 
-  public CommandManager getCommandManager() {
-    return commandManager;
-  }
-
   static String convertToSimpleParserInput(String userInput) {
     List<String> inputTokens = splitUserInput(userInput);
     return getSimpleParserInputFromTokens(inputTokens);
@@ -294,19 +280,6 @@ public class GfshParser extends SimpleParser {
     return cursor;
   }
 
-  // convenience method for testing
-  int completeAdvanced(String userInput, final List<Completion> candidates) {
-    return completeAdvanced(userInput, userInput.length(), candidates);
-  }
-
-  /**
-   * test only used to demonstrate what's the super class's completeAdvanced behavior
-   *
-   */
-  int completeSuperAdvanced(String userInput, final List<Completion> candidates) {
-    return super.completeAdvanced(userInput, userInput.length(), candidates);
-  }
-
   /**
    * @param buffer use the buffer to find the completion candidates
    *

http://git-wip-us.apache.org/repos/asf/geode/blob/0ef7f56b/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandProcessor.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandProcessor.java
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandProcessor.java
index 7ec7699..6e5b650 100755
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandProcessor.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandProcessor.java
@@ -19,6 +19,7 @@ import org.apache.geode.internal.security.SecurityServiceFactory;
 import org.apache.geode.management.cli.CommandProcessingException;
 import org.apache.geode.management.cli.CommandStatement;
 import org.apache.geode.management.cli.Result;
+import org.apache.geode.management.internal.cli.CommandManager;
 import org.apache.geode.management.internal.cli.GfshParser;
 import org.apache.geode.management.internal.cli.LogWrapper;
 import org.apache.geode.management.internal.cli.result.ResultBuilder;
@@ -55,7 +56,7 @@ public class CommandProcessor {
 
   public CommandProcessor(Properties cacheProperties, SecurityService securityService)
       throws ClassNotFoundException, IOException {
-    this.gfshParser = new GfshParser(cacheProperties);
+    this.gfshParser = new GfshParser(new CommandManager(cacheProperties));
     this.executionStrategy = new RemoteExecutionStrategy();
     this.logWrapper = LogWrapper.getInstance();
     this.securityService = securityService;

http://git-wip-us.apache.org/repos/asf/geode/blob/0ef7f56b/geode-core/src/main/java/org/apache/geode/management/internal/cli/shell/Gfsh.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/shell/Gfsh.java
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/shell/Gfsh.java
index c5ff6b6..74387d6 100755
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/shell/Gfsh.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/shell/Gfsh.java
@@ -26,6 +26,7 @@ import org.apache.geode.internal.util.SunAPINotFoundException;
 import org.apache.geode.management.cli.CommandProcessingException;
 import org.apache.geode.management.cli.Result;
 import org.apache.geode.management.internal.cli.CliUtil;
+import org.apache.geode.management.internal.cli.CommandManager;
 import org.apache.geode.management.internal.cli.GfshParser;
 import org.apache.geode.management.internal.cli.LogWrapper;
 import org.apache.geode.management.internal.cli.i18n.CliStrings;
@@ -210,7 +211,7 @@ public class Gfsh extends JLineShell {
     initializeEnvironment();
     // 7. Create Roo/SpringShell framework objects
     this.executionStrategy = new GfshExecutionStrategy(this);
-    this.parser = new GfshParser();
+    this.parser = new GfshParser(new CommandManager());
     // 8. Set max History file size
     setHistorySize(gfshConfig.getHistorySize());
 

http://git-wip-us.apache.org/repos/asf/geode/blob/0ef7f56b/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserAutoCompletionTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserAutoCompletionTest.java
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserAutoCompletionTest.java
index a122de0..aa97795 100644
--- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserAutoCompletionTest.java
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserAutoCompletionTest.java
@@ -18,268 +18,250 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertTrue;
 
 import org.apache.geode.management.internal.cli.i18n.CliStrings;
+import org.apache.geode.test.dunit.rules.GfshParserRule;
 import org.apache.geode.test.junit.categories.IntegrationTest;
-import org.junit.Before;
-import org.junit.BeforeClass;
+import org.junit.ClassRule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.springframework.shell.core.Completion;
 
-import java.util.ArrayList;
-import java.util.List;
-
 @Category(IntegrationTest.class)
 public class GfshParserAutoCompletionTest {
-  private static GfshParser parser;
-  private List<Completion> candidates;
-  private String buffer;
-  private int cursor;
-
-  @BeforeClass
-  public static void setUpClass() throws Exception {
-    parser = new GfshParser();
-  }
+  @ClassRule
+  public static GfshParserRule parser = new GfshParserRule();
 
-  @Before
-  public void setUp() throws Exception {
-    this.candidates = new ArrayList<>();
-  }
+  private String buffer;
+  private GfshParserRule.CommandCandidate candidate;
 
   @Test
   public void testCompletionDescibe() throws Exception {
     buffer = "describe";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(7);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo("describe client");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(7);
+    assertThat(candidate.getFirstCandidate()).isEqualTo("describe client");
   }
 
   @Test
   public void testCompletionDescibeWithSpace() throws Exception {
     buffer = "describe ";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(7);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo("describe client");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(7);
+    assertThat(candidate.getFirstCandidate()).isEqualTo("describe client");
   }
 
   @Test
   public void testCompletionDeploy() throws Exception {
     buffer = "deploy";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(3);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + " --dir");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(3);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + " --dir");
   }
 
   @Test
   public void testCompletionDeployWithSpace() throws Exception {
     buffer = "deploy ";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(3);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "--dir");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(3);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "--dir");
   }
 
   @Test
   public void testCompleteWithRequiredOption() throws Exception {
     buffer = "describe config";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(1);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + " --member");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(1);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + " --member");
   }
 
   @Test
   public void testCompleteWithRequiredOptionWithSpace() throws Exception {
     buffer = "describe config ";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(1);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "--member");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(1);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "--member");
   }
 
   @Test
   public void testCompleteCommand() throws Exception {
     buffer = "start ser";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(1);
-    assertThat("start server").isEqualTo(getCompleted(buffer, cursor, candidates.get(0)));
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(1);
+    assertThat("start server").isEqualTo(candidate.getFirstCandidate());
   }
 
   @Test
   public void testCompleteOptionWithOnlyOneCandidate() throws Exception {
     buffer = "start server --nam";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(1);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "e");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(1);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "e");
   }
 
   @Test
   public void testCompleteOptionWithMultipleCandidates() throws Exception {
     buffer = "start server --name=jinmei --loc";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(3);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0)))
-        .isEqualTo(buffer + "ator-wait-time");
-    assertThat(getCompleted(buffer, cursor, candidates.get(1))).isEqualTo(buffer + "ators");
-    assertThat(getCompleted(buffer, cursor, candidates.get(2))).isEqualTo(buffer + "k-memory");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(3);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "ator-wait-time");
+    assertThat(candidate.getCandidate(1)).isEqualTo(buffer + "ators");
+    assertThat(candidate.getCandidate(2)).isEqualTo(buffer + "k-memory");
   }
 
   @Test
   public void testCompleteWithExtraSpace() throws Exception {
     buffer = "start server --name=name1  --se";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo("start server --name=name1  ".length());
-    assertThat(candidates.size()).isEqualTo(3);
-    assertTrue(candidates.contains(new Completion("--server-port")));
-    assertThat(getCompleted(buffer, cursor, candidates.get(0)))
-        .isEqualTo(buffer + "curity-properties-file");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo("start server --name=name1  ".length());
+    assertThat(candidate.size()).isEqualTo(3);
+    assertTrue(candidate.getCandidates().contains(new Completion("--server-port")));
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "curity-properties-file");
   }
 
   @Test
   public void testCompleteWithDashInTheEnd() throws Exception {
     buffer = "start server --name=name1 --";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo(buffer.length() - 2);
-    assertThat(candidates.size()).isEqualTo(50);
-    assertTrue(candidates.contains(new Completion("--properties-file")));
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "J");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo(buffer.length() - 2);
+    assertThat(candidate.size()).isEqualTo(50);
+    assertTrue(candidate.getCandidates().contains(new Completion("--properties-file")));
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "J");
   }
 
   @Test
   public void testCompleteWithSpace() throws Exception {
     buffer = "start server --name=name1 ";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo(buffer.length() - 1);
-    assertThat(candidates.size()).isEqualTo(50);
-    assertTrue(candidates.contains(new Completion(" --properties-file")));
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "--J");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo(buffer.length() - 1);
+    assertThat(candidate.size()).isEqualTo(50);
+    assertTrue(candidate.getCandidates().contains(new Completion(" --properties-file")));
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "--J");
   }
 
   @Test
   public void testCompleteWithOutSpace() throws Exception {
     buffer = "start server --name=name1";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo(buffer.length());
-    assertThat(candidates.size()).isEqualTo(50);
-    assertTrue(candidates.contains(new Completion(" --properties-file")));
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + " --J");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo(buffer.length());
+    assertThat(candidate.size()).isEqualTo(50);
+    assertTrue(candidate.getCandidates().contains(new Completion(" --properties-file")));
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + " --J");
   }
 
   @Test
   public void testCompleteJ() throws Exception {
     buffer = "start server --name=name1 --J=";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo(buffer.length());
-    assertThat(candidates.size()).isEqualTo(0);
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo(buffer.length());
+    assertThat(candidate.size()).isEqualTo(0);
   }
 
   @Test
   public void testCompleteWithValue() throws Exception {
     buffer = "start server --name=name1 --J";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo(buffer.length());
-    assertThat(candidates.size()).isEqualTo(0);
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo(buffer.length());
+    assertThat(candidate.size()).isEqualTo(0);
   }
 
   @Test
   public void testCompleteWithDash() throws Exception {
     buffer = "start server --name=name1 --J=-Dfoo.bar --";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(49);
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(49);
   }
 
   @Test
   public void testCompleteWithMultipleJ() throws Exception {
     buffer = "start server --name=name1 --J=-Dme=her --J=-Dfoo=bar --l";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo("start server --name=name1 --J=-Dme=her --J=-Dfoo=bar ".length());
-    assertThat(candidates.size()).isEqualTo(4);
-    assertTrue(candidates.contains(new Completion("--locators")));
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor())
+        .isEqualTo("start server --name=name1 --J=-Dme=her --J=-Dfoo=bar ".length());
+    assertThat(candidate.size()).isEqualTo(4);
+    assertTrue(candidate.getCandidates().contains(new Completion("--locators")));
   }
 
   @Test
   public void testMultiJComplete() throws Exception {
     buffer = "start server --name=name1 --J=-Dtest=test1 --J=-Dfoo=bar";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo(buffer.length());
-    assertThat(candidates.size()).isEqualTo(49);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0)))
-        .isEqualTo(buffer + " --assign-buckets");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo(buffer.length());
+    assertThat(candidate.size()).isEqualTo(49);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + " --assign-buckets");
   }
 
   @Test
   public void testMultiJCompleteWithDifferentOrder() throws Exception {
     buffer = "start server --J=-Dtest=test1 --J=-Dfoo=bar --name=name1";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo(buffer.length());
-    assertThat(candidates.size()).isEqualTo(49);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0)))
-        .isEqualTo(buffer + " --assign-buckets");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo(buffer.length());
+    assertThat(candidate.size()).isEqualTo(49);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + " --assign-buckets");
   }
 
   @Test
   public void testJComplete3() throws Exception {
     buffer = "start server --name=name1 --locators=localhost --J=-Dfoo=bar";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo(buffer.length());
-    assertThat(candidates.size()).isEqualTo(48);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0)))
-        .isEqualTo(buffer + " --assign-buckets");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo(buffer.length());
+    assertThat(candidate.size()).isEqualTo(48);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + " --assign-buckets");
   }
 
   @Test
   public void testJComplete4() throws Exception {
     buffer = "start server --name=name1 --locators=localhost  --J=-Dfoo=bar --";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(cursor).isEqualTo(buffer.length() - 2);
-    assertThat(candidates.size()).isEqualTo(48);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0)))
-        .isEqualTo(buffer + "assign-buckets");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.getCursor()).isEqualTo(buffer.length() - 2);
+    assertThat(candidate.size()).isEqualTo(48);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "assign-buckets");
   }
 
   @Test
   public void testCompletRegionType() throws Exception {
     buffer = "create region --name=test --type";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(23);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "=LOCAL");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(23);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "=LOCAL");
   }
 
   @Test
   public void testCompletPartialRegionType() throws Exception {
     buffer = "create region --name=test --type=LO";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(5);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "CAL");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(5);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "CAL");
   }
 
   @Test
   public void testCompletWithRegionTypeWithNoSpace() throws Exception {
     buffer = "create region --name=test --type=REPLICATE";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(5);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "_HEAP_LRU");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(5);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "_HEAP_LRU");
   }
 
   @Test
   public void testCompletWithRegionTypeWithSpace() throws Exception {
     buffer = "create region --name=test --type=REPLICATE ";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(37);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0)))
-        .isEqualTo(buffer + "--async-event-queue-id");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(37);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "--async-event-queue-id");
   }
 
   @Test
   public void testCompletLogLevel() throws Exception {
     buffer = "change loglevel --loglevel";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(8);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "=ALL");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(8);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "=ALL");
   }
 
   @Test
   public void testCompletLogLevelWithEqualSign() throws Exception {
     buffer = "change loglevel --loglevel=";
-    cursor = parser.completeAdvanced(buffer, candidates);
-    assertThat(candidates.size()).isEqualTo(8);
-    assertThat(getCompleted(buffer, cursor, candidates.get(0))).isEqualTo(buffer + "ALL");
+    candidate = parser.complete(buffer);
+    assertThat(candidate.size()).isEqualTo(8);
+    assertThat(candidate.getFirstCandidate()).isEqualTo(buffer + "ALL");
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/geode/blob/0ef7f56b/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserConverterTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserConverterTest.java
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserConverterTest.java
index 1ff60d6..3c42e0b 100644
--- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserConverterTest.java
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserConverterTest.java
@@ -23,16 +23,13 @@ import org.apache.geode.management.internal.cli.converters.DiskStoreNameConverte
 import org.apache.geode.management.internal.cli.converters.FilePathConverter;
 import org.apache.geode.management.internal.cli.converters.FilePathStringConverter;
 import org.apache.geode.management.internal.cli.converters.RegionPathConverter;
+import org.apache.geode.test.dunit.rules.GfshParserRule;
 import org.apache.geode.test.junit.categories.IntegrationTest;
-import org.junit.Before;
-import org.junit.BeforeClass;
+import org.junit.ClassRule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
-import org.springframework.shell.core.Completion;
-import org.springframework.shell.core.Converter;
 import org.springframework.shell.event.ParseResult;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
@@ -40,19 +37,11 @@ import java.util.stream.Collectors;
 
 @Category(IntegrationTest.class)
 public class GfshParserConverterTest {
-  private static GfshParser parser;
-  private List<Completion> candidates;
-  private int cursor;
 
-  @BeforeClass
-  public static void setUpClass() throws Exception {
-    parser = new GfshParser();
-  }
+  private GfshParserRule.CommandCandidate commandCandidate;
 
-  @Before
-  public void setUp() throws Exception {
-    this.candidates = new ArrayList<>();
-  }
+  @ClassRule
+  public static GfshParserRule parser = new GfshParserRule();
 
   @Test
   public void testStringArrayConverter() {
@@ -118,45 +107,45 @@ public class GfshParserConverterTest {
   @Test
   public void testHelpConverterWithNo() {
     String command = "help --command=";
-    cursor = parser.completeAdvanced(command, candidates);
+    commandCandidate = parser.complete(command);
     Set<String> commands = parser.getCommandManager().getHelper().getCommands();
-    assertThat(candidates.size()).isEqualTo(commands.size());
+    assertThat(commandCandidate.size()).isEqualTo(commands.size());
   }
 
   @Test
   public void testHelpConverter() {
     String command = "help --command=conn";
-    cursor = parser.completeAdvanced(command, candidates);
-    assertThat(candidates.size()).isEqualTo(1);
-    assertThat(getCompleted(command, cursor, candidates.get(0))).isEqualTo(command + "ect");
+    commandCandidate = parser.complete(command);
+    assertThat(commandCandidate.size()).isEqualTo(1);
+    assertThat(commandCandidate.getFirstCandidate()).isEqualTo(command + "ect");
   }
 
   @Test
   public void testHintConverter() {
     String command = "hint --topic=";
-    cursor = parser.completeAdvanced(command, candidates);
+    commandCandidate = parser.complete(command);
     Set<String> topics = parser.getCommandManager().getHelper().getTopicNames();
-    assertThat(candidates.size()).isEqualTo(topics.size());
-    assertThat(getCompleted(command, cursor, candidates.get(0))).isEqualTo("hint --topic=Client");
+    assertThat(commandCandidate.size()).isEqualTo(topics.size());
+    assertThat(commandCandidate.getFirstCandidate()).isEqualTo("hint --topic=Client");
   }
 
   @Test
   public void testDiskStoreNameConverter() throws Exception {
     // spy the DiskStoreNameConverter
-    DiskStoreNameConverter spy = spyConverter(DiskStoreNameConverter.class);
+    DiskStoreNameConverter spy = parser.spyConverter(DiskStoreNameConverter.class);
 
     Set<String> diskStores = Arrays.stream("name1,name2".split(",")).collect(Collectors.toSet());
     doReturn(diskStores).when(spy).getDiskStoreNames();
 
     String command = "compact disk-store --name=";
-    cursor = parser.completeAdvanced(command, candidates);
-    assertThat(candidates).hasSize(2);
+    commandCandidate = parser.complete(command);
+    assertThat(commandCandidate.size()).isEqualTo(2);
 
   }
 
   @Test
   public void testFilePathConverter() throws Exception {
-    FilePathStringConverter spy = spyConverter(FilePathStringConverter.class);
+    FilePathStringConverter spy = parser.spyConverter(FilePathStringConverter.class);
     List<String> roots = Arrays.stream("/vol,/logs".split(",")).collect(Collectors.toList());
     List<String> siblings =
         Arrays.stream("sibling1,sibling11,test1".split(",")).collect(Collectors.toList());
@@ -164,55 +153,33 @@ public class GfshParserConverterTest {
     doReturn(siblings).when(spy).getSiblings(any());
 
     String command = "start server --properties-file=";
-    cursor = parser.completeAdvanced(command, candidates);
-    assertThat(candidates).hasSize(2);
-    assertThat(getCompleted(command, cursor, candidates.get(0))).isEqualTo(command + "/logs");
-    candidates.clear();
+    commandCandidate = parser.complete(command);
+    assertThat(commandCandidate.size()).isEqualTo(2);
+    assertThat(commandCandidate.getFirstCandidate()).isEqualTo(command + "/logs");
 
     command = "start server --properties-file=sibling";
-    cursor = parser.completeAdvanced(command, candidates);
-    assertThat(candidates).hasSize(2);
-    assertThat(getCompleted(command, cursor, candidates.get(0))).isEqualTo(command + "1");
-    candidates.clear();
+    commandCandidate = parser.complete(command);
+    assertThat(commandCandidate.size()).isEqualTo(2);
+    assertThat(commandCandidate.getFirstCandidate()).isEqualTo(command + "1");
 
-    FilePathConverter spyFilePathConverter = spyConverter(FilePathConverter.class);
+    FilePathConverter spyFilePathConverter = parser.spyConverter(FilePathConverter.class);
     spyFilePathConverter.setDelegate(spy);
     command = "run --file=test";
-    cursor = parser.completeAdvanced(command, candidates);
-    assertThat(candidates).hasSize(1);
-    assertThat(getCompleted(command, cursor, candidates.get(0))).isEqualTo(command + "1");
+    commandCandidate = parser.complete(command);
+    assertThat(commandCandidate.size()).isEqualTo(1);
+    assertThat(commandCandidate.getFirstCandidate()).isEqualTo(command + "1");
   }
 
   @Test
   public void testRegionPathConverter() throws Exception {
-    RegionPathConverter spy = spyConverter(RegionPathConverter.class);
+    RegionPathConverter spy = parser.spyConverter(RegionPathConverter.class);
     Set<String> regions = Arrays.stream("/regionA,/regionB".split(",")).collect(Collectors.toSet());
     doReturn(regions).when(spy).getAllRegionPaths();
 
     String command = "describe region --name=";
-    cursor = parser.completeAdvanced(command, candidates);
-    assertThat(candidates).hasSize(regions.size());
-    assertThat(getCompleted(command, cursor, candidates.get(0))).isEqualTo(command + "/regionA");
+    commandCandidate = parser.complete(command);
+    assertThat(commandCandidate.size()).isEqualTo(regions.size());
+    assertThat(commandCandidate.getFirstCandidate()).isEqualTo(command + "/regionA");
   }
 
-  private String getCompleted(String buffer, int cursor, Completion completed) {
-    return buffer.substring(0, cursor) + completed.getValue();
-  }
-
-  private static <T extends Converter> T spyConverter(Class<T> klass) {
-    Set<Converter<?>> converters = parser.getConverters();
-    T foundConverter = null, spy = null;
-    for (Converter converter : converters) {
-      if (klass.isAssignableFrom(converter.getClass())) {
-        foundConverter = (T) converter;
-        break;
-      }
-    }
-    if (foundConverter != null) {
-      parser.remove(foundConverter);
-      spy = spy(foundConverter);
-      parser.add(spy);
-    }
-    return spy;
-  }
 }

http://git-wip-us.apache.org/repos/asf/geode/blob/0ef7f56b/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserParsingTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserParsingTest.java
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserParsingTest.java
index a2efb36..0c2af1d 100644
--- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserParsingTest.java
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/GfshParserParsingTest.java
@@ -18,8 +18,9 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
 
 import org.apache.geode.management.internal.cli.i18n.CliStrings;
+import org.apache.geode.test.dunit.rules.GfshParserRule;
 import org.apache.geode.test.junit.categories.IntegrationTest;
-import org.junit.BeforeClass;
+import org.junit.ClassRule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.springframework.shell.event.ParseResult;
@@ -28,13 +29,10 @@ import java.util.Map;
 
 @Category(IntegrationTest.class)
 public class GfshParserParsingTest {
-  private static GfshParser parser;
+  @ClassRule
+  public static GfshParserRule parser = new GfshParserRule();
   private String buffer;
 
-  @BeforeClass
-  public static void setUpClass() throws Exception {
-    parser = new GfshParser();
-  }
 
   private Map<String, String> parseParams(String input, String commandMethod) {
     ParseResult parseResult = parser.parse(input);

http://git-wip-us.apache.org/repos/asf/geode/blob/0ef7f56b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsTest.java
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsTest.java
new file mode 100644
index 0000000..9b38d86
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information
regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version
2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain
a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under
the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
+ * or implied. See the License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+
+package org.apache.geode.management.internal.cli.commands;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import org.apache.geode.management.internal.cli.GfshParseResult;
+import org.apache.geode.management.internal.cli.shell.Gfsh;
+import org.apache.geode.test.dunit.rules.GfshParserRule;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.springframework.util.ReflectionUtils;
+
+import java.util.Properties;
+
+public class LauncherLifecycleCommandsTest {
+  @ClassRule
+  public static GfshParserRule commandRule = new GfshParserRule();
+
+  @Test
+  public void getLauncherLifecycleCommand() throws Exception {
+    LauncherLifecycleCommands spy = commandRule.spyCommand("start locator");
+    doReturn(mock(Gfsh.class)).when(spy).getGfsh();
+    commandRule.executeLastCommandWithInstance(spy);
+
+    ArgumentCaptor<Properties> propsCaptor = ArgumentCaptor.forClass(Properties.class);
+    verify(spy).createStartLocatorCommandLine(any(), any(), any(), propsCaptor.capture(),
any(),
+        any(), any(), any(), any());
+
+    Properties properties = propsCaptor.getValue();
+    System.out.println();
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode/blob/0ef7f56b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/GfshParserRule.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/test/dunit/rules/GfshParserRule.java
b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/GfshParserRule.java
new file mode 100644
index 0000000..b1bc7aa
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/GfshParserRule.java
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information
regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version
2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain
a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under
the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
+ * or implied. See the License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+
+package org.apache.geode.test.dunit.rules;
+
+import static org.mockito.Mockito.spy;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.geode.management.cli.Result;
+import org.apache.geode.management.internal.cli.CommandManager;
+import org.apache.geode.management.internal.cli.GfshParseResult;
+import org.apache.geode.management.internal.cli.GfshParser;
+import org.apache.geode.management.internal.cli.remote.RemoteExecutionStrategy;
+import org.junit.rules.ExternalResource;
+import org.springframework.shell.core.CommandMarker;
+import org.springframework.shell.core.Completion;
+import org.springframework.shell.core.Converter;
+import org.springframework.util.ReflectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+public class GfshParserRule extends ExternalResource {
+
+  private GfshParser parser;
+  private CommandManager commandManager;
+  private GfshParseResult parseResult;
+
+  @Override
+  public void before() {
+    commandManager = new CommandManager();
+    parser = new GfshParser(commandManager);
+    parseResult = null;
+  }
+
+  public GfshParseResult parse(String command) {
+    return parser.parse(command);
+  }
+
+  public <T> T spyCommand(String command) {
+    parseResult = parse(command);
+    T spy = (T) spy(parseResult.getInstance());
+    return spy;
+  }
+
+  public <T> Result executeLastCommandWithInstance(T instance) {
+    return (Result) ReflectionUtils.invokeMethod(parseResult.getMethod(), instance,
+        parseResult.getArguments());
+  }
+
+  public CommandCandidate complete(String command) {
+    List<Completion> candidates = new ArrayList<>();
+    int cursor = parser.completeAdvanced(command, command.length(), candidates);
+    return new CommandCandidate(command, cursor, candidates);
+  }
+
+  public <T extends Converter> T spyConverter(Class<T> klass) {
+    Set<Converter<?>> converters = parser.getConverters();
+    T foundConverter = null, spy = null;
+    for (Converter converter : converters) {
+      if (klass.isAssignableFrom(converter.getClass())) {
+        foundConverter = (T) converter;
+        break;
+      }
+    }
+    if (foundConverter != null) {
+      parser.remove(foundConverter);
+      spy = spy(foundConverter);
+      parser.add(spy);
+    }
+    return spy;
+  }
+
+  public GfshParser getParser() {
+    return parser;
+  }
+
+  public CommandManager getCommandManager() {
+    return commandManager;
+  }
+
+  public static class CommandCandidate {
+    private String command;
+    private int cursor;
+    private List<Completion> candidates;
+
+    public CommandCandidate(String command, int cursor, List<Completion> candidates)
{
+      this.command = command;
+      this.cursor = cursor;
+      this.candidates = candidates;
+    }
+
+    public String getCandidate(int index) {
+      return command.substring(0, cursor) + candidates.get(index).getValue();
+    }
+
+    public String getFirstCandidate() {
+      return getCandidate(0);
+    }
+
+    public int size() {
+      return candidates.size();
+    }
+
+    public int getCursor() {
+      return cursor;
+    }
+
+    public List<Completion> getCandidates() {
+      return candidates;
+    }
+  }
+}


Mime
View raw message