001: /*
002: * @(#)ClassLoadHelperUTest.java
003: *
004: * Copyright (C) 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 org.easymock.EasyMock;
030: import org.easymock.MockControl;
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: /**
036: * Tests the ClassLoadHelper class.
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 March 1, 2002
041: */
042: public class ClassLoadHelperUTest extends TestCase {
043: //-------------------------------------------------------------------------
044: // Standard JUnit Class-specific declarations
045:
046: private static final Class THIS_CLASS = ClassLoadHelperUTest.class;
047:
048: public ClassLoadHelperUTest(String name) {
049: super (name);
050: }
051:
052: //-------------------------------------------------------------------------
053: // setup
054:
055: /**
056: *
057: * @exception Exception thrown under any exceptional condition.
058: */
059: protected void setUp() throws Exception {
060: super .setUp();
061:
062: // set ourself up
063: }
064:
065: //-------------------------------------------------------------------------
066: // Tests
067:
068: public void testConstructor1() {
069: new ClassLoadHelper();
070: }
071:
072: public void testConstructor2() {
073: try {
074: new ClassLoadHelper((Class) null);
075: fail("Did not throw NullPointerException");
076: } catch (NullPointerException npe) {
077: // check exception?
078: }
079: }
080:
081: public void testConstructor2a() {
082: new ClassLoadHelper((ClassLoader) null);
083: }
084:
085: public void testConstructor3() {
086: new ClassLoadHelper(createClassLoader());
087: }
088:
089: public void testGetClass1() {
090: ClassLoadHelper clh = new ClassLoadHelper();
091: Class c = clh.getClass(null);
092: assertNull("Null string should return null.", c);
093: }
094:
095: private static final String BAD_CLASS_NAME_1 = "";
096: // totally wrong class name - can never exist due to naming rules.
097: private static final String BAD_CLASS_NAME_2 = "java.lang.2-Not A Class";
098:
099: public void testGetClass2() {
100: ClassLoadHelper clh = new ClassLoadHelper();
101:
102: Class c = clh.getClass(BAD_CLASS_NAME_1);
103: assertNull("Bad class name should return null.", c);
104: }
105:
106: public void testGetClass3() {
107: ClassLoadHelper clh = new ClassLoadHelper();
108:
109: Class c = clh.getClass(BAD_CLASS_NAME_2);
110: assertNull("Bad class name should return null.", c);
111: }
112:
113: public void testGetClass4() {
114: ClassLoadHelper clh = new ClassLoadHelper();
115:
116: Class c = clh.getClass(String.class.getName());
117: assertEquals(
118: "Should have returned the String class with the same classloader "
119: + "as the one that loaded ourself.",
120: String.class, c);
121: }
122:
123: public interface MyInterface {
124: }
125:
126: public class MyInnerClass {
127: }
128:
129: public static class MyGoodClass {
130: }
131:
132: public void testCreateObjectS1() {
133: ClassLoadHelper clh = new ClassLoadHelper();
134:
135: Object o = clh.createObject((String) null);
136: assertNull("Null string should return null.", o);
137: }
138:
139: public void testCreateObjectS2() {
140: ClassLoadHelper clh = new ClassLoadHelper();
141:
142: // Class that doesn't exist
143: Object o = clh.createObject(BAD_CLASS_NAME_1);
144: assertNull("Bad class name should return null.", o);
145: }
146:
147: public void testCreateObjectS3() {
148: ClassLoadHelper clh = new ClassLoadHelper();
149:
150: // Class that doesn't exist
151: Object o = clh.createObject(BAD_CLASS_NAME_2);
152: assertNull("Bad class name should return null.", o);
153: }
154:
155: public void testCreateObjectS4() {
156: ClassLoadHelper clh = new ClassLoadHelper();
157:
158: // Class that can't be instantiated
159: Object o = clh.createObject(MyInterface.class.getName());
160: assertNull("Interface should return null.", o);
161: }
162:
163: public void testCreateObjectS5() {
164: ClassLoadHelper clh = new ClassLoadHelper();
165:
166: // Class that can't be instantiated from this context
167: Object o = clh.createObject(MyInnerClass.class.getName());
168: assertNull("Inner class should return null.", o);
169: }
170:
171: public void testCreateObjectS6() {
172: ClassLoadHelper clh = new ClassLoadHelper();
173:
174: // Class that can't be instantiated due to no default constructor
175: Object o = clh.createObject(this .getClass().getName());
176: assertNull("No-default constructor class should return null.",
177: o);
178: }
179:
180: public void testCreateObjectS7() {
181: ClassLoadHelper clh = new ClassLoadHelper();
182:
183: Object o = clh.createObject(MyGoodClass.class.getName());
184: assertNotNull("Good class should not return null.", o);
185: assertEquals("Good class should be of the correct class.",
186: MyGoodClass.class, o.getClass());
187: }
188:
189: public void testCreateObjectSZ1() {
190: ClassLoadHelper clh = new ClassLoadHelper();
191:
192: // does not throw exception
193: Object o = clh.createObject((String) null, false);
194: assertNull("Null string should return null.", o);
195: }
196:
197: public void testCreateObjectSZ2() {
198: ClassLoadHelper clh = new ClassLoadHelper();
199:
200: // Class that doesn't exist
201: try {
202: Object o = clh.createObject(BAD_CLASS_NAME_1, false);
203: fail("Did not throw an IllegalStateException: retrieved "
204: + o);
205: } catch (IllegalStateException ise) {
206: // inspect exception?
207: }
208: }
209:
210: public void testCreateObjectSZ3() {
211: ClassLoadHelper clh = new ClassLoadHelper();
212:
213: // Class that doesn't exist
214: try {
215: Object o = clh.createObject(BAD_CLASS_NAME_2, false);
216: fail("Did not throw an IllegalStateException: retrieved "
217: + o);
218: } catch (IllegalStateException ise) {
219: // inspect exception?
220: }
221: }
222:
223: public void testCreateObjectSZ4() {
224: ClassLoadHelper clh = new ClassLoadHelper();
225:
226: // Class that can't be instantiated
227: try {
228: Object o = clh.createObject(MyInterface.class.getName(),
229: false);
230: fail("Did not throw an IllegalStateException: retrieved "
231: + o);
232: } catch (IllegalStateException ise) {
233: // inspect exception?
234: }
235: }
236:
237: public void testCreateObjectSZ5() {
238: ClassLoadHelper clh = new ClassLoadHelper();
239:
240: // Class that can't be instantiated from this context
241: try {
242: Object o = clh.createObject(MyInnerClass.class.getName(),
243: false);
244: fail("Did not throw an IllegalStateException: retrieved "
245: + o);
246: } catch (IllegalStateException ise) {
247: // inspect exception?
248: }
249: }
250:
251: public void testCreateObjectSZ6() {
252: ClassLoadHelper clh = new ClassLoadHelper();
253:
254: // Class that can't be instantiated due to no default constructor
255: try {
256: Object o = clh.createObject(this .getClass().getName(),
257: false);
258: fail("Did not throw an IllegalStateException.");
259: } catch (IllegalStateException ise) {
260: // inspect exception?
261: }
262: }
263:
264: public void testCreateObjectSZ7() {
265: ClassLoadHelper clh = new ClassLoadHelper();
266:
267: Object o = clh.createObject(MyGoodClass.class.getName(), false);
268: assertNotNull("Good class should not return null.", o);
269: assertEquals("Good class should be of the correct class.",
270: MyGoodClass.class, o.getClass());
271: }
272:
273: //-------------------------------------------------------------------------
274: // Helpers
275:
276: protected ClassLoader createClassLoader() {
277: return new ClassLoader() {
278: public Class loadClass(String s, boolean f) {
279: return null;
280: }
281: };
282: }
283:
284: //-------------------------------------------------------------------------
285: // Standard JUnit declarations
286:
287: public static Test suite() {
288: TestSuite suite = new TestSuite(THIS_CLASS);
289:
290: // Test the implementation's interface conformity.
291: /*
292: suite.addTest( IxUTestI.suite(
293: new ImplementationCreator[] {
294: new ImplementationCreator() {
295: public Object createImplementedObject() {
296: // XXXXXXXXXXXXXXXXXXXXXXXX
297: }
298: },
299: } ) );
300: */
301:
302: return suite;
303: }
304:
305: public static void main(String[] args) {
306: String[] name = { THIS_CLASS.getName() };
307:
308: // junit.textui.TestRunner.main( name );
309: // junit.swingui.TestRunner.main( name );
310:
311: junit.textui.TestRunner.main(name);
312: }
313:
314: /**
315: *
316: * @exception Exception thrown under any exceptional condition.
317: */
318: protected void tearDown() throws Exception {
319: // tear ourself down
320:
321: super.tearDown();
322: }
323: }
|