1 16 17 package org.springframework.jmx.support; 18 19 import java.beans.PropertyDescriptor ; 20 21 import javax.management.DynamicMBean ; 22 import javax.management.NotCompliantMBeanException ; 23 import javax.management.StandardMBean ; 24 25 import junit.framework.TestCase; 26 27 import org.springframework.beans.BeanWrapperImpl; 28 import org.springframework.jmx.IJmxTestBean; 29 import org.springframework.jmx.JmxTestBean; 30 import org.springframework.jmx.export.TestDynamicMBean; 31 32 35 public class JmxUtilsTests extends TestCase { 36 37 public void testIsMBeanWithDynamicMBean() throws Exception { 38 DynamicMBean mbean = new TestDynamicMBean(); 39 assertTrue("Dynamic MBean not detected correctly", JmxUtils.isMBean(mbean.getClass())); 40 } 41 42 public void testIsMBeanWithStandardMBeanWrapper() throws Exception { 43 StandardMBean mbean = new StandardMBean (new JmxTestBean(), IJmxTestBean.class); 44 assertTrue("Standard MBean not detected correctly", JmxUtils.isMBean(mbean.getClass())); 45 } 46 47 public void testIsMBeanWithStandardMBeanInherited() throws Exception { 48 StandardMBean mbean = new StandardMBeanImpl(); 49 assertTrue("Standard MBean not detected correctly", JmxUtils.isMBean(mbean.getClass())); 50 } 51 52 public void testNotAnMBean() throws Exception { 53 assertFalse("Object incorrectly identified as an MBean", JmxUtils.isMBean(Object .class)); 54 } 55 56 public void testSimpleMBean() throws Exception { 57 Foo foo = new Foo(); 58 assertTrue("Simple MBean not detected correctly", JmxUtils.isMBean(foo.getClass())); 59 } 60 61 public void testSimpleMBeanThroughInheritance() throws Exception { 62 Bar bar = new Bar(); 63 Abc abc = new Abc(); 64 assertTrue("Simple MBean (through inheritance) not detected correctly", 65 JmxUtils.isMBean(bar.getClass())); 66 assertTrue("Simple MBean (through 2 levels of inheritance) not detected correctly", 67 JmxUtils.isMBean(abc.getClass())); 68 } 69 70 public void testGetAttributeNameWithStrictCasing() { 71 PropertyDescriptor pd = new BeanWrapperImpl(AttributeTest.class).getPropertyDescriptor("name"); 72 String attributeName = JmxUtils.getAttributeName(pd, true); 73 assertEquals("Incorrect casing on attribute name", "Name", attributeName); 74 } 75 76 public void testGetAttributeNameWithoutStrictCasing() { 77 PropertyDescriptor pd = new BeanWrapperImpl(AttributeTest.class).getPropertyDescriptor("name"); 78 String attributeName = JmxUtils.getAttributeName(pd, false); 79 assertEquals("Incorrect casing on attribute name", "name", attributeName); 80 } 81 82 83 public static class AttributeTest { 84 85 private String name; 86 87 public String getName() { 88 return name; 89 } 90 91 public void setName(String name) { 92 this.name = name; 93 } 94 } 95 96 97 public static class StandardMBeanImpl extends StandardMBean implements IJmxTestBean { 98 99 public StandardMBeanImpl() throws NotCompliantMBeanException { 100 super(IJmxTestBean.class); 101 } 102 103 public int add(int x, int y) { 104 return 0; 105 } 106 107 public long myOperation() { 108 return 0; 109 } 110 111 public int getAge() { 112 return 0; 113 } 114 115 public void setAge(int age) { 116 } 117 118 public void setName(String name) { 119 } 120 121 public String getName() { 122 return null; 123 } 124 125 public void dontExposeMe() { 126 } 127 } 128 129 130 public static interface FooMBean { 131 132 String getName(); 133 } 134 135 136 public static class Foo implements FooMBean { 137 138 public String getName() { 139 return "Rob Harrop"; 140 } 141 } 142 143 144 public static class Bar extends Foo { 145 146 } 147 148 149 public static class Abc extends Bar { 150 151 } 152 153 } 154 | Popular Tags |