KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > classes > v1 > jdk0 > UrlClassLoaderUTest


1 /*
2  * @(#)UrlClassLoaderUTest.java 0.9.0 23-May-2001 - 14:40
3  *
4  * Copyright (C) 2001,2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.classes.v1.jdk0;
28
29 import net.sourceforge.groboutils.util.classes.v1.*;
30 import net.sourceforge.groboutils.junit.v1.iftc.*;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 import java.io.InputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.net.URL JavaDoc;
38
39
40 /**
41  * Offline version of this test.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @version $Date: 2003/05/06 05:35:01 $
45  * @since May 23, 2001
46  */

47 public class UrlClassLoaderUTest extends TestCase
48 {
49     private static final Class JavaDoc THIS_CLASS = UrlClassLoaderUTest.class;
50     
51     public UrlClassLoaderUTest( String JavaDoc name )
52     {
53         super( name );
54     }
55     
56     public static Test suite()
57     {
58         InterfaceTestSuite suite = IUrlClassLoaderUTestI.suite();
59         suite.addTestSuite( THIS_CLASS );
60         suite.addFactory( new CxFactory( "A" ) {
61             public Object JavaDoc createImplObject() {
62                 return createLoader();
63             }
64         } );
65         
66         return suite;
67     }
68     
69     public static void main( String JavaDoc[] args )
70     {
71         String JavaDoc[] name = { THIS_CLASS.getName() };
72         
73         // junit.textui.TestRunner.main( name );
74
// junit.swingui.TestRunner.main( name );
75

76         junit.textui.TestRunner.main( name );
77     }
78     
79     protected void setUp() throws Exception JavaDoc
80     {
81         super.setUp();
82         
83         // set ourself up
84
}
85     
86     
87     protected void tearDown() throws Exception JavaDoc
88     {
89         // tear ourself down
90

91         super.tearDown();
92     }
93
94     
95     protected static UrlClassLoader createLoader()
96     {
97         return new UrlClassLoader();
98     }
99     
100     
101     
102     
103     public static class MyInner
104     {
105         // default constructor implied
106
}
107     public static final String JavaDoc MY_INNER_RES =
108         "UrlClassLoaderUTest$MyInner.class";
109     public static final String JavaDoc MY_INNER_CLASS = MyInner.class.getName();
110     public static final String JavaDoc MY_INNER_FULL_RES =
111         '/' + MY_INNER_CLASS.replace( '.', '/' ) + ".class";
112     
113     public static class InnerClass
114     {
115         public InnerClass()
116         {
117             // do nothing
118
}
119     }
120     
121     public void testFlush1()
122     {
123         // override the original, because we can better test the flushing
124
// mechanism.
125

126         UrlClassLoader loader = createLoader();
127         
128         // load up bytecode from a separate location
129
URL JavaDoc url = this.getClass().getResource( MY_INNER_RES );
130         assertNotNull(
131             "No resource found for "+MY_INNER_RES,
132             url );
133         System.out.println("Found class in URL "+url);
134         String JavaDoc urlStr = url.toString();
135         if (urlStr.endsWith( MY_INNER_FULL_RES ))
136         {
137             urlStr = urlStr.substring( 0, urlStr.length() -
138                 MY_INNER_FULL_RES.length() );
139         }
140         
141         Class JavaDoc c = loader.loadClass( MY_INNER_CLASS, urlStr );
142         assertNotNull( "loadClass( "+MY_INNER_CLASS+" ) returned null.", c );
143         assertEquals( "loadClass( "+MY_INNER_CLASS+
144             " ) returned the wrong class.",
145             MyInner.class.getName(), c.getName() );
146         
147         byte b[] = loader.getBytecode( MY_INNER_CLASS );
148         assertGetBytecodeResult( MY_INNER_CLASS, b, url );
149         
150         loader.flush();
151         
152         // make sure the bytecode was properly eliminated.
153
b = loader.getBytecode( MY_INNER_CLASS );
154         assertEquals( "getBytecode( "+MY_INNER_CLASS+" ) did not return null.",
155             null, b );
156     }
157     
158     
159     public void testGetBytecode1()
160     {
161         UrlClassLoader loader = (UrlClassLoader)createLoader();
162         
163         // load up bytecode from a separate location
164
URL JavaDoc url = this.getClass().getResource( MY_INNER_RES );
165         assertNotNull( "No resource for inner class "+MY_INNER_RES, url );
166         System.out.println("Found class in URL "+url);
167         String JavaDoc urlStr = url.toString();
168         if (urlStr.endsWith( MY_INNER_FULL_RES ))
169         {
170             urlStr = urlStr.substring( 0, urlStr.length() -
171                 MY_INNER_FULL_RES.length() );
172         }
173         
174         Class JavaDoc c = loader.loadClass( MY_INNER_CLASS, urlStr );
175         assertNotNull( "loadClass( "+MY_INNER_CLASS+" ) returned null.", c );
176         assertEquals( "loadClass( "+MY_INNER_CLASS+
177             " ) returned the wrong class.",
178             MY_INNER_CLASS, c.getName() );
179         
180         byte b[] = loader.getBytecode( MY_INNER_CLASS );
181         assertGetBytecodeResult( MY_INNER_CLASS, b, url );
182     }
183     
184     
185     protected void assertGetBytecodeResult( String JavaDoc className, byte[] result,
186             URL JavaDoc sourceURL )
187     {
188         if (result == null)
189         {
190             // JDK 1.1 won't let us load a Class file through the resource
191
// - it will through a SecurityException. No way around this,
192
// I guess.
193
try
194             {
195                 InputStream JavaDoc is = sourceURL.openStream();
196                 is.close();
197                 fail("getBytecode( "+className+" ) returned null, "+
198                     "but no SecurityException was thrown." );
199             }
200             catch (SecurityException JavaDoc e)
201             {
202                 // this is fine - JDK 1.1 or whatever is behaving
203
// (unfortunately) as expected.
204
}
205             catch (IOException JavaDoc ioe)
206             {
207                 ioe.printStackTrace();
208                 fail("Should not have encountered an IOException.");
209             }
210         }
211         else
212         {
213             //assertNotNull( "getBytecode( "+className+
214
// " ) returned null.", result );
215
assertTrue( "getBytecode( "+className+" ) returned no data.",
216                 result.length > 0 );
217         }
218     }
219 }
220
Popular Tags