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 3a3935f  GEODE-2676: fix NPE with ShowMetricsCommand.
3a3935f is described below

commit 3a3935f8d11a3c6b75815781f6cd551584613425
Author: Jinmei Liao <[email protected]>
AuthorDate: Wed Nov 15 12:03:45 2017 -0800

    GEODE-2676: fix NPE with ShowMetricsCommand.
---
 .../internal/beans/RegionMBeanBridge.java          |  7 +++
 .../ShowMetricsCommandIntegrationTest.java         | 22 +++++-----
 .../test/junit/assertions/CommandResultAssert.java | 50 +++++++++++++++++++++-
 3 files changed, 68 insertions(+), 11 deletions(-)

diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/beans/RegionMBeanBridge.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/beans/RegionMBeanBridge.java
index 480c5c0..93da8b4 100644
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/beans/RegionMBeanBridge.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/beans/RegionMBeanBridge.java
@@ -145,6 +145,13 @@ public class RegionMBeanBridge<K, V> {
     this.regAttrs = region.getAttributes();
 
     this.isStatisticsEnabled = regAttrs.getStatisticsEnabled();
+    if (isStatisticsEnabled) {
+      try {
+        region.getStatistics();
+      } catch (UnsupportedOperationException e) {
+        this.isStatisticsEnabled = false;
+      }
+    }
 
     this.regionAttributesData = 
RegionMBeanCompositeDataFactory.getRegionAttributesData(regAttrs);
     this.membershipAttributesData =
diff --git 
a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/ShowMetricsCommandIntegrationTest.java
 
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/ShowMetricsCommandIntegrationTest.java
index b18af6c..7bbad90 100644
--- 
a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/ShowMetricsCommandIntegrationTest.java
+++ 
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/ShowMetricsCommandIntegrationTest.java
@@ -45,10 +45,10 @@ public class ShowMetricsCommandIntegrationTest {
   @ClassRule
   public static ServerStarterRule server =
       new ServerStarterRule().withRegion(RegionShortcut.REPLICATE, REGION_NAME)
-          
.withName(MEMBER_NAME).withJMXManager().withEmbeddedLocator().withAutoStart();
+          .withName(MEMBER_NAME).withJMXManager().withAutoStart();
 
   @Rule
-  public GfshCommandRule gfsh = new GfshCommandRule();
+  public GfshCommandRule gfsh = new GfshCommandRule(server::getJmxPort, 
PortType.jmxManager);
 
   @Test
   public void everyCategoryHasAUseCase() throws Exception {
@@ -68,6 +68,7 @@ public class ShowMetricsCommandIntegrationTest {
 
   @Test
   public void commandFailsWhenNotConnected() throws Exception {
+    gfsh.disconnect();
     gfsh.executeAndAssertThat("show metrics")
         .containsOutput("was found but is not currently available");
   }
@@ -80,9 +81,7 @@ public class ShowMetricsCommandIntegrationTest {
         ShowMetricsInterceptor.getValidCategoriesAsStrings(true, true, false);
     // Blank lines are permitted for grouping.
     expectedCategories.add("");
-    logger.info("Expecting categories: " + String.join(", ", 
expectedCategories));
 
-    gfsh.connectAndVerify(server.getEmbeddedLocatorPort(), PortType.locator);
     gfsh.executeAndAssertThat(cmd).tableHasColumnOnlyWithValues("Category",
         expectedCategories.toArray(new String[0]));
   }
@@ -97,7 +96,6 @@ public class ShowMetricsCommandIntegrationTest {
     expectedCategories.add("");
     logger.info("Expecting categories: " + String.join(", ", 
expectedCategories));
 
-    gfsh.connectAndVerify(server.getEmbeddedLocatorPort(), PortType.locator);
     gfsh.executeAndAssertThat(cmd).tableHasColumnOnlyWithValues("Category",
         expectedCategories.toArray(new String[0]));
   }
@@ -112,7 +110,6 @@ public class ShowMetricsCommandIntegrationTest {
     expectedCategories.add("");
     logger.info("Expecting categories: " + String.join(", ", 
expectedCategories));
 
-    gfsh.connectAndVerify(server.getEmbeddedLocatorPort(), PortType.locator);
     gfsh.executeAndAssertThat(cmd).tableHasColumnOnlyWithValues("Category",
         expectedCategories.toArray(new String[0]));
   }
@@ -127,7 +124,6 @@ public class ShowMetricsCommandIntegrationTest {
     expectedCategories.add("");
     logger.info("Expecting categories: " + String.join(", ", 
expectedCategories));
 
-    gfsh.connectAndVerify(server.getEmbeddedLocatorPort(), PortType.locator);
     gfsh.executeAndAssertThat(cmd).tableHasColumnOnlyWithValues("Category",
         expectedCategories.toArray(new String[0]));
   }
@@ -142,7 +138,6 @@ public class ShowMetricsCommandIntegrationTest {
     expectedCategories.add("");
     logger.info("Expecting categories: " + String.join(", ", 
expectedCategories));
 
-    gfsh.connectAndVerify(server.getEmbeddedLocatorPort(), PortType.locator);
     gfsh.executeAndAssertThat(cmd).tableHasColumnOnlyWithValues("Category",
         expectedCategories.toArray(new String[0]));
   }
@@ -152,7 +147,6 @@ public class ShowMetricsCommandIntegrationTest {
     String cmd =
         "show metrics 
--categories=\"cluster,cache,some_invalid_category,another_invalid_category\"";
 
-    gfsh.connectAndVerify(server.getEmbeddedLocatorPort(), PortType.locator);
     gfsh.executeAndAssertThat(cmd).containsOutput("Invalid Categories")
         
.containsOutput("some_invalid_category").containsOutput("another_invalid_category")
         .doesNotContainOutput("cache").doesNotContainOutput("cluster");
@@ -164,8 +158,16 @@ public class ShowMetricsCommandIntegrationTest {
     List<String> expectedCategories = Arrays.asList("cluster", "cache", "");
     logger.info("Expecting categories: " + String.join(", ", 
expectedCategories));
 
-    gfsh.connectAndVerify(server.getEmbeddedLocatorPort(), PortType.locator);
     gfsh.executeAndAssertThat(cmd).tableHasColumnOnlyWithValues("Category",
         expectedCategories.toArray(new String[0]));
   }
+
+  @Test
+  public void getRegionMetricsForPartitionedRegionWithStatistics() throws 
Exception {
+    String cmd = "create region --name=region2 --type=PARTITION 
--enable-statistics";
+    gfsh.executeAndAssertThat(cmd).statusIsSuccess();
+    String cmd2 = "show metrics --member=" + MEMBER_NAME + " --region=region2";
+    
gfsh.executeAndAssertThat(cmd2).statusIsSuccess().tableHasRowWithValues("Category",
 "Metric",
+        "Value", "", "missCount", "-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 51a6821..9ec2754 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
@@ -17,14 +17,16 @@ package org.apache.geode.test.junit.assertions;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.assertj.core.api.AbstractAssert;
 import org.assertj.core.api.Assertions;
 import org.json.JSONArray;
 
 import org.apache.geode.management.cli.Result;
-import org.apache.geode.management.internal.cli.json.GfJsonObject;
+import org.apache.geode.management.internal.cli.json.GfJsonException;
 import org.apache.geode.management.internal.cli.result.CommandResult;
 
 
@@ -166,6 +168,52 @@ public class CommandResultAssert
     return this;
   }
 
+
+
+  public CommandResultAssert tableHasRowWithValues(String... headersThenValues)
+      throws GfJsonException {
+    assertThat(headersThenValues.length % 2)
+        .describedAs("You need to pass even number of 
parameters.").isEqualTo(0);
+
+    int numberOfColumn = headersThenValues.length / 2;
+
+    String[] headers = Arrays.copyOfRange(headersThenValues, 0, 
numberOfColumn);
+    String[] expectedValues =
+        Arrays.copyOfRange(headersThenValues, numberOfColumn, 
headersThenValues.length);
+
+    Map<String, List<Object>> allValues = new HashMap<>();
+    int numberOfRows = -1;
+    for (String header : headers) {
+      List<Object> columnValues = 
actual.getCommandResult().getColumnValues(header);
+      if (numberOfRows > 0) {
+        assertThat(columnValues.size()).isEqualTo(numberOfRows);
+      }
+      numberOfRows = columnValues.size();
+      allValues.put(header, columnValues);
+    }
+
+    for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
+      Object[] rowValues = new Object[headers.length];
+      for (int columnIndex = 0; columnIndex < headers.length; columnIndex++) {
+        rowValues[columnIndex] = 
allValues.get(headers[columnIndex]).get(rowIndex);
+      }
+
+      // check if entire row is equal, but if not, continue to next row
+      if (Arrays.deepEquals(expectedValues, rowValues)) {
+        return this;
+      }
+    }
+
+    // did not find any matching rows, then this would pass only if we do not 
pass in any values
+    assertThat(headersThenValues.length).isEqualTo(0);
+    return this;
+  }
+
+  public CommandResultAssert tableHasRowCount(String anyColumnHeader, int 
rowSize) {
+    
assertThat(actual.getCommandResult().getColumnValues(anyColumnHeader)).isEqualTo(rowSize);
+    return this;
+  }
+
   /**
    * Verifies that each of the actual values in the column with the given 
header contains at least
    * one of the expectedValues.

-- 
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].

Reply via email to