KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > loaders > ClassLoaderTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.loaders;
18
19 import java.io.File JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLClassLoader JavaDoc;
22
23 import org.apache.xbean.classloader.JarFileClassLoader;
24
25 import junit.framework.TestCase;
26
27 /**
28  * @version $Revision: 438852 $
29  */

30 public class ClassLoaderTest extends TestCase {
31
32     public static class TestClass {
33     }
34     
35     public void testParentFirstClassLoader() throws Exception JavaDoc {
36         URLClassLoader JavaDoc pcl = (URLClassLoader JavaDoc) getClass().getClassLoader();
37         ClassLoader JavaDoc clsLoader = new JarFileClassLoader("", pcl.getURLs(), pcl, false, new String JavaDoc[0], new String JavaDoc[0]);
38         Class JavaDoc clazz = clsLoader.loadClass(TestClass.class.getName());
39         assertSame(TestClass.class, clazz);
40     }
41     
42     public void testSelfFirstClassLoader() throws Exception JavaDoc {
43         URLClassLoader JavaDoc pcl = (URLClassLoader JavaDoc) getClass().getClassLoader();
44         ClassLoader JavaDoc clsLoader = new JarFileClassLoader("", pcl.getURLs(), pcl, true, new String JavaDoc[0], new String JavaDoc[0]);
45         Class JavaDoc clazz = clsLoader.loadClass(TestClass.class.getName());
46         assertNotSame(TestClass.class, clazz);
47     }
48     
49     public void testParentFirstResource() throws Exception JavaDoc {
50         URLClassLoader JavaDoc pcl = (URLClassLoader JavaDoc) getClass().getClassLoader();
51         URL JavaDoc url = getClass().getResource("jndi.properties");
52         url = new File JavaDoc(url.toURI()).getParentFile().toURL();
53         ClassLoader JavaDoc clsLoader = new JarFileClassLoader("", new URL JavaDoc[] { url }, pcl, false, new String JavaDoc[0], new String JavaDoc[0]);
54         URL JavaDoc res1 = clsLoader.getResource("jndi.properties");
55         URL JavaDoc res2 = pcl.getResource("jndi.properties");
56         assertEquals(res2, res1);
57     }
58     
59     public void testSelfFirstResource() throws Exception JavaDoc {
60         URLClassLoader JavaDoc pcl = (URLClassLoader JavaDoc) getClass().getClassLoader();
61         URL JavaDoc url = getClass().getResource("jndi.properties");
62         url = new File JavaDoc(url.toURI()).getParentFile().toURL();
63         ClassLoader JavaDoc clsLoader = new JarFileClassLoader("", new URL JavaDoc[] { url }, pcl, true, new String JavaDoc[0], new String JavaDoc[0]);
64         URL JavaDoc res1 = clsLoader.getResource("jndi.properties");
65         URL JavaDoc res2 = pcl.getResource("jndi.properties");
66         assertFalse(res2.equals(res1));
67     }
68     
69 }
70
Popular Tags