KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ClassUtilUTest.java 0.9.0 17-APR-2001 - 14:40
3  *
4  * Copyright (C) 2001,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 junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33
34 /**
35  * This is the off-line version of the ClassUtil unit tests.
36  * As insurance, this tests to make sure that the online test version applet is
37  * not in the current classpath.
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 April 17, 2001 (GroboUtils Alpha 0.9.0)
42  */

43 public class ClassUtilUTest extends TestCase
44 {
45     private static final Class JavaDoc THIS_CLASS = ClassUtilUTest.class;
46     
47     public ClassUtilUTest( String JavaDoc name )
48     {
49         super( name );
50     }
51     
52     public static Test suite()
53     {
54         TestSuite suite = new TestSuite( THIS_CLASS );
55         
56         return suite;
57     }
58     
59     public static void main( String JavaDoc[] args )
60     {
61         String JavaDoc[] name = { THIS_CLASS.getName() };
62         
63         // junit.textui.TestRunner.main( name );
64
// junit.swingui.TestRunner.main( name );
65

66         junit.textui.TestRunner.main( name );
67     }
68     
69     protected void setUp() throws Exception JavaDoc
70     {
71         super.setUp();
72         
73         // set ourself up
74
}
75     
76     
77     protected void tearDown() throws Exception JavaDoc
78     {
79         // tear ourself down
80

81         super.tearDown();
82     }
83
84     
85     
86     public void testInstantiate1()
87     {
88         assertNotNull( "Singleton method returned null.",
89             ClassUtil.getInstance() );
90     }
91     
92     
93     private static final String JavaDoc BELIEF_CLASS = "BeliefOfTheDay";
94     
95     
96     public static class InnerClass
97     {
98         public InnerClass()
99         {
100             // do nothing
101
}
102     }
103     
104     
105     public void testGetClass1()
106     {
107         ClassUtil util = startTest();
108         
109         Class JavaDoc c = util.getClass( THIS_CLASS.getName() );
110         assertNotNull( "getClass( "+THIS_CLASS.getName()+" ) returned null.",
111             c );
112         assertEquals( "Did not load the class from the default classloader.",
113             THIS_CLASS, c );
114     }
115     
116     
117     public void testGetClass2()
118     {
119         ClassUtil util = startTest();
120         
121         Class JavaDoc c = util.getClass( BELIEF_CLASS );
122         assertEquals(
123             "getClass( Belief ) was found in the default classloader.",
124             null, c );
125     }
126     
127     
128     public void testGetClass3()
129     {
130         ClassUtil util = startTest();
131         
132         Class JavaDoc c = util.getClass( THIS_CLASS.getName(), null );
133         assertNotNull( "getClass( "+THIS_CLASS.getName()+" ) returned null.",
134             c );
135         assertEquals( "Did not load the class from the default classloader.",
136             THIS_CLASS, c );
137     }
138     
139     
140     public void testGetClass4()
141     {
142         ClassUtil util = startTest();
143         
144         Class JavaDoc c = util.getClass( BELIEF_CLASS, null );
145         assertEquals(
146             "getClass( Belief ) was found in the default classloader.",
147             null, c );
148     }
149     
150     
151     public void testFlush1()
152     {
153         ClassUtil util = startTest();
154         
155         // there really isn't a way to test that this works correctly,
156
// other than monitoring the GC and memory.
157
}
158     
159     
160     public void testCreateObject1()
161     {
162         ClassUtil util = startTest();
163         
164         Object JavaDoc o = util.createObject( InnerClass.class.getName() );
165         assertNotNull( "getClass( "+InnerClass.class.getName()+
166             " ) returned null.",
167             o );
168         assertEquals( "Did not load the class from the default classloader.",
169             InnerClass.class, o.getClass() );
170     }
171     
172     
173     public void testCreateObject2()
174     {
175         ClassUtil util = startTest();
176         
177         // since this class does not have a default constructor,
178
// this must fail
179
Object JavaDoc o = util.createObject( THIS_CLASS.getName() );
180         assertEquals( "Incorrectly loaded the class which doesn't have a "+
181             "default constructor.",
182             null, o );
183     }
184     
185     
186     public void testCreateObject3()
187     {
188         ClassUtil util = startTest();
189         
190         // since ClassUtil cannot have a public constructor, this must fail
191
// due to lack-of permissions
192
Object JavaDoc o = util.createObject( ClassUtil.class.getName() );
193         if (o != null)
194         {
195             System.out.println("Warning - your JVM allowed the creation of "+
196                 "an object which has a 'protected' level default constructor.");
197         }
198         /* see above warning
199         assertEquals(
200             "createObject( ClassUtil ) did not fail due to invalid access "+
201             "permissions.",
202             null, o );
203         */

204     }
205     
206     
207     public void testCreateObject4()
208     {
209         ClassUtil util = startTest();
210         
211         Object JavaDoc o = util.createObject( BELIEF_CLASS );
212         assertEquals(
213             "createObject( "+BELIEF_CLASS+" ) incorrectly found a class.",
214             null, o );
215     }
216     
217     
218     public void testCreateObject5()
219     {
220         ClassUtil util = startTest();
221         
222         Object JavaDoc o = util.createObject( InnerClass.class.getName(), null );
223         assertNotNull( "getClass( "+InnerClass.class.getName()+
224             " ) returned null.",
225             o );
226         assertEquals( "Did not load the class from the default classloader.",
227             InnerClass.class, o.getClass() );
228     }
229     
230     
231     public void testCreateObject6()
232     {
233         ClassUtil util = startTest();
234         
235         // since this class does not have a default constructor,
236
// this must fail
237
Object JavaDoc o = util.createObject( THIS_CLASS.getName(), null );
238         assertEquals( "Incorrectly loaded the class which doesn't have a "+
239             "default constructor.",
240             null, o );
241     }
242     
243     
244     
245     public void testCreateObject7()
246     {
247         ClassUtil util = startTest();
248         
249         assertEquals( "Did not return a null instance with a null class.",
250             null, util.createObject( (Class JavaDoc)null ) );
251     }
252     
253     
254     public void testCreateObject8()
255     {
256         ClassUtil util = startTest();
257         
258         Object JavaDoc o = util.createObject( InnerClass.class );
259         assertNotNull( "Returned null from creating a proper class.", o );
260         assertEquals( "Did not create a proper class.",
261             InnerClass.class, o.getClass() );
262     }
263     
264     
265     public void tsetIsJdk2Compatible1()
266     {
267         ClassUtil util = startTest();
268         
269         // don't know how to test this other than what ClassUtil does.
270
util.isJdk2Compatible();
271     }
272     
273     //---------------------------------
274

275     
276     // we must assert that the ClassUtil is fresh to avoid incorrect
277
// behavior.
278
protected ClassUtil startTest()
279     {
280         ClassUtil util = ClassUtil.getInstance();
281         util.flush();
282         return util;
283     }
284 }
285
Popular Tags