| 1 22 package org.jboss.test.jmx.test; 23 24 import javax.management.Attribute ; 25 import javax.management.MBeanInfo ; 26 import javax.management.MBeanServerConnection ; 27 import javax.management.MalformedObjectNameException ; 28 import javax.management.ObjectName ; 29 import javax.management.ReflectionException ; 30 import javax.management.ListenerNotFoundException ; 31 import javax.management.Notification ; 32 import javax.management.NotificationFilter ; 33 import javax.management.remote.JMXServiceURL ; 34 import javax.management.remote.JMXConnector ; 35 import javax.management.remote.JMXConnectorFactory ; 36 37 import junit.framework.Test; 38 import org.jboss.jmx.adaptor.rmi.RMIAdaptor; 39 import org.jboss.test.JBossTestCase; 40 import org.jboss.test.jmx.invoker.CustomClass; 41 import org.jboss.test.jmx.invoker.InvokerTestMBean; 42 import org.jboss.test.jmx.invoker.Listener; 43 import org.jboss.test.jmx.invoker.BadListener; 44 import org.jboss.test.jmx.invoker.RunTimerFilter; 45 46 53 public class JMXConnectorUnitTestCase 54 extends JBossTestCase 55 { 56 private JMXConnector connector; 57 private MBeanServerConnection server; 58 59 public JMXConnectorUnitTestCase(String name) 60 { 61 super(name); 62 } 63 64 public static Test suite() 65 throws Exception  66 { 67 return getDeploySetup(JMXConnectorUnitTestCase.class, "invoker-adaptor-test.ear"); 68 } 69 70 75 ObjectName getObjectName() throws MalformedObjectNameException  76 { 77 return InvokerTestMBean.OBJECT_NAME; 78 } 79 80 public void setUp() throws Exception  81 { 82 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost/jndi/rmi://localhost:1090/jmxconnector"); 83 connector = JMXConnectorFactory.connect(url); 84 server = connector.getMBeanServerConnection(); 85 } 86 87 public void tearDown() throws Exception  88 { 89 if(connector != null) 90 { 91 connector.close(); 92 } 93 } 94 95 public void testGetSomething() 96 throws Exception  97 { 98 log.info("+++ testGetSomething"); 99 assertEquals("something", server.getAttribute(getObjectName(), "Something")); 100 } 101 102 public void testGetCustom() 103 throws Exception  104 { 105 log.info("+++ testGetCustom"); 106 CustomClass custom = (CustomClass) server.getAttribute(getObjectName(), "Custom"); 107 assertEquals("InitialValue", custom.getValue()); 108 } 109 110 public void testGetCustomXMBean() 111 throws Exception  112 { 113 log.info("+++ testGetCustomXMBean"); 114 ObjectName xmbean = new ObjectName ("jboss.test:service=InvokerTest,type=XMBean"); 115 CustomClass custom = (CustomClass) server.getAttribute(xmbean, "Custom"); 116 assertEquals("InitialValue", custom.getValue()); 117 } 118 public void testGetXMBeanInfo() 119 throws Exception  120 { 121 log.info("+++ testGetXMBeanInfo"); 122 ObjectName xmbean = new ObjectName ("jboss.test:service=InvokerTest,type=XMBean"); 123 MBeanInfo info = server.getMBeanInfo(xmbean); 124 log.info("MBeanInfo: "+info); 125 } 126 public void testXMBeanDoSomething() 127 throws Exception  128 { 129 log.info("+++ testXMBeanDoSomething"); 130 ObjectName xmbean = new ObjectName ("jboss.test:service=InvokerTest,type=XMBean"); 131 Object [] args = {}; 132 String [] sig = {}; 133 CustomClass custom = (CustomClass) server.invoke(xmbean, "doSomething", args, sig); 134 log.info("doSomething: "+custom); 135 } 136 137 public void testSetCustom() 138 throws Exception  139 { 140 log.info("+++ testSetCustom"); 141 server.setAttribute(getObjectName(), new Attribute ("Custom", new CustomClass("changed"))); 142 CustomClass custom = (CustomClass) server.getAttribute(getObjectName(), "Custom"); 143 assertEquals("changed", custom.getValue()); 144 } 145 146 152 public void testClassNotFoundException() throws Exception  153 { 154 log.info("+++ testClassNotFoundException"); 155 ObjectName name = new ObjectName ("jboss.test:test=testClassNotFoundException"); 156 try 157 { 158 server.createMBean("org.jboss.text.jmx.DoesNotExist", name); 159 fail("Was able to create org.jboss.text.jmx.DoesNotExist mbean"); 160 } 161 catch (ReflectionException e) 162 { 163 Exception ex = e.getTargetException(); 164 assertTrue("ReflectionException.target is ClassNotFoundException", 165 ex instanceof ClassNotFoundException ); 166 } 167 } 168 169 172 public void testNotification() throws Exception  173 { 174 log.info("+++ testNotification"); 175 Listener listener = new Listener(10); 176 server.addNotificationListener(getObjectName(), listener, new RunTimerFilter(), "runTimer"); 177 synchronized( listener ) 178 { 179 listener.wait(15000); 180 } 181 server.removeNotificationListener(getObjectName(), listener); 182 int count = listener.getCount(); 183 assertTrue("Received 10 notifications, count="+count, count == 10); 184 } 185 186 192 public void testNotificationWithBadListener() throws Exception  193 { 194 log.info("+++ testNotificationWithBadListener"); 195 BadListener badListener = new BadListener(); 197 server.addNotificationListener(getObjectName(), badListener, null, "BadListener"); 198 Listener listener = new Listener(10); 199 server.addNotificationListener(getObjectName(), listener, new RunTimerFilter(), "runTimer"); 201 synchronized( listener ) 203 { 204 listener.wait(25000); 205 } 206 server.removeNotificationListener(getObjectName(), listener); 207 int count = listener.getCount(); 208 assertTrue("Received 10 notifications from Listener, count="+count, 209 count == 10); 210 count = badListener.getCount(); 211 assertTrue("Received >= 1 notifications from BadListener, count="+count, 212 count >= 1); 213 try 214 { 215 server.removeNotificationListener(getObjectName(), badListener); 216 } 217 catch(ListenerNotFoundException e) 218 { 219 log.debug("The BadListener was not found", e); 220 } 221 } 222 223 } 224 | Popular Tags |