KickJava   Java API By Example, From Geeks To Geeks.

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


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: SharedLibrariesClassLoaderTest.java 17:06:54 ddesjardins $
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.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 import junit.framework.TestCase;
33
34 /**
35  *
36  * @author ddesjardins - eBMWebsourcing
37  */

38 public class ComponentClassLoaderTest extends TestCase {
39
40     /**
41      * ComponentClassLoader to test
42      */

43     private ComponentClassLoader componentClassLoader;
44
45     /**
46      * Shared library class loader
47      */

48     private SharedLibraryClassLoader sharedLibraryClassLoader;
49
50     public void setUp() throws IOException JavaDoc {
51         SharedLibrariesClassLoader sharedLibrariesClassLoader = new SharedLibrariesClassLoader();
52         List JavaDoc<URL JavaDoc> baseUrls = new ArrayList JavaDoc<URL JavaDoc>();
53         List JavaDoc<String JavaDoc> classpathLocations = new ArrayList JavaDoc<String JavaDoc>();
54         classpathLocations.add("petals-core-test.jar");
55         String JavaDoc baseDir = this.getClass().getResource(".").toString();
56         baseDir = baseDir.substring(0, baseDir.indexOf("target"));
57         baseDir = baseDir.substring(baseDir.indexOf(":") + 1);
58         baseUrls.add(new URL JavaDoc("file:" + baseDir.replace(File.separator, "/")
59             + "src/test-data/"));
60         baseUrls.add(new URL JavaDoc("file:" + baseDir.replace(File.separator, "/")
61             + "target/test-classes/"));
62         baseUrls.add(new URL JavaDoc("file:" + baseDir.replace(File.separator, "/")
63             + "target/"));
64         sharedLibraryClassLoader = new SharedLibraryClassLoader(baseUrls
65                 .toArray(new URL JavaDoc[0]), classpathLocations);
66         sharedLibraryClassLoader.setParentFirst(false);
67         sharedLibrariesClassLoader.addSharedLibraryClassLoader("1",
68                 sharedLibraryClassLoader);
69
70         baseUrls = new ArrayList JavaDoc<URL JavaDoc>();
71         classpathLocations = new ArrayList JavaDoc<String JavaDoc>();
72         classpathLocations.add("petals-common-test.jar");
73
74         baseDir = baseDir.replace("core", "container");
75         baseUrls.add(new URL JavaDoc("file:" + baseDir.replace(File.separator, "/")
76             + "src/test-data/"));
77         // baseUrls.add(new URL("file:" + baseDir.replace(File.separator, "/") +
78
// "target/classes/"));
79
List JavaDoc<String JavaDoc> slNames = new ArrayList JavaDoc<String JavaDoc>();
80         slNames.add("1");
81         componentClassLoader = new ComponentClassLoader(baseUrls
82                 .toArray(new URL JavaDoc[0]), classpathLocations,
83                 sharedLibrariesClassLoader, slNames);
84     }
85
86     /**
87      * Display the classpath
88      */

89     public void testGetClasspath() {
90         String JavaDoc compoCP = "testGetClasspath - component classpath "
91             + componentClassLoader.getClasspath();
92         compoCP.length();
93         String JavaDoc slCP = "testGetClasspath - shared library classpath "
94             + sharedLibraryClassLoader.getClasspath();
95         slCP.length();
96     }
97
98     /**
99      * Test to get a resource from itself The resource is in the component
100      * classpath and in the shared library classpath
101      */

102     public void testGetResourceInSelfAndParentSelfFirst() {
103         componentClassLoader.setParentFirst(false);
104         URL JavaDoc url = componentClassLoader.getResource("resourceBoth.txt");
105         if (url != null) {
106
107         } else {
108
109             fail("testGetResourceInSelfAndParentSelfFirst - resource not found ");
110         }
111     }
112
113     /**
114      * Test to get a resource from its parent The resource is in the component
115      * classpath and in the shared library classpath
116      */

117     public void testGetResourceInSelfAndParentParentFirst() {
118
119         componentClassLoader.setParentFirst(true);
120         URL JavaDoc url = componentClassLoader.getResource("resourceBoth.txt");
121         if (url != null) {
122
123         } else {
124
125             fail("testGetResourceInSelfAndParentParentFirst - resource not found ");
126         }
127     }
128
129     /**
130      * Test to get a resource as stream from itself The resource is in the
131      * component classpath and in the shared library classpath
132      *
133      * @throws IOException
134      */

135     public void testGetResourceAsStreamInSelfAndParentSelfFirst()
136         throws IOException JavaDoc {
137
138         componentClassLoader.setParentFirst(false);
139         InputStream JavaDoc inputStream = componentClassLoader
140                 .getResourceAsStream("resourceBoth.txt");
141         if (inputStream != null) {
142             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(inputStream);
143             byte[] content = new byte[dis.available()];
144             dis.readFully(content);
145
146         } else {
147
148             fail("testGetResourceAsStreamInSelfAndParentSelfFirst - resource not found ");
149         }
150     }
151
152     /**
153      * Test to get a resource as stream from its parent The resource is in the
154      * component classpath and in the shared library classpath
155      *
156      * @throws IOException
157      *
158      */

159     public void testGetResourceAsStreamInSelfAndParentParentFirst()
160         throws IOException JavaDoc {
161
162         componentClassLoader.setParentFirst(true);
163         InputStream JavaDoc inputStream = componentClassLoader
164                 .getResourceAsStream("resourceBoth.txt");
165         if (inputStream != null) {
166             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(inputStream);
167             byte[] content = new byte[dis.available()];
168             dis.readFully(content);
169
170         } else {
171
172             fail("testGetResourceAsStreamInSelfAndParentParentFirst - resource not found ");
173         }
174     }
175
176     /**
177      * Test to get a resource as stream from its self The resource is in the
178      * shared library classpath
179      *
180      * @throws IOException
181      */

182     public void testGetResourceAsStreamInParentSelfFirst() throws IOException JavaDoc {
183
184         componentClassLoader.setParentFirst(false);
185         InputStream JavaDoc inputStream = componentClassLoader
186                 .getResourceAsStream("resourceCore.txt");
187         if (inputStream != null) {
188             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(inputStream);
189             byte[] content = new byte[dis.available()];
190             dis.readFully(content);
191
192         } else {
193
194             fail("testGetResourceAsStreamInParentSelfFirst - resource not found ");
195         }
196     }
197
198     /**
199      * Test to get a resource as stream from its parent The resource is in the
200      * component classpath
201      *
202      * @throws IOException
203      */

204     public void testGetResourceAsStreamInSelfParentFirst() throws IOException JavaDoc {
205
206         componentClassLoader.setParentFirst(true);
207         InputStream JavaDoc inputStream = componentClassLoader
208                 .getResourceAsStream("resourceContainer.txt");
209         if (inputStream != null) {
210             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(inputStream);
211             byte[] content = new byte[dis.available()];
212             dis.readFully(content);
213
214         } else {
215
216             fail("testGetResourceAsStreamInSelfParentFirst - resource not found ");
217         }
218     }
219
220     /**
221      * Test to load a class from its parent
222      */

223     public void testLoadClassParentFirst() {
224
225         componentClassLoader.setParentFirst(true);
226         Class JavaDoc clazz;
227         try {
228             clazz = componentClassLoader.loadClass(
229                     "org.objectweb.petals.ServiceContext", false);
230             if (clazz != null) {
231
232             } else {
233
234                 fail("testLoadClassParentFirst - class not found ");
235             }
236         } catch (ClassNotFoundException JavaDoc e) {
237
238             fail("testLoadClassParentFirst - class not found ");
239         }
240     }
241
242     /**
243      * Test to load a class from its self
244      */

245     public void testLoadClassSelfFirst() {
246
247         componentClassLoader.setParentFirst(false);
248         Class JavaDoc clazz;
249         try {
250             clazz = componentClassLoader.loadClass(
251                     "org.objectweb.petals.PetalsException", false);
252             if (clazz != null) {
253
254             } else {
255
256                 fail("testLoadClassSelfFirst - class not found ");
257             }
258         } catch (ClassNotFoundException JavaDoc e) {
259
260             fail("testLoadClassSelfFirst - class not found ");
261         }
262     }
263
264     /**
265      * Test to load a class from its parent The class is in both classpath
266      * (parent and self)
267      *
268      * @throws IllegalAccessException
269      * @throws InstantiationException
270      */

271     public void testLoadClassInSelfAndParentParentFirst()
272         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
273
274         componentClassLoader.setParentFirst(true);
275         Class JavaDoc clazz;
276         try {
277             clazz = componentClassLoader.loadClass(
278                     "org.objectweb.petals.classloader.TestClassloader", false);
279             if (clazz != null) {
280
281             } else {
282
283                 fail("testLoadClassInSelfAndParentParentFirst - class not found ");
284             }
285         } catch (ClassNotFoundException JavaDoc e) {
286
287             fail("testLoadClassInSelfAndParentParentFirst - class not found ");
288         }
289     }
290
291     /**
292      * Test to load a class from its self The class is in both classpath (parent
293      * and self)
294      *
295      * @throws IllegalAccessException
296      * @throws InstantiationException
297      */

298     public void testLoadClassInSelfAndParentSelfFirst()
299         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
300
301         componentClassLoader.setParentFirst(false);
302         Class JavaDoc clazz;
303         try {
304             clazz = componentClassLoader.loadClass(
305                     "org.objectweb.petals.classloader.TestClassloader", false);
306             if (clazz != null) {
307
308             } else {
309
310                 fail("testLoadClassInSelfAndParentSelfFirst - class not found ");
311             }
312         } catch (ClassNotFoundException JavaDoc e) {
313
314             fail("testLoadClassInSelfAndParentSelfFirst - class not found ");
315         }
316     }
317
318 }
319
Popular Tags