KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > loading > MLetTEST


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.loading;
23
24 import java.net.URL JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.management.Attribute JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.MBeanServerFactory JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.ServiceNotFoundException JavaDoc;
34 import javax.management.loading.MLet JavaDoc;
35
36 import junit.framework.AssertionFailedError;
37 import junit.framework.TestCase;
38
39 /**
40  * MLet tests.
41  *
42  * @author <a HREF="mailto:jplindfo@helsinki.fi">Juha Lindfors</a>
43  */

44 public class MLetTEST extends TestCase
45 {
46    private MBeanServer JavaDoc server;
47
48    private URL JavaDoc location;
49    
50    public MLetTEST(String JavaDoc s) throws Exception JavaDoc
51    {
52       super(s);
53       
54       // Workout the output location for the dynamically loaded files
55
location = getClass().getResource("/org/jboss/test/jmx/compliance/loading/MLetTEST.class");
56       String JavaDoc jarPath = location.getPath();
57       int i = jarPath.indexOf('!');
58       if (i != -1)
59       {
60          jarPath = jarPath.substring(0, i);
61          location = new URL JavaDoc(jarPath);
62          location = new URL JavaDoc(location, "file:../");
63       }
64    }
65
66    protected void setUp() throws Exception JavaDoc
67    {
68       super.setUp();
69       server = MBeanServerFactory.createMBeanServer();
70    }
71
72    public void testCreateAndRegister() throws Exception JavaDoc
73    {
74
75       MLet JavaDoc mlet = new MLet JavaDoc();
76       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
77    
78       try
79       {
80          server.registerMBean(mlet, name);
81       }
82       finally
83       {
84          server.unregisterMBean(name);
85       }
86    }
87    
88    public void testMLetLoadClassFromURLInConstructor() throws Exception JavaDoc
89    {
90       // NOTE:
91
// the urls used here are relative to the location of the build.xml
92

93       final URL JavaDoc MBEANS_URL = new URL JavaDoc(location, "lib/jmxcompliance-MyMBeans.jar");
94       
95       // make sure the class is not available
96
try
97       {
98          server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial");
99          
100          fail("class org.jboss.test.jmx.compliance.loading.support.Trivial was already found in CL repository.");
101       }
102       catch (ClassNotFoundException JavaDoc e)
103       {
104          // expected
105
}
106
107       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
108       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
109       MLet JavaDoc mlet = new MLet JavaDoc(new URL JavaDoc[] { MBEANS_URL });
110       
111       // make sure the class is not available
112
try
113       {
114          server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial");
115          
116          fail("class org.jboss.test.jmx.compliance.loading.support.Trivial found in CL repository after MLet construction.");
117       }
118       catch (ClassNotFoundException JavaDoc e)
119       {
120          // expected
121
}
122
123       try
124       {
125          server.registerMBean(mlet, name);
126          server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial");
127       }
128       finally
129       {
130          server.unregisterMBean(name);
131       }
132       // make sure the class is not available
133
try
134       {
135          server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial");
136          
137          fail("class org.jboss.test.jmx.compliance.loading.support.Trivial was still found in CL repository.");
138       }
139       catch (ClassNotFoundException JavaDoc e)
140       {
141          // expected
142
}
143    }
144    
145    public void testBasicMLetFileLoad() throws Exception JavaDoc
146    {
147       // NOTE:
148
// the urls used here are relative to the location of the build.xml
149

150       final URL JavaDoc MLET_URL = new URL JavaDoc(location, "etc/tests/BasicConfig.mlet");
151       
152       // make sure the classes are loaded from mlet, not system cl
153
try
154       {
155          server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial");
156          
157          fail("class org.jboss.test.jmx.compliance.loading.support.Trivial was already found in CL repository.");
158       }
159       catch (ClassNotFoundException JavaDoc e)
160       {
161          // expected
162
}
163       // make sure the classes are loaded from mlet, not system cl
164
try
165       {
166          server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial2");
167          
168          fail("class org.jboss.test.jmx.compliance.loading.support.Trivial2 was already found in CL repository.");
169       }
170       catch (ClassNotFoundException JavaDoc e)
171       {
172          // expected
173
}
174      
175       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
176       MLet JavaDoc mlet = new MLet JavaDoc();
177       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
178       server.registerMBean(mlet, name);
179       
180       server.invoke(name, "getMBeansFromURL",
181                                       new Object JavaDoc[] { MLET_URL.toString() },
182                                       new String JavaDoc[] { String JavaDoc.class.getName() }
183       );
184
185       try
186       {
187          assertTrue(server.isRegistered(new ObjectName JavaDoc("test:name=Trivial")));
188          assertTrue(server.isRegistered(new ObjectName JavaDoc("test:name=Trivial2")));
189       }
190       catch (AssertionFailedError e)
191       {
192          URL JavaDoc[] urls = mlet.getURLs();
193          URL JavaDoc url = null;
194          
195          if (urls != null && urls.length > 0)
196             url = urls[0];
197          
198          fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" +
199               MLET_URL + "' resulting into MLET codebase URL '" + url + "' and therefore fails " +
200               "to load the required classes from the Java archive (MyMBeans.jar)");
201       }
202
203       assertTrue(server.getMBeanInfo(new ObjectName JavaDoc("test:name=Trivial")) != null);
204       assertTrue(server.getMBeanInfo(new ObjectName JavaDoc("test:name=Trivial2")) != null);
205       
206       assertTrue(server.getAttribute(new ObjectName JavaDoc("test:name=Trivial2"), "Something").equals("foo"));
207
208       server.invoke(new ObjectName JavaDoc("test:name=Trivial"), "doOperation",
209       new Object JavaDoc[] { "Test" },
210       new String JavaDoc[] { String JavaDoc.class.getName() }
211       );
212       
213       server.invoke(new ObjectName JavaDoc("test:name=Trivial2"), "doOperation",
214       new Object JavaDoc[] { "Test" },
215       new String JavaDoc[] { String JavaDoc.class.getName() }
216       );
217    }
218
219    /**
220     * Make sure the versioning MLet installer won't replace MBeans that were
221     * not registered with version information.
222     */

