KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloading > UCLDomainClassLoaderTest


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.classloading;
23
24 import java.net.URL JavaDoc;
25 import java.net.URLClassLoader JavaDoc;
26 import java.security.ProtectionDomain JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Enumeration JavaDoc;
30
31 import org.jboss.test.AbstractSystemTest;
32 import org.jboss.mx.loading.DomainClassLoaderUCLImpl;
33 import org.jboss.mx.loading.UnifiedLoaderRepositoryDCL;
34
35 /**
36  * Test of the legacy UCL class loader as a DomainClassLoader.
37  *
38  * @author Scott.Stark@jboss.org
39  * @version $Revision:$
40  */

41 public class UCLDomainClassLoaderTest extends AbstractSystemTest
42 {
43    // Classes that should not be found in the unit test classpath
44
static String JavaDoc[] SUPPORT_CLASSES = {
45       "org.jboss.test.support.jar1.simple1.Bean1",
46       "org.jboss.test.support.jar1.simple1.ISimpleBean",
47       "org.jboss.test.support.jar2.simple1.Jar1Consumer"
48    };
49    // Resources that should not be found in the unit test classpath
50
static String JavaDoc[] SUPPORT_RESOURCES = {
51       "resource1.xml"
52    };
53
54    public UCLDomainClassLoaderTest(String JavaDoc name)
55    {
56       super(name);
57    }
58
59
60    /**
61     * Validate that SUPPORT_CLASSES are not on the unit test classpath
62     */

63    @Override JavaDoc
64    protected void setUp() throws Exception JavaDoc
65    {
66       super.setUp();
67       // Assert that Bean1 is not visible to this test class loader
68
ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
69       for(String JavaDoc name : SUPPORT_CLASSES)
70       {
71          try
72          {
73             Class JavaDoc beanClass = tcl.loadClass(name);
74             fail("Was able to find "+name+", "+beanClass.getProtectionDomain());
75          }
76          catch(ClassNotFoundException JavaDoc e)
77          {
78             getLog().debug(name+" is not on unit test classpath");
79          }
80       }
81       for(String JavaDoc name : SUPPORT_RESOURCES)
82       {
83          URL JavaDoc resURL = tcl.getResource(name);
84          if( resURL != null )
85             fail("Was able to find "+name+", "+resURL);
86          else
87             getLog().debug(name+" is not on unit test classpath");
88       }
89    }
90
91
92    /**
93     * Basic test of loading classes from /simple1.jar
94     * @throws Exception
95     */

96    public void testSimple1Jar()
97       throws Exception JavaDoc
98    {
99       String JavaDoc beanClassName = "org.jboss.test.support.jar1.simple1.Bean1";
100
101       UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL();
102       URL JavaDoc[] cp = buildClasspath("/jar1.jar");
103       getLog().info("Using classpath: "+Arrays.asList(cp));
104       DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository);
105       Class JavaDoc beanClass = repository.loadClass(beanClassName);
106       assertNotNull(beanClass);
107       ProtectionDomain JavaDoc pd = beanClass.getProtectionDomain();
108       getLog().info(beanClass+", PD: "+pd);
109       String JavaDoc path = pd.getCodeSource().getLocation().getPath();
110       assertTrue("Bean1.PD ends with jar1.jar", path.endsWith("jar1.jar"));
111       Object JavaDoc bean = beanClass.newInstance();
112       assertSame(loader, bean.getClass().getClassLoader());
113    }
114
115    /**
116     * Basic test of loading classes in /jar2.jar which has a Class-Path
117     * manifest entry pointing to /jar1.jar
118     *
119     * @throws Exception
120     */

121    public void testSimple2JarWithManifestCP()
122       throws Exception JavaDoc
123    {
124       String JavaDoc beanClassName = "org.jboss.test.support.jar2.Jar1Consumer";
125
126       UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL();
127       URL JavaDoc[] cp = buildClasspath("/jar2CPjar1.jar");
128       getLog().info("Using classpath: "+Arrays.asList(cp));
129       DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository);
130       Class JavaDoc beanClass = repository.loadClass(beanClassName);
131       assertNotNull(beanClass);
132       ProtectionDomain JavaDoc pd = beanClass.getProtectionDomain();
133       getLog().info(beanClass+", PD: "+pd);
134       String JavaDoc path = pd.getCodeSource().getLocation().getPath();
135       assertTrue("Jar1Consumer.PD ends with jar2CPjar1.jar", path.endsWith("jar2CPjar1.jar"));
136       Object JavaDoc bean = beanClass.newInstance();
137       assertSame(loader, bean.getClass().getClassLoader());
138
139       Class JavaDoc beanClass2 = repository.loadClass("org.jboss.test.support.jar1.simple1.ISimpleBean");
140       assertNotNull(beanClass2);
141       pd = beanClass2.getProtectionDomain();
142       getLog().info(beanClass2+", PD: "+pd);
143       path = pd.getCodeSource().getLocation().getPath();
144       assertTrue("ISimpleBean.PD ends with jar1.jar", path.endsWith("jar1.jar"));
145       assertSame(loader, beanClass2.getClassLoader());
146    }
147
148    /**
149     * Test that multiple independent jars can be combined to form a classpath
150     * @throws Exception
151     */

