001: /*
002: * @(#)IUrlClassLoaderUTestI.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;
028:
029: import net.sourceforge.groboutils.junit.v1.iftc.*;
030: import junit.framework.Test;
031: import junit.framework.TestCase;
032: import junit.framework.TestSuite;
033:
034: import java.net.URL;
035:
036: /**
037: * Offline tests.
038: *
039: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
040: * @version $Date: 2003/02/10 22:52:39 $
041: * @since May 23, 2001 (GroboUtils Alpha 0.9.0)
042: */
043: public class IUrlClassLoaderUTestI extends InterfaceTestCase {
044: private static final Class THIS_CLASS = IUrlClassLoaderUTestI.class;
045:
046: public IUrlClassLoaderUTestI(String name, ImplFactory f) {
047: super (name, IUrlClassLoader.class, f);
048: }
049:
050: public IUrlClassLoader createLoader() {
051: return (IUrlClassLoader) createImplObject();
052: }
053:
054: public static InterfaceTestSuite suite() {
055: InterfaceTestSuite suite = new InterfaceTestSuite(THIS_CLASS);
056:
057: return suite;
058: }
059:
060: //-------------------------------------------------------------------------
061:
062: protected void setUp() throws Exception {
063: super .setUp();
064:
065: // set ourself up
066: }
067:
068: protected void tearDown() throws Exception {
069: // tear ourself down
070:
071: super .tearDown();
072: }
073:
074: public void testInstantiate() {
075: assertNotNull("Creation should not return null.",
076: createLoader());
077: }
078:
079: public static class MyInner {
080: // default constructor implied
081: }
082:
083: public static final String MY_INNER_RES = "IUrlClassLoaderUTestI$MyInner.class";
084: public static final String MY_INNER_CLASS = MyInner.class.getName();
085: public static final String MY_INNER_FULL_RES = '/'
086: + MY_INNER_CLASS.replace('.', '/') + ".class";
087:
088: public static class InnerClass {
089: public InnerClass() {
090: // do nothing
091: }
092: }
093:
094: public static final String BELIEF_CLASS = "BeliefOfTheDay";
095:
096: public void testGetClass1() {
097: IUrlClassLoader loader = createLoader();
098:
099: Class c = loader.loadClass(MY_INNER_CLASS, null);
100: assertNotNull("loadClass( " + MY_INNER_CLASS
101: + " ) returned null.", c);
102: assertEquals(
103: "Did not load the class from the default classloader.",
104: MyInner.class, c);
105: }
106:
107: public void testGetClass2() {
108: IUrlClassLoader loader = createLoader();
109:
110: Class c = loader.loadClass(BELIEF_CLASS, null);
111: assertEquals(
112: "getClass( Belief ) was found in the default classloader.",
113: null, c);
114: }
115:
116: public void testGetClass3() {
117: IUrlClassLoader loader = createLoader();
118:
119: URL url = this .getClass().getResource(MY_INNER_RES);
120: assertNotNull("Did not find resource for inner class "
121: + MY_INNER_RES, url);
122: System.out.println("Found class in URL " + url);
123: String urlStr = url.toString();
124: if (urlStr.endsWith(MY_INNER_FULL_RES)) {
125: urlStr = urlStr.substring(0, urlStr.length()
126: - MY_INNER_FULL_RES.length());
127: }
128:
129: Class c = loader.loadClass(MY_INNER_CLASS, urlStr);
130: assertNotNull("getClass( " + MY_INNER_CLASS
131: + " ) returned null.", c);
132: assertEquals("getClass( " + MY_INNER_CLASS
133: + " ) returned the wrong class.", MY_INNER_CLASS, c
134: .getName());
135: }
136:
137: public void testFlush1() {
138: IUrlClassLoader loader = createLoader();
139:
140: Class c = loader.loadClass(this .getClass().getName(), null);
141: loader.flush();
142:
143: // Can't really test this for correctness, just for error throwing.
144: // Testing for GC size is really inaccurate.
145: }
146:
147: }
|