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