KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > core > serviceurl > JMXServiceURLTest


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package test.compliance.core.serviceurl;
10
11 import java.net.InetAddress JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.net.UnknownHostException JavaDoc;
14 import javax.management.remote.JMXServiceURL JavaDoc;
15
16 import junit.framework.TestCase;
17
18 /**
19  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
20  */

21 public class JMXServiceURLTest extends TestCase
22 {
23    private final String JavaDoc protocol = "rmi";
24    private final String JavaDoc defaultProtocol = "jmxmp";
25    private final String JavaDoc ipv6Host = "1080:0:0:0:8:800:200C:417A";
26    private final String JavaDoc longSlashPath = "/jndi/rmi://localhost:1099/jmxconnector";
27    private final String JavaDoc longSemicolonPath = ";jndi/rmi://localhost:1099/jmxconnector";
28    private final String JavaDoc host = "localhost";
29    private final int port = 5400;
30
31    public JMXServiceURLTest(String JavaDoc name)
32    {
33       super(name);
34    }
35
36    public void testValidURLWithIPv6Host()
37    {
38
39       JMXServiceURL JavaDoc serviceURL = null;
40
41       try
42       {
43          serviceURL = new JMXServiceURL JavaDoc("service:jmx:" + protocol + "://[" + ipv6Host + "]" + longSlashPath);
44       }
45       catch(Throwable JavaDoc 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 JavaDoc("service:jmx:" + protocol + "://[" + ipv6Host + "]:" + port + longSlashPath);
57       }
58       catch(Throwable JavaDoc 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 JavaDoc url = "service:jmx:" + protocol + "://[" + ipv6Host + "]:" + port + longSlashPath;
72       JMXServiceURL JavaDoc serviceURL = null;
73
74       try
75       {
76          serviceURL = new JMXServiceURL JavaDoc(url);
77       }
78       catch(Throwable JavaDoc ex)
79       {
80          assertTrue("Got exception " + ex, false);
81       }
82       assertEquals(url, serviceURL.toString());
83    }
84
85    public void testEquals()
86    {
87       String JavaDoc url = "service:jmx:" + protocol + "://[" + ipv6Host + "]:" + port + longSlashPath;
88       JMXServiceURL JavaDoc serviceURL = null;
89       JMXServiceURL JavaDoc serviceURL2 = null;
90
91       try
92       {
93          serviceURL = new JMXServiceURL JavaDoc(url);
94          serviceURL2 = new JMXServiceURL JavaDoc(url);
95       }
96       catch(Throwable JavaDoc ex)
97       {
98          assertTrue("Got exception " + ex, false);
99       }
100       assertEquals(serviceURL, serviceURL2);
101    }
102
103    public void testNoHostOrPort()
104    {
105       JMXServiceURL JavaDoc serviceURL = null;
106       try
107       {
108          serviceURL = new JMXServiceURL JavaDoc("service:jmx:" + protocol + "://" + longSlashPath);
109       }
110       catch(Throwable JavaDoc 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 JavaDoc 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 JavaDoc serviceURL = null;
131       try
132       {
133          serviceURL = new JMXServiceURL JavaDoc("service:jmx:" + protocol + "://" + host + longSlashPath);
134       }
135       catch(Throwable JavaDoc 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 JavaDoc("service:jmx:" + protocol + "://" + host + ":" + port + longSlashPath);
147       }
148       catch(Throwable JavaDoc 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 JavaDoc serviceURL = null;
161
162       try
163       {
164          serviceURL = new JMXServiceURL JavaDoc("service:jmx:" + protocol + "://" + host + longSemicolonPath);
165       }
166       catch(Throwable JavaDoc 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 JavaDoc("service:jmx:" + protocol + "://" + host + ":" + port + longSemicolonPath);
178       }
179       catch(Throwable JavaDoc 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 JavaDoc serviceURL = null;
193
194       try
195       {
196          serviceURL = new JMXServiceURL JavaDoc("service:jmx:://" + host + ":" + port);
197       }
198       catch(Throwable JavaDoc 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 JavaDoc serviceURL = null;
210
211       try
212       {
213          serviceURL = new JMXServiceURL JavaDoc(protocol, "[" + ipv6Host + "]", port);
214       }
215       catch(Throwable JavaDoc 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 JavaDoc serviceURL = new JMXServiceURL JavaDoc(null);
229       }
230       catch(MalformedURLException JavaDoc e)
231       {
232          assertTrue("Got MalformedULRException and expected NullPointerException.", false);
233       }
234       catch(NullPointerException JavaDoc npex)
235       {
236          assertTrue(true);
237          return;
238       }
239       catch(Throwable JavaDoc 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 JavaDoc serviceURL = new JMXServiceURL JavaDoc("rmi://myhost:0");
251       }
252       catch(MalformedURLException JavaDoc e)
253       {
254          assertTrue(true);
255          return;
256       }
257       catch(Throwable JavaDoc 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 JavaDoc serviceURL = new JMXServiceURL JavaDoc("service:jmx:://:1234");
269       }
270       catch(MalformedURLException JavaDoc e)
271       {
272          assertTrue(true);
273          return;
274       }
275       catch(Throwable JavaDoc 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 JavaDoc serviceURL = new JMXServiceURL JavaDoc("service:jmx:myhost:0");
287       }
288       catch(MalformedURLException JavaDoc e)
289       {
290          assertTrue(true);
291          return;
292       }
293       catch(Throwable JavaDoc 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 JavaDoc serviceURL = new JMXServiceURL JavaDoc("service:jmx:rm$i://myhost:0");
305       }
306       catch(MalformedURLException JavaDoc e)
307       {
308          assertTrue(true);
309          return;
310       }
311       catch(Throwable JavaDoc 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