KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)UrlClassLoaderOnlineUTest.java 0.9.0 23-May-2001 - 14:40
3  *
4  * Copyright (C) 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
36 /**
37  * Just like util.http.tests, this uses the Sourceforge account to ensure
38  * that the URLs work correctly. It loads the sample applet "BeliefOfTheDay"
39  * to make sure that this is able to load classes remotely.
40  * As insurance, this also tests to make sure that the same applet is not
41  * in the current classpath.
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 June 28, 2002
46  */

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

81         junit.textui.TestRunner.main( name );
82     }
83     
84     protected void setUp() throws Exception JavaDoc
85     {
86         super.setUp();
87         
88         // set ourself up
89
}
90     
91     
92     protected void tearDown() throws Exception JavaDoc
93     {
94         // tear ourself down
95

96         super.tearDown();
97     }
98
99     
100     protected static UrlClassLoader createLoader()
101     {
102         return new UrlClassLoader();
103     }
104     
105     
106     public void testFlush1()
107     {
108         // override the original, because we can better test the flushing
109
// mechanism.
110

111         UrlClassLoader loader = createLoader();
112         
113         // load up bytecode from a separate location
114
Class JavaDoc c = loader.loadClass( BELIEF_CLASS, BELIEF_JAR );
115         assertNotNull( "loadClass( "+BELIEF_CLASS+
116             " ) returned null.", c );
117         assertEquals( "loadClass( "+BELIEF_CLASS+
118             " ) returned the wrong class.",
119             BELIEF_CLASS, c.getName() );
120         
121         // this needs to be run in the same test, to ensure we don't load the
122
// same class if referenced from a different JAR.
123
byte b[] = loader.getBytecode( BELIEF_CLASS );
124         assertNotNull( "getBytecode( "+BELIEF_CLASS+" ) returned null.", b );
125         assertTrue( "getBytecode( "+BELIEF_CLASS+" ) returned no data.",
126             b.length > 0 );
127         
128         loader.flush();
129         
130         // make sure the bytecode was properly eliminated.
131
b = loader.getBytecode( BELIEF_CLASS );
132         assertEquals( "getBytecode( "+BELIEF_CLASS+" ) did not return null.",
133             null, b );
134     }
135     
136     public void testGetBytecode1()
137     {
138         UrlClassLoader loader = (UrlClassLoader)createLoader();
139         
140         // load up bytecode from a separate location
141
Class JavaDoc c = null;
142         // I've had time-out issues with sourceforge. Try 3 times.
143
for (int i = 0; i < 3 && c == null; ++i)
144         {
145             c = loader.loadClass( BELIEF_CLASS, BELIEF_JAR );
146         }
147         assertNotNull( "loadClass( "+BELIEF_CLASS+
148             " ) returned null 3 times.", c );
149         assertEquals( "loadClass( "+BELIEF_CLASS+
150             " ) returned the wrong class.",
151             BELIEF_CLASS, c.getName() );
152         
153         // this needs to be run in the same test, to ensure we don't load the
154
// same class if referenced from a different JAR.
155
byte b[] = loader.getBytecode( BELIEF_CLASS );
156         assertNotNull( "getBytecode( "+BELIEF_CLASS+" ) returned null.", b );
157         assertTrue( "getBytecode( "+BELIEF_CLASS+" ) returned no data.",
158             b.length > 0 );
159     }
160 }
161
Popular Tags