223    public void testConflictingMLetFileLoad() throws Exception JavaDoc
224    {
225       // NOTE:
226
// the urls used here are relative to the location of the build.xml
227

228       final URL JavaDoc MLET_URL1 = new URL JavaDoc(location, "etc/tests/BasicConfig2.mlet");
229       final URL JavaDoc MLET_URL2 = new URL JavaDoc(location, "etc/tests/BasicConfig2.mlet");
230       
231       // make sure the classes are loaded from mlet, not system cl
232
try
233       {
234          server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial3");
235          
236          fail("class org.jboss.test.jmx.compliance.loading.support.Trivial was already found in CL repository.");
237       }
238       catch (ClassNotFoundException JavaDoc e)
239       {
240          // expected
241
}
242       // make sure the classes are loaded from mlet, not system cl
243
try
244       {
245          server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial4");
246          
247          fail("class org.jboss.test.jmx.compliance.loading.support.Trivial2 was already found in CL repository.");
248       }
249       catch (ClassNotFoundException JavaDoc e)
250       {
251          // expected
252
}
253      
254       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
255       MLet JavaDoc mlet = new MLet JavaDoc();
256       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
257       server.registerMBean(mlet, name);
258       
259       Set JavaDoc result = (Set JavaDoc) server.invoke(name, "getMBeansFromURL",
260                                       new Object JavaDoc[] { MLET_URL1.toString() },
261                                       new String JavaDoc[] { String JavaDoc.class.getName() }
262       );
263       checkResult(result);
264
265       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:name=Trivial2");
266       server.setAttribute(oname, new Attribute JavaDoc("Something", "Something"));
267       
268       mlet = new MLet JavaDoc();
269       name = new ObjectName JavaDoc("test:name=mlet2");
270       server.registerMBean(mlet, name);
271       
272       server.invoke(name, "getMBeansFromURL",
273                                new Object JavaDoc[] { MLET_URL2.toString() },
274                                new String JavaDoc[] { String JavaDoc.class.getName() }
275       );
276       
277       oname = new ObjectName JavaDoc("test:name=Trivial2");
278       String JavaDoc value = (String JavaDoc)server.getAttribute(oname, "Something");
279
280       assertTrue(value.equals("Something"));
281    }
282
283       
284    public void testMalformedURLLoad() throws Exception JavaDoc
285    {
286       // NOTE:
287
// the urls used here are relative to the location of the build.xml
288

289       final URL JavaDoc MLET_URL = new URL JavaDoc(location, "etc/tests/BasicConfig.mlet");
290       
291       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
292       MLet JavaDoc mlet = new MLet JavaDoc();
293       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
294       try
295       {
296          server.registerMBean(mlet, name);
297          
298          server.invoke(name, "getMBeansFromURL",
299          new Object JavaDoc[] { MLET_URL.getPath() },
300          new String JavaDoc[] { String JavaDoc.class.getName() }
301          );
302          
303          // should not reach here
304
fail("FAILS IN RI: Malformed URL in getMBeansURL() should result in ServiceNotFoundException thrown.");
305       }
306       catch (AssertionFailedError e)
307       {
308          // defensive: in case assertXXX() or fail() are later added
309
throw e;
310       }
311       catch (MBeanException JavaDoc e)
312       {
313          assertTrue(e.getTargetException() instanceof ServiceNotFoundException JavaDoc);
314       }
315       finally
316       {
317          try
318          {
319             server.unregisterMBean(name);
320          }
321          catch (Exception JavaDoc ignored) {}
322       }
323    }
324    
325    public void testMissingMLetTagInLoad() throws Exception JavaDoc
326    {
327       // NOTE:
328
// the urls used here are relative to the location of the build.xml
329

330       final URL JavaDoc MLET_URL = new URL JavaDoc(location, "etc/tests/MissingMLET.mlet");
331
332       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
333       MLet JavaDoc mlet = new MLet JavaDoc();
334       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
335       try
336       {
337          server.registerMBean(mlet, name);
338          
339          server.invoke(name, "getMBeansFromURL",
340          new Object JavaDoc[] { MLET_URL.toString() },
341          new String JavaDoc[] { String JavaDoc.class.getName() }
342          );
343          
344          // should not reach here
345
fail("MLet text file missing the MLET tag should result in ServiceNotFoundException thrown.");
346       }
347       catch (AssertionFailedError e)
348       {
349          // defensive: in case assertXXX() or fail() are added later
350
throw e;
351       }
352       catch (MBeanException JavaDoc e)
353       {
354          assertTrue(e.getTargetException() instanceof ServiceNotFoundException JavaDoc);
355       }
356       finally
357       {
358          try
359          {
360             server.unregisterMBean(name);
361          }
362          catch (Exception JavaDoc ignored) {}
363       }
364    }
365
366    public void testMissingMandatoryArchiveTagInLoad() throws Exception JavaDoc
367    {
368       // NOTE:
369
// the urls used here are relative to the location of the build.xml
370

371       final URL JavaDoc MLET_URL = new URL JavaDoc(location, "etc/tests/MissingMandatoryArchive.mlet");
372
373       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
374       MLet JavaDoc mlet = new MLet JavaDoc();
375       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
376       try
377       {
378          server.registerMBean(mlet, name);
379          
380          server.invoke(name, "getMBeansFromURL",
381          new Object JavaDoc[] { MLET_URL.toString() },
382          new String JavaDoc[] { String JavaDoc.class.getName() }
383          );
384          
385          // should not reach here
386
fail("MLet text file missing mandatory ARCHIVE attribute should result in ServiceNotFoundException thrown.");
387       }
388       catch (AssertionFailedError e)
389       {
390          // defensive: in case assertXXX() or fail() are added later
391
throw e;
392       }
393       catch (MBeanException JavaDoc e)
394       {
395          assertTrue(e.getTargetException() instanceof ServiceNotFoundException JavaDoc);
396       }
397       finally
398       {
399          try
400          {
401             server.unregisterMBean(name);
402          }
403          catch (Exception JavaDoc ignored) {}
404       }
405    }
406    
407    public void testMissingMandatoryCodeTagInLoad() throws Exception JavaDoc
408    {
409       // NOTE:
410
// the urls used here are relative to the location of the build.xml
411

412       final URL JavaDoc MLET_URL = new URL JavaDoc(location, "etc/tests/MissingMandatoryCode.mlet");
413       
414       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
415       MLet JavaDoc mlet = new MLet JavaDoc();
416       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
417       try
418       {
419          server.registerMBean(mlet, name);
420          
421          server.invoke(name, "getMBeansFromURL",
422          new Object JavaDoc[] { MLET_URL.toString() },
423          new String JavaDoc[] { String JavaDoc.class.getName() }
424          );
425          
426          // should not reach here
427
fail("MLet text file missing mandatory CODE attribute should result in ServiceNotFoundException thrown.");
428       }
429       catch (AssertionFailedError e)
430       {
431          // defensive: in case assertXXX() or fail() are added later
432
throw e;
433       }
434       catch (MBeanException JavaDoc e)
435       {
436          assertTrue(e.getTargetException() instanceof ServiceNotFoundException JavaDoc);
437       }
438       finally
439       {
440          try
441          {
442             server.unregisterMBean(name);
443          }
444          catch (Exception JavaDoc ignored) {}
445       }
446    }
447    
448    public void testArchiveListInMLet() throws Exception JavaDoc
449    {
450       // NOTE:
451
// the urls used here are relative to the location of the build.xml
452

453       final URL JavaDoc MLET_URL = new URL JavaDoc(location, "etc/tests/ArchiveList.mlet");
454    
455       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
456       MLet JavaDoc mlet = new MLet JavaDoc();
457       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
458
459       try
460       {
461          server.registerMBean(mlet, name);
462       
463          server.invoke(name, "getMBeansFromURL",
464          new Object JavaDoc[] { MLET_URL.toString() },
465          new String JavaDoc[] { String JavaDoc.class.getName() }
466          );
467       
468          Class JavaDoc c = null;
469       
470          try
471          {
472             c = mlet.loadClass("org.jboss.test.jmx.compliance.loading.support.AClass");
473          }
474          catch (ClassNotFoundException JavaDoc e)
475          {
476             URL JavaDoc[] urls = mlet.getURLs();
477             fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" +
478                  MLET_URL + "' resulting into MLET codebase URL '" + urls[0] + "' and therefore fails " +
479                  "to load the required classes from the Java archive.");
480          }
481       
482          Object JavaDoc o = c.newInstance();
483       
484          server.setAttribute(new ObjectName JavaDoc("test:name=AnotherTrivial"), new Attribute JavaDoc("Something", o));
485          o = server.getAttribute(new ObjectName JavaDoc("test:name=AnotherTrivial"), "Something");
486       
487          assertTrue(o.getClass().isAssignableFrom(c));
488       }
489       finally
490       {
491          try
492          {
493             server.unregisterMBean(name);
494          }
495          catch (Exception JavaDoc ignored) {}
496       }
497    }
498    
499    public void testUnexpectedEndOfFile() throws Exception JavaDoc
500    {
501       // NOTE:
502
// the urls used here are relative to the location of the build.xml
503

504       final URL JavaDoc MLET_URL = new URL JavaDoc(location, "etc/tests/UnexpectedEnd.mlet");
505
506       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
507       MLet JavaDoc mlet = new MLet JavaDoc();
508       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
509       try
510       {
511          server.registerMBean(mlet, name);
512          
513          server.invoke(name, "getMBeansFromURL",
514          new Object JavaDoc[] { MLET_URL.toString() },
515          new String JavaDoc[] { String JavaDoc.class.getName() }
516          );
517          
518          // should not reach here
519
fail("Unexpected end of file from mlet text file did not cause an exception.");
520       }
521       catch (AssertionFailedError e)
522       {
523          throw e;
524       }
525       catch (MBeanException JavaDoc e)
526       {
527          assertTrue(e.getTargetException() instanceof ServiceNotFoundException JavaDoc);
528       }
529       finally
530       {
531          try
532          {
533             server.unregisterMBean(name);
534          }
535          catch (Exception JavaDoc ignored) {}
536       }
537    }
538    
539    public void testMissingEndMLetTag() throws Exception JavaDoc
540    {
541       // NOTE:
542
// the urls used here are relative to the location of the build.xml
543

544       final URL JavaDoc MLET_URL = new URL JavaDoc(location, "etc/tests/MissingEndTag.mlet");
545
546       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
547       MLet JavaDoc mlet = new MLet JavaDoc();
548       ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=mlet");
549       try
550       {
551          server.registerMBean(mlet, name);
552          
553          server.invoke(name, "getMBeansFromURL",
554          new Object JavaDoc[] { MLET_URL.toString() },
555          new String JavaDoc[] { String JavaDoc.class.getName() }
556          );
557       
558          assertTrue(!server.isRegistered(new ObjectName JavaDoc("test:name=Trivial")));
559       }
560       catch (AssertionFailedError e)
561       {
562          throw e;
563       }
564       catch (MBeanException JavaDoc e)
565       {
566          assertTrue(e.getTargetException() instanceof ServiceNotFoundException JavaDoc);
567       }
568       finally
569       {
570          try
571          {
572             server.unregisterMBean(name);
573          }
574          catch (Exception JavaDoc ignored) {}
575       }
576    }
577    
578    
579    protected void checkResult(Set JavaDoc result)
580    {
581       for (Iterator JavaDoc i = result.iterator(); i.hasNext();)
582       {
583          Object JavaDoc mbean = i.next();
584          if (mbean instanceof Throwable JavaDoc)
585             throw new RuntimeException JavaDoc((Throwable JavaDoc) mbean);
586       }
587    }
588 }
589
Popular Tags