001: /*
002: * @(#)ClassUtilUTest.java 0.9.0 17-APR-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 junit.framework.Test;
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032:
033: /**
034: * This is the off-line version of the ClassUtil unit tests.
035: * As insurance, this tests to make sure that the online test version applet is
036: * not in the current classpath.
037: *
038: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
039: * @version $Date: 2003/02/10 22:52:38 $
040: * @since April 17, 2001 (GroboUtils Alpha 0.9.0)
041: */
042: public class ClassUtilUTest extends TestCase {
043: private static final Class THIS_CLASS = ClassUtilUTest.class;
044:
045: public ClassUtilUTest(String name) {
046: super (name);
047: }
048:
049: public static Test suite() {
050: TestSuite suite = new TestSuite(THIS_CLASS);
051:
052: return suite;
053: }
054:
055: public static void main(String[] args) {
056: String[] name = { THIS_CLASS.getName() };
057:
058: // junit.textui.TestRunner.main( name );
059: // junit.swingui.TestRunner.main( name );
060:
061: junit.textui.TestRunner.main(name);
062: }
063:
064: protected void setUp() throws Exception {
065: super .setUp();
066:
067: // set ourself up
068: }
069:
070: protected void tearDown() throws Exception {
071: // tear ourself down
072:
073: super .tearDown();
074: }
075:
076: public void testInstantiate1() {
077: assertNotNull("Singleton method returned null.", ClassUtil
078: .getInstance());
079: }
080:
081: private static final String BELIEF_CLASS = "BeliefOfTheDay";
082:
083: public static class InnerClass {
084: public InnerClass() {
085: // do nothing
086: }
087: }
088:
089: public void testGetClass1() {
090: ClassUtil util = startTest();
091:
092: Class c = util.getClass(THIS_CLASS.getName());
093: assertNotNull("getClass( " + THIS_CLASS.getName()
094: + " ) returned null.", c);
095: assertEquals(
096: "Did not load the class from the default classloader.",
097: THIS_CLASS, c);
098: }
099:
100: public void testGetClass2() {
101: ClassUtil util = startTest();
102:
103: Class c = util.getClass(BELIEF_CLASS);
104: assertEquals(
105: "getClass( Belief ) was found in the default classloader.",
106: null, c);
107: }
108:
109: public void testGetClass3() {
110: ClassUtil util = startTest();
111:
112: Class c = util.getClass(THIS_CLASS.getName(), null);
113: assertNotNull("getClass( " + THIS_CLASS.getName()
114: + " ) returned null.", c);
115: assertEquals(
116: "Did not load the class from the default classloader.",
117: THIS_CLASS, c);
118: }
119:
120: public void testGetClass4() {
121: ClassUtil util = startTest();
122:
123: Class c = util.getClass(BELIEF_CLASS, null);
124: assertEquals(
125: "getClass( Belief ) was found in the default classloader.",
126: null, c);
127: }
128:
129: public void testFlush1() {
130: ClassUtil util = startTest();
131:
132: // there really isn't a way to test that this works correctly,
133: // other than monitoring the GC and memory.
134: }
135:
136: public void testCreateObject1() {
137: ClassUtil util = startTest();
138:
139: Object o = util.createObject(InnerClass.class.getName());
140: assertNotNull("getClass( " + InnerClass.class.getName()
141: + " ) returned null.", o);
142: assertEquals(
143: "Did not load the class from the default classloader.",
144: InnerClass.class, o.getClass());
145: }
146:
147: public void testCreateObject2() {
148: ClassUtil util = startTest();
149:
150: // since this class does not have a default constructor,
151: // this must fail
152: Object o = util.createObject(THIS_CLASS.getName());
153: assertEquals(
154: "Incorrectly loaded the class which doesn't have a "
155: + "default constructor.", null, o);
156: }
157:
158: public void testCreateObject3() {
159: ClassUtil util = startTest();
160:
161: // since ClassUtil cannot have a public constructor, this must fail
162: // due to lack-of permissions
163: Object o = util.createObject(ClassUtil.class.getName());
164: if (o != null) {
165: System.out
166: .println("Warning - your JVM allowed the creation of "
167: + "an object which has a 'protected' level default constructor.");
168: }
169: /* see above warning
170: assertEquals(
171: "createObject( ClassUtil ) did not fail due to invalid access "+
172: "permissions.",
173: null, o );
174: */
175: }
176:
177: public void testCreateObject4() {
178: ClassUtil util = startTest();
179:
180: Object o = util.createObject(BELIEF_CLASS);
181: assertEquals("createObject( " + BELIEF_CLASS
182: + " ) incorrectly found a class.", null, o);
183: }
184:
185: public void testCreateObject5() {
186: ClassUtil util = startTest();
187:
188: Object o = util.createObject(InnerClass.class.getName(), null);
189: assertNotNull("getClass( " + InnerClass.class.getName()
190: + " ) returned null.", o);
191: assertEquals(
192: "Did not load the class from the default classloader.",
193: InnerClass.class, o.getClass());
194: }
195:
196: public void testCreateObject6() {
197: ClassUtil util = startTest();
198:
199: // since this class does not have a default constructor,
200: // this must fail
201: Object o = util.createObject(THIS_CLASS.getName(), null);
202: assertEquals(
203: "Incorrectly loaded the class which doesn't have a "
204: + "default constructor.", null, o);
205: }
206:
207: public void testCreateObject7() {
208: ClassUtil util = startTest();
209:
210: assertEquals(
211: "Did not return a null instance with a null class.",
212: null, util.createObject((Class) null));
213: }
214:
215: public void testCreateObject8() {
216: ClassUtil util = startTest();
217:
218: Object o = util.createObject(InnerClass.class);
219: assertNotNull("Returned null from creating a proper class.", o);
220: assertEquals("Did not create a proper class.",
221: InnerClass.class, o.getClass());
222: }
223:
224: public void tsetIsJdk2Compatible1() {
225: ClassUtil util = startTest();
226:
227: // don't know how to test this other than what ClassUtil does.
228: util.isJdk2Compatible();
229: }
230:
231: //---------------------------------
232:
233: // we must assert that the ClassUtil is fresh to avoid incorrect
234: // behavior.
235: protected ClassUtil startTest() {
236: ClassUtil util = ClassUtil.getInstance();
237: util.flush();
238: return util;
239: }
240: }
|