01: package org.objectweb.celtix.resource;
02:
03: import java.io.BufferedReader;
04: import java.io.File;
05: import java.io.FileWriter;
06: import java.io.IOException;
07: import java.io.InputStream;
08: import java.io.InputStreamReader;
09: import java.net.URL;
10: import java.net.URLClassLoader;
11: import junit.framework.Test;
12: import junit.framework.TestCase;
13: import junit.framework.TestSuite;
14: import junit.textui.TestRunner;
15:
16: public class ClassLoaderResolverTest extends TestCase {
17: private static final String RESOURCE_DATA = "this is the resource data";
18:
19: private String resourceName;
20: private ClassLoaderResolver clr;
21:
22: public void setUp() throws IOException {
23: File resource = File.createTempFile("test", "resource");
24: resource.deleteOnExit();
25: resourceName = resource.getName();
26:
27: FileWriter writer = new FileWriter(resource);
28: writer.write(RESOURCE_DATA);
29: writer.write("\n");
30: writer.close();
31:
32: URL[] urls = { resource.getParentFile().toURL() };
33: ClassLoader loader = new URLClassLoader(urls);
34: assertNotNull(loader.getResourceAsStream(resourceName));
35: assertNull(ClassLoader.getSystemResourceAsStream(resourceName));
36: clr = new ClassLoaderResolver(loader);
37: }
38:
39: public void tearDown() {
40: clr = null;
41: resourceName = null;
42: }
43:
44: public void testResolve() {
45: assertNull(clr.resolve(resourceName, null));
46: assertNotNull(clr.resolve(resourceName, URL.class));
47: }
48:
49: public void testGetAsStream() throws IOException {
50: InputStream in = clr.getAsStream(resourceName);
51: assertNotNull(in);
52:
53: BufferedReader reader = new BufferedReader(
54: new InputStreamReader(in));
55: String content = reader.readLine();
56:
57: assertEquals("resource content incorrect", RESOURCE_DATA,
58: content);
59: }
60:
61: public static Test suite() {
62: return new TestSuite(ClassLoaderResolverTest.class);
63: }
64:
65: public static void main(String[] args) {
66: TestRunner.main(new String[] { ClassLoaderResolverTest.class
67: .getName() });
68: }
69: }
|