From d9d27ce76f29862026ce9342ed49d09416359856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20Mei=C3=9Fner?= Date: Thu, 30 Oct 2014 12:02:20 +0100 Subject: [PATCH] Added some testcases for different config interfaces and config without JMXBean interface. --- .../owner/jmx/InvalidJMXBeanTest.java | 33 ++++ .../org/aeonbits/owner/jmx/JMXMBeanTest.java | 141 +++++++++++++++++- 2 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 owner/src/test/java/org/aeonbits/owner/jmx/InvalidJMXBeanTest.java diff --git a/owner/src/test/java/org/aeonbits/owner/jmx/InvalidJMXBeanTest.java b/owner/src/test/java/org/aeonbits/owner/jmx/InvalidJMXBeanTest.java new file mode 100644 index 00000000..90014349 --- /dev/null +++ b/owner/src/test/java/org/aeonbits/owner/jmx/InvalidJMXBeanTest.java @@ -0,0 +1,33 @@ +package org.aeonbits.owner.jmx; + +import java.lang.management.ManagementFactory; +import java.util.Properties; + +import javax.management.InstanceAlreadyExistsException; +import javax.management.MBeanRegistrationException; +import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; +import javax.management.NotCompliantMBeanException; +import javax.management.ObjectName; + +import org.aeonbits.owner.Config; +import org.aeonbits.owner.ConfigFactory; +import org.junit.Test; + +public class InvalidJMXBeanTest { + + private static interface ConfigNoJMX extends Config { + @DefaultValue("1") + int number(); + } + + @Test(expected=NotCompliantMBeanException.class) + public void testConfigWithoutJMXInterface() throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException{ + Properties props = new Properties(); + ConfigNoJMX config = ConfigFactory.create(ConfigNoJMX.class, props); + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + ObjectName mbeanName = new ObjectName("org.aeonbits.owner.jmx:type=testConfigWithoutJMXInterface,id=ConfigNoJMX"); + mbs.registerMBean(config, mbeanName); + } + +} diff --git a/owner/src/test/java/org/aeonbits/owner/jmx/JMXMBeanTest.java b/owner/src/test/java/org/aeonbits/owner/jmx/JMXMBeanTest.java index fe71b223..7974fc63 100644 --- a/owner/src/test/java/org/aeonbits/owner/jmx/JMXMBeanTest.java +++ b/owner/src/test/java/org/aeonbits/owner/jmx/JMXMBeanTest.java @@ -11,24 +11,27 @@ import javax.management.AttributeNotFoundException; import javax.management.InstanceAlreadyExistsException; import javax.management.InstanceNotFoundException; +import javax.management.IntrospectionException; +import javax.management.InvalidAttributeValueException; import javax.management.MBeanException; -import javax.management.MBeanRegistrationException; +import javax.management.MBeanInfo; +import javax.management.MBeanOperationInfo; +import javax.management.MBeanParameterInfo; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; import javax.management.ReflectionException; -import junit.framework.Assert; - -import org.aeonbits.owner.ConfigCache; import org.aeonbits.owner.ConfigFactory; import org.aeonbits.owner.JMXBean; +import org.aeonbits.owner.Mutable; +import org.aeonbits.owner.Reloadable; import org.junit.Test; public class JMXMBeanTest { - private static interface JMXConfig extends JMXBean { + private static interface JMXConfigMutableReloadable extends JMXBean, Mutable, Reloadable { @DefaultValue("8080") int port(); @@ -38,14 +41,40 @@ private static interface JMXConfig extends JMXBean { @DefaultValue("42") int maxThreads(); } - + + private static interface JMXConfigOnlyAccessible extends JMXBean { + @DefaultValue("1") + int number(); + } + + private static interface JMXConfigMutableNoReload extends JMXBean, Mutable { + @DefaultValue("1") + int number(); + } + + /** + * + * Simple test case for JMX accessible mbeans. + * + * Registers a config of JMXConfigMutableReloadable.class + * under object name org.aeonbits.owner.jmx:type=testBeanHandling,id=JMXConfigMutableReloadable and + * tests getAttribute(s) methods and invokes setProperty actions with it. + * + * @throws MalformedObjectNameException + * @throws AttributeNotFoundException + * @throws InstanceNotFoundException + * @throws MBeanException + * @throws ReflectionException + * @throws InstanceAlreadyExistsException + * @throws NotCompliantMBeanException + */ @Test public void testBeanHandling() throws MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, InstanceAlreadyExistsException, NotCompliantMBeanException { Properties props = new Properties(); - JMXConfig config = ConfigFactory.create(JMXConfig.class, props); + JMXConfigMutableReloadable config = ConfigFactory.create(JMXConfigMutableReloadable.class, props); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName mbeanName = new ObjectName( - "org.aeonbits.owner.jmx:type=JMXMBeanTest,id=JMXMBeanTest"); + "org.aeonbits.owner.jmx:type=testBeanHandling,id=JMXConfigMutableReloadable"); mbs.registerMBean(config, mbeanName); assertEquals("8080", mbs.getAttribute(mbeanName, "port")); @@ -63,4 +92,100 @@ public void testBeanHandling() throws MalformedObjectNameException, AttributeNot mbs.invoke(mbeanName, "reload", null, null); assertEquals(attrList, mbs.getAttributes(mbeanName, new String[] { "port", "hostname", "maxThreads"})); } + + /** + * + * Test case for registering multiple mbeans with same configuration object. + * + * @throws MalformedObjectNameException + * @throws AttributeNotFoundException + * @throws InstanceNotFoundException + * @throws MBeanException + * @throws ReflectionException + * @throws InstanceAlreadyExistsException + * @throws NotCompliantMBeanException + * @throws InvalidAttributeValueException + */ + @Test + public void testMultipleBeanHandling() throws MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, InstanceAlreadyExistsException, NotCompliantMBeanException, InvalidAttributeValueException { + Properties props = new Properties(); + + JMXConfigMutableReloadable config = ConfigFactory.create(JMXConfigMutableReloadable.class, props); + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + ObjectName mbeanName1 = new ObjectName( + "org.aeonbits.owner.jmx:type=testMultipleBeanHandling,id=JMXConfigMutableReloadable"); + ObjectName mbeanName2 = new ObjectName( + "org.aeonbits.owner.jmx:type=testMultipleBeanHandling2,id=JMXConfigMutableReloadable"); + mbs.registerMBean(config, mbeanName1); + mbs.registerMBean(config, mbeanName2); + mbs.setAttribute(mbeanName1, new Attribute("port", "7878")); + assertEquals("7878", mbs.getAttribute(mbeanName1, "port")); + assertEquals(mbs.getAttribute(mbeanName2, "port"), mbs.getAttribute(mbeanName1, "port")); + } + + /** + * Test of MBeanInfo description of immutable (and not reloadable) config instance + * + * @throws MalformedObjectNameException + * @throws AttributeNotFoundException + * @throws InstanceNotFoundException + * @throws MBeanException + * @throws ReflectionException + * @throws InstanceAlreadyExistsException + * @throws NotCompliantMBeanException + * @throws IntrospectionException + */ + @Test + public void testBeanNotMutableAndReloadable() throws MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, InstanceAlreadyExistsException, NotCompliantMBeanException, IntrospectionException { + Properties props = new Properties(); + JMXConfigOnlyAccessible config = ConfigFactory.create(JMXConfigOnlyAccessible.class, props); + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + ObjectName mbeanName = new ObjectName( + "org.aeonbits.owner.jmx:type=testBeanNotReloadable,id=JMXConfigOnlyAccessible"); + mbs.registerMBean(config, mbeanName); + MBeanInfo info = mbs.getMBeanInfo(mbeanName); + + MBeanOperationInfo[] operationsInfo = new MBeanOperationInfo[]{ + new MBeanOperationInfo("getProperty", "getProperties", + new MBeanParameterInfo[] { new MBeanParameterInfo("Propertykey", "java.lang.String", "Key of the property") }, + "java.lang.String", MBeanOperationInfo.INFO) + }; + assertArrayEquals(operationsInfo, info.getOperations()); + } + + /** + * Test of MBeanInfo description of not reloadable config instance + * + * @throws MalformedObjectNameException + * @throws AttributeNotFoundException + * @throws InstanceNotFoundException + * @throws MBeanException + * @throws ReflectionException + * @throws InstanceAlreadyExistsException + * @throws NotCompliantMBeanException + * @throws IntrospectionException + */ + @Test + public void testBeanNotReloadable() throws MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, InstanceAlreadyExistsException, NotCompliantMBeanException, IntrospectionException { + Properties props = new Properties(); + JMXConfigMutableNoReload config = ConfigFactory.create(JMXConfigMutableNoReload.class, props); + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + ObjectName mbeanName = new ObjectName( + "org.aeonbits.owner.jmx:type=testBeanNotReloadable,id=JMXConfigMutableNoReload"); + mbs.registerMBean(config, mbeanName); + MBeanInfo info = mbs.getMBeanInfo(mbeanName); + + MBeanOperationInfo[] operationsInfo = new MBeanOperationInfo[]{ + new MBeanOperationInfo("getProperty", "getProperties", + new MBeanParameterInfo[] { new MBeanParameterInfo("Propertykey", "java.lang.String", "Key of the property") }, + "java.lang.String", MBeanOperationInfo.INFO), + new MBeanOperationInfo("setProperty", "setProperties", + new MBeanParameterInfo[] { + new MBeanParameterInfo("Propertykey", "java.lang.String", "Key of the property"), + new MBeanParameterInfo("Propertyvalue", "java.lang.String", "Value of the property") + }, + "void", MBeanOperationInfo.ACTION) + }; + assertArrayEquals(operationsInfo, info.getOperations()); + } }