152    public void testMulitipleJars()
153       throws Exception JavaDoc
154    {
155       String JavaDoc beanClassName = "org.jboss.test.support.jar2.Jar1Consumer";
156       UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL();
157       URL JavaDoc[] cp = buildClasspath("/jar1.jar", "/jar2.jar");
158       getLog().info("Using classpath: "+Arrays.asList(cp));
159       DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository);
160       Class JavaDoc beanClass = repository.loadClass(beanClassName);
161       assertNotNull(beanClass);
162       ProtectionDomain JavaDoc pd = beanClass.getProtectionDomain();
163       getLog().info(beanClass+", PD: "+pd);
164       String JavaDoc path = pd.getCodeSource().getLocation().getPath();
165       assertTrue("Jar1Consumer.PD ends with jar2.jar", path.endsWith("jar2.jar"));
166       Object JavaDoc bean = beanClass.newInstance();
167       assertSame(loader, bean.getClass().getClassLoader());
168
169       Class JavaDoc beanClass2 = repository.loadClass("org.jboss.test.support.jar1.simple1.ISimpleBean");
170       assertNotNull(beanClass);
171       pd = beanClass2.getProtectionDomain();
172       getLog().info(beanClass2+", PD: "+pd);
173       path = pd.getCodeSource().getLocation().getPath();
174       assertTrue("ISimpleBean.PD ends with jar1.jar", path.endsWith("jar1.jar"));
175       assertSame(loader, beanClass2.getClassLoader());
176    }
177
178    /**
179     * Test that multiple class loaders combine to form a uniform repository
180     * @throws Exception
181     */

182    public void testMutipleClassLoaders()
183       throws Exception JavaDoc
184    {
185       String JavaDoc beanClassName = "org.jboss.test.support.jar2.Jar1Consumer";
186       UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL();
187       URL JavaDoc[] cp1 = buildClasspath("/jar1.jar");
188       URL JavaDoc[] cp2 = buildClasspath("/jar2.jar");
189       getLog().info("Using classpath: "+Arrays.asList(cp1));
190       DomainClassLoaderUCLImpl loader1 = new DomainClassLoaderUCLImpl(cp1, repository);
191       loader1.getURLs();
192       getLog().info("Using classpath: "+Arrays.asList(cp2));
193       DomainClassLoaderUCLImpl loader2 = new DomainClassLoaderUCLImpl(cp2, repository);
194       Class JavaDoc beanClass = loader1.loadClass(beanClassName);
195       assertNotNull(beanClass);
196       ProtectionDomain JavaDoc pd = beanClass.getProtectionDomain();
197       getLog().info(beanClass+", PD: "+pd);
198       String JavaDoc path = pd.getCodeSource().getLocation().getPath();
199       assertTrue("Jar1Consumer.PD ends with jar2.jar", path.endsWith("jar2.jar"));
200       Object JavaDoc bean = beanClass.newInstance();
201       assertSame(loader2, bean.getClass().getClassLoader());
202
203       Class JavaDoc beanClass2 = repository.loadClass("org.jboss.test.support.jar1.simple1.ISimpleBean");
204       assertNotNull(beanClass);
205       pd = beanClass2.getProtectionDomain();
206       getLog().info(beanClass2+", PD: "+pd);
207       path = pd.getCodeSource().getLocation().getPath();
208       assertTrue("ISimpleBean.PD ends with jar1.jar", path.endsWith("jar1.jar"));
209       assertSame(loader1, beanClass2.getClassLoader());
210    }
211
212    /**
213     * Test finding resources in a single jar
214     * @throws Exception
215     */

216    public void testSimple1Resource()
217       throws Exception JavaDoc
218    {
219       UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL();
220       URL JavaDoc[] cp = buildClasspath("/jar1.jar");
221       getLog().info("Using classpath: "+Arrays.asList(cp));
222       DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository);
223       URL JavaDoc resURL = repository.getResource("resource1.xml", loader);
224       assertNotNull(resURL);
225       getLog().info(resURL);
226       String JavaDoc path = resURL.getPath();
227       assertTrue("res path contains jar1.jar", path.indexOf("jar1.jar") > 0);
228
229       Enumeration JavaDoc<URL JavaDoc> resURLs = repository.findResources("resource1.xml");
230       int count = 0;
231       while( resURLs.hasMoreElements() )
232       {
233          getLog().info(resURLs.nextElement());
234          count ++;
235       }
236       assertEquals("1 resource1.xml", 1, count);
237    }
238
239    /**
240     * Basic test of finding resources in /jar2.jar which has a Class-Path
241     * manifest entry pointing to /jar1.jar
242     *
243     * @throws Exception
244     */

