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: * @(#)TestDelegatingClassLoader.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:
034: import java.util.ArrayList;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Properties;
038:
039: import com.sun.jbi.JBIProvider;
040:
041: /**
042: * Tests for the various methods on the DelegatingClassLoader class.
043: *
044: * @author Sun Microsystems, Inc.
045: */
046: public class TestDelegatingClassLoader extends junit.framework.TestCase {
047: /**
048: * Value of the $SRCROOT environment variable
049: */
050: private String mSrcroot;
051:
052: /**
053: * ClassLoaderFactory
054: */
055: private ClassLoaderFactory mClassLoaderFactory;
056:
057: /**
058: * EnvironmentContext
059: */
060: private EnvironmentContext mEnvironmentContext;
061:
062: /**
063: * The constructor for this testcase, forwards the test name to
064: * the jUnit TestCase base class.
065: * @param aTestName String with the name of this test.
066: */
067: public TestDelegatingClassLoader(String aTestName) {
068: super (aTestName);
069: }
070:
071: /**
072: * Setup for the test. This creates the ClassLoaderFactory instance
073: * and other objects needed for the tests.
074: * @throws Exception when set up fails for any reason.
075: */
076: public void setUp() throws Exception {
077: super .setUp();
078: mSrcroot = System.getProperty("junit.srcroot");
079: mEnvironmentContext = new EnvironmentContext(
080: new ScaffoldPlatformContext(), new JBIFramework(),
081: new Properties());
082: mClassLoaderFactory = ClassLoaderFactory.getInstance();
083: }
084:
085: /**
086: * Cleanup for the test.
087: * @throws Exception when tearDown fails for any reason.
088: */
089: public void tearDown() throws Exception {
090: super .tearDown();
091: }
092:
093: // ===================== Component classloader test methods ==================
094:
095: /**
096: * testDCLAddingSharedNameLoaders.
097: * tests the adding of Shared Classloaders to the Delegating Classloader.
098: */
099: public void testDCLAddingSharedNameLoaders() {
100: String testName = "testDCLAddingSharedNameLoaders";
101: DelegatingClassLoader dcl = new DelegatingClassLoader(null);
102: try {
103: //create a valid sharednamespace
104: List sharedLibList = new ArrayList();
105: List sharedLibElements = new ArrayList();
106: String path = getTestJarsPath() + File.separator + "a.jar";
107: sharedLibElements.add(path);
108: SharedLibrary sharedLib = new SharedLibrary("sl2",
109: "Test namespace", "/sl2", sharedLibElements);
110: log("created sharednamespace");
111:
112: // create a shared classloader
113: ClassLoader sharedCL = mClassLoaderFactory
114: .createSharedClassLoader(sharedLib);
115: // add it to the delegating classloader
116: dcl.addSharedClassLoader(sharedCL);
117: log(testName + ":passed");
118: } catch (Exception e) {
119: fail(testName + ":error adding shared classloader to dcl");
120: }
121: }
122:
123: /**
124: * testDCLAddingSharedNameLoadersNullCL
125: * tests the adding of null Shared Classloaders to the Delegating Classloader
126: * @throws Exception if an unexpected error occurs
127: */
128: public void testDCLAddingSharedNameLoadersNullCL() {
129: String testName = "testDCLAddingSharedNameLoadersNullCL";
130: DelegatingClassLoader dcl = new DelegatingClassLoader(null);
131: try {
132: // create a shared classloader
133: ClassLoader sharedCL = mClassLoaderFactory
134: .createSharedClassLoader(null);
135: // add it to the delegating classloader
136: dcl.addSharedClassLoader(sharedCL);
137: fail(testName + ":error adding shared classloader to dcl");
138: } catch (Exception e) {
139: log(testName + ":passed");
140: assertTrue("Wrong Exception caught:" + e.toString(),
141: (-1 < e.getMessage().indexOf("Null argument")));
142: }
143: }
144:
145: /**
146: * testDCLLoadClass
147: * tests the loading of classes corresponding to different classloaders
148: * added to a DelegatingClassLoader
149: * @throws Exception if an unexpected error occurs
150: */
151: public void testDCLLoadClass() {
152: String testName = "testDCLLoadClass";
153: DelegatingClassLoader dcl = new DelegatingClassLoader(null);
154: try {
155: //create a valid sharednamespace
156: List sharedLibList1 = new ArrayList();
157: List sharedLibElements1 = new ArrayList();
158: String path1 = getTestJarsPath() + File.separator + "a.jar";
159: sharedLibElements1.add(path1);
160: SharedLibrary sharedLib1 = new SharedLibrary("sl11",
161: "Jar file A", "/sl11", sharedLibElements1);
162: log("created sharednamespace1");
163:
164: // create a shared classloader(1)
165: ClassLoader sharedCL1 = mClassLoaderFactory
166: .createSharedClassLoader(sharedLib1);
167: //create a valid sharednamespace
168: List sharedLibList2 = new ArrayList();
169: List sharedLibElements2 = new ArrayList();
170: String path2 = getTestJarsPath() + File.separator + "b.jar";
171: sharedLibElements2.add(path2);
172: SharedLibrary sharedLib2 = new SharedLibrary("sl12",
173: "Jar file B", "/sl12", sharedLibElements2);
174: log("created sharednamespace2");
175:
176: // create a shared classloader(2)
177: ClassLoader sharedCL2 = mClassLoaderFactory
178: .createSharedClassLoader(sharedLib2);
179:
180: // add the above classloaders to the DCL
181: dcl.addSharedClassLoader(sharedCL1);
182: dcl.addSharedClassLoader(sharedCL2);
183:
184: // load classes from a.jar and b.jar using the DCL
185: Class testBJarClass = dcl.loadClass("engine1.Bootstrap1");
186: Object testBJarObj = testBJarClass.newInstance();
187: log("loaded engine");
188: Class testAJarClass = dcl.loadClass("binding1.Bootstrap1");
189: Object testAJarObj = testAJarClass.newInstance();
190: log("loaded binding");
191: log(testName + " passed OK");
192: } catch (Exception e) {
193: CLUtils.dumpStackTrace(e);
194: log(testName + ":failed");
195: fail(testName + " failed with exception:" + e.getMessage());
196: }
197: }
198:
199: /**
200: * testCCLLoadJBIClassNoSharedLib
201: * tests the loading of a JBI class by the DelegatingClassLoader
202: * when there is no Shared Lib (and hence no shared class loader)
203: * this tests the implementation of loadClass designed for exactly
204: * this purpose.
205: * @throws Exception if an unexpected error occurs
206: *
207: */
208: public void testCCLLoadJBIClassNoSharedLib()
209:
210: {
211: String testName = "testCCLLoadJBIClassNoSharedLib";
212: String className = "com.sun.jbi.ServiceLifecycle";
213:
214: try {
215: DelegatingClassLoader dcl = new DelegatingClassLoader(null);
216: Class jbiClass = dcl.loadClass(className);
217: log(className + " loaded by :" + jbiClass.getClassLoader());
218: log(testName + " Passed");
219: } catch (Exception e) {
220: fail(testName + " failed with exception:" + e.toString());
221: }
222:
223: }
224:
225: /**
226: * testDCLLoadClassWithSharedLibSelfFirstTrue
227: * tests the loading of classes corresponding to different classloaders
228: * added to a DelegatingClassLoader.
229: * The "selfFirst" Flag is TRUE at the SharedLibrary Level
230: * @throws Exception if an unexpected error occurs
231: */
232: public void testDCLLoadClassWithSharedLibSelfFirstTrue() {
233: String testName = "testDCLLoadClassWithSharedLibSelfFirstTrue";
234: boolean selfFirst = true; // selfFirst = true
235: String expectedClassLoader = "CustomClassLoader";
236: String className = "com.sun.workflow.classes.Utils";
237: String library = "utilsv2.jar";
238: testDCLLoadClassWithSharedLib(testName, selfFirst,
239: expectedClassLoader, className, library);
240: }
241:
242: /**
243: * testDCLLoadClassWithSharedLibSelfFirstFalse
244: * tests the loading of classes corresponding to different classloaders
245: * added to a DelegatingClassLoader.
246: * The "selfFirst" Flag is FALSE at the SharedLibrary Level
247: * @throws Exception if an unexpected error occurs
248: */
249: public void testDCLLoadClassWithSharedLibSelfFirstFalse() {
250: String testName = "testDCLLoadClassWithSharedLibSelfFirstFalse";
251: boolean selfFirst = false; // selfFirst = false
252: String expectedClassLoader = "sun.misc";
253: String className = "com.sun.workflow.classes.Utils";
254: String library = "utilsv2.jar";
255: testDCLLoadClassWithSharedLib(testName, selfFirst,
256: expectedClassLoader, className, library);
257: }
258:
259: /**
260: * testDCLLoadClassWithPrivateSelfFirstFalseSharedLibSelfFirstTrue
261: * tests if a class is loaded correctly by the shared loader when the
262: * same class is present in the component private path , shared lib ,
263: * and the parent of the shared lib (system class loader) .
264: *
265: * The "selfFirst" Flag is TRUE at the SharedLibrary Level
266: * The "parentFirst" Flag is TRUE at the Component Level
267: *`
268: * @throws Exception if an unexpected error occurs
269: */
270: public void testDCLLoadClassWithPrivateSelfFirstFalseSharedLibSelfFirstTrue() {
271: String testName = "testDCLLoadClassWithPrivateAndSharedLibSelfFirstTrue";
272: try {
273: start(testName);
274: boolean componentSelfFirst = false; // parentFirst = true
275: boolean sLselfFirst = true; // selfFirst = true
276:
277: // create shared lib (selfFirst = true)
278: // and associated delegating class loader
279: List libElements = new ArrayList(); // component class path
280:
281: String libName = "utils.jar";
282: libElements.add(libName);
283:
284: String root = "/sl13";
285:
286: // create Shared Library
287: SharedLibrary sl = createSharedLibrary("sl13", libElements,
288: root, sLselfFirst);
289:
290: // create a shared class loader for the above shared lib
291: ClassLoader scL = mClassLoaderFactory
292: .createSharedClassLoader(sl);
293:
294: // add to shared lib list
295: List slList = new ArrayList();
296: slList.add(sl.getName());
297:
298: // create component (parentFirst) with shared lib as parent
299: Component comp = createComponent("comp0001", libElements);
300: comp.setSharedLibraryNames(slList);
301: comp.setComponentClassLoaderSelfFirst(componentSelfFirst);
302:
303: // create a class loader for the above component
304: ClassLoader ccL = mClassLoaderFactory
305: .createComponentClassLoader(comp);
306:
307: String className = "com.sun.workflow.classes.Utils";
308: Class loadedClass = ccL.loadClass(className);
309: String expectedClassLoader = "CustomClassLoader";
310:
311: String actualClassLoader = loadedClass.getClassLoader()
312: .toString();
313: log(testName + " actual ClassLoader = " + actualClassLoader);
314: if (actualClassLoader.indexOf(expectedClassLoader) == -1) {
315: fail("Class :" + className
316: + " was loaded by wrong class loader:"
317: + loadedClass.getClassLoader().toString());
318: }
319:
320: log(testName + " Passed");
321: } catch (Exception e) {
322: CLUtils.dumpStackTrace(e);
323: fail(testName + " Failed due to " + e.toString());
324: }
325: }
326:
327: /**
328: * testDCLLoadClassWithSharedLib
329: * tests the loading of classes java.xxx class
330: * @throws Exception if an unexpected error occurs
331: */
332: private void testDCLLoadClassWithSharedLib(String testName,
333: boolean selfFirst, String expectedClassLoader,
334: String className, String library) {
335:
336: DelegatingClassLoader dcl = new DelegatingClassLoader(null);
337: try {
338: //create a valid sharednamespace
339: List sharedLibList1 = new ArrayList();
340: List sharedLibElements1 = new ArrayList();
341: String path1 = getTestJarsPath() + File.separator + library;
342: sharedLibElements1.add(path1);
343: String slName = testName + "SL";
344: SharedLibrary sharedLib1 = new SharedLibrary(testName,
345: "UtilsV2", "/" + slName, selfFirst,
346: sharedLibElements1);
347: log("created sharednamespace1");
348:
349: // create a shared classloader(1)
350: ClassLoader sharedCL1 = mClassLoaderFactory
351: .createSharedClassLoader(sharedLib1);
352:
353: // add the above classloaders to the DCL
354: dcl.addSharedClassLoader(sharedCL1);
355:
356: // load classes from utilsv2.jar using DCL
357: Class testUtilsClass = dcl.loadClass(className);
358: Object testUtilsObj = testUtilsClass.newInstance();
359: log("loaded:" + className);
360:
361: String loader = testUtilsClass.getClassLoader().toString();
362:
363: if (loader.indexOf(expectedClassLoader) != -1) {
364: log(testName + " passed OK");
365: } else {
366: fail(testName + " failed - wrong class loader");
367: }
368: } catch (Exception e) {
369: CLUtils.dumpStackTrace(e);
370: log(testName + ":failed");
371: fail(testName + " failed with exception:" + e.toString());
372: }
373: }
374:
375: // creates a shared library and a
376: // class loader associated with it
377: //
378: private SharedLibrary createSharedLibrary(String name,
379: List sharedJars, String root, boolean selfFirst)
380: throws Exception {
381: DelegatingClassLoader dcl = new DelegatingClassLoader(null);
382: try {
383: // create a valid sharednamespace
384: List sharedLibList = new ArrayList();
385: List sharedLibElements = new ArrayList();
386:
387: for (Iterator i = sharedJars.iterator(); i.hasNext();) {
388: String lib = (String) i.next();
389: String path = getTestJarsPath() + File.separator + lib;
390: sharedLibElements.add(path);
391: }
392:
393: SharedLibrary sharedLib = new SharedLibrary(name,
394: "Test namespace", root, selfFirst,
395: sharedLibElements);
396: log("created sharednamespace");
397:
398: // create a shared classloader
399: /*ClassLoader sharedCL = mClassLoaderFactory.createSharedClassLoader
400: (sharedLib) ;
401: // add it to the delegating classloader
402: dcl.addSharedClassLoader(sharedCL); */
403: log("created sharednamespace");
404: return sharedLib;
405: } catch (Exception e) {
406: log(e.toString());
407: throw e;
408: }
409: }
410:
411: // create and return a component
412: //
413: Component createComponent(String name, List jars) {
414: List cpList = new ArrayList();
415:
416: for (Iterator i = jars.iterator(); i.hasNext();) {
417: String libName = (String) i.next();
418: String path = getTestJarsPath() + File.separator + libName;
419: cpList.add(path);
420: }
421:
422: Component comp = new Component();
423: comp.setName(name);
424: comp.setComponentClassPathElements(cpList);
425: log("created component");
426: return comp;
427: }
428:
429: /**
430: * private logging method
431: */
432: private static void log(String msg) {
433: CLUtils.log("[TestDelegatingClassLoader]-" + msg);
434: }
435:
436: /**
437: * private logging method
438: */
439: private String getTestJarsPath() {
440: return (CLUtils.getTestJarsPath(mSrcroot));
441: }
442:
443: /*
444: * private logging method to indicate start of test
445: */
446: private void start(String testName) {
447: log(testName + "-" + "Started");
448: }
449:
450: /*
451: * private logging method to indicate normal start of test
452: */
453: private void endOK(String testName) {
454: log(testName + "-" + "Ended OK");
455: }
456:
457: /*
458: * private logging method to indicate end of test due to an exception
459: */
460: private void endException(String testName, Exception e) {
461: log(testName + "-" + "Ended With Following Exception");
462: log(e.toString());
463: }
464: }
|