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 2016/03/09 03:47:15 UTC

incubator-geode git commit: GEODE-17: clean up MXBeanCreationRule

Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-17-2 3e2ae3330 -> dc7d73e71


GEODE-17: clean up MXBeanCreationRule


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

Branch: refs/heads/feature/GEODE-17-2
Commit: dc7d73e71a3eb8e649532f15299ef76b32e553b9
Parents: 3e2ae33
Author: Jinmei Liao <ji...@pivotal.io>
Authored: Mon Mar 7 09:59:39 2016 -0800
Committer: Jinmei Liao <ji...@pivotal.io>
Committed: Mon Mar 7 09:59:39 2016 -0800

----------------------------------------------------------------------
 .../CacheServerMBeanSecurityJUnitTest.java      |  2 +-
 .../internal/security/MXBeanCreationRule.java   | 71 +++++---------------
 .../junit/rules/DescribedExternalResource.java  | 64 ++++++++++++++++++
 3 files changed, 83 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/dc7d73e7/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/CacheServerMBeanSecurityJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/CacheServerMBeanSecurityJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/CacheServerMBeanSecurityJUnitTest.java
index e9e8942..ee388e1 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/CacheServerMBeanSecurityJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/CacheServerMBeanSecurityJUnitTest.java
@@ -43,7 +43,7 @@ public class CacheServerMBeanSecurityJUnitTest {
   private CacheServerMXBean cacheServerMXBean;
 
   @Rule
-  public MXBeanCreationRule<CacheServerMXBean> mxRule = new MXBeanCreationRule(jmxManagerPort, CacheServerMXBean.class);
+  public MXBeanCreationRule<CacheServerMXBean> mxRule = new MXBeanCreationRule(jmxManagerPort, CacheServerMXBean.class, "GemFire:service=CacheServer,*");
 
   @ClassRule
   public static RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/dc7d73e7/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/MXBeanCreationRule.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/MXBeanCreationRule.java b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/MXBeanCreationRule.java
index 4662a8d..03dcf9b 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/MXBeanCreationRule.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/MXBeanCreationRule.java
@@ -16,9 +16,8 @@
  */
 package com.gemstone.gemfire.management.internal.security;
 
-import org.junit.rules.ExternalResource;
+import com.gemstone.gemfire.test.junit.rules.DescribedExternalResource;
 import org.junit.runner.Description;
-import org.junit.runners.model.Statement;
 
 import javax.management.JMX;
 import javax.management.MBeanServerConnection;
@@ -36,53 +35,25 @@ import java.util.Map;
  *
  * @param <T> The type of MBean which will be returned.
  */
-public class MXBeanCreationRule<T> extends ExternalResource {
+public class MXBeanCreationRule<T> extends DescribedExternalResource {
 
   private final int jmxServerPort;
-  private Class<T> proxyClass;
+  private final Class<T> proxyClass;
+  private final String objectName;
   private JMXConnector jmxConnector;
   private ObjectName beanObjectName;
   private MBeanServerConnection mbeanServer;
 
-  private static class JMXConfigurationStatement extends Statement {
-
-    private final Description description;
-    private final Statement base;
-    private final MXBeanCreationRule mxRule;
-
-    JMXConfigurationStatement(Statement base, Description description, MXBeanCreationRule rule) {
-      this.description = description;
-      this.base = base;
-      this.mxRule = rule;
-    }
-
-    @Override
-    public void evaluate() throws Throwable {
-      mxRule.createConnection(getUser(), getPassword());
-      try {
-        base.evaluate();
-      } finally {
-        mxRule.close();
-      }
-    }
-
-    private String getUser() {
-      return description.getAnnotation(JMXConnectionConfiguration.class).user();
-    }
-
-    private String getPassword() {
-      return description.getAnnotation(JMXConnectionConfiguration.class).password();
-    }
-  }
 
   /**
    * Rule constructor
    * @param port The JMX server port to connect to
    * @param proxyClass The class for which a proxy MBean will be created
    */
-  public MXBeanCreationRule(int port, Class<T> proxyClass) {
+  public MXBeanCreationRule(int port, Class<T> proxyClass, String objectName) {
     this.jmxServerPort = port;
     this.proxyClass = proxyClass;
+    this.objectName = objectName;
   }
 
   /**
@@ -93,11 +64,9 @@ public class MXBeanCreationRule<T> extends ExternalResource {
     return JMX.newMBeanProxy(mbeanServer, beanObjectName, proxyClass);
   }
 
-  private void close() throws Exception{
-    jmxConnector.close();
-  }
-
-  private void createConnection(String user, String password) throws Exception {
+  protected void before(Description description) throws Throwable {
+    String user = description.getAnnotation(JMXConnectionConfiguration.class).user();
+    String password = description.getAnnotation(JMXConnectionConfiguration.class).password();
     Map<String, String[]> env = new HashMap<>();
     env.put(JMXConnector.CREDENTIALS, new String[] {user, password});
     JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" + jmxServerPort + "/jmxrmi");
@@ -105,22 +74,18 @@ public class MXBeanCreationRule<T> extends ExternalResource {
     jmxConnector = JMXConnectorFactory.connect(url, env);
     mbeanServer = jmxConnector.getMBeanServerConnection();
 
-    ObjectName objectNamePattern = lookupObjectName();
-    ObjectInstance bean = (ObjectInstance) mbeanServer.queryMBeans(objectNamePattern, null).toArray()[0];
+    ObjectInstance bean = (ObjectInstance) mbeanServer.queryMBeans(ObjectName.getInstance(objectName), null).toArray()[0];
     beanObjectName = bean.getObjectName();
   }
 
-  private ObjectName lookupObjectName() throws Exception {
-    switch (proxyClass.getName()) {
-      case "com.gemstone.gemfire.management.CacheServerMXBean":
-        return(ObjectName.getInstance("GemFire:service=CacheServer,*"));
-      default:
-        throw new RuntimeException("Unknown MBean class: " + proxyClass.getName());
-    }
+  /**
+   * Override to tear down your specific external resource.
+   */
+  protected void after(Description description) throws Throwable {
+    jmxConnector.close();
+    jmxConnector = null;
+    mbeanServer = null;
+    beanObjectName = null;
   }
 
-  @Override
-  public Statement apply(Statement base, Description description) {
-    return new JMXConfigurationStatement(base, description, this);
-  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/dc7d73e7/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/rules/DescribedExternalResource.java
----------------------------------------------------------------------
diff --git a/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/rules/DescribedExternalResource.java b/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/rules/DescribedExternalResource.java
new file mode 100644
index 0000000..543b7fc
--- /dev/null
+++ b/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/rules/DescribedExternalResource.java
@@ -0,0 +1,64 @@
+/*
+ * 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.
+ */
+
+/**
+ * this class extends the capability of JUnit's ExternalResource in that
+ * it provides a Description object in the before and after methods, so that
+ * the implementation would have access to the annotation of the test methods
+ */
+package com.gemstone.gemfire.test.junit.rules;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public class DescribedExternalResource implements TestRule {
+  public Statement apply(Statement base, Description description) {
+    return statement(base, description);
+  }
+
+  private Statement statement(final Statement base, final Description description) {
+    return new Statement() {
+      @Override
+      public void evaluate() throws Throwable {
+        before(description);
+        try {
+          base.evaluate();
+        } finally {
+          after(description);
+        }
+      }
+    };
+  }
+
+
+  /**
+   * Override to set up your specific external resource.
+   *
+   * @throws Throwable if setup fails (which will disable {@code after}
+   */
+  protected void before(Description description) throws Throwable {
+    // do nothing
+  }
+
+  /**
+   * Override to tear down your specific external resource.
+   */
+  protected void after(Description description) throws Throwable {
+    // do nothing
+  }
+}