1 8 9 package test.javax.management.remote; 10 11 import java.io.IOException ; 12 import java.lang.reflect.InvocationHandler ; 13 import java.lang.reflect.Method ; 14 import java.lang.reflect.Proxy ; 15 import java.net.MalformedURLException ; 16 import java.util.Map ; 17 import javax.management.MBeanServer ; 18 import javax.management.remote.JMXConnector ; 19 import javax.management.remote.JMXConnectorFactory ; 20 import javax.management.remote.JMXConnectorServer ; 21 import javax.management.remote.JMXConnectorServerFactory ; 22 import javax.management.remote.JMXServiceURL ; 23 import javax.management.remote.MBeanServerForwarder ; 24 25 import test.MX4JTestCase; 26 27 30 public abstract class JMXConnectorServerTestCase extends MX4JTestCase 31 { 32 public JMXConnectorServerTestCase(String name) 33 { 34 super(name); 35 } 36 37 public abstract JMXServiceURL createJMXConnectorServerAddress() throws MalformedURLException ; 38 39 public abstract Map getEnvironment(); 40 41 public void testNewJMXConnectorServerWithNullURL() throws Exception 42 { 43 try 44 { 45 JMXConnectorServerFactory.newJMXConnectorServer(null, null, null); 46 fail(); 47 } 48 catch (NullPointerException x) 49 { 50 } 51 } 52 53 public void testNewJMXConnectorServerWithFactory() throws Exception 54 { 55 JMXServiceURL url = createJMXConnectorServerAddress(); 56 JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), null); 57 if (server == null) fail(); 58 } 59 60 public void testStartWithoutMBeanServer() throws Exception 61 { 62 JMXServiceURL url = createJMXConnectorServerAddress(); 63 JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), null); 64 try 65 { 66 server.start(); 67 fail(); 68 } 69 catch (IllegalStateException x) 70 { 71 } 72 } 73 74 public void testMBeanServerForwarderNoMBeanServer() throws Exception 75 { 76 JMXServiceURL url = createJMXConnectorServerAddress(); 77 JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), null); 78 MBeanServerForwarder forwarder = (MBeanServerForwarder )Proxy.newProxyInstance(getClass().getClassLoader(), new Class []{MBeanServerForwarder .class}, new InvocationHandler () 79 { 80 public Object invoke(Object proxy, Method method, Object [] args) 81 throws Throwable 82 { 83 return null; 84 } 85 }); 86 87 try 88 { 89 server.setMBeanServerForwarder(forwarder); 90 fail("No MBeanServer to forward to"); 91 } 92 catch (IllegalStateException x) 93 { 94 } 95 } 96 97 public void testMBeanServerForwarder() throws Exception 98 { 99 JMXServiceURL url = createJMXConnectorServerAddress(); 100 JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 101 MBeanServerForwarder forwarder = (MBeanServerForwarder )Proxy.newProxyInstance(getClass().getClassLoader(), new Class []{MBeanServerForwarder .class}, new InvocationHandler () 102 { 103 public Object invoke(Object proxy, Method method, Object [] args) 104 throws Throwable 105 { 106 return null; 107 } 108 }); 109 110 server.setMBeanServerForwarder(forwarder); 111 112 try 113 { 114 server.start(); 115 if (server.getMBeanServer() != forwarder) fail(); 116 } 117 finally 118 { 119 server.stop(); 120 } 121 } 122 123 public void testStart() throws Exception 124 { 125 JMXConnectorServer cntorServer = null; 126 try 127 { 128 JMXServiceURL url = createJMXConnectorServerAddress(); 129 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 130 cntorServer.start(); 131 } 132 finally 133 { 134 if (cntorServer != null) cntorServer.stop(); 135 } 136 } 137 138 public void testStartStart() throws Exception 139 { 140 JMXConnectorServer cntorServer = null; 141 try 142 { 143 JMXServiceURL url = createJMXConnectorServerAddress(); 144 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 145 cntorServer.start(); 146 cntorServer.start(); 147 } 148 finally 149 { 150 if (cntorServer != null) cntorServer.stop(); 151 } 152 } 153 154 public void testStartStop() throws Exception 155 { 156 JMXServiceURL url = createJMXConnectorServerAddress(); 157 JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 158 cntorServer.start(); 159 cntorServer.stop(); 160 } 161 162 public void testStartStopStart() throws Exception 163 { 164 JMXServiceURL url = createJMXConnectorServerAddress(); 165 JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 166 cntorServer.start(); 167 cntorServer.stop(); 168 try 169 { 170 cntorServer.start(); 171 fail(); 172 } 173 catch (IOException x) 174 { 175 } 176 } 177 178 public void testStartStopStop() throws Exception 179 { 180 JMXServiceURL url = createJMXConnectorServerAddress(); 181 JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 182 cntorServer.start(); 183 cntorServer.stop(); 184 cntorServer.stop(); 185 } 186 187 public void testTwoConnectorServers() throws Exception 188 { 189 JMXConnectorServer cntorServer1 = null; 190 JMXConnectorServer cntorServer2 = null; 191 JMXConnector cntor10 = null; 192 JMXConnector cntor11 = null; 193 JMXConnector cntor12 = null; 194 JMXConnector cntor20 = null; 195 try 196 { 197 JMXServiceURL url1 = createJMXConnectorServerAddress(); 198 JMXServiceURL url2 = new JMXServiceURL (url1.getProtocol(), url1.getHost(), (url1.getPort() > 0) ? (url1.getPort() + 1) : 0, url1.getURLPath()); 199 200 MBeanServer server = newMBeanServer(); 201 cntorServer1 = JMXConnectorServerFactory.newJMXConnectorServer(url1, getEnvironment(), server); 202 cntorServer2 = JMXConnectorServerFactory.newJMXConnectorServer(url2, getEnvironment(), server); 203 cntorServer1.start(); 204 cntorServer2.start(); 205 206 cntor10 = JMXConnectorFactory.connect(cntorServer1.getAddress(), getEnvironment()); 207 cntor11 = JMXConnectorFactory.connect(cntorServer1.getAddress(), getEnvironment()); 208 cntor12 = JMXConnectorFactory.connect(cntorServer1.getAddress(), getEnvironment()); 209 cntor20 = JMXConnectorFactory.connect(cntorServer2.getAddress(), getEnvironment()); 210 211 if (cntor10.getConnectionId().equals(cntor11.getConnectionId())) fail(); 212 if (cntor10.getConnectionId().equals(cntor12.getConnectionId())) fail(); 213 if (cntor10.getConnectionId().equals(cntor20.getConnectionId())) fail(); 214 215 } 216 finally 217 { 218 if (cntor20 != null) cntor20.close(); 219 if (cntor12 != null) cntor12.close(); 220 if (cntor11 != null) cntor11.close(); 221 if (cntor10 != null) cntor10.close(); 222 223 if (cntorServer2 != null) cntorServer2.stop(); 224 if (cntorServer1 != null) cntorServer1.stop(); 225 } 226 } 227 228 public void testStartWithProviderClassLoader() throws Exception 229 { 230 JMXConnectorServer cntorServer = null; 231 try 232 { 233 JMXServiceURL url = createJMXConnectorServerAddress(); 234 MBeanServer server = newMBeanServer(); 235 Map serverEnv = getEnvironment(); 236 serverEnv.put(JMXConnectorServerFactory.PROTOCOL_PROVIDER_CLASS_LOADER, getClass().getClassLoader()); 237 Thread.currentThread().setContextClassLoader(getClass().getClassLoader().getParent()); 238 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, serverEnv, server); 239 cntorServer.start(); 240 } 241 finally 242 { 243 if (cntorServer != null) cntorServer.stop(); 244 } 245 } 246 } 247 | Popular Tags |