001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestClassLoaderFactory.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.framework;
030:
031: import com.sun.jbi.framework.ScaffoldPlatformContext;
032: import java.io.File;
033: import java.io.InputStream;
034:
035: import java.net.URL;
036: import java.net.URLClassLoader;
037:
038: import java.util.ArrayList;
039: import java.util.List;
040: import java.util.Properties;
041:
042: import javax.jbi.JBIException;
043: import com.sun.jbi.JBIProvider;
044:
045: /**
046: * Tests for the various methods on the ClassLoaderFactory class.
047: *
048: * @author Sun Microsystems, Inc.
049: */
050: public class TestClassLoaderFactory extends junit.framework.TestCase {
051: /**
052: * Value of the $SRCROOT environment variable
053: */
054: private String mSrcroot;
055:
056: /**
057: * ClassLoaderFactory
058: */
059: private ClassLoaderFactory mClassLoaderFactory;
060:
061: /**
062: * EnvironmentContext
063: */
064: private EnvironmentContext mEnvironmentContext;
065:
066: /**
067: * Component
068: */
069: private Component mComponent;
070:
071: /**
072: * The constructor for this testcase, forwards the test name to
073: * the jUnit TestCase base class.
074: * @param aTestName String with the name of this test.
075: */
076: public TestClassLoaderFactory(String aTestName) {
077: super (aTestName);
078: }
079:
080: /**
081: * Setup for the test. This creates the ClassLoaderFactory instance
082: * and other objects needed for the tests.
083: * @throws Exception when set up fails for any reason.
084: */
085: public void setUp() throws Exception {
086: super .setUp();
087: mSrcroot = System.getProperty("junit.srcroot");
088: mEnvironmentContext = new EnvironmentContext(
089: new ScaffoldPlatformContext(), new JBIFramework(),
090: new Properties());
091: mClassLoaderFactory = ClassLoaderFactory.getInstance();
092: mComponent = new Component();
093: }
094:
095: /**
096: * Cleanup for the test.
097: * @throws Exception when tearDown fails for any reason.
098: */
099: public void tearDown() throws Exception {
100: super .tearDown();
101: }
102:
103: // ============================= Bootstrap classloader test methods ================================
104:
105: /**
106: * testCreateBootstrapClassLoader
107: * tests the creation of a Bootstrap classloadera by
108: * 1.creating the class loader with a specified class path containing 2 jar files (a.jar,b.jar)
109: * 2.loading 1 class from each JAR file using reflection
110: * 3.creating an instance of the classpath
111: * @throws Exception if an unexpected error occurs
112: */
113: public void testCreateBootstrapClassLoader() throws Exception {
114: final String testName = "testCreateBootstrapClassLoader";
115: try {
116: start(testName);
117: // set up the class paths
118: List cpList = new ArrayList();
119: String path1 = getTestJarsPath() + File.separator + "a.jar";
120: String path2 = getTestJarsPath() + File.separator + "b.jar";
121: String path3 = getTestClassPath();
122: String path4 = getTestResourcePath();
123: cpList.add(path1);
124: cpList.add(path2);
125: cpList.add(path3);
126: cpList.add(path4);
127:
128: mComponent.setName("bootcomponent0");
129: mComponent.setBootstrapClassPathElements(cpList);
130: log("created component");
131:
132: // create a bootstrap classloader
133: ClassLoader bootCL = mClassLoaderFactory
134: .createBootstrapClassLoader(mComponent);
135: URLClassLoader booturlCL = (URLClassLoader) bootCL;
136: // load a class using the classloader
137:
138: Class testAJarClass = bootCL
139: .loadClass("binding1.Bootstrap1");
140: Class testBJarClass = bootCL
141: .loadClass("engine1.Bootstrap1");
142: Class testLoadClass = bootCL
143: .loadClass("com.sun.workflow.classes.WFEngine");
144: //InputStream testLoadResource = bootCL.getResourceAsStream("com/sun/workflow/resources/resources.properties") ;
145: InputStream testLoadResource = bootCL
146: .getResourceAsStream("resources.properties");
147:
148: // create classes to make sure classloading succeeded
149: Object binding1CL = testAJarClass.newInstance();
150: Object engine1CL = testBJarClass.newInstance();
151: Object wfEngineCL = testLoadClass.newInstance();
152:
153: //
154: // MOVE THIS CODE TO A UTILS CLASS
155: // load class resource to make sure classloading succeeded
156: Properties p = new Properties();
157: p.load(testLoadResource);
158:
159: // if we get here, the test has passed
160: log("testCreateBootstrapClassLoader Passed ");
161: endOK(testName);
162: } catch (JBIException e) {
163: // test failure
164: endException(testName, e);
165:
166: fail("Failure getting/setting BootstrapClass");
167: } catch (ClassNotFoundException cnfe) {
168: // test failure
169: endException(testName, cnfe);
170:
171: fail("Failure getting/setting BootstrapClass");
172: }
173: }
174:
175: /**
176: * testCreateBootstrapClassLoaderLoadClass
177: * tests loading of a class by the bootstrap class loader based
178: * on the values passed in
179: * @throws Exception if an unexpected error occurs
180: */
181: public void testBootstrapClassLoaderLoadClass(String testName,
182: boolean selfFirst, String expectedClassLoader,
183: String className, List libraries) throws Exception {
184: try {
185: start(testName);
186: // set up the class paths
187: List cpList = new ArrayList();
188: String path1 = getTestJarsPath() + File.separator + "a.jar";
189: String path2 = getTestJarsPath() + File.separator + "b.jar";
190: String path3 = getTestClassPath();
191: String path4 = getTestResourcePath();
192: cpList.add(path1);
193: cpList.add(path2);
194: cpList.add(path3);
195: cpList.add(path4);
196:
197: mComponent.setName("bootcomponent0");
198: mComponent.setBootstrapClassPathElements(cpList);
199: log("created component");
200:
201: // create a bootstrap classloader
202: ClassLoader bootCL = mClassLoaderFactory
203: .createBootstrapClassLoader(mComponent);
204: URLClassLoader booturlCL = (URLClassLoader) bootCL;
205: // load a class using the classloader
206:
207: Class testAJarClass = bootCL
208: .loadClass("binding1.Bootstrap1");
209: Class testBJarClass = bootCL
210: .loadClass("engine1.Bootstrap1");
211: Class testLoadClass = bootCL
212: .loadClass("com.sun.workflow.classes.WFEngine");
213: //InputStream testLoadResource = bootCL.getResourceAsStream("com/sun/workflow/resources/resources.properties") ;
214: InputStream testLoadResource = bootCL
215: .getResourceAsStream("resources.properties");
216:
217: // create classes to make sure classloading succeeded
218: Object binding1CL = testAJarClass.newInstance();
219: Object engine1CL = testBJarClass.newInstance();
220: Object wfEngineCL = testLoadClass.newInstance();
221:
222: //
223: // MOVE THIS CODE TO A UTILS CLASS
224: // load class resource to make sure classloading succeeded
225: Properties p = new Properties();
226: p.load(testLoadResource);
227:
228: // if we get here, the test has passed
229: log("testCreateBootstrapClassLoader Passed ");
230: endOK(testName);
231: } catch (JBIException e) {
232: // test failure
233: endException(testName, e);
234:
235: fail("Failure getting/setting BootstrapClass");
236: } catch (ClassNotFoundException cnfe) {
237: // test failure
238: endException(testName, cnfe);
239:
240: fail("Failure getting/setting BootstrapClass");
241: }
242: }
243:
244: /**
245: * testCreateBootstrapClassLoaderBadEmptyPaths
246: * tests the creation of a Bootstrap classloadera by
247: * 1.creating the class loader with a specified class path containing 2 jar files (a.jar,b.jar)
248: * 2.loading 1 class from each JAR file using reflection
249: * 3.creating an instance of the classpath
250: * @throws Exception if an unexpected error occurs
251: */
252: public void testCreateBootstrapClassLoaderBadEmptyPaths()
253: throws Exception {
254: try {
255: // set up the class paths
256: List cpList = new ArrayList();
257:
258: mComponent.setName("bootcomponent1");
259: mComponent.setBootstrapClassPathElements(cpList);
260: log("created component");
261:
262: // create a bootstrap classloader
263: ClassLoader bootCL = mClassLoaderFactory
264: .createBootstrapClassLoader(mComponent);
265:
266: // test fails if we get here
267: fail("testCreateBootstrapClassLoaderBadEmptyPaths:JBIException not caught");
268: } catch (IllegalArgumentException e) {
269: // test passes if we catch an exception
270: log("Test Passed with Exception:" + e.toString());
271: // Verification
272: assertTrue("Incorrect exception received: " + e.toString(),
273: (-1 < e.getMessage().indexOf("Empty list")));
274: }
275: }
276:
277: /**
278: * testCreateBootstrapClassLoaderBadNullPaths
279: * tests the creation of a Bootstrap classloadera by
280: * 1.creating the class loader with a specified class path containing 2 jar files (a.jar,b.jar)
281: * 2.loading 1 class from each JAR file using reflection
282: * 3.creating an instance of the classpath
283: * @throws Exception if an unexpected error occurs
284: */
285: public void testCreateBootstrapClassLoaderBadNullPaths()
286: throws Exception {
287: try {
288: // create a bootstrap classloader
289: ClassLoader bootCL = mClassLoaderFactory
290: .createBootstrapClassLoader(null);
291:
292: // test fails if we get here
293: fail("testCreateBootstrapClassLoaderBadNullPaths:JBIException not caught");
294: } catch (IllegalArgumentException e) {
295: // test passes if we catch an exception
296: log("Test Passed with Exception:" + e.toString());
297: // Verification
298: assertTrue("Incorrect exception received: " + e.toString(),
299: (-1 < e.getMessage().indexOf("Null")));
300: }
301: }
302:
303: // ============================= Component classloader test methods ================================
304:
305: /**
306: * tests the successful creation of a component class loader
307: * by loading a class in its classpath
308: * TO DO after implementing corresponding functionality in
309: * ClassLoaderFactory.createComponentClassLoader()
310: * @throws Exception if an unexpected error occurs
311: */
312: public void testCreateComponentClassLoader() throws Exception {
313: final String testName = "testCreateComponentClassLoader";
314: try {
315: start(testName);
316: // create a component
317: List cpList = new ArrayList();
318: String path1 = getTestJarsPath() + File.separator + "a.jar";
319: String path2 = getTestJarsPath() + File.separator + "b.jar";
320: String path3 = getTestClassPath();
321: String path4 = getTestResourcePath();
322: cpList.add(path1);
323: cpList.add(path2);
324: cpList.add(path3);
325: cpList.add(path4);
326: mComponent.setName("component1");
327: mComponent.setComponentClassPathElements(cpList);
328: log("created component");
329:
330: //create a valid shared library list
331: List sharedLibList = new ArrayList();
332: List sharedLibElements = new ArrayList();
333: String path5 = getTestJarsPath() + File.separator
334: + "quotes.jar";
335: sharedLibElements.add(path5);
336: SharedLibrary sharedLib3 = new SharedLibrary("sharedLib3",
337: "jar file c", "/sharedLib3", sharedLibElements);
338: sharedLibList.add("sharedLib3");
339: mComponent.setSharedLibraryNames(sharedLibList);
340: log("created shared library");
341:
342: //create and install the shared classloader before
343: //installing the component classloader that references it.
344: ClassLoader sharedLoader = mClassLoaderFactory
345: .createSharedClassLoader(sharedLib3);
346:
347: // create the component classloader
348: ClassLoader compClassLoader = mClassLoaderFactory
349: .createComponentClassLoader(mComponent);
350:
351: if (compClassLoader == null)
352: fail(testName + ":create classloader unsucessful-NULL");
353:
354: // attempt to load a component class from a JAR using this classloader
355: Class testAJarClass = compClassLoader
356: .loadClass("binding1.Bootstrap1");
357: Object testAJarObj = testAJarClass.newInstance();
358:
359: // attempt to load a component class from a classpath using this classloader
360: Class testClassFromPath = compClassLoader
361: .loadClass("com.sun.workflow.classes.WFEngine");
362: Object testClassFromPathObj = testClassFromPath
363: .newInstance();
364:
365: // attempt to load a component class from a classpath using this classloader
366: //InputStream testResourceFromPath = compClassLoader.getResourceAsStream("com/sun/workflow/resources/resources.properties") ;
367: InputStream testResourceFromPath = compClassLoader
368: .getResourceAsStream("resources.properties");
369: // MOVE THIS CODE TO A UTILS CLASS
370: //
371: Properties p = new Properties();
372: p.load(testResourceFromPath);
373:
374: // also attempt to load a shared class using the same component classloader
375: Class testCJarClass = compClassLoader
376: .loadClass("library1.QuoteEngine");
377: Object testCJatObj = testCJarClass.newInstance();
378: //double sunStockPrice = QuoteEngine.getQuote("SUNW");
379: log(testName
380: + ":component classloader successfully created-PASSED ");
381: endOK(testName);
382: } catch (JBIException je) {
383: endException(testName, je);
384: je.printStackTrace();
385: // exception means failure
386: fail(testName + ":unable to create a component classloader"
387: + je.toString());
388: } catch (ClassNotFoundException cnfe) {
389: endException(testName, cnfe);
390: fail(testName
391: + ":classloader unable to load component class"
392: + cnfe.toString());
393: }
394: }
395:
396: /**
397: * testCreateComponentClassLoaderBadNullComponent
398: * tests the creation of a Component classloader by
399: * creating the class loader with a null Component reference and a valid
400: * Shared Library List
401: * @throws Exception if an unexpected error occurs
402: */
403: public void testCreateComponentClassLoaderBadNullComponent()
404: throws Exception {
405: try {
406: //create a valid shared library list - currently null
407: // TO DO after implementing corresponding functionality in
408: // ClassLoaderFactory.createComponentClassLoader()
409:
410: // create a component classloader
411: ClassLoader compCL = mClassLoaderFactory
412: .createComponentClassLoader(null);
413:
414: // test fails if we get here
415: fail("testCreateComponentClassLoaderBadNullComponent:JBIException not caught");
416: } catch (IllegalArgumentException e) {
417: // test passes if we catch an exception
418: log("Test Passed with Exception:" + e.toString());
419: // Verification
420: assertTrue("Incorrect exception received: " + e.toString(),
421: (-1 < e.getMessage().indexOf("Null")));
422: }
423: }
424:
425: /**
426: * testCreateComponentClassLoaderBadNullNamespaces
427: * tests the creation of a Component classloader by
428: * creating the class loader with valid Component reference and a null
429: * Shared Library List
430: * @throws Exception if an unexpected error occurs
431: */
432: public void testCreateComponentClassLoaderBadNullSNs()
433: throws Exception {
434: final String testName = "testCreateComponentClassLoaderBadNullSNs";
435: try {
436: // create a component
437: List cpList = new ArrayList();
438: String path1 = getTestJarsPath() + File.separator + "a.jar";
439: String path2 = getTestJarsPath() + File.separator + "b.jar";
440: cpList.add(path1);
441: cpList.add(path2);
442: mComponent.setName("component1");
443: mComponent.setComponentClassPathElements(cpList);
444: log("created component");
445:
446: // create a component classloader
447: ClassLoader compCL = mClassLoaderFactory
448: .createComponentClassLoader(mComponent);
449:
450: // attempt to load a component class using this classloader
451: Class testAJarClass = compCL
452: .loadClass("binding1.Bootstrap1");
453: Object testAJarObj = testAJarClass.newInstance();
454:
455: // test succeeds if we get here
456: log(testName + ":" + "passed");
457: } catch (Exception e) {
458: // test fails if we catch an exception
459: log("Test Failed with Exception:" + e.toString());
460: // Verification
461: fail(testName + ":JBIException not caught");
462: }
463: }
464:
465: /**
466: * testCreateComponentClassLoaderParent
467: * tests if the created ClassLoader has the correct parent
468: * @throws Exception if an unexpected error occurs
469: */
470: public void testCreateComponentClassLoaderCorrectParent()
471: throws Exception {
472: try {
473: // create a component
474: Component comp = new Component();
475: List cpList = new ArrayList();
476: String path1 = getTestJarsPath() + File.separator + "a.jar";
477: String path2 = getTestJarsPath() + File.separator + "b.jar";
478: cpList.add(path1);
479: cpList.add(path2);
480: comp.setName("component1");
481: comp.setComponentClassPathElements(cpList);
482: log("created component");
483:
484: //create a valid shared library list - currently null
485: // TO DO after implementing corresponding functionality in
486: // ClassLoaderFactory.createComponentClassLoader()
487: List sharedLibList = new ArrayList();
488: List sharedLibElements = new ArrayList();
489: String path = getTestJarsPath() + File.separator
490: + "quotes.jar";
491: sharedLibElements.add(path);
492: SharedLibrary sharedLib = new SharedLibrary("dummy",
493: "dummy", "dummy", sharedLibElements);
494: sharedLibList.add("dummy");
495: comp.setSharedLibraryNames(sharedLibList);
496: log("created shared library");
497:
498: // create a shared library classloader
499: ClassLoader sharedCL = mClassLoaderFactory
500: .createSharedClassLoader(sharedLib);
501:
502: // create a component classloader for the same shared library
503: ClassLoader compCL = mClassLoaderFactory
504: .createComponentClassLoader(comp);
505: ClassLoader compCLParent = compCL.getParent();
506: log("Component ClassLoader parent is :" + compCLParent);
507:
508: // test that the parent is the Delegating Classloader
509: log("COMPCL:"
510: + new Integer(compCLParent.toString().indexOf(
511: "DelegatingClassLoader")).toString());
512:
513: assertFalse("Correct Parent of Component ClassLoader"
514: + compCL.toString(), (-1 == compCLParent.toString()
515: .indexOf("DelegatingClassLoader")));
516: log("testCreateComponentClassLoaderCorrectParent Passed");
517: } catch (JBIException e) {
518: // test passes if we catch an exception
519: log("Test Failed with Exception:" + e.toString());
520: fail("Unable to get ClassLoader Chain Of Component Classloader");
521: }
522: }
523:
524: // ====================== Shared classloader test methods ====================
525:
526: /**
527: * tests the successful creation of a shared library class loader
528: * by loading a class in its classpath
529: * @throws Exception if an unexpected error occurs
530: */
531: public void testCreateSharedClassLoader() throws Exception {
532: final String testName = "testCreateSharedClassLoader";
533: try {
534: //create a valid shared library list
535: List sharedLibElements = new ArrayList();
536: String path1 = getTestJarsPath() + File.separator + "a.jar";
537: String path2 = getTestJarsPath() + File.separator + "b.jar";
538: sharedLibElements.add(path1);
539: sharedLibElements.add(path2);
540: SharedLibrary sharedLib = new SharedLibrary("sharedLib2",
541: "Jar files a and b", "/sharedLib2",
542: sharedLibElements);
543: log("created shared library");
544:
545: // create the classloader
546: ClassLoader sharedClassLoader = mClassLoaderFactory
547: .createSharedClassLoader(sharedLib);
548: if (sharedClassLoader == null)
549: fail(testName
550: + ":create shared classloader unsucessful-NULL");
551:
552: // attempt to load a component class using this classloader
553: Class testAJarClass = sharedClassLoader
554: .loadClass("binding1.Bootstrap1");
555: Object testAJarObj = testAJarClass.newInstance();
556:
557: Class testBJarClass = sharedClassLoader
558: .loadClass("engine1.Bootstrap1");
559: Object testBJarObj = testBJarClass.newInstance();
560:
561: if ((testAJarObj == null) || (testBJarObj == null))
562: fail(testName
563: + ":unable to create object for shared class -NULL");
564: log(testName
565: + ":shared library classloader,objects test passed");
566: } catch (JBIException je) {
567: // exception means failure
568: fail(testName + ":unable to create a shared classloader"
569: + je.toString());
570: } catch (ClassNotFoundException cnfe) {
571: fail(testName + ":classloader unable to load shared class"
572: + cnfe.toString());
573: }
574: }
575:
576: /**
577: * tests that an exception is thrown if a null shared library
578: * is passed to it.
579: * @throws Exception if an unexpected error occurs
580: */
581: public void testCreateSharedClassLoaderNullNamespace()
582: throws Exception {
583: final String testName = "testCreateSharedClassLoaderNullNamespace";
584: try {
585: // create the classloader
586: ClassLoader sharedClassLoader = mClassLoaderFactory
587: .createSharedClassLoader(null);
588: // if we reach here , we've failed
589: fail(testName + ":"
590: + "failed by missing to catch an exception");
591: } catch (IllegalArgumentException je) {
592: // exception means failure
593: assertTrue(testName + ":"
594: + "failed by missing to catch an exception",
595: -1 < je.toString().indexOf("Null argument"));
596: log(testName + ": passed");
597: }
598: }
599:
600: /**
601: * tests that repeated calls to createSharedClassLoader do not throw
602: * an exception
603: * @throws Exception if an unexpected error occurs
604: */
605: public void testCreateSharedClassLoaderTwice() throws Exception {
606: final String testName = "testCreateSharedClassLoaderTwice";
607: try {
608: //create a valid shared library list
609: List sharedLibElements = new ArrayList();
610: String path1 = getTestJarsPath() + File.separator + "a.jar";
611: String path2 = getTestJarsPath() + File.separator + "b.jar";
612: sharedLibElements.add(path1);
613: sharedLibElements.add(path2);
614: SharedLibrary sharedLib = new SharedLibrary("sharedLib1",
615: "Jar files a and b", "/sharedLib1",
616: sharedLibElements);
617: log("created shared library");
618:
619: // create the classloader
620: ClassLoader sharedClassLoader = mClassLoaderFactory
621: .createSharedClassLoader(sharedLib);
622: ClassLoader sharedClassLoader1 = mClassLoaderFactory
623: .createSharedClassLoader(sharedLib);
624:
625: assertTrue(testName + ":"
626: + "shared classloader instances are different",
627: (sharedClassLoader == sharedClassLoader1));
628: // if we reach here , we're ok
629: log(testName + ":passed");
630: } catch (JBIException je) {
631: // exception means failure
632: fail(testName + ":failed with exception:" + je.toString());
633: }
634: }
635:
636: /**
637: * tests the successful removal of a shared library classloader w/o
638: * an error
639: * @throws Exception if an unexpected error occurs
640: */
641: public void testRemoveSharedClassLoader() throws Exception {
642: final String testName = "testRemoveSharedClassLoader";
643: try {
644: //create a valid shared library list
645: List sharedLibElements = new ArrayList();
646: String path1 = getTestJarsPath() + File.separator + "a.jar";
647: String path2 = getTestJarsPath() + File.separator + "b.jar";
648: sharedLibElements.add(path1);
649: sharedLibElements.add(path2);
650: SharedLibrary sharedLib = new SharedLibrary("sharedLib4",
651: "Jar files a and b", "/sharedLib4",
652: sharedLibElements);
653: log("created shared library");
654:
655: // create the classloader
656: ClassLoader sharedClassLoader = mClassLoaderFactory
657: .createSharedClassLoader(sharedLib);
658: // remove the classloader
659: mClassLoaderFactory.removeSharedClassLoader(sharedLib
660: .getName());
661:
662: // test if classloader exists.
663: //
664: mClassLoaderFactory.getSharedClassLoader(sharedLib
665: .getName());
666: fail(testName
667: + " failed--- did not remove shared classloader");
668: } catch (JBIException je) {
669: // correct exception means failure
670: log(testName + " passed OK"
671: + je.toString().indexOf(" no shared"));
672: assertTrue(testName + ":"
673: + "failed by not removing classloader", -1 < je
674: .toString().indexOf("No shared classloader found"));
675: }
676: }
677:
678: /**
679: * tests that null argument passed to removeSharedClassLoader
680: * throws the correct exception.
681: * @throws Exception if an unexpected error occurs
682: */
683: public void testRemoveSharedClassLoaderNullSL() throws Exception {
684: final String testName = "testRemoveSharedClassLoaderNullSL";
685: try {
686: // remove the classloader
687: mClassLoaderFactory.removeSharedClassLoader(null);
688:
689: fail(testName + " failed no exception thrown");
690: } catch (IllegalArgumentException e) {
691: // correct exception means failure
692: assertTrue(testName + ":"
693: + "failed by not removing classloader", -1 < e
694: .toString().indexOf("Null argument"));
695: log(testName + " passed OK");
696: }
697: }
698:
699: /**
700: * tests that an invalid shared library passed to removeSharedClassLoader
701: * throws the correct exception.
702: * @throws Exception if an unexpected error occurs
703: */
704: public void testRemoveSharedClassLoaderInvalidSL() throws Exception {
705: final String testName = "testRemoveSharedClassLoaderInvalidSL";
706: try {
707: //create a valid shared library list
708: List sharedLibElements = new ArrayList();
709: String path1 = getTestJarsPath() + File.separator + "a.jar";
710: String path2 = getTestJarsPath() + File.separator + "b.jar";
711: sharedLibElements.add(path1);
712: sharedLibElements.add(path2);
713: SharedLibrary sharedLib = new SharedLibrary("sharedLib5",
714: "Jar files a and b", "/sharedLib5",
715: sharedLibElements);
716: log("created shared library");
717:
718: // remove the classloader
719: mClassLoaderFactory.removeSharedClassLoader(sharedLib
720: .getName());
721:
722: fail(testName + " failed no exception thrown");
723: } catch (JBIException je) {
724: // correct exception means failure
725: assertTrue(testName + ":" + "failed with wrong exception"
726: + je.toString(), -1 < je.toString().indexOf(
727: "No shared classloader"));
728: log(testName + " passed OK");
729: }
730: }
731:
732: /**
733: * tests the successful removal of a component classloader
734: * w/o an error
735: * @throws Exception if an unexpected error occurs
736: */
737: public void testRemoveComponentClassLoader() throws Exception {
738: final String testName = "testRemoveComponentClassLoader";
739: try {
740: List cpList = new ArrayList();
741: String path1 = getTestJarsPath() + File.separator + "a.jar";
742: String path2 = getTestJarsPath() + File.separator + "b.jar";
743: String path3 = getTestClassPath();
744: String path4 = getTestResourcePath();
745: cpList.add(path1);
746: cpList.add(path2);
747: cpList.add(path3);
748: cpList.add(path4);
749: Component comp = new Component();
750: comp.setName(testName);
751: comp.setComponentClassPathElements(cpList);
752: log("created component");
753:
754: // create a component classloader
755: ClassLoader compClassLoader = mClassLoaderFactory
756: .createComponentClassLoader(comp);
757: // remove the classloader
758: mClassLoaderFactory.removeComponentClassLoader(comp
759: .getName());
760:
761: // test if classloader exists.
762: //
763: mClassLoaderFactory.getComponentClassLoader(comp.getName());
764: fail(testName
765: + " failed--- did not remove component classloader");
766: } catch (JBIException je) {
767: // correct exception means failure
768: assertTrue(testName + ":"
769: + "failed by not removing classloader", -1 < je
770: .toString().indexOf(
771: "No component classloader found"));
772: log(testName + " passed OK");
773: }
774: }
775:
776: /**
777: * tests the successful removal of a component classloader containing
778: * shared libraries w/o an error
779: * @throws Exception if an unexpected error occurs
780: */
781: public void testRemoveComponentClassLoaderWithSharedLib()
782: throws Exception {
783: final String testName = "testRemoveComponentClassLoaderWithSharedLib";
784: try {
785: List cpList = new ArrayList();
786: String path1 = getTestJarsPath() + File.separator + "a.jar";
787: String path2 = getTestJarsPath() + File.separator + "b.jar";
788: String path3 = getTestClassPath();
789: String path4 = getTestResourcePath();
790: cpList.add(path1);
791: cpList.add(path2);
792: cpList.add(path3);
793: cpList.add(path4);
794: Component comp = new Component();
795: comp.setName(testName);
796: comp.setComponentClassPathElements(cpList);
797: log("created component");
798:
799: //create a valid shared library list
800: List sharedLibElements = new ArrayList();
801: String path5 = getTestJarsPath() + File.separator + "a.jar";
802: String path6 = getTestJarsPath() + File.separator + "b.jar";
803: sharedLibElements.add(path5);
804: sharedLibElements.add(path6);
805:
806: log("created shared library element list");
807:
808: //create a valid shared library id list
809: List sharedLibIds = new ArrayList();
810: String slid1 = "sharedLib6";
811: sharedLibIds.add(slid1);
812: // link namspace to component
813: comp.setSharedLibraryNames(sharedLibIds);
814:
815: // create shared library
816: SharedLibrary sharedLib = new SharedLibrary("sharedLib6",
817: "Jar files a and b", "/sharedLib6",
818: sharedLibElements);
819:
820: // create Shared Classloader
821: // as this needs to exist prior to a component that
822: // references it
823:
824: ClassLoader sharedClassLoader = mClassLoaderFactory
825: .createSharedClassLoader(sharedLib);
826: ClassLoader compClassLoader = mClassLoaderFactory
827: .createComponentClassLoader(comp);
828: // remove the classloader
829: mClassLoaderFactory.removeComponentClassLoader(comp
830: .getName());
831:
832: // test if classloader exists.
833: //
834: mClassLoaderFactory.getComponentClassLoader(comp.getName());
835: fail(testName
836: + " failed--- did not remove component classloader");
837: } catch (JBIException je) {
838: // correct exception means failure
839: assertTrue(testName + ":"
840: + "failed by not removing classloader", -1 < je
841: .toString().indexOf(
842: "No component classloader found"));
843: log(testName + " passed OK");
844: }
845: }
846:
847: /**
848: * tests that null argument passed to removeComponentClassLoader
849: * throws the correct exception.
850: * @throws Exception if an unexpected error occurs
851: */
852: public void testComponentClassLoaderNullComponent()
853: throws Exception {
854: final String testName = "testComponentClassLoaderNullComponent";
855: try {
856: // remove the classloader
857: mClassLoaderFactory.removeComponentClassLoader(null);
858:
859: fail(testName + " failed no exception thrown");
860: } catch (IllegalArgumentException e) {
861: // correct exception means failure
862: assertTrue(testName + ":"
863: + "failed by not removing classloader", -1 < e
864: .toString().indexOf(
865: "Null argument received for componentId"));
866: log(testName + " passed OK");
867: }
868: }
869:
870: /**
871: * tests that an invalid component id passed to removeSharedClassLoader
872: * throws the correct exception.
873: * @throws Exception if an unexpected error occurs
874: */
875: public void testRemoveComponentClassLoaderInvalidComponent()
876: throws Exception {
877: final String testName = "testRemoveComponentClassLoaderInvalidComponent";
878: try {
879: Component c = new Component();
880: c.setName("invalidcomponent");
881:
882: log("created component");
883:
884: // remove the classloader
885: mClassLoaderFactory.removeComponentClassLoader(c.getName());
886:
887: fail(testName + " failed no exception thrown");
888: } catch (JBIException je) {
889: // correct exception means failure
890: assertTrue(testName + ":" + "failed with wrong exception"
891: + je.toString(), -1 < je.toString().indexOf(
892: "No component classloader found for"));
893: log(testName + " passed OK");
894: }
895: }
896:
897: // ======================== general classloader factory utility test methods ==========================
898:
899: /**
900: * testgetInstance
901: * tests that only a single instance of the ClassLoaderFactory exists at a point in time
902: * @throws Exception if an unexpected error occurs
903: */
904: public void testgetInstance() throws Exception {
905: // create a new instance and compare it to the one already
906: // created in setup
907: ClassLoaderFactory cf1 = ClassLoaderFactory.getInstance();
908: ClassLoaderFactory cf2 = ClassLoaderFactory.getInstance();
909:
910: // java.lang.Object guarantees that 2 references that refer to the same object
911: // will always return true when Object.equals() is invoked on any of them with the other
912: // as the target.
913: assertEquals(cf1, cf2);
914: log("Test testgetInstance() passed:" + (cf1 == cf2));
915: }
916:
917: // ============================= private utility test methods ================================
918:
919: /*
920: * private logging method
921: */
922: private static void log(String msg) {
923: System.out.println("[TestClassLoaderFactory]-" + msg);
924: }
925:
926: /*
927: * location of the path to the unit test data JAR files
928: */
929: private String getTestJarsPath() {
930: return (CLUtils.getTestJarsPath(mSrcroot));
931: }
932:
933: /*
934: * location of the path to the unit test data class files
935: */
936: private String getTestClassPath() {
937: return (CLUtils.getTestClassPath(mSrcroot));
938: }
939:
940: /*
941: * location of the path to the unit test data resource files
942: */
943: private String getTestResourcePath() {
944: return (CLUtils.getTestResourcePath(mSrcroot));
945: }
946:
947: /*
948: * private logging method to indicate start of test
949: */
950: private void start(String testName) {
951: log(testName + "-" + "Started");
952: }
953:
954: /*
955: * private logging method to indicate normal start of test
956: */
957: private void endOK(String testName) {
958: log(testName + "-" + "Ended OK");
959: }
960:
961: /*
962: * private logging method to indicate end of test due to an exception
963: */
964: private void endException(String testName, Exception e) {
965: log(testName + "-" + "Ended With Following Exception");
966: log(e.toString());
967: }
968:
969: }
|