KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloader > test > BasicLoaderUnitTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.classloader.test;
23
24 import java.io.File JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30 import junit.framework.Test;
31
32 import org.jboss.mx.loading.UnifiedLoaderRepository3;
33 import org.jboss.mx.loading.RepositoryClassLoader;
34 import org.jboss.test.util.ClassMover;
35 import org.jboss.logging.Logger;
36 import javassist.ClassPool;
37 import javassist.CtClass;
38 import javassist.CtField;
39 import javassist.Modifier;
40
41 /** Basic tests of the org.jboss.mx.loading.* classes
42  *
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 58115 $
45  */

46 public class BasicLoaderUnitTestCase extends TestCase
47 {
48    static Logger log = Logger.getLogger(BasicLoaderUnitTestCase.class);
49    String JavaDoc jbosstestDeployDir;
50
51    public BasicLoaderUnitTestCase(String JavaDoc name)
52    {
53       super(name);
54    }
55
56    protected void setUp() throws Exception JavaDoc
57    {
58       jbosstestDeployDir = System.getProperty("jbosstest.deploy.dir");
59       if( jbosstestDeployDir == null )
60          throw new Exception JavaDoc("System property jbosstest.deploy.dir is not defined");
61    }
62
63    /** Test the UnifiedLoaderRepository for multi-threaded class loading
64     */

65    public void testNoClassDefFoundError() throws Exception JavaDoc
66    {
67       UnifiedLoaderRepository3 ulr = new UnifiedLoaderRepository3();
68       File JavaDoc cwd = new File JavaDoc(jbosstestDeployDir);
69       URL JavaDoc cp = new URL JavaDoc(cwd.toURL(), "../classes");
70       log.info("Using cp: " + cp);
71       ClassLoader JavaDoc loader = ulr.newClassLoader(cp, true);
72
73       File JavaDoc bakFile = null;
74       try
75       {
76          bakFile = ClassMover.move("org.jboss.test.classloader.test.ex.BaseException");
77          loader.loadClass("org.jboss.test.classloader.test.ex.DerivedException");
78          fail("Should not have loaded DerivedException");
79       }
80       catch (NoClassDefFoundError JavaDoc e)
81       {
82          String JavaDoc msg = e.getMessage();
83          log.info("NCDFE msg: " + msg, e);
84          int index = msg.indexOf("BaseException");
85          assertTrue("Saw BaseException in NCDFE: (" + index + "), msg=" + msg, index > 0);
86       }
87       finally
88       {
89          if (bakFile != null)
90             ClassMover.restore(bakFile);
91       }
92
93    }
94
95    public void testNoClassDefFoundError2() throws Exception JavaDoc
96    {
97       UnifiedLoaderRepository3 ulr = new UnifiedLoaderRepository3();
98       File JavaDoc cwd = new File JavaDoc(jbosstestDeployDir);
99       URL JavaDoc cp = new URL JavaDoc(cwd.toURL(), "../classes");
100       log.info("Using cp: " + cp);
101       ClassLoader JavaDoc loader = ulr.newClassLoader(cp, true);
102
103       File JavaDoc bakFile = null;
104       try
105       {
106          bakFile = ClassMover.move("org.jboss.test.classloader.test.ex.BaseException");
107          Class JavaDoc c = loader.loadClass("org.jboss.test.classloader.test.ex.ExThrower");
108          c.getMethods();
109          fail("Should not have gotten ExThrower methods");
110       }
111       catch (NoClassDefFoundError JavaDoc e)
112       {
113          String JavaDoc msg = e.getMessage();
114          log.info("CNFE msg: " + msg, e);
115          int index = msg.indexOf("BaseException");
116          assertTrue("Saw BaseException in CNFE: (" + index + "), msg=" + msg, index > 0);
117       }
118       finally
119       {
120          if (bakFile != null)
121             ClassMover.restore(bakFile);
122       }
123
124    }
125
126    public void testDeadlockScenario1() throws Exception JavaDoc
127    {
128       File JavaDoc libDir = new File JavaDoc(jbosstestDeployDir);
129       log.info("Using cp: " + libDir);
130       DeadlockTests32 test = new DeadlockTests32(libDir);
131       test.testDeadLock();
132    }
133
134    public void testDeadlockScenario2() throws Exception JavaDoc
135    {
136       File JavaDoc libDir = new File JavaDoc(jbosstestDeployDir);
137       log.info("Using cp: " + libDir);
138       DeadlockTests32 test = new DeadlockTests32(libDir);
139       test.testDeadLockAndCircularity();
140    }
141
142    /**
143     Validate that the added order/classpath order is used when loading classes
144     from the ULR.
145
146     @throws Exception
147     */

148    public void testClasspathOrdering() throws Exception JavaDoc
149    {
150       File JavaDoc libDir = new File JavaDoc(jbosstestDeployDir);
151       File JavaDoc classes1 = new File JavaDoc(libDir, "classes1");
152       classes1.mkdir();
153
154       // Create a test.Info class with a static String version = "Version 1.0"
155
ClassPool defaultPool = ClassPool.getDefault();
156       ClassPool classes1Pool = new ClassPool(defaultPool);
157       CtClass info = classes1Pool.makeClass("test.Info");
158       CtClass s = classes1Pool.get("java.lang.String");
159       CtField version = new CtField(s, "version", info);
160       version.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
161       info.addField(version, CtField.Initializer.constant("Version 1.0"));
162       info.writeFile(classes1.getAbsolutePath());
163
164       // Create a test.Info class with a static String version = "Version 2.0"
165
ClassPool classes2Pool = new ClassPool(defaultPool);
166       info = classes2Pool.makeClass("test.Info");
167       File JavaDoc classes2 = new File JavaDoc(libDir, "classes2");
168       classes2.mkdir();
169       version = new CtField(s, "version", info);
170       version.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
171       info.addField(version, CtField.Initializer.constant("Version 2.0"));
172       info.writeFile(classes2.getAbsolutePath());
173
174       // Create a test.Info class with a static String version = "Version 3.0"
175
ClassPool classes3Pool = new ClassPool(defaultPool);
176       info = classes3Pool.makeClass("test.Info");
177       File JavaDoc classes3 = new File JavaDoc(libDir, "classes3");
178       classes3.mkdir();
179       version = new CtField(s, "version", info);
180       version.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
181       info.addField(version, CtField.Initializer.constant("Version 3.0"));
182       info.writeFile(classes3.getAbsolutePath());
183
184       // Create a URL with classpath {classes1, classes2, classes3}
185
UnifiedLoaderRepository3 ulr = new UnifiedLoaderRepository3();
186       RepositoryClassLoader loader1 = ulr.newClassLoader(classes1.toURL(), true);
187       loader1.addURL(classes2.toURL());
188       RepositoryClassLoader loader2 = ulr.newClassLoader(classes3.toURL(), true);
189
190       // This should see version 1
191
Class JavaDoc infoClass = loader1.loadClass("test.Info");
192       log.info("#1.1"+infoClass.getProtectionDomain().getCodeSource());
193       Field JavaDoc theVersion = infoClass.getField("version");
194       String JavaDoc v = (String JavaDoc) theVersion.get(null);
195       assertTrue(v, v.equals("Version 1.0"));
196
197       // This should also see version 1
198
infoClass = loader2.loadClass("test.Info");
199       log.info("#1.2"+infoClass.getProtectionDomain().getCodeSource());
200       theVersion = infoClass.getField("version");
201       v = (String JavaDoc) theVersion.get(null);
202       assertTrue(v, v.equals("Version 1.0"));
203       ulr.removeClassLoader(loader1);
204       ulr.removeClassLoader(loader2);
205       ulr.flush();
206
207       // Create a URL with classpath {classes2, classes1, classes3}
208
ulr = new UnifiedLoaderRepository3();
209       loader1 = ulr.newClassLoader(classes2.toURL(), true);
210       loader1.addURL(classes1.toURL());
211       loader2 = ulr.newClassLoader(classes3.toURL(), true);
212
213       // This should see version 2
214
infoClass = loader1.loadClass("test.Info");
215       log.info("#2.1"+infoClass.getProtectionDomain().getCodeSource());
216       theVersion = infoClass.getField("version");
217       v = (String JavaDoc) theVersion.get(null);
218       assertTrue(v, v.equals("Version 2.0"));
219
220       // This should also see version 2
221
infoClass = loader2.loadClass("test.Info");
222       log.info("#2.2"+infoClass.getProtectionDomain().getCodeSource());
223       theVersion = infoClass.getField("version");
224       v = (String JavaDoc) theVersion.get(null);
225       assertTrue(v, v.equals("Version 2.0"));
226       ulr.removeClassLoader(loader1);
227       ulr.removeClassLoader(loader2);
228       ulr.flush();
229
230       // Create a URL with classpath {classes3, classes1, classes2}
231
ulr = new UnifiedLoaderRepository3();
232       loader1 = ulr.newClassLoader(classes3.toURL(), true);
233       loader1.addURL(classes1.toURL());
234       loader2 = ulr.newClassLoader(classes2.toURL(), true);
235
236       // This should see version 3
237
infoClass = loader1.loadClass("test.Info");
238       log.info("#3.1"+infoClass.getProtectionDomain().getCodeSource());
239       theVersion = infoClass.getField("version");
240       v = (String JavaDoc) theVersion.get(null);
241       assertTrue(v, v.equals("Version 3.0"));
242
243       // This should also see version 3
244
infoClass = loader2.loadClass("test.Info");
245       log.info("#3.2"+infoClass.getProtectionDomain().getCodeSource());
246       theVersion = infoClass.getField("version");
247       v = (String JavaDoc) theVersion.get(null);
248       assertTrue(v, v.equals("Version 3.0"));
249    }
250
251    /**
252     * This needs to run with a -Xmx10m in order for these iter values
253     * to produce an OME when the blacklist is not a SoftSet
254     *
255     * @throws Exception
256     */

257    public void testBlacklistLeak()
258       throws Exception JavaDoc
259    {
260       UnifiedLoaderRepository3 ulr = new UnifiedLoaderRepository3();
261       URL JavaDoc cs = getClass().getProtectionDomain().getCodeSource().getLocation();
262       RepositoryClassLoader loader = ulr.newClassLoader(cs, true);
263       //
264
String JavaDoc base = "org.jboss";
265       for(int n = 0; n < 1000; n ++)
266          base += ".package"+n;
267       for(int n = 0; n < 1000; n ++)
268       {
269          String JavaDoc name = base + ".Class"+n;
270          try
271          {
272             Class JavaDoc c = loader.loadClass(name);
273             fail("was able to load: "+name);
274          }
275          catch(ClassNotFoundException JavaDoc e)
276          {
277             
278          }
279          catch(Error JavaDoc e)
280          {
281             fail("Failed with error: "+e+", iter: "+n);
282          }
283       }
284    }
285
286    public static Test suite()
287    {
288       TestSuite suite = new TestSuite();
289       suite.addTest(new BasicLoaderUnitTestCase("testNoClassDefFoundError"));
290       suite.addTest(new BasicLoaderUnitTestCase("testNoClassDefFoundError2"));
291       suite.addTest(new BasicLoaderUnitTestCase("testDeadlockScenario1"));
292       suite.addTest(new BasicLoaderUnitTestCase("testDeadlockScenario2"));
293       suite.addTest(new BasicLoaderUnitTestCase("testClasspathOrdering"));
294       suite.addTest(new BasicLoaderUnitTestCase("testClasspathOrdering"));
295       return suite;
296    }
297 }
298
299
Popular Tags