KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > classes > v1 > ClassLoadHelperUTest


1 /*
2  * @(#)ClassLoadHelperUTest.java
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;
28
29 import org.easymock.EasyMock;
30 import org.easymock.MockControl;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35
36 /**
37  * Tests the ClassLoadHelper class.
38  *
39  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
40  * @version $Date: 2003/02/10 22:52:38 $
41  * @since March 1, 2002
42  */

43 public class ClassLoadHelperUTest extends TestCase
44 {
45     //-------------------------------------------------------------------------
46
// Standard JUnit Class-specific declarations
47

48     private static final Class JavaDoc THIS_CLASS = ClassLoadHelperUTest.class;
49     
50     public ClassLoadHelperUTest( String JavaDoc name )
51     {
52         super( name );
53     }
54
55     
56     //-------------------------------------------------------------------------
57
// setup
58

59     /**
60      *
61      * @exception Exception thrown under any exceptional condition.
62      */

63     protected void setUp() throws Exception JavaDoc
64     {
65         super.setUp();
66         
67         // set ourself up
68
}
69
70
71     //-------------------------------------------------------------------------
72
// Tests
73

74     
75     public void testConstructor1()
76     {
77         new ClassLoadHelper();
78     }
79     
80     
81     public void testConstructor2()
82     {
83         try
84         {
85             new ClassLoadHelper( (Class JavaDoc)null );
86             fail("Did not throw NullPointerException");
87         }
88         catch (NullPointerException JavaDoc npe)
89         {
90             // check exception?
91
}
92     }
93     
94     
95     public void testConstructor2a()
96     {
97         new ClassLoadHelper( (ClassLoader JavaDoc)null );
98     }
99     
100     
101     public void testConstructor3()
102     {
103         new ClassLoadHelper( createClassLoader() );
104     }
105     
106     
107     public void testGetClass1()
108     {
109         ClassLoadHelper clh = new ClassLoadHelper();
110         Class JavaDoc c = clh.getClass( null );
111         assertNull(
112             "Null string should return null.",
113             c );
114     }
115     
116     
117     private static final String JavaDoc BAD_CLASS_NAME_1 = "";
118     // totally wrong class name - can never exist due to naming rules.
119
private static final String JavaDoc BAD_CLASS_NAME_2 = "java.lang.2-Not A Class";
120     
121     public void testGetClass2()
122     {
123         ClassLoadHelper clh = new ClassLoadHelper();
124         
125         Class JavaDoc c = clh.getClass( BAD_CLASS_NAME_1 );
126         assertNull(
127             "Bad class name should return null.",
128             c );
129     }
130     
131     public void testGetClass3()
132     {
133         ClassLoadHelper clh = new ClassLoadHelper();
134         
135         Class JavaDoc c = clh.getClass( BAD_CLASS_NAME_2 );
136         assertNull(
137             "Bad class name should return null.",
138             c );
139     }
140     
141     
142     public void testGetClass4()
143     {
144         ClassLoadHelper clh = new ClassLoadHelper();
145         
146         Class JavaDoc c = clh.getClass( String JavaDoc.class.getName() );
147         assertEquals(
148             "Should have returned the String class with the same classloader "+
149             "as the one that loaded ourself.",
150             String JavaDoc.class,
151             c );
152     }
153     
154     
155     public interface MyInterface {}
156     public class MyInnerClass {}
157     public static class MyGoodClass {}
158     
159     public void testCreateObjectS1()
160     {
161         ClassLoadHelper clh = new ClassLoadHelper();
162         
163         Object JavaDoc o = clh.createObject( (String JavaDoc)null );
164         assertNull(
165             "Null string should return null.",
166             o );
167     }
168     
169     public void testCreateObjectS2()
170     {
171         ClassLoadHelper clh = new ClassLoadHelper();
172         
173         // Class that doesn't exist
174
Object JavaDoc o = clh.createObject( BAD_CLASS_NAME_1 );
175         assertNull(
176             "Bad class name should return null.",
177             o );
178     }
179     
180     public void testCreateObjectS3()
181     {
182         ClassLoadHelper clh = new ClassLoadHelper();
183         
184         // Class that doesn't exist
185
Object JavaDoc o = clh.createObject( BAD_CLASS_NAME_2 );
186         assertNull(
187             "Bad class name should return null.",
188             o );
189     }
190     
191     public void testCreateObjectS4()
192     {
193         ClassLoadHelper clh = new ClassLoadHelper();
194         
195         // Class that can't be instantiated
196
Object JavaDoc o = clh.createObject( MyInterface.class.getName() );
197         assertNull(
198             "Interface should return null.",
199             o );
200     }
201     
202     public void testCreateObjectS5()
203     {
204         ClassLoadHelper clh = new ClassLoadHelper();
205         
206         // Class that can't be instantiated from this context
207
Object JavaDoc o = clh.createObject( MyInnerClass.class.getName() );
208         assertNull(
209             "Inner class should return null.",
210             o );
211     }
212     
213     public void testCreateObjectS6()
214     {
215         ClassLoadHelper clh = new ClassLoadHelper();
216         
217         // Class that can't be instantiated due to no default constructor
218
Object JavaDoc o = clh.createObject( this.getClass().getName() );
219         assertNull(
220             "No-default constructor class should return null.",
221             o );
222     }
223     
224     public void testCreateObjectS7()
225     {
226         ClassLoadHelper clh = new ClassLoadHelper();
227         
228         Object JavaDoc o = clh.createObject( MyGoodClass.class.getName() );
229         assertNotNull(
230             "Good class should not return null.",
231             o );
232         assertEquals(
233             "Good class should be of the correct class.",
234             MyGoodClass.class,
235             o.getClass() );
236     }
237
238     
239     public void testCreateObjectSZ1()
240     {
241         ClassLoadHelper clh = new ClassLoadHelper();
242         
243         // does not throw exception
244
Object JavaDoc o = clh.createObject( (String JavaDoc)null, false );
245         assertNull(
246             "Null string should return null.",
247             o );
248     }
249     
250     public void testCreateObjectSZ2()
251     {
252         ClassLoadHelper clh = new ClassLoadHelper();
253         
254         // Class that doesn't exist
255
try
256         {
257             Object JavaDoc o = clh.createObject( BAD_CLASS_NAME_1, false );
258             fail( "Did not throw an IllegalStateException: retrieved "+o );
259         }
260         catch (IllegalStateException JavaDoc ise)
261         {
262             // inspect exception?
263
}
264     }
265     
266     public void testCreateObjectSZ3()
267     {
268         ClassLoadHelper clh = new ClassLoadHelper();
269         
270         // Class that doesn't exist
271
try
272         {
273             Object JavaDoc o = clh.createObject( BAD_CLASS_NAME_2, false );
274             fail( "Did not throw an IllegalStateException: retrieved "+o );
275         }
276         catch (IllegalStateException JavaDoc ise)
277         {
278             // inspect exception?
279
}
280     }
281     
282     public void testCreateObjectSZ4()
283     {
284         ClassLoadHelper clh = new ClassLoadHelper();
285         
286         // Class that can't be instantiated
287
try
288         {
289             Object JavaDoc o = clh.createObject( MyInterface.class.getName(), false );
290             fail( "Did not throw an IllegalStateException: retrieved "+o );
291         }
292         catch (IllegalStateException JavaDoc ise)
293         {
294             // inspect exception?
295
}
296     }
297     
298     public void testCreateObjectSZ5()
299     {
300         ClassLoadHelper clh = new ClassLoadHelper();
301         
302         // Class that can't be instantiated from this context
303
try
304         {
305             Object JavaDoc o = clh.createObject( MyInnerClass.class.getName(), false );
306             fail( "Did not throw an IllegalStateException: retrieved "+o );
307         }
308         catch (IllegalStateException JavaDoc ise)
309         {
310             // inspect exception?
311
}
312     }
313     
314     public void testCreateObjectSZ6()
315     {
316         ClassLoadHelper clh = new ClassLoadHelper();
317         
318         // Class that can't be instantiated due to no default constructor
319
try
320         {
321             Object JavaDoc o = clh.createObject( this.getClass().getName(), false );
322             fail( "Did not throw an IllegalStateException." );
323         }
324         catch (IllegalStateException JavaDoc ise)
325         {
326             // inspect exception?
327
}
328     }
329     
330     public void testCreateObjectSZ7()
331     {
332         ClassLoadHelper clh = new ClassLoadHelper();
333         
334         Object JavaDoc o = clh.createObject( MyGoodClass.class.getName(), false );
335         assertNotNull(
336             "Good class should not return null.",
337             o );
338         assertEquals(
339             "Good class should be of the correct class.",
340             MyGoodClass.class,
341             o.getClass() );
342     }
343     
344     
345     
346     
347     //-------------------------------------------------------------------------
348
// Helpers
349

350     
351     protected ClassLoader JavaDoc createClassLoader()
352     {
353         return new ClassLoader JavaDoc() {
354             public Class JavaDoc loadClass( String JavaDoc s, boolean f )
355             {
356                 return null;
357             }
358         };
359     }
360     
361     
362     //-------------------------------------------------------------------------
363
// Standard JUnit declarations
364

365     
366     public static Test suite()
367     {
368         TestSuite suite = new TestSuite( THIS_CLASS );
369         
370         // Test the implementation's interface conformity.
371
/*
372         suite.addTest( IxUTestI.suite(
373             new ImplementationCreator[] {
374                 new ImplementationCreator() {
375                     public Object createImplementedObject() {
376                         // XXXXXXXXXXXXXXXXXXXXXXXX
377                     }
378                 },
379             } ) );
380         */

381         
382         return suite;
383     }
384     
385     public static void main( String JavaDoc[] args )
386     {
387         String JavaDoc[] name = { THIS_CLASS.getName() };
388         
389         // junit.textui.TestRunner.main( name );
390
// junit.swingui.TestRunner.main( name );
391

392         junit.textui.TestRunner.main( name );
393     }
394     
395     
396     /**
397      *
398      * @exception Exception thrown under any exceptional condition.
399      */

400     protected void tearDown() throws Exception JavaDoc
401     {
402         // tear ourself down
403

404         
405         super.tearDown();
406     }
407 }
408
409
Popular Tags