KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > classloader > PetalsClassLoaderTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: PetalsClassLoaderTest.java 1061 2006-09-13 15:45:20Z ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.classloader;
23
24 import java.io.DataInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLStreamHandlerFactory JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.List JavaDoc;
33
34 import junit.framework.TestCase;
35
36 import org.easymock.classextension.EasyMock;
37
38 /**
39  * Test the petals class loader
40  *
41  * @author ddesjardins - eBMWebsourcing
42  */

43 public class PetalsClassLoaderTest extends TestCase {
44
45     /**
46      * PetalsClassLoader to test
47      */

48     private PetalsClassLoader petalsClassLoader;
49
50     public void setUp() throws IOException JavaDoc {
51         List JavaDoc<URL JavaDoc> baseUrls = new ArrayList JavaDoc<URL JavaDoc>();
52         List JavaDoc<String JavaDoc> classpathLocations = new ArrayList JavaDoc<String JavaDoc>();
53         String JavaDoc baseDir = this.getClass().getResource(".").toString();
54         baseDir = baseDir.substring(0, baseDir.indexOf("target"));
55         baseDir = baseDir.substring(baseDir.indexOf(":") + 1);
56         baseUrls.add(new URL JavaDoc("file:"
57             + baseDir.replace(File.separator, "/")));
58         baseUrls.add(new URL JavaDoc("file:"
59             + baseDir.replace(File.separator, "/") + "target/test-classes/"));
60         baseUrls.add(new URL JavaDoc("file:"
61             + baseDir.replace(File.separator, "/") + "target/"));
62         baseUrls.add(new URL JavaDoc("file:"
63             + baseDir.replace(File.separator, "/")
64             + "src/test-data/junit-3.8.jar"));
65         petalsClassLoader = new PetalsClassLoader(baseUrls.toArray(new URL JavaDoc[0]),
66             classpathLocations);
67         petalsClassLoader.setParentFirst(false);
68     }
69
70     /**
71      * Test to get the classpath
72      *
73      */

74     public void testGetClasspath() {
75         petalsClassLoader.getClasspath();
76
77     }
78
79     /**
80      * Test to load a class from a directory
81      *
82      */

83     public void testLoadClassFromDirectory() {
84         try {
85
86             Class JavaDoc class1 = petalsClassLoader.loadClass(
87                 "org.objectweb.petals.classloader.ComponentClassLoaderTest",
88                 false);
89             class1.getName();
90
91         } catch (ClassNotFoundException JavaDoc e) {
92
93             fail("Class not found : org.objectweb.petals.classloader.ComponentClassLoaderTest");
94         }
95     }
96
97     /**
98      * Test to load a class from a jar
99      *
100      */

101     public void testLoadClassFromJar() {
102         try {
103
104             Class JavaDoc class1 = petalsClassLoader.loadClass(
105                 "junit.framework.TestCase", false);
106             class1.getName();
107
108         } catch (ClassNotFoundException JavaDoc e) {
109
110             fail("Class not found : junit.framework.TestCase");
111         }
112     }
113
114     /**
115      * Test to get a resource from a jar
116      *
117      */

118     public void testGetResourceFromAJar() {
119         URL JavaDoc url = petalsClassLoader.getResource("META-INF/MANIFEST.MF");
120         if (url != null) {
121         } else {
122             fail("testGetResource - resource not found");
123         }
124     }
125
126     /**
127      * Test to get a resource from a directory
128      *
129      */

130     public void testGetResourceFromADirectory() {
131         URL JavaDoc url = petalsClassLoader.getResource("ResourceToLoad");
132         if (url != null) {
133         } else {
134             fail("testGetResource - resource not found");
135         }
136     }
137
138     /**
139      * Test to get a resource that does not exist
140      *
141      */

142     public void testGetResourceThatDoesNotExist() {
143         URL JavaDoc url = petalsClassLoader.getResource("test");
144         if (url == null) {
145         } else {
146             fail("testGetResource - resource found problem");
147         }
148     }
149
150     /**
151      * Test to get a resource as a stream from a directory
152      *
153      * @throws IOException
154      */

155     public void testGetResourceAsStream() throws IOException JavaDoc {
156         InputStream JavaDoc inputStream = petalsClassLoader
157             .getResourceAsStream("ResourceToLoad");
158         if (inputStream != null) {
159             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(inputStream);
160             byte[] content = new byte[dis.available()];
161             dis.readFully(content);
162         } else {
163             fail("testGetResourceAsStream - stream not found");
164         }
165     }
166
167     /**
168      * Test to get a resource as a stream from a jar
169      *
170      * @throws IOException
171      */

172     public void testGetResourceAsStreamFromAJar() throws IOException JavaDoc {
173         InputStream JavaDoc inputStream = petalsClassLoader
174             .getResourceAsStream("META-INF/MANIFEST.MF");
175         if (inputStream != null) {
176             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(inputStream);
177             byte[] content = new byte[dis.available()];
178             dis.readFully(content);
179         } else {
180             fail("testGetResourceAsStream - stream not found");
181         }
182     }
183
184     /**
185      * Test to get resources
186      *
187      * @throws IOException
188      */

189     public void testGetResources() throws IOException JavaDoc {
190         Enumeration JavaDoc<URL JavaDoc> enumeration = petalsClassLoader
191             .getResources("ResourceToLoad");
192         if (enumeration.hasMoreElements()) {
193         } else {
194             fail("testGetResource - resource not found");
195         }
196     }
197
198     public void testConstructor() throws Throwable JavaDoc {
199         URLStreamHandlerFactory JavaDoc urlStreamHandlerFactory = EasyMock
200             .createMock(URLStreamHandlerFactory JavaDoc.class);
201         PetalsClassLoader petalsClassLoader = new PetalsClassLoader(new URL JavaDoc[0],
202             new ArrayList JavaDoc<String JavaDoc>(), this.getClass().getClassLoader(),
203             urlStreamHandlerFactory);
204         assertEquals(petalsClassLoader.parent, this.getClass().getClassLoader());
205         petalsClassLoader.close();
206         assertEquals(petalsClassLoader.getBases().length, 0);
207     }
208
209 }
210
Popular Tags