001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.classloader;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040:
041: import java.net.URL;
042: import java.security.SecureClassLoader;
043:
044: /**
045: * Test cases for {@link StickyClassLoader}.
046: *
047: * @version $Id: StickyClassLoaderTest.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: public class StickyClassLoaderTest extends DrJavaTestCase {
050: private final String myName = getClass().getName();
051: private final ClassLoader myLoader = getClass().getClassLoader();
052:
053: /**
054: * Make sure getClass().getClassLoader() sticks, regardless of where
055: * the class data came from.
056: */
057: public void testLoaderSticks() throws Throwable {
058: StickyClassLoader loader = new StickyClassLoader(myLoader,
059: myLoader);
060:
061: Class c = loader.loadClass(myName);
062: assertEquals("getClassLoader()", loader, c.getClassLoader());
063: assertEquals("getName()", myName, c.getName());
064: }
065:
066: /**
067: * Make sure it works even for java.* classes.
068: */
069: public void testLoaderUsesSystemForJavaClasses() throws Throwable {
070: StickyClassLoader loader = new StickyClassLoader(myLoader,
071: myLoader);
072:
073: Class c = loader.loadClass("java.lang.Object");
074: assertEquals("java.lang.Object", c.getName());
075: }
076:
077: /**
078: * Tests that add-on Java packages, such as javax.mail.*, can be
079: * loaded through the secondary loader if not found in the system
080: * loader.
081: */
082: public void testLoaderFindsNonSystemJavaClasses() throws Throwable {
083: class LoadingClassException extends RuntimeException {
084: }
085:
086: ClassLoader testLoader = new SecureClassLoader() {
087: public URL getResource(String name) {
088: throw new LoadingClassException();
089: }
090: };
091: StickyClassLoader loader = new StickyClassLoader(myLoader,
092: testLoader);
093:
094: try {
095: loader.loadClass("javax.mail.FakeClass");
096: // Should not have actually found it...
097: fail("FakeClass should not exist.");
098: } catch (LoadingClassException lce) {
099: // Good, that's what we want to happen
100: }
101: // If a ClassNotFoundException is thrown, then StickyClassLoader
102: // is not looking in the secondary loader.
103: }
104:
105: /**
106: * Make sure getClass().getClassLoader() does not stick if the class
107: * was on the useOldLoader list.
108: */
109: public void testLoaderRespectsOldList() throws Throwable {
110: StickyClassLoader loader = new StickyClassLoader(myLoader,
111: myLoader, new String[] { myName });
112:
113: Class c = loader.loadClass(myName);
114: assertEquals("getClassLoader()", myLoader, c.getClassLoader());
115: assertEquals("getName()", myName, c.getName());
116: }
117:
118: /**
119: * Make sure that if we load A through sticky loader, and A requires B
120: * to be loaded, B is also loaded through sticky loader.
121: * We load the BMaker interface through the old loader so we can
122: * cast to that interface.
123: */
124: public void testLoaderSticksTransitively() throws Throwable {
125: String[] names = { myName + "$BMaker" };
126:
127: StickyClassLoader loader = new StickyClassLoader(myLoader,
128: myLoader, names);
129: Class c = loader.loadClass(myName + "$A");
130: assertEquals("getClassLoader()", loader, c.getClassLoader());
131:
132: Object aObj = c.newInstance();
133: BMaker aCasted = (BMaker) aObj;
134:
135: Object b = aCasted.makeB();
136:
137: assertEquals("getClass().getName()", myName + "$A$B", b
138: .getClass().getName());
139:
140: assertEquals("getClass().getClassLoader()", loader, b
141: .getClass().getClassLoader());
142: }
143:
144: /**
145: * Makes sure that a class that was loaded once before (implicitly) is not
146: * loaded a second time. This test corresponds to bug #520519. As of
147: * util-20020219-2255, this test case causes a LinkageError to be thrown, since
148: * One is loaded twice. This problem was caused by the StickyClassLoader
149: * not checking whether the class was already loaded before loading it!
150: */
151: public void testDoesntLoadSameClassTwice() throws Throwable {
152: StickyClassLoader loader = new StickyClassLoader(myLoader,
153: myLoader);
154: loader.loadClass(myName + "$Two");
155: loader.loadClass(myName + "$One");
156: }
157:
158: public static class One {
159: }
160:
161: public static class Two extends One {
162: }
163:
164: public interface BMaker {
165: public Object makeB();
166: }
167:
168: public static class A implements BMaker {
169: private static class B {
170: }
171:
172: public Object makeB() {
173: return new B();
174: }
175: }
176: }
|