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: * @(#)TestCustomClassLoader.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:
044: import com.sun.jbi.JBIProvider;
045:
046: /**
047: * Tests for the various methods on the CustomClassLoader class.
048: *
049: * @author Sun Microsystems, Inc.
050: */
051: public class TestCustomClassLoader extends junit.framework.TestCase {
052: /**
053: * Value of the $SRCROOT environment variable
054: */
055: private String mSrcroot;
056:
057: /**
058: * ClassLoaderFactory
059: */
060: private CustomClassLoader mCustomClassLoader;
061:
062: /**
063: * EnvironmentContext
064: */
065: private EnvironmentContext mEnvironmentContext;
066:
067: /**
068: * The constructor for this testcase, forwards the test name to
069: * the jUnit TestCase base class.
070: * @param aTestName String with the name of this test.
071: */
072: public TestCustomClassLoader(String aTestName) {
073: super (aTestName);
074: }
075:
076: /**
077: * Setup for the test. This creates the ClassLoaderFactory instance
078: * and other objects needed for the tests.
079: * @throws Exception when set up fails for any reason.
080: */
081: public void setUp() throws Exception {
082: super .setUp();
083: mSrcroot = System.getProperty("junit.srcroot");
084: mEnvironmentContext = new EnvironmentContext(
085: new ScaffoldPlatformContext(), new JBIFramework(),
086: new Properties());
087: }
088:
089: /**
090: * Cleanup for the test.
091: * @throws Exception when tearDown fails for any reason.
092: */
093: public void tearDown() throws Exception {
094: super .tearDown();
095: }
096:
097: // ===================== Custom classloader test methods ==================
098:
099: /**
100: * testCreateCustomClassLoader
101: * tests the creation of a CustomClassloader with the System Classloader
102: * as the parent and the "selfFirst" flag set to 'false'
103: * @throws Exception if an unexpected error occurs
104: */
105: public void testCreateCustomClassLoader() {
106: String testName = "testCreateCustomClassLoader";
107: try {
108: // create a set of URLs for this loader
109: String path = getTestJarsPath() + File.separator + "a.jar";
110: List l = new ArrayList();
111: l.add(path);
112: URL[] urls = CLUtils.list2URLArray(l);
113:
114: // parent of current classloader = null --> System classloader
115: ClassLoader parent = null;
116: boolean selfFirst = false;
117:
118: // create a custom classloader
119: CustomClassLoader ccl = new CustomClassLoader(urls, parent,
120: selfFirst);
121: log("created custom classloader with selfFirst=false");
122:
123: log(testName + ":passed");
124: } catch (Exception e) {
125: fail(testName + ":error creating custom classloader");
126: }
127: }
128:
129: /**
130: * testCCLLoadSystemClassSelfFirstFalse
131: * tests the loading of a class with the system classloader
132: * as the parent and the "selfFirst" flag set to 'false'
133: * <code>java.lang.String</code> is not overloaded locally
134: * @throws Exception if an unexpected error occurs
135: */
136: public void testCCLLoadSystemClassSelfFirstFalse() {
137:
138: String testName = "testCCLLoadSystemClassSelfFirstFalse";
139: String className = "java.util.HashMap"; // J2SE Class
140: boolean selfFirst = false;
141:
142: try {
143: // create a set of URLs for this loader
144: String path = getTestJarsPath() + File.separator + "a.jar";
145: List l = new ArrayList();
146: l.add(path);
147: URL[] urls = CLUtils.list2URLArray(l);
148:
149: // create Custom ClassLoader instance
150: CustomClassLoader ccL = new CustomClassLoader(urls, null,
151: selfFirst);
152: try {
153: Class c = ccL.loadClass(className);
154: log(testName + " Class :" + className
155: + " was loaded by:" + c.getClassLoader());
156:
157: // The JVM Bootstrap ClassLoader should load "java.XXX.XXX
158: // classes . Sun JVM impl represents this as null
159: // @see java.lang.ClassLoader
160: if (c.getClassLoader() == null) {
161:
162: log(testName + ":passed");
163: } else {
164: fail(testName + className
165: + " was loaded with wrong"
166: + " classloader =" + c.getClassLoader());
167: }
168: } catch (ClassNotFoundException cnfe) {
169: fail(testName + ":error loading class:" + className);
170: }
171: } catch (Exception e) {
172: fail(testName + e.toString());
173:
174: }
175: }
176:
177: /**
178: * testCCLLoadSystemClassSelfFirstTrue
179: * tests the loading of a class with the system classloader
180: * as the parent and the "selfFirst" flag set to 'true'
181: * <code>java.lang.String</code> is not overloaded locally
182: * @throws Exception if an unexpected error occurs
183: */
184: public void testCCLLoadSystemClassSelfFirstTrue() {
185:
186: String testName = "testCCLLoadClassSelfFirstTrue";
187: String className = "java.util.HashMap"; // J2SE Class
188: boolean selfFirst = true;
189:
190: try {
191: // create a set of URLs for this loader
192: String path = getTestJarsPath() + File.separator + "a.jar";
193: List l = new ArrayList();
194: l.add(path);
195: URL[] urls = CLUtils.list2URLArray(l);
196:
197: // create Custom ClassLoader instance
198: CustomClassLoader ccL = new CustomClassLoader(urls, null,
199: selfFirst);
200: try {
201: Class c = ccL.loadClass(className);
202: log(testName + " Class :" + className
203: + " was loaded by:" + c.getClassLoader());
204:
205: // The JVM Bootstrap ClassLoader should load "java.XXX.XXX
206: // classes . Sun JVM impl represents this as null
207: // @see java.lang.ClassLoader
208: if (c.getClassLoader() == null) {
209:
210: log(testName + ":passed");
211: } else {
212: fail(testName + className
213: + " was loaded with wrong"
214: + " classloader =" + c.getClassLoader());
215: }
216: } catch (ClassNotFoundException cnfe) {
217: fail(testName + ":error loading class:" + className);
218: }
219: } catch (Exception e) {
220: fail(testName + e.toString());
221:
222: }
223: }
224:
225: /**
226: * testCCLLoadLocalClassSelfFirstFalse
227: * tests the loading of a class with the system classloader
228: * as the parent and the "selfFirst" flag set to 'false'
229: * The class is visible to both the current as well as the
230: * parent (system) classloader (workflow2.jar is in the ANT
231: * class path) .
232: * The System (Ant) class loader should load this class .
233: * @throws Exception if an unexpected error occurs
234: */
235: public void testCCLLoadLocalClassSelfFirstFalse() {
236: String testName = "testCCLLoadLocalClassSelfFirstFalse";
237: boolean selfFirst = false;
238: String expectedClassLoader = "sun.misc";
239: String className = "com.sun.workflow.classes.Utils"; // Custom class
240: String library = "utilsv2.jar"; // Custom library
241:
242: testCCLLoadLocalClass(testName, selfFirst, expectedClassLoader,
243: library, className);
244: }
245:
246: /**
247: * testCCLLoadLocalClassSelfFirstTrue
248: * tests the loading of a class with the system classloader
249: * as the parent and the "selfFirst" flag set to 'true'
250: * The class is visible to both the current as well as the
251: * parent (system) classloader (workflow2.jar is in the ANT
252: * class path) .
253: * The Custom class loader should load this class .
254: * @throws Exception if an unexpected error occurs
255: */
256: public void testCCLLoadLocalClassSelfFirstTrue() {
257: String testName = "testCCLLoadLocalClassSelfFirstTrue";
258: boolean selfFirst = true;
259: String expectedClassLoader = "CustomClassLoader";
260: String className = "com.sun.workflow.classes.Utils"; // Custom class
261: String library = "utilsv2.jar"; // Custom library
262:
263: testCCLLoadLocalClass(testName, selfFirst, expectedClassLoader,
264: library, className);
265: }
266:
267: /**
268: * testCCLLoadLocalClass
269: * tests the loading of a class from a library (JAR) with a specific
270: * class name , value of "selfFirst" . It matches
271: * the classloader used to load the class with the one
272: * passed in and accordingly passes/fails the test .
273: * @throws Exception if an unexpected error occurs
274: */
275: private void testCCLLoadLocalClass(String testName,
276: boolean selfFirst, String expectedClassLoader,
277: String library, String className)
278:
279: {
280: //String className = "com.sun.workflow.classes.Utils" ; // Custom class
281: // Classes in this local JAR file(utilsv2.jar) are duplicates
282: // of classes found in the System (ANT) class path i.e. utils.jar
283: //String library = "utilsv2.jar" ;
284:
285: try {
286: // create a set of URLs for this loader
287: String path = getTestJarsPath() + File.separator + library;
288: List l = new ArrayList();
289: l.add(path);
290: URL[] urls = CLUtils.list2URLArray(l);
291:
292: // create Custom ClassLoader instance
293: CustomClassLoader ccL = new CustomClassLoader(urls, null,
294: selfFirst);
295: try {
296: Class c = ccL.loadClass(className);
297: log(testName + " Class :" + className
298: + " was loaded by:" + c.getClassLoader());
299:
300: ClassLoader cLoader = c.getClassLoader();
301:
302: if (cLoader != null) {
303: String clName = cLoader.toString();
304: if (clName.indexOf(expectedClassLoader) != -1) {
305: log(testName + ":passed");
306: } else {
307: fail(testName + className
308: + " was loaded with wrong"
309: + " classloader =" + clName);
310: }
311: }
312: // Sun JVM Bootstrap class loader is NULL
313: else if (expectedClassLoader == null) {
314: // if both current , expected class loaders are NULL
315: // we are probably dealing with the loading of a core
316: // class such as java.lang.String
317:
318: log(testName + ":passed");
319: }
320: } catch (ClassNotFoundException cnfe) {
321: fail(testName + ":error loading class:" + className);
322: }
323: } catch (Exception e) {
324: CLUtils.dumpStackTrace(e);
325: fail(testName + e.toString());
326:
327: }
328: }
329:
330: /**
331: * testCCLFilterJavaClassSelfFirstTrue
332: * tests the loading of a java.XXX class with the system classloader
333: * as the parent and the "selfFirst" flag set to 'true'
334: * The parent class loader should load this class even though the class
335: * has been overloaded and is made available in the local Class Path.
336: * This local version should never be loaded .
337: * @throws Exception if an unexpected error occurs
338: */
339: public void testCCLFilterJavaClassSelfFirstTrue() {
340: String testName = "testCCLFilterJavaClassSelfFirstTrue";
341: boolean selfFirst = true;
342: // the JVM bootstrap class loader is represented as NULL
343: // in the Sun JVM
344: String expectedStrClassLoader = null;
345: String classNameJavaStr = "java.lang.String";
346: String classNameJavaXStr = "javax.swing.Action";
347:
348: // DummyMarkerCLTest - marker class inside the JAR file to make sure
349: // the JAR file is actually being used . Else java.lang.String
350: // could still get loaded by the correct loader but the test would
351: // not be True test of filtering
352: String classNameDummy = "DummyMarkerCLTest";
353: String expectedDummyClassLoader = "Custom";
354:
355: String javaLibrary = "mystring.jar"; // contains java.lang.String
356: String javaxLibrary = "myjavax.jar"; // contains javax.swing.Action
357:
358: // if this bombs with a ClassNotFoundException, that means
359: // that "mystring.jar" is not really being used .
360: testCCLLoadLocalClass(testName, selfFirst,
361: expectedDummyClassLoader, javaLibrary, classNameDummy);
362:
363: // check java.XXX classes
364: testCCLLoadLocalClass(testName, selfFirst,
365: expectedStrClassLoader, javaLibrary, classNameJavaStr);
366:
367: log(testName + " Java.XX test Passed");
368:
369: // check javax.XXX classes
370: testCCLLoadLocalClass(testName, selfFirst,
371: expectedStrClassLoader, javaxLibrary, classNameJavaXStr);
372:
373: log(testName + " JavaX.XX test Passed");
374: }
375:
376: /**
377: * testCCLFilterWSDLSelfFirstTrue
378: * tests the loading of a java.XXX class with the system classloader
379: * as the parent and the "selfFirst" flag set to 'true'
380: * The Custom class loader should load this class since this is a
381: * case of a javax.XXX class not being available in the parent's
382: * classloader.
383: * @throws Exception if an unexpected error occurs
384: */
385: public void testCCLFilterWSDLClassSelfFirstTrue() {
386: String testName = "testCCLFilterWSDLClassSelfFirstTrue";
387: boolean selfFirst = true;
388: String expectedStrClassLoader = "CustomClassLoader";
389: String classNameJavaXStr = "javax.my.wsdl.xml.WSDLLocator";
390: String javaxLibrary = "ws.jar"; //contains javax.my.wsdl.xml.WSDLLocator
391:
392: /* NB -- commented on on 2/16/2006 so that all junit tests pass.
393: * Raghavan Srinivasan has promised to (help) fix this test when he
394: * gets back from vacation. -- MYK
395: // check javax.wsdl classes
396: testCCLLoadLocalClass ( testName , selfFirst ,
397: expectedStrClassLoader, javaxLibrary, classNameJavaXStr) ;
398: */
399:
400: log(testName + " JavaX.wsdl.XX test Passed");
401: }
402:
403: /**
404: * testCCLFilterJavaClassSelfFirstFalse
405: * tests the loading of a java.XXX class with the system classloader
406: * as the parent and the "selfFirst" flag set to 'false'
407: * The Custom class loader should load this class . The version of class
408: * has been overloaded and is made available in the local Class Path.
409: * @throws Exception if an unexpected error occurs
410: */
411: /*public void testCCLFilterJavaClassSelfFirstFalse()
412: {
413: String testName = "testCCLFilterJavaClassSelfFirstFalse";
414: boolean selfFirst = false ;
415: // the JVM bootstrap class loader is represented as NULL
416: // in the Sun JVM
417: String expectedStrClassLoader = null ;
418: String classNameJavaStr = "java.lang.String";
419: String classNameJavaXStr = "javax.swing.Action";
420:
421: // DummyMarkerCLTest - marker class inside the JAR file to make sure
422: // the JAR file is actually being used . Else java.lang.String
423: // could still get loaded by the correct loader but the test would
424: // not be True test of filtering
425: String classNameDummy = "DummyMarkerCLTest";
426: String expectedDummyClassLoader = "Custom" ;
427:
428: String library = "mystring.jar" ; // Custom library
429:
430: // if this bombs with a ClassNotFoundException, that means
431: // that "mystring.jar" is not really being used .
432: testCCLLoadLocalClass ( testName , selfFirst ,
433: expectedDummyClassLoader, library, classNameDummy) ;
434:
435: // check java.XXX classes
436: testCCLLoadLocalClass ( testName , selfFirst ,
437: expectedStrClassLoader, library, classNameJavaStr) ;
438:
439: log (testName + " Java.XX test Passed") ;
440:
441: // check javax.XXX classes
442: testCCLLoadLocalClass ( testName , selfFirst ,
443: expectedStrClassLoader, library, classNameJavaXStr) ;
444: log (testName + " JavaX.XX test Passed") ;
445: } */
446:
447: /**
448: * testCCLLoadLocalResource
449: * tests the loading of a resource from a library (JAR) with a specific
450: * class name , value of "selfFirst". It matches
451: * the classloader used to load the class with the one
452: * passed in and accordingly passes/fails the test .
453: * @throws Exception if an unexpected error occurs
454: */
455: private void testCCLLoadLocalResource(String testName,
456: boolean selfFirst, String key, String expectedVal,
457: String library, String resourceName)
458:
459: {
460: // Resources in this local JAR file(resources.jar) are duplicates
461: // of classes found in the System (ANT) class path i.e. resources.jar
462:
463: try {
464: // create a set of URLs for this loader
465: String path = getTestResourcePath();
466: List l = new ArrayList();
467: l.add(path);
468: if (library != null) {
469: String lPath = getTestJarsPath()
470: + java.io.File.separator + library;
471: System.err.println("LPATH=" + lPath);
472: l.add(lPath);
473: }
474:
475: URL[] urls = CLUtils.list2URLArray(l);
476:
477: // create Custom ClassLoader instance
478: CustomClassLoader ccL = new CustomClassLoader(urls, null,
479: selfFirst);
480: InputStream i = ccL.getResourceAsStream(resourceName);
481: if (i == null) {
482: fail("Resource:" + resourceName + " was not loaded");
483: }
484:
485: Properties p = new Properties();
486: p.load(i);
487:
488: String keyVal = p.getProperty(key);
489: log(testName + "Resource Val:" + key + "=" + keyVal);
490:
491: boolean failMe = false;
492:
493: // Java Bootstrap classloader is represented
494: // as null
495: if (keyVal == null) {
496: if (expectedVal == null) {
497: // test passes
498: } else // expectedVal == false
499: {
500: failMe = true;
501: }
502: } else // keyVal !=null
503: {
504: if (expectedVal == null) {
505: failMe = true;
506: } else // expectedVal !=null
507: {
508: failMe = !(keyVal.equals(expectedVal));
509: }
510: }
511:
512: if (failMe) {
513: fail(testName + resourceName + " was loaded with wrong"
514: + " classloader:" + keyVal);
515: }
516:
517: log(testName + " Resource :" + resourceName + " was loaded");
518: log(testName + ":passed");
519: } catch (Exception e) {
520: CLUtils.dumpStackTrace(e);
521: fail(testName + ":error loading class:" + resourceName
522: + " due to:" + e.toString());
523: ;
524:
525: }
526: }
527:
528: /**
529: * testCCLLoadLocalResourceSelfFirstTrue
530: * tests the loading of a resource with the system classloader
531: * as the parent and the "selfFirst" flag set to 'true'
532: * The resource is visible to both the current as well as the
533: * parent (system) classloader (resources.jar is in the ANT
534: * class path) .
535: * The Custom class loader should load this resource .
536: * @throws Exception if an unexpected error occurs
537: */
538: public void testCCLLoadLocalResourceSelfFirstTrue() {
539: String testName = "testCCLLoadLocalResourceSelfFirstTrue";
540: boolean selfFirst = true;
541: String key = "PHONE_NUMBER";
542: String expectedVal = "(###)###-####";
543: String resName = "resources.properties"; // Custom resource
544: String library = null; // Custom library
545:
546: testCCLLoadLocalResource(testName, selfFirst, key, expectedVal,
547: library, resName);
548: }
549:
550: /**
551: * testCCLLoadLocalResourceSelfFirstFalse
552: * tests the loading of a resource with the system classloader
553: * as the parent and the "selfFirst" flag set to 'false'
554: * The resource is visible to both the current as well as the
555: * parent (system) classloader (resources.jar is in the ANT
556: * class path) .
557: * The System (sun.misc.XXX) class loader should load this resource .
558: * @throws Exception if an unexpected error occurs
559: */
560: public void testCCLLoadLocalResourceSelfFirstFalse() {
561: String testName = "testCCLLoadLocalResourceSelfFirstFalse";
562: boolean selfFirst = false;
563: String key = "PHONE_NUMBER";
564: String expectedVal = null;
565: String resName = "resources.properties"; // Custom resource
566: String library = null; // Custom library
567:
568: testCCLLoadLocalResource(testName, selfFirst, key, expectedVal,
569: library, resName);
570: }
571:
572: /**
573: * testCCLLoadLocalJavaxResourceSelfFirstFalse
574: * tests the loading of a resource with the system classloader
575: * as the parent and the "selfFirst" flag set to 'false'.
576: * The resource is present in the component class path only.
577: * The resource is a javax resource which means that the
578: * parent-first is always enforced.
579: *
580: * The Custom class loader should load this class .
581: * @throws Exception if an unexpected error occurs
582: */
583: public void testCCLLoadLocalJavaxResourceSelfFirstFalse() {
584: String testName = "testCCLLoadLocalJavaxResourceSelfFirstFalse";
585: boolean selfFirst = false;
586: String key = "PRODUCT_NAME";
587: String expectedVal = "JBI";
588: String resName = "javax/test/resources.properties"; // Custom resource
589: String library = "image4j.jar"; // Custom library
590:
591: testCCLLoadLocalResource(testName, selfFirst, key, expectedVal,
592: library, resName);
593: }
594:
595: /**
596: * testCCLLoadLocalJavaxResourceSelfFirstTrue
597: * tests the loading of a resource with the system classloader
598: * as the parent and the "selfFirst" flag set to 'true'.
599: * The resource is present in the component class path only.
600: * The resource is a javax resource which means that the
601: * parent-first is always enforced.
602: *
603: * The Custom class loader should load this class .
604: * @throws Exception if an unexpected error occurs
605: */
606: public void testCCLLoadLocalJavaxResourceSelfFirstTrue() {
607: String testName = "testCCLLoadLocalJavaxResourceSelfFirstTrue";
608: boolean selfFirst = true;
609: String key = "PRODUCT_NAME";
610: String expectedVal = "JBI";
611: String resName = "javax/test/resources.properties"; // Custom resource
612: String library = "image4j.jar"; // Custom library
613:
614: testCCLLoadLocalResource(testName, selfFirst, key, expectedVal,
615: library, resName);
616: }
617:
618: //-------------------------------------------------
619: // private methods -- refactor into Util class
620: //
621: /*
622: * private logging method
623: */
624: private static void log(String msg) {
625: System.out.println("[TestCustomClassLoader]-" + msg);
626: }
627:
628: /*
629: * private method
630: */
631: private String getTestJarsPath() {
632: return (CLUtils.getTestJarsPath(mSrcroot));
633: }
634:
635: /*
636: * private method
637: */
638: private String getTestResourcePath() {
639: return (CLUtils.getTestResourcePath(mSrcroot));
640: }
641:
642: /*
643: * private logging method to indicate start of test
644: */
645: private void start(String testName) {
646: log(testName + "-" + "Started");
647: }
648:
649: /*
650: * private logging method to indicate normal start of test
651: */
652: private void endOK(String testName) {
653: log(testName + "-" + "Ended OK");
654: }
655:
656: /*
657: * private logging method to indicate end of test due to an exception
658: */
659: private void endException(String testName, Exception e) {
660: log(testName + "-" + "Ended With Following Exception");
661: log(e.toString());
662: }
663:
664: private String dump(Class c) {
665: String source = c.getProtectionDomain().getCodeSource()
666: .toString();
667: System.out.println("Class :" + c + " was loaded from :"
668: + source);
669: return source;
670: }
671: }
|