KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > loading > MLetTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.loading;
10
11 import java.io.File JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Random JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import javax.management.MBeanRegistration JavaDoc;
20 import javax.management.MBeanServer JavaDoc;
21 import javax.management.ObjectInstance JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23 import javax.management.ReflectionException JavaDoc;
24 import javax.management.loading.MLet JavaDoc;
25 import javax.management.loading.PrivateMLet JavaDoc;
26
27 import mx4j.MX4JSystemKeys;
28 import mx4j.loading.MLetParseException;
29 import mx4j.loading.MLetParser;
30 import mx4j.loading.MLetTag;
31 import mx4j.server.DefaultClassLoaderRepository;
32 import test.MX4JTestCase;
33
34 /**
35  * @version $Revision: 1.19 $
36  */

37 public class MLetTest extends MX4JTestCase
38 {
39    public MLetTest(String JavaDoc s)
40    {
41       super(s);
42    }
43
44    public void testInvalidMLetFileParsing() throws Exception JavaDoc
45    {
46       MLetParser parser = new MLetParser();
47
48       String JavaDoc content = null;
49       try
50       {
51          parser.parse(content);
52          fail();
53       }
54       catch (MLetParseException x)
55       {
56       }
57
58       content = "";
59       try
60       {
61          parser.parse(content);
62          fail();
63       }
64       catch (MLetParseException x)
65       {
66       }
67
68       content = " ";
69       try
70       {
71          parser.parse(content);
72          fail();
73       }
74       catch (MLetParseException x)
75       {
76       }
77
78       content = " <!--";
79       try
80       {
81          parser.parse(content);
82          fail();
83       }
84       catch (MLetParseException x)
85       {
86       }
87
88       content = " -->";
89       try
90       {
91          parser.parse(content);
92          fail();
93       }
94       catch (MLetParseException x)
95       {
96       }
97
98       content = "<!-- -->\n<!-- ->";
99       try
100       {
101          parser.parse(content);
102          fail();
103       }
104       catch (MLetParseException x)
105       {
106       }
107
108       content = "<!- -->";
109       try
110       {
111          parser.parse(content);
112          fail();
113       }
114       catch (MLetParseException x)
115       {
116       }
117
118       // Invalid, missing attributes
119
content = "<!-- -->\n<MLET/>";
120       try
121       {
122          parser.parse(content);
123          fail();
124       }
125       catch (MLetParseException x)
126       {
127       }
128
129       // Invalid, missing archive attribute
130
content = "<MLET CODE = \" test.mx4j.MBeanNormal\"/>";
131       try
132       {
133          parser.parse(content);
134          fail();
135       }
136       catch (MLetParseException x)
137       {
138       }
139
140       // Invalid, missing archive attribute
141
content = "\n<MLET CODE=\" test.mx4j.MBeanNormal\">\n</MLET>";
142       try
143       {
144          parser.parse(content);
145          fail();
146       }
147       catch (MLetParseException x)
148       {
149       }
150
151       // Invalid, missing archive attribute
152
content = "\n<MLET OBJECT=\"mx4j-mbean.ser\">\n</MLET>";
153       try
154       {
155          parser.parse(content);
156          fail();
157       }
158       catch (MLetParseException x)
159       {
160       }
161
162       // Invalid, missing code or object attribute
163
content = "<MLET ARCHIVE = \"..\\lib\"></MLET>";
164       try
165       {
166          parser.parse(content);
167          fail();
168       }
169       catch (MLetParseException x)
170       {
171       }
172
173       // Invalid, either code or object attribute must be present
174
content = "<MLET CODE=\"test.mx4j.MBeanNormal\" OBJECT=\"mx4j-mbean.ser\" ARCHIVE = \"..\\lib\"></MLET>";
175       try
176       {
177          parser.parse(content);
178          fail();
179       }
180       catch (MLetParseException x)
181       {
182       }
183
184       // Invalid arg tag
185
// content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\">\n<></MLET>";
186
// try {parser.parse(content); fail();}
187
// catch (MLetParseException x) {}
188

189       // Invalid arg tag
190
content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\">\n<ARG></MLET>";
191       try
192       {
193          parser.parse(content);
194          fail();
195       }
196       catch (MLetParseException x)
197       {
198       }
199
200       // Invalid arg tag
201
content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\">\n<ARG type=\"int\"></MLET>";
202       try
203       {
204          parser.parse(content);
205          fail();
206       }
207       catch (MLetParseException x)
208       {
209       }
210
211       // Invalid arg tag
212
content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\">\n<ARG value=\"int\"></MLET>";
213       try
214       {
215          parser.parse(content);
216          fail();
217       }
218       catch (MLetParseException x)
219       {
220       }
221
222       // Invalid name tag
223
content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\" name>\n</MLET>";
224       try
225       {
226          parser.parse(content);
227          fail();
228       }
229       catch (MLetParseException x)
230       {
231       }
232
233       // Invalid name tag
234
content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\" name=>\n</MLET>";
235       try
236       {
237          parser.parse(content);
238          fail();
239       }
240       catch (MLetParseException x)
241       {
242       }
243
244       // Invalid version tag
245
content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\" version>\n</MLET>";
246       try
247       {
248          parser.parse(content);
249          fail();
250       }
251       catch (MLetParseException x)
252       {
253       }
254
255       // Invalid version tag
256
content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\" version=>\n</MLET>";
257       try
258       {
259          parser.parse(content);
260          fail();
261       }
262       catch (MLetParseException x)
263       {
264       }
265
266    }
267
268    public void testValidMLetFileParsing() throws Exception JavaDoc
269    {
270       MLetParser parser = new MLetParser();
271       String JavaDoc content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE=\"mx4j-tests.jar\" NAME=\":name=MLetTest1\"/>";
272       parser.parse(content);
273
274       content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE=\"mx4j-tests.jar\" NAME=\":name=MLetTest2\">\n\t<ARG TYPE=\"int\" VALUE=\"5\">\n</MLET>\n";
275       parser.parse(content);
276
277       content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE=\"mx4j-tests.jar\" CODEBASE=\"dist\\test\\\" NAME=\":name=MLetTest3\">\n\t<ARG TYPE=\"int\" VALUE=\"5\"/>\n</MLET>\n";
278       parser.parse(content);
279    }
280
281    public void testCompleteMLetFileParsing() throws Exception JavaDoc
282    {
283       MLetParser parser = new MLetParser();
284
285       StringBuffer JavaDoc content = new StringBuffer JavaDoc();
286       content.append("<!-- Comment -->");
287       content.append("<MLET \n");
288       content.append(" Code=\"mx4j.tools.naming.NamingService\"\n");
289       content.append(" archive = \" ../lib \"\n");
290       content.append(" CodeBase= \"http://localhost:8080/download\"\n");
291       content.append(" NAME=\":name=test\"\n");
292       content.append(" Version=\"1\">\n");
293       content.append(" <!-- Comment -->");
294       content.append(" <ARG \n");
295       content.append(" Type=\"boolean\"\n");
296       content.append(" VALUE = \"true\">\n");
297       content.append(" <!-- Comment -->");
298       content.append(" <ARG \n");
299       content.append(" TYPE = \"boolean\"\n");
300       content.append(" value=\"true\"/>\n");
301       content.append("</MLet>");
302
303       parser.parse(content.toString());
304    }
305
306    public void testCodebaseForGetMBeansFromURL() throws Exception JavaDoc
307    {
308       Class JavaDoc cls = Simple.class;
309       String JavaDoc className = cls.getName();
310       URL JavaDoc url = cls.getProtectionDomain().getCodeSource().getLocation();
311       String JavaDoc urlString = url.toExternalForm();
312       int index = urlString.lastIndexOf('/') + 1;
313       String JavaDoc jar = urlString.substring(index);
314
315       String JavaDoc codebase = ".";
316       String JavaDoc content = "<MLET CODE=\"" + className + "\" NAME=\":name=test\" ARCHIVE=\"" + jar + "\" CODEBASE=\"" + codebase + "\"/>";
317       MLetParser parser = new MLetParser();
318       List JavaDoc tags = parser.parse(content);
319       MLetTag tag = (MLetTag)tags.get(0);
320       URL JavaDoc mletFileURL = new URL JavaDoc("http://mx4j.sourceforge.net/mlets/mbeans.mlet");
321       URL JavaDoc codebaseURL = tag.normalizeCodeBase(mletFileURL);
322       assertEquals(codebaseURL.toExternalForm(), "http://mx4j.sourceforge.net/mlets/");
323
324       content = "<MLET CODE=\"" + className + "\" NAME=\":name=test\" ARCHIVE=\"" + jar + "\"/>";
325       tags = parser.parse(content);
326       tag = (MLetTag)tags.get(0);
327       codebaseURL = tag.normalizeCodeBase(mletFileURL);
328       assertEquals(codebaseURL.toExternalForm(), "http://mx4j.sourceforge.net/mlets/");
329
330       codebase = "../lib";
331       content = "<MLET CODE=\"" + className + "\" NAME=\":name=test\" ARCHIVE=\"" + jar + "\" CODEBASE=\"" + codebase + "\"/>";
332       tags = parser.parse(content);
333       tag = (MLetTag)tags.get(0);
334       codebaseURL = tag.normalizeCodeBase(mletFileURL);
335       assertEquals(codebaseURL.toExternalForm(), "http://mx4j.sourceforge.net/lib/");
336
337       codebase = "ftp://mx4j.sourceforge.net/mbeans";
338       content = "<MLET CODE=\"" + className + "\" NAME=\":name=test\" ARCHIVE=\"" + jar + "\" CODEBASE=\"" + codebase + "\"/>";
339       tags = parser.parse(content);
340       tag = (MLetTag)tags.get(0);
341       codebaseURL = tag.normalizeCodeBase(mletFileURL);
342       assertEquals(codebaseURL.toExternalForm(), codebase + "/");
343    }
344
345    public void testGetMBeansFromURL() throws Exception JavaDoc
346    {
347       Class JavaDoc cls = Simple.class;
348       String JavaDoc className = cls.getName();
349       URL JavaDoc url = cls.getProtectionDomain().getCodeSource().getLocation();
350       String JavaDoc urlString = url.toExternalForm();
351       int index = urlString.lastIndexOf('/') + 1;
352       String JavaDoc jar = urlString.substring(index);
353       String JavaDoc codebase = urlString.substring(0, index);
354
355       // Write an MLet file
356
File JavaDoc mletFile = File.createTempFile("mlet", null);
357       mletFile.deleteOnExit();
358       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(mletFile);
359       String JavaDoc content = "<MLET CODE=\"" + className + "\" NAME=\":name=test\" ARCHIVE=\"" + jar + "\" CODEBASE=\"" + codebase + "\"/>";
360       fos.write(content.getBytes());
361       fos.close();
362
363       System.setProperty(MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY, CLRWithOnlyMLets.class.getName());
364       MBeanServer JavaDoc server = newMBeanServer();
365
366       ObjectName JavaDoc mletName = new ObjectName JavaDoc(":loader=mlet1");
367
368       MLet JavaDoc mlet = new MLet JavaDoc();
369       server.registerMBean(mlet, mletName);
370
371       Set JavaDoc mbeans = mlet.getMBeansFromURL(mletFile.toURL());
372       if (mbeans.size() != 1) fail("Loaded wrong number of MBeans");
373       ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc)mbeans.iterator().next();
374       if (!instance.getClassName().equals(className)) fail("Loaded a different MBean");
375    }
376
377    public void testGetMBeansFromURLWithNoName() throws Exception JavaDoc
378    {
379       Class JavaDoc cls = SimpleRegistration.class;
380       String JavaDoc className = cls.getName();
381       URL JavaDoc url = cls.getProtectionDomain().getCodeSource().getLocation();
382       String JavaDoc urlString = url.toExternalForm();
383       int index = urlString.lastIndexOf('/') + 1;
384       String JavaDoc jar = urlString.substring(index);
385       String JavaDoc codebase = urlString.substring(0, index);
386
387       // Write an MLet file
388
File JavaDoc mletFile = File.createTempFile("mletnoname", null);
389       mletFile.deleteOnExit();
390       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(mletFile);
391       String JavaDoc content = "<MLET CODE=\"" + className + "\" ARCHIVE=\"" + jar + "\" CODEBASE=\"" + codebase + "\"/>";
392       fos.write(content.getBytes());
393       fos.close();
394
395       System.setProperty(MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY, CLRWithOnlyMLets.class.getName());
396       MBeanServer JavaDoc server = newMBeanServer();
397
398       ObjectName JavaDoc mletName = new ObjectName JavaDoc(":loader=mlet1");
399       MLet JavaDoc mlet = new MLet JavaDoc();
400       server.registerMBean(mlet, mletName);
401
402       Set JavaDoc mbeans = mlet.getMBeansFromURL(mletFile.toURL());
403       if (mbeans.size() != 1) fail("Loaded wrong number of MBeans");
404       ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc)mbeans.iterator().next();
405       if (!instance.getClassName().equals(className)) fail("Loaded a different MBean");
406    }
407
408    public void testMLetDelegatesToCLR() throws Exception JavaDoc
409    {
410       mletDelegationToCLR(true);
411    }
412
413    public void testMLetDoesNotDelegateToCLR() throws Exception JavaDoc
414    {
415       try
416       {
417          mletDelegationToCLR(false);
418          fail("MLet does not delegate, cannot load the class");
419       }
420       catch (ReflectionException JavaDoc ignored)
421       {
422       }
423    }
424
425    public void mletDelegationToCLR(boolean delegates) throws Exception JavaDoc
426    {
427       System.setProperty(MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY, CLRWithOnlyMLets.class.getName());
428       MBeanServer JavaDoc server = newMBeanServer();
429
430       ObjectName JavaDoc loaderName = new ObjectName JavaDoc("Loader", "id", "0");
431       ObjectName JavaDoc mletName = new ObjectName JavaDoc("Loader", "id", "1");
432       ObjectName JavaDoc mbeanName = new ObjectName JavaDoc("MBean", "id", "0");
433
434       Class JavaDoc cls = Simple.class;
435       URL JavaDoc url = cls.getProtectionDomain().getCodeSource().getLocation();
436       String JavaDoc className = cls.getName();
437
438       MLet JavaDoc loader = new MLet JavaDoc(new URL JavaDoc[]{url}, ClassLoader.getSystemClassLoader().getParent());
439       server.registerMBean(loader, loaderName);
440
441       MLet JavaDoc mlet = new MLet JavaDoc(new URL JavaDoc[0], ClassLoader.getSystemClassLoader().getParent(), delegates);
442
443       // Be sure the MLet cannot load the class
444
try
445       {
446          mlet.loadClass(className);
447          fail("MLet should not be able to load the class");
448       }
449       catch (ClassNotFoundException JavaDoc ignored)
450       {
451       }
452
453       server.registerMBean(mlet, mletName);
454
455       // Try to create the MBean
456
server.createMBean(className, mbeanName, mletName);
457    }
458
459    public void testSingleMLetLoadClass() throws Exception JavaDoc
460    {
461       System.setProperty(MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY, CLRWithOnlyMLets.class.getName());
462       MBeanServer JavaDoc server = newMBeanServer();
463
464       ObjectName JavaDoc loaderName = new ObjectName JavaDoc("Loader", "id", "0");
465       ObjectName JavaDoc mbeanName = new ObjectName JavaDoc("MBean", "id", "0");
466
467       Class JavaDoc cls = Simple.class;
468       URL JavaDoc url = cls.getProtectionDomain().getCodeSource().getLocation();
469       String JavaDoc className = cls.getName();
470
471       MLet JavaDoc mlet = new MLet JavaDoc(new URL JavaDoc[]{url}, ClassLoader.getSystemClassLoader().getParent());
472       server.registerMBean(mlet, loaderName);
473
474       server.createMBean(className, mbeanName, loaderName);
475    }
476
477    public void testManyMLetLoadClass() throws Exception JavaDoc
478    {
479       int loaderCount = 100;
480       int mbeanCount = 200;
481
482       long elapsed = manyMLetLoadClass(loaderCount, mbeanCount, Simple.class.getName(), false);
483
484       System.out.println("Loading " + mbeanCount + " MBeans with " + loaderCount + " MLets took " + elapsed + " ms, average is " + (elapsed / mbeanCount));
485 // Assume registering a valid MBean will take no more than 500 ms
486
if (elapsed > mbeanCount * 500) fail("Test took too much time, probably a problem in MLet loading");
487    }
488
489    public void testMultiMLetLoadNonExistingClass() throws Exception JavaDoc
490    {
491       int loaderCount = 100;
492       int mbeanCount = 200;
493
494       long elapsed = manyMLetLoadClass(loaderCount, mbeanCount, "dummy", true);
495
496       System.out.println("Loading " + mbeanCount + " non-existing MBeans with " + loaderCount + " MLets took " + elapsed + " ms, average is " + (elapsed / mbeanCount));
497 // We're looking for a non-existing class, so we have to ask to all classloaders in the CLR before the chosen MLet
498
// The time we spend looking for the class is roughly linear with the number of classloaders in the queried,
499
// that in average is loaderCount / 2 and at most loaderCount:
500
// elapsed = mbeanCount * loaderCount * k
501
// Assume that k > 5 ms is too bad performance
502
if (elapsed > mbeanCount * loaderCount * 5) fail("Test took too much time, probably a problem in MLet loading");
503    }
504
505    private long manyMLetLoadClass(int loaderCount, int mbeanCount, String JavaDoc className, boolean ignoreExceptionOnCreation) throws Exception JavaDoc
506    {
507       ObjectName JavaDoc[] loaders = new ObjectName JavaDoc[loaderCount];
508       ObjectName JavaDoc[] mbeans = new ObjectName JavaDoc[mbeanCount];
509
510       MBeanServer JavaDoc server = newMBeanServer();
511
512       URL JavaDoc url = getClass().getProtectionDomain().getCodeSource().getLocation();
513
514       // Register some MLet
515
for (int i = 0; i < loaderCount; ++i)
516       {
517          loaders[i] = new ObjectName JavaDoc("Loader", "id", String.valueOf(i));
518          MLet JavaDoc mlet = new MLet JavaDoc(new URL JavaDoc[]{url}, ClassLoader.getSystemClassLoader().getParent());
519          server.registerMBean(mlet, loaders[i]);
520       }
521
522       long start = System.currentTimeMillis();
523
524       Random JavaDoc random = new Random JavaDoc(start);
525       for (int i = 0; i < mbeanCount; ++i)
526       {
527          mbeans[i] = new ObjectName JavaDoc("MBean", "id", String.valueOf(i));
528
529          // Choose an MLet to load the MBean
530
int id = random.nextInt(loaderCount);
531          ObjectName JavaDoc loader = loaders[id];
532
533          if (ignoreExceptionOnCreation)
534          {
535             try
536             {
537                server.createMBean(className, mbeans[i], loader);
538             }
539             catch (ReflectionException JavaDoc ignored)
540             {
541             }
542          }
543          else
544          {
545             server.createMBean(className, mbeans[i], loader);
546          }
547       }
548
549       long end = System.currentTimeMillis();
550
551       return end - start;
552    }
553
554    public void testChildMLetRegisteredBeforeParentMLet() throws Exception JavaDoc
555    {
556       // A test to be sure the MLet implementation does not recurse infinitely when loading classes.
557

558       URL JavaDoc url = getClass().getProtectionDomain().getCodeSource().getLocation();
559       MLet JavaDoc parent = new MLet JavaDoc(new URL JavaDoc[]{url}, ClassLoader.getSystemClassLoader().getParent());
560       MLet JavaDoc child = new MLet JavaDoc(new URL JavaDoc[0], parent);
561
562       ObjectName JavaDoc parentName = new ObjectName JavaDoc("MLet", "type", "parent");
563       ObjectName JavaDoc childName = new ObjectName JavaDoc("MLet", "type", "child");
564
565       MBeanServer JavaDoc server = newMBeanServer();
566
567       // First register the child, then the parent.
568
server.registerMBean(child, childName);
569       server.registerMBean(parent, parentName);
570
571       // Now in the CLR there are: SystemCL, childMLet, parentMLet.
572
// If I ask someting to parentMLet that it cannot find, it will delegate to the CLR,
573
// and when it comes to the childMLet, the delegation mechanism will ask again to the parentMLet.
574
// If we're not smart in the MLet implementation this ends up in an infinite loop.
575
try
576       {
577          parent.loadClass("dummy");
578          fail("Class does not exist");
579       }
580       catch (ClassNotFoundException JavaDoc ignored)
581       {
582       }
583    }
584
585    public void testPrivateMLetNotAddedToCLR() throws Exception JavaDoc
586    {
587       Class JavaDoc cls = Simple.class;
588       URL JavaDoc url = cls.getProtectionDomain().getCodeSource().getLocation();
589       PrivateMLet JavaDoc mlet = new PrivateMLet JavaDoc(new URL JavaDoc[]{url}, ClassLoader.getSystemClassLoader().getParent(), true);
590
591       ObjectName JavaDoc mletName = new ObjectName JavaDoc(":MLet=Private");
592       ObjectName JavaDoc mbeanName = new ObjectName JavaDoc(":MBean=Simple");
593
594       System.setProperty(MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY, CLRWithOnlyMLets.class.getName());
595       MBeanServer JavaDoc server = newMBeanServer();
596
597       // The private MLet should not be registered in the CLR
598
server.registerMBean(mlet, mletName);
599
600       try
601       {
602          // Ask the CLR to load the class
603
server.createMBean(cls.getName(), mbeanName);
604          fail("Class cannot be found by the CLR");
605       }
606       catch (ReflectionException JavaDoc ignored)
607       {
608       }
609    }
610
611    public void testFindLibrary() throws Exception JavaDoc
612    {
613       MLet JavaDoc mlet = new MLet JavaDoc();
614       Method JavaDoc method = mlet.getClass().getDeclaredMethod("findLibrary", new Class JavaDoc[]{String JavaDoc.class});
615       method.setAccessible(true);
616       String JavaDoc result = (String JavaDoc)method.invoke(mlet, new Object JavaDoc[]{"stat"});
617       if (result != null) fail("MLet can load non-existing libraries");
618    }
619
620    public void testFindLibraryWithLibraryDirectoryInClassPath() throws Exception JavaDoc
621    {
622       // Prepare the library
623
String JavaDoc library = "mlet";
624       String JavaDoc libraryName = System.mapLibraryName(library);
625       File JavaDoc libraryFile = new File JavaDoc(libraryName);
626       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(libraryFile);
627       fos.write("library".getBytes());
628       fos.close();
629
630       assertTrue(libraryFile.exists());
631       assertTrue(libraryFile.length() > 0);
632
633       MLet JavaDoc mlet = new MLet JavaDoc(new URL JavaDoc[]{libraryFile.getCanonicalFile().getParentFile().toURL()});
634       Method JavaDoc method = mlet.getClass().getDeclaredMethod("findLibrary", new Class JavaDoc[]{String JavaDoc.class});
635       method.setAccessible(true);
636       String JavaDoc result = (String JavaDoc)method.invoke(mlet, new Object JavaDoc[]{library});
637
638       assertNotNull(result);
639       assertTrue(libraryFile.exists());
640       assertTrue(libraryFile.length() > 0);
641    }
642
643    public void testFindLibraryWithLibraryDirectoryNotInClassPath() throws Exception JavaDoc
644    {
645       // Prepare the library
646
String JavaDoc library = "mlet";
647       String JavaDoc libraryName = System.mapLibraryName(library);
648       File JavaDoc libraryFile = new File JavaDoc(libraryName);
649       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(libraryFile);
650       fos.write("library".getBytes());
651       fos.close();
652
653       assertTrue(libraryFile.exists());
654       assertTrue(libraryFile.length() > 0);
655
656       MLet JavaDoc mlet = new MLet JavaDoc(new URL JavaDoc[]{libraryFile.getCanonicalFile().getParentFile().toURL()});
657
658       // Set the library directory to some temp directory
659
File JavaDoc temp = File.createTempFile("abc", null);
660       temp.deleteOnExit();
661       mlet.setLibraryDirectory(temp.getCanonicalFile().getParentFile().getCanonicalPath());
662
663       Method JavaDoc method = mlet.getClass().getDeclaredMethod("findLibrary", new Class JavaDoc[]{String JavaDoc.class});
664       method.setAccessible(true);
665       String JavaDoc result = (String JavaDoc)method.invoke(mlet, new Object JavaDoc[]{library});
666
667       assertNotNull(result);
668       assertTrue(libraryFile.exists());
669       assertTrue(libraryFile.length() > 0);
670
671       File JavaDoc tempLibrary = new File JavaDoc(mlet.getLibraryDirectory(), libraryName);
672       assertTrue(tempLibrary.exists());
673       assertTrue(tempLibrary.length() > 0);
674    }
675
676    public void testDefaultMletName() throws Exception JavaDoc {
677       MBeanServer JavaDoc mbServer = newMBeanServer();
678       MLet JavaDoc mlet = new MLet JavaDoc();
679       ObjectName JavaDoc mletName = new ObjectName JavaDoc(mbServer.getDefaultDomain(), "type", "MLet");
680       assertFalse(mbServer.isRegistered(mletName));
681       assertEquals(mletName, mbServer.registerMBean(mlet, null).getObjectName());
682       assertTrue(mbServer.isRegistered(mletName));
683    }
684
685    public interface SimpleMBean
686    {
687    }
688
689    public static class Simple implements SimpleMBean
690    {
691    }
692
693    public static class SimpleRegistration extends Simple implements MBeanRegistration JavaDoc
694    {
695       public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
696       {
697          return ObjectName.getInstance(":name=simple");
698       }
699
700       public void postRegister(Boolean JavaDoc registrationDone)
701       {
702       }
703
704       public void preDeregister() throws Exception JavaDoc
705       {
706       }
707
708       public void postDeregister()
709       {
710       }
711    }
712
713    public static class CLRWithOnlyMLets extends DefaultClassLoaderRepository
714    {
715       protected void addClassLoader(ClassLoader JavaDoc cl)
716       {
717          if (cl == null) return;
718          if (cl.getClass() == MLet JavaDoc.class || cl.getClass() == PrivateMLet JavaDoc.class) super.addClassLoader(cl);
719       }
720    }
721 }
722
Popular Tags