1 package org.codehaus.classworlds;
2
3 import junit.framework.TestCase;
4
5 import java.io.File;
6 import java.io.InputStream;
7 import java.net.URL;
8
9 public class RealmClassLoaderTest
10 extends TestCase
11 {
12 private ClassWorld world;
13 private ClassRealm realm;
14
15 public void setUp()
16 throws Exception
17 {
18 System.setProperty( "java.protocol.handler.pkgs",
19 "org.codehaus.classworlds.uberjar.protocol" );
20
21 System.setProperty( "classworlds.bootstrapped",
22 "true" );
23
24 this.world = new ClassWorld();
25
26 this.realm = this.world.newRealm( "realm" );
27 }
28
29 public void testFindResource_Simple()
30 throws Exception
31 {
32 URL url = getJarUrl( "nested.jar" );
33
34 this.realm.addConstituent( url );
35
36 RealmClassLoader cl = (RealmClassLoader) this.realm.getClassLoader();
37
38 URL resource = cl.findResource( "nested.properties" );
39
40 assertNotNull( resource );
41
42 byte[] buffer = new byte[1024];
43 int read = 0;
44 StringBuffer content = new StringBuffer();
45
46 InputStream in = resource.openStream();
47
48 while ( ( read = in.read( buffer,
49 0,
50 1024 ) ) >= 0 )
51 {
52 content.append( new String( buffer,
53 0,
54 read ) );
55 }
56
57 assertTrue( content.toString().startsWith( "nested.properties" ) );
58 }
59
60 public void testGetResourceAsStream_Simple()
61 throws Exception
62 {
63 URL url = getJarUrl( "nested.jar" );
64
65 this.realm.addConstituent( url );
66
67 RealmClassLoader cl = (RealmClassLoader) this.realm.getClassLoader();
68
69 InputStream in = cl.getResourceAsStream( "nested.properties" );
70
71 assertNotNull( in );
72
73 byte[] buffer = new byte[1024];
74 int read = 0;
75 StringBuffer content = new StringBuffer();
76
77 while ( ( read = in.read( buffer,
78 0,
79 1024 ) ) >= 0 )
80 {
81 content.append( new String( buffer,
82 0,
83 read ) );
84 }
85
86 assertTrue( content.toString().startsWith( "nested.properties" ) );
87 }
88
89 public void testStandardJarUrl()
90 throws Exception
91 {
92 File testDir = new File ( System.getProperty( "basedir" ), "target/test-data" );
93 URL url = new URL( "jar:file:" + testDir + "/a.jar!/" );
94 // URL url = new URL( "file:" + testDir + "/a.jar" );
95
96 // This will produce something like the following:
97 //
98 // jar:file:/home/jvanzyl/js/org/codehaus/classworlds/test-data/a.jar!/
99
100 this.realm.addConstituent( url );
101
102 RealmClassLoader cl = (RealmClassLoader) this.realm.getClassLoader();
103
104 cl.loadClass( "a.A" );
105 }
106
107
108 public void testFindResource_NotFound()
109 throws Exception
110 {
111 URL url = getJarUrl( "nested.jar" );
112
113 this.realm.addConstituent( url );
114
115 RealmClassLoader cl = (RealmClassLoader) this.realm.getClassLoader();
116
117 URL resource = cl.findResource( "deadbeef" );
118
119 assertNull( resource );
120 }
121
122 public void testGetResourceAsStream_NotFound()
123 throws Exception
124 {
125 URL url = getJarUrl( "nested.jar" );
126
127 this.realm.addConstituent( url );
128
129 RealmClassLoader cl = (RealmClassLoader) this.realm.getClassLoader();
130
131 InputStream in = cl.getResourceAsStream( "deadbeef" );
132
133 assertNull( in );
134 }
135
136 public void testFindResource_Nested()
137 throws Exception
138 {
139 URL url = buildUrl( "nested.jar",
140 "!/lib/a.jar");
141
142 this.realm.addConstituent( url );
143
144 RealmClassLoader cl = (RealmClassLoader) this.realm.getClassLoader();
145
146 URL resource = cl.findResource( "a.properties" );
147
148 assertNotNull( resource );
149
150 byte[] buffer = new byte[1024];
151 int read = 0;
152 StringBuffer content = new StringBuffer();
153
154 InputStream in = resource.openStream();
155
156 while ( ( read = in.read( buffer,
157 0,
158 1024 ) ) >= 0 )
159 {
160 content.append( new String( buffer,
161 0,
162 read ) );
163 }
164
165 assertTrue( content.toString().startsWith( "a properties" ) );
166 }
167
168 public void testGetResourceAsStream_Nested()
169 throws Exception
170 {
171 URL url = buildUrl( "nested.jar",
172 "!/lib/a.jar");
173
174 this.realm.addConstituent( url );
175
176 RealmClassLoader cl = (RealmClassLoader) this.realm.getClassLoader();
177
178 InputStream in = cl.getResourceAsStream( "a.properties" );
179
180 assertNotNull( in );
181
182 byte[] buffer = new byte[1024];
183 int read = 0;
184 StringBuffer content = new StringBuffer();
185
186 while ( ( read = in.read( buffer,
187 0,
188 1024 ) ) >= 0 )
189 {
190 content.append( new String( buffer,
191 0,
192 read ) );
193 }
194
195 assertTrue( content.toString().startsWith( "a properties" ) );
196 }
197
198 protected URL getJarUrl(String jarName)
199 throws Exception
200 {
201 File testDir = new File ( System.getProperty( "basedir" ),
202 "target/test-data" );
203
204 File jarFile = new File( testDir,
205 jarName );
206
207
208 String urlText = "jar:" + jarFile.toURL();
209
210 return new URL( urlText );
211 }
212
213 protected URL buildUrl(String jarName,
214 String path)
215 throws Exception
216 {
217 URL jarUrl = getJarUrl( jarName );
218
219 String urlText = jarUrl.toExternalForm() + path;
220
221 URL url = new URL( urlText );
222
223 return url;
224 }
225 }
This page was automatically generated by Maven