Source Code Cross Referenced for MLetTest.java in  » JMX » mx4j » test » javax » management » loading » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » mx4j » test.javax.management.loading 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) The MX4J Contributors.
003:         * All rights reserved.
004:         *
005:         * This software is distributed under the terms of the MX4J License version 1.0.
006:         * See the terms of the MX4J License in the documentation provided with this software.
007:         */
008:
009:        package test.javax.management.loading;
010:
011:        import java.io.File;
012:        import java.io.FileOutputStream;
013:        import java.lang.reflect.Method;
014:        import java.net.URL;
015:        import java.util.List;
016:        import java.util.Random;
017:        import java.util.Set;
018:        import java.util.Iterator;
019:        import javax.management.MBeanRegistration;
020:        import javax.management.MBeanServer;
021:        import javax.management.ObjectInstance;
022:        import javax.management.ObjectName;
023:        import javax.management.ReflectionException;
024:        import javax.management.loading.MLet;
025:        import javax.management.loading.PrivateMLet;
026:
027:        import mx4j.MX4JSystemKeys;
028:        import mx4j.loading.MLetParseException;
029:        import mx4j.loading.MLetParser;
030:        import mx4j.loading.MLetTag;
031:        import mx4j.server.DefaultClassLoaderRepository;
032:        import test.MX4JTestCase;
033:
034:        /**
035:         * @version $Revision: 1.19 $
036:         */
037:        public class MLetTest extends MX4JTestCase {
038:            public MLetTest(String s) {
039:                super (s);
040:            }
041:
042:            public void testInvalidMLetFileParsing() throws Exception {
043:                MLetParser parser = new MLetParser();
044:
045:                String content = null;
046:                try {
047:                    parser.parse(content);
048:                    fail();
049:                } catch (MLetParseException x) {
050:                }
051:
052:                content = "";
053:                try {
054:                    parser.parse(content);
055:                    fail();
056:                } catch (MLetParseException x) {
057:                }
058:
059:                content = " ";
060:                try {
061:                    parser.parse(content);
062:                    fail();
063:                } catch (MLetParseException x) {
064:                }
065:
066:                content = " <!--";
067:                try {
068:                    parser.parse(content);
069:                    fail();
070:                } catch (MLetParseException x) {
071:                }
072:
073:                content = " -->";
074:                try {
075:                    parser.parse(content);
076:                    fail();
077:                } catch (MLetParseException x) {
078:                }
079:
080:                content = "<!-- -->\n<!-- ->";
081:                try {
082:                    parser.parse(content);
083:                    fail();
084:                } catch (MLetParseException x) {
085:                }
086:
087:                content = "<!- -->";
088:                try {
089:                    parser.parse(content);
090:                    fail();
091:                } catch (MLetParseException x) {
092:                }
093:
094:                // Invalid, missing attributes
095:                content = "<!-- -->\n<MLET/>";
096:                try {
097:                    parser.parse(content);
098:                    fail();
099:                } catch (MLetParseException x) {
100:                }
101:
102:                // Invalid, missing archive attribute
103:                content = "<MLET CODE = \" test.mx4j.MBeanNormal\"/>";
104:                try {
105:                    parser.parse(content);
106:                    fail();
107:                } catch (MLetParseException x) {
108:                }
109:
110:                // Invalid, missing archive attribute
111:                content = "\n<MLET CODE=\" test.mx4j.MBeanNormal\">\n</MLET>";
112:                try {
113:                    parser.parse(content);
114:                    fail();
115:                } catch (MLetParseException x) {
116:                }
117:
118:                // Invalid, missing archive attribute
119:                content = "\n<MLET OBJECT=\"mx4j-mbean.ser\">\n</MLET>";
120:                try {
121:                    parser.parse(content);
122:                    fail();
123:                } catch (MLetParseException x) {
124:                }
125:
126:                // Invalid, missing code or object attribute
127:                content = "<MLET ARCHIVE = \"..\\lib\"></MLET>";
128:                try {
129:                    parser.parse(content);
130:                    fail();
131:                } catch (MLetParseException x) {
132:                }
133:
134:                // Invalid, either code or object attribute must be present
135:                content = "<MLET CODE=\"test.mx4j.MBeanNormal\" OBJECT=\"mx4j-mbean.ser\" ARCHIVE = \"..\\lib\"></MLET>";
136:                try {
137:                    parser.parse(content);
138:                    fail();
139:                } catch (MLetParseException x) {
140:                }
141:
142:                // Invalid arg tag
143:                //      content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\">\n<></MLET>";
144:                //      try {parser.parse(content); fail();}
145:                //      catch (MLetParseException x) {}
146:
147:                // Invalid arg tag
148:                content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\">\n<ARG></MLET>";
149:                try {
150:                    parser.parse(content);
151:                    fail();
152:                } catch (MLetParseException x) {
153:                }
154:
155:                // Invalid arg tag
156:                content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\">\n<ARG type=\"int\"></MLET>";
157:                try {
158:                    parser.parse(content);
159:                    fail();
160:                } catch (MLetParseException x) {
161:                }
162:
163:                // Invalid arg tag
164:                content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\">\n<ARG value=\"int\"></MLET>";
165:                try {
166:                    parser.parse(content);
167:                    fail();
168:                } catch (MLetParseException x) {
169:                }
170:
171:                // Invalid name tag
172:                content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\" name>\n</MLET>";
173:                try {
174:                    parser.parse(content);
175:                    fail();
176:                } catch (MLetParseException x) {
177:                }
178:
179:                // Invalid name tag
180:                content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\" name=>\n</MLET>";
181:                try {
182:                    parser.parse(content);
183:                    fail();
184:                } catch (MLetParseException x) {
185:                }
186:
187:                // Invalid version tag
188:                content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\" version>\n</MLET>";
189:                try {
190:                    parser.parse(content);
191:                    fail();
192:                } catch (MLetParseException x) {
193:                }
194:
195:                // Invalid version tag
196:                content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE = \"..\\lib\" version=>\n</MLET>";
197:                try {
198:                    parser.parse(content);
199:                    fail();
200:                } catch (MLetParseException x) {
201:                }
202:
203:            }
204:
205:            public void testValidMLetFileParsing() throws Exception {
206:                MLetParser parser = new MLetParser();
207:                String content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE=\"mx4j-tests.jar\" NAME=\":name=MLetTest1\"/>";
208:                parser.parse(content);
209:
210:                content = "<MLET CODE=\"test.mx4j.MBeanNormal\" ARCHIVE=\"mx4j-tests.jar\" NAME=\":name=MLetTest2\">\n\t<ARG TYPE=\"int\" VALUE=\"5\">\n</MLET>\n";
211:                parser.parse(content);
212:
213:                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";
214:                parser.parse(content);
215:            }
216:
217:            public void testCompleteMLetFileParsing() throws Exception {
218:                MLetParser parser = new MLetParser();
219:
220:                StringBuffer content = new StringBuffer();
221:                content.append("<!-- Comment -->");
222:                content.append("<MLET \n");
223:                content
224:                        .append("      Code=\"mx4j.tools.naming.NamingService\"\n");
225:                content.append("      archive = \" ../lib \"\n");
226:                content
227:                        .append("      CodeBase= \"http://localhost:8080/download\"\n");
228:                content.append("      NAME=\":name=test\"\n");
229:                content.append("      Version=\"1\">\n");
230:                content.append("      <!-- Comment -->");
231:                content.append("      <ARG \n");
232:                content.append("           Type=\"boolean\"\n");
233:                content.append("           VALUE = \"true\">\n");
234:                content.append("      <!-- Comment -->");
235:                content.append("      <ARG \n");
236:                content.append("           TYPE = \"boolean\"\n");
237:                content.append("           value=\"true\"/>\n");
238:                content.append("</MLet>");
239:
240:                parser.parse(content.toString());
241:            }
242:
243:            public void testCodebaseForGetMBeansFromURL() throws Exception {
244:                Class cls = Simple.class;
245:                String className = cls.getName();
246:                URL url = cls.getProtectionDomain().getCodeSource()
247:                        .getLocation();
248:                String urlString = url.toExternalForm();
249:                int index = urlString.lastIndexOf('/') + 1;
250:                String jar = urlString.substring(index);
251:
252:                String codebase = ".";
253:                String content = "<MLET CODE=\"" + className
254:                        + "\" NAME=\":name=test\" ARCHIVE=\"" + jar
255:                        + "\" CODEBASE=\"" + codebase + "\"/>";
256:                MLetParser parser = new MLetParser();
257:                List tags = parser.parse(content);
258:                MLetTag tag = (MLetTag) tags.get(0);
259:                URL mletFileURL = new URL(
260:                        "http://mx4j.sourceforge.net/mlets/mbeans.mlet");
261:                URL codebaseURL = tag.normalizeCodeBase(mletFileURL);
262:                assertEquals(codebaseURL.toExternalForm(),
263:                        "http://mx4j.sourceforge.net/mlets/");
264:
265:                content = "<MLET CODE=\"" + className
266:                        + "\" NAME=\":name=test\" ARCHIVE=\"" + jar + "\"/>";
267:                tags = parser.parse(content);
268:                tag = (MLetTag) tags.get(0);
269:                codebaseURL = tag.normalizeCodeBase(mletFileURL);
270:                assertEquals(codebaseURL.toExternalForm(),
271:                        "http://mx4j.sourceforge.net/mlets/");
272:
273:                codebase = "../lib";
274:                content = "<MLET CODE=\"" + className
275:                        + "\" NAME=\":name=test\" ARCHIVE=\"" + jar
276:                        + "\" CODEBASE=\"" + codebase + "\"/>";
277:                tags = parser.parse(content);
278:                tag = (MLetTag) tags.get(0);
279:                codebaseURL = tag.normalizeCodeBase(mletFileURL);
280:                assertEquals(codebaseURL.toExternalForm(),
281:                        "http://mx4j.sourceforge.net/lib/");
282:
283:                codebase = "ftp://mx4j.sourceforge.net/mbeans";
284:                content = "<MLET CODE=\"" + className
285:                        + "\" NAME=\":name=test\" ARCHIVE=\"" + jar
286:                        + "\" CODEBASE=\"" + codebase + "\"/>";
287:                tags = parser.parse(content);
288:                tag = (MLetTag) tags.get(0);
289:                codebaseURL = tag.normalizeCodeBase(mletFileURL);
290:                assertEquals(codebaseURL.toExternalForm(), codebase + "/");
291:            }
292:
293:            public void testGetMBeansFromURL() throws Exception {
294:                Class cls = Simple.class;
295:                String className = cls.getName();
296:                URL url = cls.getProtectionDomain().getCodeSource()
297:                        .getLocation();
298:                String urlString = url.toExternalForm();
299:                int index = urlString.lastIndexOf('/') + 1;
300:                String jar = urlString.substring(index);
301:                String codebase = urlString.substring(0, index);
302:
303:                // Write an MLet file
304:                File mletFile = File.createTempFile("mlet", null);
305:                mletFile.deleteOnExit();
306:                FileOutputStream fos = new FileOutputStream(mletFile);
307:                String content = "<MLET CODE=\"" + className
308:                        + "\" NAME=\":name=test\" ARCHIVE=\"" + jar
309:                        + "\" CODEBASE=\"" + codebase + "\"/>";
310:                fos.write(content.getBytes());
311:                fos.close();
312:
313:                System.setProperty(
314:                        MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY,
315:                        CLRWithOnlyMLets.class.getName());
316:                MBeanServer server = newMBeanServer();
317:
318:                ObjectName mletName = new ObjectName(":loader=mlet1");
319:
320:                MLet mlet = new MLet();
321:                server.registerMBean(mlet, mletName);
322:
323:                Set mbeans = mlet.getMBeansFromURL(mletFile.toURL());
324:                if (mbeans.size() != 1)
325:                    fail("Loaded wrong number of MBeans");
326:                ObjectInstance instance = (ObjectInstance) mbeans.iterator()
327:                        .next();
328:                if (!instance.getClassName().equals(className))
329:                    fail("Loaded a different MBean");
330:            }
331:
332:            public void testGetMBeansFromURLWithNoName() throws Exception {
333:                Class cls = SimpleRegistration.class;
334:                String className = cls.getName();
335:                URL url = cls.getProtectionDomain().getCodeSource()
336:                        .getLocation();
337:                String urlString = url.toExternalForm();
338:                int index = urlString.lastIndexOf('/') + 1;
339:                String jar = urlString.substring(index);
340:                String codebase = urlString.substring(0, index);
341:
342:                // Write an MLet file
343:                File mletFile = File.createTempFile("mletnoname", null);
344:                mletFile.deleteOnExit();
345:                FileOutputStream fos = new FileOutputStream(mletFile);
346:                String content = "<MLET CODE=\"" + className + "\" ARCHIVE=\""
347:                        + jar + "\" CODEBASE=\"" + codebase + "\"/>";
348:                fos.write(content.getBytes());
349:                fos.close();
350:
351:                System.setProperty(
352:                        MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY,
353:                        CLRWithOnlyMLets.class.getName());
354:                MBeanServer server = newMBeanServer();
355:
356:                ObjectName mletName = new ObjectName(":loader=mlet1");
357:                MLet mlet = new MLet();
358:                server.registerMBean(mlet, mletName);
359:
360:                Set mbeans = mlet.getMBeansFromURL(mletFile.toURL());
361:                if (mbeans.size() != 1)
362:                    fail("Loaded wrong number of MBeans");
363:                ObjectInstance instance = (ObjectInstance) mbeans.iterator()
364:                        .next();
365:                if (!instance.getClassName().equals(className))
366:                    fail("Loaded a different MBean");
367:            }
368:
369:            public void testMLetDelegatesToCLR() throws Exception {
370:                mletDelegationToCLR(true);
371:            }
372:
373:            public void testMLetDoesNotDelegateToCLR() throws Exception {
374:                try {
375:                    mletDelegationToCLR(false);
376:                    fail("MLet does not delegate, cannot load the class");
377:                } catch (ReflectionException ignored) {
378:                }
379:            }
380:
381:            public void mletDelegationToCLR(boolean delegates) throws Exception {
382:                System.setProperty(
383:                        MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY,
384:                        CLRWithOnlyMLets.class.getName());
385:                MBeanServer server = newMBeanServer();
386:
387:                ObjectName loaderName = new ObjectName("Loader", "id", "0");
388:                ObjectName mletName = new ObjectName("Loader", "id", "1");
389:                ObjectName mbeanName = new ObjectName("MBean", "id", "0");
390:
391:                Class cls = Simple.class;
392:                URL url = cls.getProtectionDomain().getCodeSource()
393:                        .getLocation();
394:                String className = cls.getName();
395:
396:                MLet loader = new MLet(new URL[] { url }, ClassLoader
397:                        .getSystemClassLoader().getParent());
398:                server.registerMBean(loader, loaderName);
399:
400:                MLet mlet = new MLet(new URL[0], ClassLoader
401:                        .getSystemClassLoader().getParent(), delegates);
402:
403:                // Be sure the MLet cannot load the class
404:                try {
405:                    mlet.loadClass(className);
406:                    fail("MLet should not be able to load the class");
407:                } catch (ClassNotFoundException ignored) {
408:                }
409:
410:                server.registerMBean(mlet, mletName);
411:
412:                // Try to create the MBean
413:                server.createMBean(className, mbeanName, mletName);
414:            }
415:
416:            public void testSingleMLetLoadClass() throws Exception {
417:                System.setProperty(
418:                        MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY,
419:                        CLRWithOnlyMLets.class.getName());
420:                MBeanServer server = newMBeanServer();
421:
422:                ObjectName loaderName = new ObjectName("Loader", "id", "0");
423:                ObjectName mbeanName = new ObjectName("MBean", "id", "0");
424:
425:                Class cls = Simple.class;
426:                URL url = cls.getProtectionDomain().getCodeSource()
427:                        .getLocation();
428:                String className = cls.getName();
429:
430:                MLet mlet = new MLet(new URL[] { url }, ClassLoader
431:                        .getSystemClassLoader().getParent());
432:                server.registerMBean(mlet, loaderName);
433:
434:                server.createMBean(className, mbeanName, loaderName);
435:            }
436:
437:            public void testManyMLetLoadClass() throws Exception {
438:                int loaderCount = 100;
439:                int mbeanCount = 200;
440:
441:                long elapsed = manyMLetLoadClass(loaderCount, mbeanCount,
442:                        Simple.class.getName(), false);
443:
444:                System.out.println("Loading " + mbeanCount + " MBeans with "
445:                        + loaderCount + " MLets took " + elapsed
446:                        + " ms, average is " + (elapsed / mbeanCount));
447:                // Assume registering a valid MBean will take no more than 500 ms
448:                if (elapsed > mbeanCount * 500)
449:                    fail("Test took too much time, probably a problem in MLet loading");
450:            }
451:
452:            public void testMultiMLetLoadNonExistingClass() throws Exception {
453:                int loaderCount = 100;
454:                int mbeanCount = 200;
455:
456:                long elapsed = manyMLetLoadClass(loaderCount, mbeanCount,
457:                        "dummy", true);
458:
459:                System.out.println("Loading " + mbeanCount
460:                        + " non-existing MBeans with " + loaderCount
461:                        + " MLets took " + elapsed + " ms, average is "
462:                        + (elapsed / mbeanCount));
463:                // We're looking for a non-existing class, so we have to ask to all classloaders in the CLR before the chosen MLet
464:                // The time we spend looking for the class is roughly linear with the number of classloaders in the queried,
465:                // that in average is loaderCount / 2 and at most loaderCount:
466:                // elapsed = mbeanCount * loaderCount * k
467:                // Assume that k > 5 ms is too bad performance
468:                if (elapsed > mbeanCount * loaderCount * 5)
469:                    fail("Test took too much time, probably a problem in MLet loading");
470:            }
471:
472:            private long manyMLetLoadClass(int loaderCount, int mbeanCount,
473:                    String className, boolean ignoreExceptionOnCreation)
474:                    throws Exception {
475:                ObjectName[] loaders = new ObjectName[loaderCount];
476:                ObjectName[] mbeans = new ObjectName[mbeanCount];
477:
478:                MBeanServer server = newMBeanServer();
479:
480:                URL url = getClass().getProtectionDomain().getCodeSource()
481:                        .getLocation();
482:
483:                // Register some MLet
484:                for (int i = 0; i < loaderCount; ++i) {
485:                    loaders[i] = new ObjectName("Loader", "id", String
486:                            .valueOf(i));
487:                    MLet mlet = new MLet(new URL[] { url }, ClassLoader
488:                            .getSystemClassLoader().getParent());
489:                    server.registerMBean(mlet, loaders[i]);
490:                }
491:
492:                long start = System.currentTimeMillis();
493:
494:                Random random = new Random(start);
495:                for (int i = 0; i < mbeanCount; ++i) {
496:                    mbeans[i] = new ObjectName("MBean", "id", String.valueOf(i));
497:
498:                    // Choose an MLet to load the MBean
499:                    int id = random.nextInt(loaderCount);
500:                    ObjectName loader = loaders[id];
501:
502:                    if (ignoreExceptionOnCreation) {
503:                        try {
504:                            server.createMBean(className, mbeans[i], loader);
505:                        } catch (ReflectionException ignored) {
506:                        }
507:                    } else {
508:                        server.createMBean(className, mbeans[i], loader);
509:                    }
510:                }
511:
512:                long end = System.currentTimeMillis();
513:
514:                return end - start;
515:            }
516:
517:            public void testChildMLetRegisteredBeforeParentMLet()
518:                    throws Exception {
519:                // A test to be sure the MLet implementation does not recurse infinitely when loading classes.
520:
521:                URL url = getClass().getProtectionDomain().getCodeSource()
522:                        .getLocation();
523:                MLet parent = new MLet(new URL[] { url }, ClassLoader
524:                        .getSystemClassLoader().getParent());
525:                MLet child = new MLet(new URL[0], parent);
526:
527:                ObjectName parentName = new ObjectName("MLet", "type", "parent");
528:                ObjectName childName = new ObjectName("MLet", "type", "child");
529:
530:                MBeanServer server = newMBeanServer();
531:
532:                // First register the child, then the parent.
533:                server.registerMBean(child, childName);
534:                server.registerMBean(parent, parentName);
535:
536:                // Now in the CLR there are: SystemCL, childMLet, parentMLet.
537:                // If I ask someting to parentMLet that it cannot find, it will delegate to the CLR,
538:                // and when it comes to the childMLet, the delegation mechanism will ask again to the parentMLet.
539:                // If we're not smart in the MLet implementation this ends up in an infinite loop.
540:                try {
541:                    parent.loadClass("dummy");
542:                    fail("Class does not exist");
543:                } catch (ClassNotFoundException ignored) {
544:                }
545:            }
546:
547:            public void testPrivateMLetNotAddedToCLR() throws Exception {
548:                Class cls = Simple.class;
549:                URL url = cls.getProtectionDomain().getCodeSource()
550:                        .getLocation();
551:                PrivateMLet mlet = new PrivateMLet(new URL[] { url },
552:                        ClassLoader.getSystemClassLoader().getParent(), true);
553:
554:                ObjectName mletName = new ObjectName(":MLet=Private");
555:                ObjectName mbeanName = new ObjectName(":MBean=Simple");
556:
557:                System.setProperty(
558:                        MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY,
559:                        CLRWithOnlyMLets.class.getName());
560:                MBeanServer server = newMBeanServer();
561:
562:                // The private MLet should not be registered in the CLR
563:                server.registerMBean(mlet, mletName);
564:
565:                try {
566:                    // Ask the CLR to load the class
567:                    server.createMBean(cls.getName(), mbeanName);
568:                    fail("Class cannot be found by the CLR");
569:                } catch (ReflectionException ignored) {
570:                }
571:            }
572:
573:            public void testFindLibrary() throws Exception {
574:                MLet mlet = new MLet();
575:                Method method = mlet.getClass().getDeclaredMethod(
576:                        "findLibrary", new Class[] { String.class });
577:                method.setAccessible(true);
578:                String result = (String) method.invoke(mlet,
579:                        new Object[] { "stat" });
580:                if (result != null)
581:                    fail("MLet can load non-existing libraries");
582:            }
583:
584:            public void testFindLibraryWithLibraryDirectoryInClassPath()
585:                    throws Exception {
586:                // Prepare the library
587:                String library = "mlet";
588:                String libraryName = System.mapLibraryName(library);
589:                File libraryFile = new File(libraryName);
590:                FileOutputStream fos = new FileOutputStream(libraryFile);
591:                fos.write("library".getBytes());
592:                fos.close();
593:
594:                assertTrue(libraryFile.exists());
595:                assertTrue(libraryFile.length() > 0);
596:
597:                MLet mlet = new MLet(new URL[] { libraryFile.getCanonicalFile()
598:                        .getParentFile().toURL() });
599:                Method method = mlet.getClass().getDeclaredMethod(
600:                        "findLibrary", new Class[] { String.class });
601:                method.setAccessible(true);
602:                String result = (String) method.invoke(mlet,
603:                        new Object[] { library });
604:
605:                assertNotNull(result);
606:                assertTrue(libraryFile.exists());
607:                assertTrue(libraryFile.length() > 0);
608:            }
609:
610:            public void testFindLibraryWithLibraryDirectoryNotInClassPath()
611:                    throws Exception {
612:                // Prepare the library
613:                String library = "mlet";
614:                String libraryName = System.mapLibraryName(library);
615:                File libraryFile = new File(libraryName);
616:                FileOutputStream fos = new FileOutputStream(libraryFile);
617:                fos.write("library".getBytes());
618:                fos.close();
619:
620:                assertTrue(libraryFile.exists());
621:                assertTrue(libraryFile.length() > 0);
622:
623:                MLet mlet = new MLet(new URL[] { libraryFile.getCanonicalFile()
624:                        .getParentFile().toURL() });
625:
626:                // Set the library directory to some temp directory
627:                File temp = File.createTempFile("abc", null);
628:                temp.deleteOnExit();
629:                mlet.setLibraryDirectory(temp.getCanonicalFile()
630:                        .getParentFile().getCanonicalPath());
631:
632:                Method method = mlet.getClass().getDeclaredMethod(
633:                        "findLibrary", new Class[] { String.class });
634:                method.setAccessible(true);
635:                String result = (String) method.invoke(mlet,
636:                        new Object[] { library });
637:
638:                assertNotNull(result);
639:                assertTrue(libraryFile.exists());
640:                assertTrue(libraryFile.length() > 0);
641:
642:                File tempLibrary = new File(mlet.getLibraryDirectory(),
643:                        libraryName);
644:                assertTrue(tempLibrary.exists());
645:                assertTrue(tempLibrary.length() > 0);
646:            }
647:
648:            public void testDefaultMletName() throws Exception {
649:                MBeanServer mbServer = newMBeanServer();
650:                MLet mlet = new MLet();
651:                ObjectName mletName = new ObjectName(mbServer
652:                        .getDefaultDomain(), "type", "MLet");
653:                assertFalse(mbServer.isRegistered(mletName));
654:                assertEquals(mletName, mbServer.registerMBean(mlet, null)
655:                        .getObjectName());
656:                assertTrue(mbServer.isRegistered(mletName));
657:            }
658:
659:            public interface SimpleMBean {
660:            }
661:
662:            public static class Simple implements  SimpleMBean {
663:            }
664:
665:            public static class SimpleRegistration extends Simple implements 
666:                    MBeanRegistration {
667:                public ObjectName preRegister(MBeanServer server,
668:                        ObjectName name) throws Exception {
669:                    return ObjectName.getInstance(":name=simple");
670:                }
671:
672:                public void postRegister(Boolean registrationDone) {
673:                }
674:
675:                public void preDeregister() throws Exception {
676:                }
677:
678:                public void postDeregister() {
679:                }
680:            }
681:
682:            public static class CLRWithOnlyMLets extends
683:                    DefaultClassLoaderRepository {
684:                protected void addClassLoader(ClassLoader cl) {
685:                    if (cl == null)
686:                        return;
687:                    if (cl.getClass() == MLet.class
688:                            || cl.getClass() == PrivateMLet.class)
689:                        super.addClassLoader(cl);
690:                }
691:            }
692:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.