1 9 package test.compliance.core.serviceurl; 10 11 import java.net.InetAddress ; 12 import java.net.MalformedURLException ; 13 import java.net.UnknownHostException ; 14 import javax.management.remote.JMXServiceURL ; 15 16 import junit.framework.TestCase; 17 18 21 public class JMXServiceURLTest extends TestCase 22 { 23 private final String protocol = "rmi"; 24 private final String defaultProtocol = "jmxmp"; 25 private final String ipv6Host = "1080:0:0:0:8:800:200C:417A"; 26 private final String longSlashPath = "/jndi/rmi://localhost:1099/jmxconnector"; 27 private final String longSemicolonPath = ";jndi/rmi://localhost:1099/jmxconnector"; 28 private final String host = "localhost"; 29 private final int port = 5400; 30 31 public JMXServiceURLTest(String name) 32 { 33 super(name); 34 } 35 36 public void testValidURLWithIPv6Host() 37 { 38 39 JMXServiceURL serviceURL = null; 40 41 try 42 { 43 serviceURL = new JMXServiceURL ("service:jmx:" + protocol + "://[" + ipv6Host + "]" + longSlashPath); 44 } 45 catch(Throwable ex) 46 { 47 assertTrue("Got exception " + ex, false); 48 } 49 assertEquals(protocol, serviceURL.getProtocol()); 50 assertEquals(ipv6Host, serviceURL.getHost()); 51 assertEquals(longSlashPath, serviceURL.getURLPath()); 52 assertEquals(0, serviceURL.getPort()); 53 54 try 55 { 56 serviceURL = new JMXServiceURL ("service:jmx:" + protocol + "://[" + ipv6Host + "]:" + port + longSlashPath); 57 } 58 catch(Throwable ex) 59 { 60 assertTrue("Got exception " + ex, false); 61 } 62 assertEquals(protocol, serviceURL.getProtocol()); 63 assertEquals(ipv6Host, serviceURL.getHost()); 64 assertEquals(longSlashPath, serviceURL.getURLPath()); 65 assertEquals(port, serviceURL.getPort()); 66 67 } 68 69 public void testToString() 70 { 71 String url = "service:jmx:" + protocol + "://[" + ipv6Host + "]:" + port + longSlashPath; 72 JMXServiceURL serviceURL = null; 73 74 try 75 { 76 serviceURL = new JMXServiceURL (url); 77 } 78 catch(Throwable ex) 79 { 80 assertTrue("Got exception " + ex, false); 81 } 82 assertEquals(url, serviceURL.toString()); 83 } 84 85 public void testEquals() 86 { 87 String url = "service:jmx:" + protocol + "://[" + ipv6Host + "]:" + port + longSlashPath; 88 JMXServiceURL serviceURL = null; 89 JMXServiceURL serviceURL2 = null; 90 91 try 92 { 93 serviceURL = new JMXServiceURL (url); 94 serviceURL2 = new JMXServiceURL (url); 95 } 96 catch(Throwable ex) 97 { 98 assertTrue("Got exception " + ex, false); 99 } 100 assertEquals(serviceURL, serviceURL2); 101 } 102 103 public void testNoHostOrPort() 104 { 105 JMXServiceURL serviceURL = null; 106 try 107 { 108 serviceURL = new JMXServiceURL ("service:jmx:" + protocol + "://" + longSlashPath); 109 } 110 catch(Throwable ex) 111 { 112 assertTrue("Got exception " + ex, false); 113 } 114 assertEquals(protocol, serviceURL.getProtocol()); 115 try 116 { 117 assertEquals(InetAddress.getLocalHost().getHostName(), serviceURL.getHost()); 118 } 119 catch(UnknownHostException e) 120 { 121 e.printStackTrace(); 122 } 123 assertEquals(longSlashPath, serviceURL.getURLPath()); 124 assertEquals(0, serviceURL.getPort()); 125 126 } 127 128 public void testValidURLWithSlashPathSeperator() 129 { 130 JMXServiceURL serviceURL = null; 131 try 132 { 133 serviceURL = new JMXServiceURL ("service:jmx:" + protocol + "://" + host + longSlashPath); 134 } 135 catch(Throwable ex) 136 { 137 assertTrue("Got exception " + ex, false); 138 } 139 assertEquals(protocol, serviceURL.getProtocol()); 140 assertEquals(host, serviceURL.getHost()); 141 assertEquals(longSlashPath, serviceURL.getURLPath()); 142 assertEquals(0, serviceURL.getPort()); 143 144 try 145 { 146 serviceURL = new JMXServiceURL ("service:jmx:" + protocol + "://" + host + ":" + port + longSlashPath); 147 } 148 catch(Throwable ex) 149 { 150 assertTrue("Got exception " + ex, false); 151 } 152 assertEquals(protocol, serviceURL.getProtocol()); 153 assertEquals(host, serviceURL.getHost()); 154 assertEquals(longSlashPath, serviceURL.getURLPath()); 155 assertEquals(port, serviceURL.getPort()); 156 } 157 158 public void testValidURLWithSemicolonPathSeperator() 159 { 160 JMXServiceURL serviceURL = null; 161 162 try 163 { 164 serviceURL = new JMXServiceURL ("service:jmx:" + protocol + "://" + host + longSemicolonPath); 165 } 166 catch(Throwable ex) 167 { 168 assertTrue("Got exception " + ex, false); 169 } 170 assertEquals(protocol, serviceURL.getProtocol()); 171 assertEquals(host, serviceURL.getHost()); 172 assertEquals(longSemicolonPath, serviceURL.getURLPath()); 173 assertEquals(0, serviceURL.getPort()); 174 175 try 176 { 177 serviceURL = new JMXServiceURL ("service:jmx:" + protocol + "://" + host + ":" + port + longSemicolonPath); 178 } 179 catch(Throwable ex) 180 { 181 assertTrue("Got exception " + ex, false); 182 } 183 assertEquals(protocol, serviceURL.getProtocol()); 184 assertEquals(host, serviceURL.getHost()); 185 assertEquals(longSemicolonPath, serviceURL.getURLPath()); 186 assertEquals(port, serviceURL.getPort()); 187 188 } 189 190 public void testNoProtocolWithSuffix() 191 { 192 JMXServiceURL serviceURL = null; 193 194 try 195 { 196 serviceURL = new JMXServiceURL ("service:jmx:://" + host + ":" + port); 197 } 198 catch(Throwable ex) 199 { 200 assertTrue("Got exception " + ex, false); 201 } 202 assertEquals(defaultProtocol, serviceURL.getProtocol()); 203 assertEquals(host, serviceURL.getHost()); 204 assertEquals(port, serviceURL.getPort()); 205 } 206 207 public void testParamConstructWithIPv6Host() 208 { 209 JMXServiceURL serviceURL = null; 210 211 try 212 { 213 serviceURL = new JMXServiceURL (protocol, "[" + ipv6Host + "]", port); 214 } 215 catch(Throwable ex) 216 { 217 assertTrue("Got exception " + ex, false); 218 } 219 assertEquals(protocol, serviceURL.getProtocol()); 220 assertEquals(ipv6Host, serviceURL.getHost()); 221 assertEquals(port, serviceURL.getPort()); 222 } 223 224 public void testNullURL() 225 { 226 try 227 { 228 JMXServiceURL serviceURL = new JMXServiceURL (null); 229 } 230 catch(MalformedURLException e) 231 { 232 assertTrue("Got MalformedULRException and expected NullPointerException.", false); 233 } 234 catch(NullPointerException npex) 235 { 236 assertTrue(true); 237 return; 238 } 239 catch(Throwable ex) 240 { 241 assertTrue("Expected NullPointerException, but got " + ex, false); 242 } 243 assertTrue("Did not throw NullPointerException as expected.", false); 244 } 245 246 public void testNoPrefix() 247 { 248 try 249 { 250 JMXServiceURL serviceURL = new JMXServiceURL ("rmi://myhost:0"); 251 } 252 catch(MalformedURLException e) 253 { 254 assertTrue(true); 255 return; 256 } 257 catch(Throwable ex) 258 { 259 assertTrue("Expected MalformedURLException, but got " + ex, false); 260 } 261 assertTrue("Did not throw MalformedURLException as expected.", false); 262 } 263 264 public void testPortWithoutHost() 265 { 266 try 267 { 268 JMXServiceURL serviceURL = new JMXServiceURL ("service:jmx:://:1234"); 269 } 270 catch(MalformedURLException e) 271 { 272 assertTrue(true); 273 return; 274 } 275 catch(Throwable ex) 276 { 277 assertTrue("Expected MalformedURLException, but got " + ex, false); 278 } 279 assertTrue("Did not throw MalformedURLException as expected.", false); 280 } 281 282 public void testNoProtocolWithoutSuffix() 283 { 284 try 285 { 286 JMXServiceURL serviceURL = new JMXServiceURL ("service:jmx:myhost:0"); 287 } 288 catch(MalformedURLException e) 289 { 290 assertTrue(true); 291 return; 292 } 293 catch(Throwable ex) 294 { 295 assertTrue("Expected MalformedURLException, but got " + ex, false); 296 } 297 assertTrue("Did not throw MalformedURLException as expected.", false); 298 } 299 300 public void testInvalidProtocolCharacter() 301 { 302 try 303 { 304 JMXServiceURL serviceURL = new JMXServiceURL ("service:jmx:rm$i://myhost:0"); 305 } 306 catch(MalformedURLException e) 307 { 308 assertTrue(true); 309 return; 310 } 311 catch(Throwable ex) 312 { 313 assertTrue("Expected MalformedURLException, but got " + ex, false); 314 } 315 assertTrue("Did not throw MalformedURLException as expected.", false); 316 } 317 318 } 319 | Popular Tags |