You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ji...@apache.org on 2017/11/21 17:00:09 UTC

[geode] branch develop updated: GEODE-3788: GfshParserRule enhancement (#1082)

This is an automated email from the ASF dual-hosted git repository.

jinmeiliao pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 7d80ee4  GEODE-3788: GfshParserRule enhancement (#1082)
7d80ee4 is described below

commit 7d80ee498d9f093084cc21cc8540bf70b68ee434
Author: jinmeiliao <ji...@pivotal.io>
AuthorDate: Tue Nov 21 09:00:06 2017 -0800

    GEODE-3788: GfshParserRule enhancement (#1082)
---
 .../StartLocatorCommandIntegrationTest.java        |  6 ------
 .../StartServerCommandIntegrationTest.java         |  6 ------
 .../internal/cli/remote/CommandExecutor.java       | 17 +++++++++++++----
 .../cli/commands/DestroyRegionCommandTest.java     |  7 +++----
 .../internal/cli/remote/CommandExecutorTest.java   | 22 +++++++++++++---------
 .../test/dunit/rules/LocatorServerStartupRule.java |  7 +++++++
 .../test/junit/assertions/CommandResultAssert.java |  3 ++-
 .../geode/test/junit/rules/GfshParserRule.java     |  9 ++++++---
 8 files changed, 44 insertions(+), 33 deletions(-)

diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandIntegrationTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandIntegrationTest.java
index f8cd315..d4dc4e8 100644
--- a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandIntegrationTest.java
+++ b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandIntegrationTest.java
@@ -29,7 +29,6 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
-import org.junit.rules.ExpectedException;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mockito;
 
@@ -45,9 +44,6 @@ public class StartLocatorCommandIntegrationTest {
   @Rule
   public GfshParserRule commandRule = new GfshParserRule();
 
-  @Rule
-  public ExpectedException thrown = ExpectedException.none();
-
   private StartLocatorCommand spy;
 
   @Before
@@ -58,7 +54,6 @@ public class StartLocatorCommandIntegrationTest {
 
   @Test
   public void startLocatorWorksWithNoOptions() throws Exception {
-    thrown.expect(NullPointerException.class);
     commandRule.executeCommandWithInstance(spy, "start locator");
 
     ArgumentCaptor<Properties> gemfirePropertiesCaptor = ArgumentCaptor.forClass(Properties.class);
@@ -75,7 +70,6 @@ public class StartLocatorCommandIntegrationTest {
     String startLocatorCommand = new CommandStringBuilder("start locator")
         .addOption(JMX_MANAGER_HOSTNAME_FOR_CLIENTS, FAKE_HOSTNAME).toString();
 
-    thrown.expect(NullPointerException.class);
     commandRule.executeCommandWithInstance(spy, startLocatorCommand);
 
     ArgumentCaptor<Properties> gemfirePropertiesCaptor = ArgumentCaptor.forClass(Properties.class);
diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandIntegrationTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandIntegrationTest.java
index 92f8749..1e6b7c2 100644
--- a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandIntegrationTest.java
+++ b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandIntegrationTest.java
@@ -29,7 +29,6 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
-import org.junit.rules.ExpectedException;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mockito;
 
@@ -45,9 +44,6 @@ public class StartServerCommandIntegrationTest {
   @Rule
   public GfshParserRule commandRule = new GfshParserRule();
 
-  @Rule
-  public ExpectedException thrown = ExpectedException.none();
-
   private StartServerCommand spy;
 
   @Before
@@ -58,7 +54,6 @@ public class StartServerCommandIntegrationTest {
 
   @Test
   public void startServerWorksWithNoOptions() throws Exception {
-    thrown.expect(NullPointerException.class);
     commandRule.executeCommandWithInstance(spy, "start server");
 
     ArgumentCaptor<Properties> gemfirePropertiesCaptor = ArgumentCaptor.forClass(Properties.class);
@@ -75,7 +70,6 @@ public class StartServerCommandIntegrationTest {
     String startServerCommand = new CommandStringBuilder("start server")
         .addOption(JMX_MANAGER_HOSTNAME_FOR_CLIENTS, FAKE_HOSTNAME).toString();
 
-    thrown.expect(NullPointerException.class);
     commandRule.executeCommandWithInstance(spy, startServerCommand);
 
     ArgumentCaptor<Properties> gemfirePropertiesCaptor = ArgumentCaptor.forClass(Properties.class);
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandExecutor.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandExecutor.java
index d4f5218..5a165ea 100755
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandExecutor.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandExecutor.java
@@ -34,9 +34,15 @@ import org.apache.geode.security.NotAuthorizedException;
 public class CommandExecutor {
   private Logger logger = LogService.getLogger();
 
+  // used by the product
   public Object execute(ParseResult parseResult) {
+    return execute(null, parseResult);
+  }
+
+  // used by the GfshParserRule to pass in a mock command
+  public Object execute(Object command, ParseResult parseResult) {
     try {
-      Object result = invokeCommand(parseResult);
+      Object result = invokeCommand(command, parseResult);
 
       if (result == null) {
         return ResultBuilder.createGemFireErrorResult("Command returned null: " + parseResult);
@@ -83,9 +89,12 @@ public class CommandExecutor {
     }
   }
 
-  protected Object invokeCommand(ParseResult parseResult) {
-    return ReflectionUtils.invokeMethod(parseResult.getMethod(), parseResult.getInstance(),
+  protected Object invokeCommand(Object command, ParseResult parseResult) {
+    // if no command instance is passed in, use the one in the parseResult
+    if (command == null) {
+      command = parseResult.getInstance();
+    }
+    return ReflectionUtils.invokeMethod(parseResult.getMethod(), command,
         parseResult.getArguments());
   }
-
 }
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyRegionCommandTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyRegionCommandTest.java
index f4352b2..12f43ef 100644
--- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyRegionCommandTest.java
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyRegionCommandTest.java
@@ -15,7 +15,6 @@
 
 package org.apache.geode.management.internal.cli.commands;
 
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -122,9 +121,9 @@ public class DestroyRegionCommandTest {
     when(result2.isSuccessful()).thenReturn(false);
     when(result2.getThrowable()).thenReturn(new IllegalArgumentException("something happened"));
 
-    assertThatThrownBy(
-        () -> parser.executeCommandWithInstance(command, "destroy region --name=test"))
-            .isInstanceOf(IllegalArgumentException.class);
+    parser.executeAndAssertThat(command, "destroy region --name=test").statusIsError()
+        .containsOutput("something happened");
+
 
     // verify that xmlEntiry returned by the result1 is not saved to Cluster config
     verify(command, never()).persistClusterConfiguration(any(), any());
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/remote/CommandExecutorTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/remote/CommandExecutorTest.java
index 4f51f25..85001de 100644
--- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/remote/CommandExecutorTest.java
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/remote/CommandExecutorTest.java
@@ -58,14 +58,14 @@ public class CommandExecutorTest {
 
   @Test
   public void returnsResultAsExpected() throws Exception {
-    doReturn(result).when(executor).invokeCommand(any());
+    doReturn(result).when(executor).invokeCommand(any(), any());
     Object thisResult = executor.execute(parseResult);
     assertThat(thisResult).isSameAs(result);
   }
 
   @Test
   public void testNullResult() throws Exception {
-    doReturn(null).when(executor).invokeCommand(any());
+    doReturn(null).when(executor).invokeCommand(any(), any());
     Object thisResult = executor.execute(parseResult);
     assertThat(thisResult.toString()).contains("Command returned null");
   }
@@ -73,7 +73,7 @@ public class CommandExecutorTest {
   @Test
   public void anyRuntimeExceptionGetsCaught() throws Exception {
     ;
-    doThrow(new RuntimeException("my message here")).when(executor).invokeCommand(any());
+    doThrow(new RuntimeException("my message here")).when(executor).invokeCommand(any(), any());
     Object thisResult = executor.execute(parseResult);
     assertThat(((CommandResult) thisResult).getStatus()).isEqualTo(Result.Status.ERROR);
     assertThat(thisResult.toString()).contains("my message here");
@@ -81,7 +81,8 @@ public class CommandExecutorTest {
 
   @Test
   public void notAuthorizedExceptionGetsThrown() throws Exception {
-    doThrow(new NotAuthorizedException("Not Authorized")).when(executor).invokeCommand(any());
+    doThrow(new NotAuthorizedException("Not Authorized")).when(executor).invokeCommand(any(),
+        any());
     assertThatThrownBy(() -> executor.execute(parseResult))
         .isInstanceOf(NotAuthorizedException.class);
   }
@@ -89,7 +90,8 @@ public class CommandExecutorTest {
   @Test
   public void anyIllegalArgumentExceptionGetsCaught() throws Exception {
     ;
-    doThrow(new IllegalArgumentException("my message here")).when(executor).invokeCommand(any());
+    doThrow(new IllegalArgumentException("my message here")).when(executor).invokeCommand(any(),
+        any());
     Object thisResult = executor.execute(parseResult);
     assertThat(((CommandResult) thisResult).getStatus()).isEqualTo(Result.Status.ERROR);
     assertThat(thisResult.toString()).contains("my message here");
@@ -98,7 +100,8 @@ public class CommandExecutorTest {
   @Test
   public void anyIllegalStateExceptionGetsCaught() throws Exception {
     ;
-    doThrow(new IllegalStateException("my message here")).when(executor).invokeCommand(any());
+    doThrow(new IllegalStateException("my message here")).when(executor).invokeCommand(any(),
+        any());
     Object thisResult = executor.execute(parseResult);
     assertThat(((CommandResult) thisResult).getStatus()).isEqualTo(Result.Status.ERROR);
     assertThat(thisResult.toString()).contains("my message here");
@@ -107,7 +110,7 @@ public class CommandExecutorTest {
   @Test
   public void anyUserErrorExceptionGetsCaught() throws Exception {
     ;
-    doThrow(new UserErrorException("my message here")).when(executor).invokeCommand(any());
+    doThrow(new UserErrorException("my message here")).when(executor).invokeCommand(any(), any());
     Object thisResult = executor.execute(parseResult);
     assertThat(((CommandResult) thisResult).getStatus()).isEqualTo(Result.Status.ERROR);
     assertThat(thisResult.toString()).contains("my message here");
@@ -117,7 +120,7 @@ public class CommandExecutorTest {
   public void anyEntityNotFoundException_statusOK() throws Exception {
     ;
     doThrow(new EntityNotFoundException("my message here", true)).when(executor)
-        .invokeCommand(any());
+        .invokeCommand(any(), any());
     Object thisResult = executor.execute(parseResult);
     assertThat(((CommandResult) thisResult).getStatus()).isEqualTo(Result.Status.OK);
     assertThat(thisResult.toString()).contains("Skipping: my message here");
@@ -126,7 +129,8 @@ public class CommandExecutorTest {
   @Test
   public void anyEntityNotFoundException_statusERROR() throws Exception {
     ;
-    doThrow(new EntityNotFoundException("my message here")).when(executor).invokeCommand(any());
+    doThrow(new EntityNotFoundException("my message here")).when(executor).invokeCommand(any(),
+        any());
     Object thisResult = executor.execute(parseResult);
     assertThat(((CommandResult) thisResult).getStatus()).isEqualTo(Result.Status.ERROR);
     assertThat(thisResult.toString()).contains("my message here");
diff --git a/geode-core/src/test/java/org/apache/geode/test/dunit/rules/LocatorServerStartupRule.java b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/LocatorServerStartupRule.java
index ff68361..a3c2944 100644
--- a/geode-core/src/test/java/org/apache/geode/test/dunit/rules/LocatorServerStartupRule.java
+++ b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/LocatorServerStartupRule.java
@@ -16,6 +16,7 @@
 
 package org.apache.geode.test.dunit.rules;
 
+import static org.apache.geode.distributed.ConfigurationProperties.GROUPS;
 import static org.apache.geode.distributed.ConfigurationProperties.JMX_MANAGER_PORT;
 import static org.apache.geode.distributed.ConfigurationProperties.NAME;
 import static org.apache.geode.test.dunit.Host.getHost;
@@ -184,6 +185,12 @@ public class LocatorServerStartupRule extends ExternalResource implements Serial
     return startServerVM(index, new Properties(), locatorPort);
   }
 
+  public MemberVM startServerVM(int index, String group, int locatorPort) throws IOException {
+    Properties properties = new Properties();
+    properties.put(GROUPS, group);
+    return startServerVM(index, properties, locatorPort);
+  }
+
   public MemberVM startServerVM(int index, Properties properties) throws IOException {
     return startServerVM(index, properties, -1);
   }
diff --git a/geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultAssert.java b/geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultAssert.java
index 9ec2754..4b0a43b 100644
--- a/geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultAssert.java
+++ b/geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultAssert.java
@@ -210,7 +210,8 @@ public class CommandResultAssert
   }
 
   public CommandResultAssert tableHasRowCount(String anyColumnHeader, int rowSize) {
-    assertThat(actual.getCommandResult().getColumnValues(anyColumnHeader)).isEqualTo(rowSize);
+    assertThat(actual.getCommandResult().getColumnValues(anyColumnHeader).size())
+        .isEqualTo(rowSize);
     return this;
   }
 
diff --git a/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshParserRule.java b/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshParserRule.java
index 20592be..a6e181d 100644
--- a/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshParserRule.java
+++ b/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshParserRule.java
@@ -23,7 +23,6 @@ import java.util.Set;
 import org.junit.rules.ExternalResource;
 import org.springframework.shell.core.Completion;
 import org.springframework.shell.core.Converter;
-import org.springframework.util.ReflectionUtils;
 
 import org.apache.geode.internal.ClassPathLoader;
 import org.apache.geode.management.cli.CliMetaData;
@@ -32,6 +31,7 @@ import org.apache.geode.management.internal.cli.CliAroundInterceptor;
 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.CommandExecutor;
 import org.apache.geode.management.internal.cli.result.CommandResult;
 import org.apache.geode.management.internal.cli.result.ResultBuilder;
 import org.apache.geode.test.junit.assertions.CommandResultAssert;
@@ -40,11 +40,13 @@ public class GfshParserRule extends ExternalResource {
 
   private GfshParser parser;
   private CommandManager commandManager;
+  private CommandExecutor commandExecutor;
 
   @Override
   public void before() {
     commandManager = new CommandManager();
     parser = new GfshParser(commandManager);
+    commandExecutor = new CommandExecutor();
   }
 
   public GfshParseResult parse(String command) {
@@ -78,12 +80,13 @@ public class GfshParserRule extends ExternalResource {
       }
     }
 
-    return (CommandResult) ReflectionUtils.invokeMethod(parseResult.getMethod(), instance,
-        parseResult.getArguments());
+    return (CommandResult) commandExecutor.execute(instance, parseResult);
   }
 
   public <T> CommandResultAssert executeAndAssertThat(T instance, String command) {
     CommandResult result = executeCommandWithInstance(instance, command);
+    System.out.println("Command Result:");
+    System.out.println(ResultBuilder.resultAsString(result));
     return new CommandResultAssert(result);
   }
 

-- 
To stop receiving notification emails like this one, please contact
['"commits@geode.apache.org" <co...@geode.apache.org>'].