1 8 9 package test.javax.management.remote.rmi; 10 11 import java.io.File ; 12 import java.io.Serializable ; 13 import java.net.URL ; 14 import java.util.Set ; 15 import javax.management.Attribute ; 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerConnection ; 18 import javax.management.Notification ; 19 import javax.management.NotificationFilter ; 20 import javax.management.NotificationListener ; 21 import javax.management.ObjectInstance ; 22 import javax.management.ObjectName ; 23 import javax.management.loading.PrivateMLet ; 24 import javax.management.remote.JMXConnector ; 25 import javax.management.remote.JMXConnectorFactory ; 26 import javax.management.remote.JMXConnectorServer ; 27 import javax.management.remote.JMXConnectorServerFactory ; 28 import javax.management.remote.JMXServiceURL ; 29 30 import test.MX4JTestCase; 31 import test.javax.management.remote.support.Marshalling; 32 import test.javax.management.remote.support.Unknown; 33 34 37 public class RMIMarshallingTest extends MX4JTestCase 38 { 39 private MBeanServer server = null; 40 private MBeanServerConnection conn = null; 41 private JMXConnectorServer cntorServer = null; 42 private JMXConnector cntor = null; 43 private ObjectName mbeanName; 44 private ObjectName mbeanLoaderName; 45 46 47 public RMIMarshallingTest(String s) 48 { 49 super(s); 50 } 51 52 public void setUp() throws Exception 53 { 54 super.setUp(); 55 File mbeanJar = new File ("dist/test/mx4j-tests.jar"); 57 PrivateMLet mbeanLoader = new PrivateMLet (new URL []{mbeanJar.toURL()}, getClass().getClassLoader().getParent(), false); 58 mbeanLoaderName = ObjectName.getInstance("marshal:type=mlet"); 59 60 server = newMBeanServer(); 61 server.registerMBean(mbeanLoader, mbeanLoaderName); 62 63 JMXServiceURL url = new JMXServiceURL ("rmi", "localhost", 0); 64 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server); 65 cntorServer.start(); 66 67 cntor = JMXConnectorFactory.connect(cntorServer.getAddress()); 68 conn = cntor.getMBeanServerConnection(); 69 mbeanName = ObjectName.getInstance("marshal:type=mbean"); 70 71 } 72 73 public void tearDown() throws Exception 74 { 75 if (cntor != null) cntor.close(); 76 if (cntorServer != null) cntorServer.stop(); 77 } 78 79 protected static class MockNotificationListener implements NotificationListener , Serializable 80 { 81 long numberOfNotifications = 0; 82 83 public void handleNotification(Notification notification, Object handback) 84 { 85 assertEquals(notification.getSequenceNumber(), numberOfNotifications); 87 assertEquals(notification.getType(), Unknown.class.getName()); 88 numberOfNotifications++; 89 synchronized (this) 90 { 91 this.notify(); 92 } 93 } 94 95 public synchronized void waitOnNotification(long timeout, long sequence) throws InterruptedException 96 { 97 long to; 98 if (timeout > 0) 99 to = System.currentTimeMillis() + timeout; 100 else 101 to = -1; 102 while (numberOfNotifications < sequence) { 104 this.wait(timeout); 105 if (to > -1 && System.currentTimeMillis() >= to) { 107 Thread.currentThread().interrupt(); 108 break; 109 } 110 } 111 } 112 113 } 114 115 protected static class MockNotificationFilter implements NotificationFilter , Serializable 116 { 117 118 public boolean isNotificationEnabled(Notification notification) 119 { 120 return true; 122 } 123 124 } 125 126 private void createMBean() throws Exception 127 { 128 ObjectInstance inst = conn.createMBean(Marshalling.class.getName(), mbeanName, new Object []{new Unknown()}, new String []{Unknown.class.getName()}); 129 } 130 131 public void testCreateMBean() throws Exception 132 { 133 conn.createMBean(Marshalling.class.getName(), mbeanName); 134 checkRegistration(); 135 136 conn.createMBean(Marshalling.class.getName(), 137 mbeanName, new Object []{new Unknown()}, 138 new String []{Unknown.class.getName()}); 139 checkRegistration(); 140 } 141 142 private void checkRegistration() throws Exception 143 { 144 if (!conn.isRegistered(mbeanName)) fail(); 146 if (!server.isRegistered(mbeanName)) fail(); 147 conn.unregisterMBean(mbeanName); 148 } 149 150 public void testInstanceOf() throws Exception 151 { 152 createMBean(); 153 if (!conn.isInstanceOf(mbeanName, Marshalling.class.getName())) fail(); 155 if (!server.isInstanceOf(mbeanName, Marshalling.class.getName())) fail(); 156 } 157 158 public void testInvocationUnknownReturn() throws Exception 159 { 160 createMBean(); 161 Object returned = conn.invoke(mbeanName, "unknownReturnValue", new Object [0], new String [0]); 163 if (!returned.getClass().getName().equals(Unknown.class.getName())) fail(); 164 returned = server.invoke(mbeanName, "unknownReturnValue", new Object [0], new String [0]); 165 if (!returned.getClass().getName().equals(Unknown.class.getName())) fail(); 166 Object remoteUnk = conn.invoke(mbeanName, "unknownArgument", new Object []{new Unknown()}, new String []{Unknown.class.getName()}); 167 Object localUnk = server.invoke(mbeanName, "unknownArgument", new Object []{new Unknown()}, new String []{Unknown.class.getName()}); 168 assertEquals(remoteUnk, localUnk); 169 } 170 171 public void testUnregisterMBean() throws Exception 172 { 173 createMBean(); 174 conn.unregisterMBean(mbeanName); 176 if (conn.isRegistered(mbeanName)) fail(); 177 if (server.isRegistered(mbeanName)) fail(); 178 179 } 180 181 public void testNotifications() throws Exception 182 { 183 createMBean(); 184 MockNotificationListener listener = new MockNotificationListener(); 185 conn.addNotificationListener(mbeanName, 186 listener, 187 new MockNotificationFilter(), 188 new Object ()); 189 190 Thread.sleep(1000L); 191 Attribute att = new Attribute ("UnknownAttribute", new Unknown()); 192 193 conn.setAttribute(mbeanName, att); 194 195 Thread.sleep(1000L); 196 try 198 { 199 listener.waitOnNotification(1000L, 1); 200 } 201 catch (InterruptedException ignore) 202 { 203 } 204 205 assertEquals(1, listener.numberOfNotifications); 206 207 conn.removeNotificationListener(mbeanName, listener); 208 209 Thread.sleep(1000L); 210 conn.setAttribute(mbeanName, att); 211 212 Thread.sleep(1000L); 213 try 215 { 216 listener.waitOnNotification(1000L, 2); 217 } 218 catch (InterruptedException ignore) 219 { 220 } 221 222 assertEquals(1, listener.numberOfNotifications); 223 } 224 225 public void testQuery() throws Exception 226 { 227 createMBean(); 228 ObjectName pattern = mbeanName; 229 ObjectName query = mbeanName; 230 Set beans = conn.queryMBeans(pattern, query); 231 Object [] set = beans.toArray(); 232 assertEquals(1, set.length); 233 ObjectInstance inst = (ObjectInstance )set[0]; 235 assertTrue(inst.getClassName().equals(Marshalling.class.getName())); 236 237 beans = conn.queryNames(pattern, query); 238 set = beans.toArray(); 239 assertEquals(1, set.length); 240 ObjectName nm = (ObjectName )set[0]; 242 assertTrue(nm.equals(mbeanName)); 243 } 244 245 public void testAttributes() throws Exception 246 { 247 createMBean(); 248 Unknown value = new Unknown(); 249 Attribute att = new Attribute ("UnknownAttribute", value); 250 conn.setAttribute(mbeanName, att); 251 Object returned = conn.getAttribute(mbeanName, "UnknownAttribute"); 252 assertTrue(returned.getClass().getName().equals(Unknown.class.getName())); 253 } 254 } 255 | Popular Tags |