245    public void testSimple2ResourcesWithManifestCP()
246       throws Exception JavaDoc
247    {
248       UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL();
249       URL JavaDoc[] cp = buildClasspath("/jar2CPjar1.jar");
250       getLog().info("Using classpath: "+Arrays.asList(cp));
251       DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository);
252       URL JavaDoc resURL = repository.getResource("resource1.xml", loader);
253       assertNotNull(resURL);
254       getLog().info(resURL);
255       String JavaDoc path = resURL.getPath();
256       assertTrue("res path contains jar1.jar", path.indexOf("jar1.jar") > 0);
257
258       Enumeration JavaDoc<URL JavaDoc> resURLs = repository.findResources("resource1.xml");
259       int count = 0;
260       while( resURLs.hasMoreElements() )
261       {
262          getLog().info(resURLs.nextElement());
263          count ++;
264       }
265       assertEquals("1 resource1.xml", 1, count);
266    }
267
268    /**
269     * Test that multiple independent jars can be combined to form a resource path
270     * @throws Exception
271     */

272    public void testMulitipleJarsResource()
273       throws Exception JavaDoc
274    {
275       UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL();
276       URL JavaDoc[] cp = buildClasspath("/jar1.jar", "/jar2.jar");
277       getLog().info("Using classpath: "+Arrays.asList(cp));
278       DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository);
279       URL JavaDoc resURL = repository.getResource("resource1.xml", loader);
280       assertNotNull(resURL);
281       getLog().info(resURL);
282       String JavaDoc path = resURL.getPath();
283       assertTrue("res path contains jar1.jar", path.indexOf("jar1.jar") > 0);
284
285       Enumeration JavaDoc<URL JavaDoc> resURLs = repository.findResources("resource1.xml");
286       int count = 0;
287       while( resURLs.hasMoreElements() )
288       {
289          getLog().info(resURLs.nextElement());
290          count ++;
291       }
292       assertEquals("2 resource1.xml", 2, count);
293    }
294
295    /**
296     * Test that multiple class loaders combine to form a uniform repository
297     * for resources
298     * @throws Exception
299     */

300    public void testMutipleClassLoadersResource()
301       throws Exception JavaDoc
302    {
303       UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL();
304       URL JavaDoc[] cp1 = buildClasspath("/jar1.jar");
305       URL JavaDoc[] cp2 = buildClasspath("/jar2.jar");
306       getLog().info("Using classpath: "+Arrays.asList(cp1));
307       DomainClassLoaderUCLImpl loader1 = new DomainClassLoaderUCLImpl(cp1, repository);
308       loader1.getURLs();
309       getLog().info("Using classpath: "+Arrays.asList(cp2));
310       DomainClassLoaderUCLImpl loader2 = new DomainClassLoaderUCLImpl(cp2, repository);
311       URL JavaDoc resURL = repository.getResource("resource1.xml", loader1);
312       assertNotNull(resURL);
313       getLog().info(resURL);
314       String JavaDoc path = resURL.getPath();
315       assertTrue("res path contains jar1.jar", path.indexOf("jar1.jar") > 0);
316       resURL = repository.getResource("resource1.xml", loader2);
317       assertNotNull(resURL);
318       getLog().info(resURL);
319       path = resURL.getPath();
320       assertTrue("res path contains jar2.jar", path.indexOf("jar2.jar") > 0);
321
322       Enumeration JavaDoc<URL JavaDoc> resURLs = repository.findResources("resource1.xml");
323       int count = 0;
324       while( resURLs.hasMoreElements() )
325       {
326          getLog().info(resURLs.nextElement());
327          count ++;
328       }
329       assertEquals("2 resource1.xml", 2, count);
330    }
331
332    private URL JavaDoc[] buildClasspath(String JavaDoc... jars)
333    {
334       ArrayList JavaDoc<URL JavaDoc> cp = new ArrayList JavaDoc<URL JavaDoc>();
335       for(String JavaDoc jar : jars)
336       {
337          // This needs to use the fully pathname syntax
338
URL JavaDoc jarURL = super.getResource("/org/jboss/test/classloading/" + jar);
339          if( jarURL == null )
340             fail("Failed to find resource for jar: "+jar);
341          cp.add(jarURL);
342       }
343       URL JavaDoc[] tmp = new URL JavaDoc[cp.size()];
344       cp.toArray(tmp);
345       return tmp;
346    }
347 }
348
Popular Tags