KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > classloader > StickyClassLoaderTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.classloader;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37
38 import java.net.URL JavaDoc;
39 import java.security.SecureClassLoader JavaDoc;
40
41 /**
42  * Test cases for {@link StickyClassLoader}.
43  *
44  * @version $Id: StickyClassLoaderTest.java 3553 2006-02-20 21:22:09Z rcartwright $
45  */

46 public class StickyClassLoaderTest extends DrJavaTestCase {
47   private final String JavaDoc myName = getClass().getName();
48   private final ClassLoader JavaDoc myLoader = getClass().getClassLoader();
49
50   /**
51    * Make sure getClass().getClassLoader() sticks, regardless of where
52    * the class data came from.
53    */

54   public void testLoaderSticks() throws Throwable JavaDoc {
55     StickyClassLoader loader = new StickyClassLoader(myLoader, myLoader);
56
57     Class JavaDoc c = loader.loadClass(myName);
58     assertEquals("getClassLoader()", loader, c.getClassLoader());
59     assertEquals("getName()", myName, c.getName());
60   }
61
62   /**
63    * Make sure it works even for java.* classes.
64    */

65   public void testLoaderUsesSystemForJavaClasses() throws Throwable JavaDoc {
66     StickyClassLoader loader = new StickyClassLoader(myLoader, myLoader);
67
68     Class JavaDoc c = loader.loadClass("java.lang.Object");
69     assertEquals("java.lang.Object", c.getName());
70   }
71
72   /**
73    * Tests that add-on Java packages, such as javax.mail.*, can be
74    * loaded through the secondary loader if not found in the system
75    * loader.
76    */

77   public void testLoaderFindsNonSystemJavaClasses() throws Throwable JavaDoc {
78     class LoadingClassException extends RuntimeException JavaDoc { }
79
80     ClassLoader JavaDoc testLoader = new SecureClassLoader JavaDoc() {
81       public URL JavaDoc getResource(String JavaDoc name) {
82         throw new LoadingClassException();
83       }
84     };
85     StickyClassLoader loader = new StickyClassLoader(myLoader, testLoader);
86
87     try {
88       loader.loadClass("javax.mail.FakeClass");
89       // Should not have actually found it...
90
fail("FakeClass should not exist.");
91     }
92     catch (LoadingClassException lce) {
93       // Good, that's what we want to happen
94
}
95     // If a ClassNotFoundException is thrown, then StickyClassLoader
96
// is not looking in the secondary loader.
97
}
98
99   /**
100    * Make sure getClass().getClassLoader() does not stick if the class
101    * was on the useOldLoader list.
102    */

103   public void testLoaderRespectsOldList() throws Throwable JavaDoc {
104     StickyClassLoader loader = new StickyClassLoader(myLoader,
105                                                      myLoader,
106                                                      new String JavaDoc[] { myName });
107
108     Class JavaDoc c = loader.loadClass(myName);
109     assertEquals("getClassLoader()", myLoader, c.getClassLoader());
110     assertEquals("getName()", myName, c.getName());
111   }
112
113   /**
114    * Make sure that if we load A through sticky loader, and A requires B
115    * to be loaded, B is also loaded through sticky loader.
116    * We load the BMaker interface through the old loader so we can
117    * cast to that interface.
118    */

119   public void testLoaderSticksTransitively() throws Throwable JavaDoc {
120     String JavaDoc[] names = { myName + "$BMaker" };
121
122     StickyClassLoader loader = new StickyClassLoader(myLoader, myLoader, names);
123     Class JavaDoc c = loader.loadClass(myName + "$A");
124     assertEquals("getClassLoader()", loader, c.getClassLoader());
125
126     Object JavaDoc aObj = c.newInstance();
127     BMaker aCasted = (BMaker) aObj;
128
129     Object JavaDoc b = aCasted.makeB();
130
131     assertEquals("getClass().getName()",
132                  myName + "$A$B",
133                  b.getClass().getName());
134
135     assertEquals("getClass().getClassLoader()",
136                  loader,
137                  b.getClass().getClassLoader());
138   }
139
140   /**
141    * Makes sure that a class that was loaded once before (implicitly) is not
142    * loaded a second time. This test corresponds to bug #520519. As of
143    * util-20020219-2255, this test case causes a LinkageError to be thrown, since
144    * One is loaded twice. This problem was caused by the StickyClassLoader
145    * not checking whether the class was already loaded before loading it!
146    */

147   public void testDoesntLoadSameClassTwice() throws Throwable JavaDoc {
148     StickyClassLoader loader = new StickyClassLoader(myLoader, myLoader);
149     loader.loadClass(myName + "$Two");
150     loader.loadClass(myName + "$One");
151   }
152
153   public static class One { }
154   public static class Two extends One { }
155
156   public interface BMaker {
157     public Object JavaDoc makeB();
158   }
159
160   public static class A implements BMaker {
161     private static class B { }
162
163     public Object JavaDoc makeB() { return new B(); }
164   }
165 }
166
Popular Tags