1 8 9 package test.javax.management; 10 11 import javax.management.Attribute ; 12 import javax.management.AttributeList ; 13 import javax.management.JMRuntimeException ; 14 import javax.management.MBeanServer ; 15 import javax.management.MBeanServerFactory ; 16 import javax.management.NotCompliantMBeanException ; 17 import javax.management.ObjectName ; 18 import javax.management.RuntimeMBeanException ; 19 20 import test.MX4JTestCase; 21 import test.javax.management.support.MBeanDynamic; 22 23 26 public class DynamicMBeanFunctionalityTest extends MX4JTestCase 27 { 28 private MBeanServer m_server; 29 private ObjectName m_name; 30 31 public DynamicMBeanFunctionalityTest(String s) 32 { 33 super(s); 34 } 35 36 protected void setUp() throws Exception 37 { 38 m_server = MBeanServerFactory.createMBeanServer("dynamic"); 39 m_name = new ObjectName (":type=dynamic"); 40 Object dynamic = new MBeanDynamic(); 41 m_server.registerMBean(dynamic, m_name); 42 } 43 44 protected void tearDown() throws Exception 45 { 46 m_server.unregisterMBean(m_name); 47 MBeanServerFactory.releaseMBeanServer(m_server); 48 } 49 50 public void testGetAttributes() throws Exception 51 { 52 AttributeList list = null; 53 54 list = m_server.getAttributes(m_name, new String [0]); 55 assertEquals(list.size(), 0); 56 57 list = m_server.getAttributes(m_name, new String []{"doesNotExist"}); 58 if (list.size() != 0) 59 { 60 fail("Attribute does not exist"); 61 } 62 63 String attributeName = "DynamicAttribute1"; 64 65 list = m_server.getAttributes(m_name, new String []{attributeName}); 66 if (list.size() != 1 && ((Attribute )list.get(0)).getName().equals(attributeName)) 67 { 68 fail("Attribute exists"); 69 } 70 } 71 72 public void testGetSetAttribute() throws Exception 73 { 74 String attributeName = "DynamicAttribute1"; 75 76 Object valueBefore = m_server.getAttribute(m_name, attributeName); 77 78 Object newValue = "newValue"; 79 Attribute attribute = new Attribute (attributeName, newValue); 80 m_server.setAttribute(m_name, attribute); 81 82 Object valueAfter = m_server.getAttribute(m_name, attributeName); 83 if (valueAfter.equals(valueBefore) || !valueAfter.equals(newValue)) 84 { 85 fail("setAttribute does not work"); 86 } 87 } 88 89 public void testSetAttributes() throws Exception 90 { 91 String attributeName1 = "DynamicAttribute1"; 92 Object value1Before = m_server.getAttribute(m_name, attributeName1); 93 String attributeName2 = "DynamicAttribute2"; 94 Object value2Before = m_server.getAttribute(m_name, attributeName2); 95 AttributeList changeThese = new AttributeList (); 96 AttributeList list = m_server.setAttributes(m_name, changeThese); 97 if (list.size() != 0) 98 { 99 fail("No Attributes were changed"); 100 } 101 if (!value1Before.equals(m_server.getAttribute(m_name, attributeName1)) || !value2Before.equals(m_server.getAttribute(m_name, attributeName2))) 102 { 103 fail("Attribute was not changed"); 104 } 105 Attribute attr = new Attribute (attributeName2, "Value2"); 106 changeThese.add(attr); 107 list = m_server.setAttributes(m_name, changeThese); 108 if (list.size() != 1) 109 { 110 fail("One attribute was changed"); 111 } 112 if (!list.get(0).equals(attr)) 113 { 114 fail("Wrong return value"); 115 } 116 if (!value1Before.equals(m_server.getAttribute(m_name, attributeName1)) || 117 value2Before.equals(m_server.getAttribute(m_name, attributeName2)) || 118 !attr.getValue().equals(m_server.getAttribute(m_name, attributeName2))) 119 { 120 fail("Attribute was not changed"); 121 } 122 } 123 124 public void testInvoke() throws Exception 125 { 126 String attributeName1 = "DynamicAttribute1"; 127 Object value1 = m_server.getAttribute(m_name, attributeName1); 128 129 Boolean result = (Boolean )m_server.invoke(m_name, "dynamicOperation", new Object []{"dummy"}, new String []{"java.lang.String"}); 130 if (result.booleanValue()) 131 { 132 fail("Operation does not work"); 133 } 134 result = (Boolean )m_server.invoke(m_name, "dynamicOperation", new Object []{value1}, new String []{"java.lang.String"}); 135 if (!result.booleanValue()) 136 { 137 fail("Operation does not work"); 138 } 139 } 140 141 public void testGetMBeanInfoRegistrationException() throws Exception 142 { 143 try 144 { 145 m_server.createMBean("test.javax.management.support.ExceptionGeneratingDMB", 146 new ObjectName (":register=no"), 147 new Object []{new Boolean (false)}, 148 new String []{"boolean"}); 149 fail("Expecting NotCompliantMBeanException"); 150 } 151 catch (NotCompliantMBeanException x) 152 { 153 } 154 } 155 156 public void testGetMBeanInfoInvocationException() throws Exception 157 { 158 try 159 { 160 ObjectName objname = new ObjectName (":register=yes"); 161 m_server.createMBean("test.javax.management.support.ExceptionGeneratingDMB", 162 objname, 163 new Object []{new Boolean (true)}, 164 new String []{"boolean"}); 165 m_server.getMBeanInfo(objname); 166 fail("Expecting RuntimeMBeanException"); 167 } 168 catch (RuntimeMBeanException x) 169 { 170 } 171 } 172 173 public void testNullMBeanInfo() throws Exception 174 { 175 try 176 { 177 ObjectName objname = new ObjectName (":id=testNullMBeanInfo"); 178 m_server.createMBean("test.javax.management.support.NullMBeanInfoDMB", objname); 179 m_server.getMBeanInfo(objname); 180 fail("Expecting JMRuntimeException"); 181 } 182 catch (JMRuntimeException x) 183 { 184 } 185 } 186 } 187 | Popular Tags |