001: /*
002: * @(#)UrlClassLoaderUTest.java 0.9.0 23-May-2001 - 14:40
003: *
004: * Copyright (C) 2001,2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.classes.v1.jdk0;
028:
029: import net.sourceforge.groboutils.util.classes.v1.*;
030: import net.sourceforge.groboutils.junit.v1.iftc.*;
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: import java.io.InputStream;
036: import java.io.IOException;
037: import java.net.URL;
038:
039: /**
040: * Offline version of this test.
041: *
042: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
043: * @version $Date: 2003/05/06 05:35:01 $
044: * @since May 23, 2001
045: */
046: public class UrlClassLoaderUTest extends TestCase {
047: private static final Class THIS_CLASS = UrlClassLoaderUTest.class;
048:
049: public UrlClassLoaderUTest(String name) {
050: super (name);
051: }
052:
053: public static Test suite() {
054: InterfaceTestSuite suite = IUrlClassLoaderUTestI.suite();
055: suite.addTestSuite(THIS_CLASS);
056: suite.addFactory(new CxFactory("A") {
057: public Object createImplObject() {
058: return createLoader();
059: }
060: });
061:
062: return suite;
063: }
064:
065: public static void main(String[] args) {
066: String[] name = { THIS_CLASS.getName() };
067:
068: // junit.textui.TestRunner.main( name );
069: // junit.swingui.TestRunner.main( name );
070:
071: junit.textui.TestRunner.main(name);
072: }
073:
074: protected void setUp() throws Exception {
075: super .setUp();
076:
077: // set ourself up
078: }
079:
080: protected void tearDown() throws Exception {
081: // tear ourself down
082:
083: super .tearDown();
084: }
085:
086: protected static UrlClassLoader createLoader() {
087: return new UrlClassLoader();
088: }
089:
090: public static class MyInner {
091: // default constructor implied
092: }
093:
094: public static final String MY_INNER_RES = "UrlClassLoaderUTest$MyInner.class";
095: public static final String MY_INNER_CLASS = MyInner.class.getName();
096: public static final String MY_INNER_FULL_RES = '/'
097: + MY_INNER_CLASS.replace('.', '/') + ".class";
098:
099: public static class InnerClass {
100: public InnerClass() {
101: // do nothing
102: }
103: }
104:
105: public void testFlush1() {
106: // override the original, because we can better test the flushing
107: // mechanism.
108:
109: UrlClassLoader loader = createLoader();
110:
111: // load up bytecode from a separate location
112: URL url = this .getClass().getResource(MY_INNER_RES);
113: assertNotNull("No resource found for " + MY_INNER_RES, url);
114: System.out.println("Found class in URL " + url);
115: String urlStr = url.toString();
116: if (urlStr.endsWith(MY_INNER_FULL_RES)) {
117: urlStr = urlStr.substring(0, urlStr.length()
118: - MY_INNER_FULL_RES.length());
119: }
120:
121: Class c = loader.loadClass(MY_INNER_CLASS, urlStr);
122: assertNotNull("loadClass( " + MY_INNER_CLASS
123: + " ) returned null.", c);
124: assertEquals("loadClass( " + MY_INNER_CLASS
125: + " ) returned the wrong class.", MyInner.class
126: .getName(), c.getName());
127:
128: byte b[] = loader.getBytecode(MY_INNER_CLASS);
129: assertGetBytecodeResult(MY_INNER_CLASS, b, url);
130:
131: loader.flush();
132:
133: // make sure the bytecode was properly eliminated.
134: b = loader.getBytecode(MY_INNER_CLASS);
135: assertEquals("getBytecode( " + MY_INNER_CLASS
136: + " ) did not return null.", null, b);
137: }
138:
139: public void testGetBytecode1() {
140: UrlClassLoader loader = (UrlClassLoader) createLoader();
141:
142: // load up bytecode from a separate location
143: URL url = this .getClass().getResource(MY_INNER_RES);
144: assertNotNull("No resource for inner class " + MY_INNER_RES,
145: url);
146: System.out.println("Found class in URL " + url);
147: String urlStr = url.toString();
148: if (urlStr.endsWith(MY_INNER_FULL_RES)) {
149: urlStr = urlStr.substring(0, urlStr.length()
150: - MY_INNER_FULL_RES.length());
151: }
152:
153: Class c = loader.loadClass(MY_INNER_CLASS, urlStr);
154: assertNotNull("loadClass( " + MY_INNER_CLASS
155: + " ) returned null.", c);
156: assertEquals("loadClass( " + MY_INNER_CLASS
157: + " ) returned the wrong class.", MY_INNER_CLASS, c
158: .getName());
159:
160: byte b[] = loader.getBytecode(MY_INNER_CLASS);
161: assertGetBytecodeResult(MY_INNER_CLASS, b, url);
162: }
163:
164: protected void assertGetBytecodeResult(String className,
165: byte[] result, URL sourceURL) {
166: if (result == null) {
167: // JDK 1.1 won't let us load a Class file through the resource
168: // - it will through a SecurityException. No way around this,
169: // I guess.
170: try {
171: InputStream is = sourceURL.openStream();
172: is.close();
173: fail("getBytecode( " + className + " ) returned null, "
174: + "but no SecurityException was thrown.");
175: } catch (SecurityException e) {
176: // this is fine - JDK 1.1 or whatever is behaving
177: // (unfortunately) as expected.
178: } catch (IOException ioe) {
179: ioe.printStackTrace();
180: fail("Should not have encountered an IOException.");
181: }
182: } else {
183: //assertNotNull( "getBytecode( "+className+
184: // " ) returned null.", result );
185: assertTrue("getBytecode( " + className
186: + " ) returned no data.", result.length > 0);
187: }
188: }
189: }
|