KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > iftc > InnerClassNameEUTest


1 /*
2  * @(#)JUnitTestSuiteEUTest.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.junit.v1.iftc;
28
29 //import net.sourceforge.groboutils.testing.junitlog.v1.*;
30
import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 import java.io.IOException JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37
38 /**
39  * Tests the functionality of the JUnit TestSuite class for conformance to
40  * expected behaviors.
41  *
42  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
43  * @since October 31, 2002
44  * @version $Date: 2003/02/10 22:52:22 $
45  */

46 public class InnerClassNameEUTest extends TestCase
47 {
48     //-------------------------------------------------------------------------
49
// Standard JUnit Class-specific declarations
50

51     private static final Class JavaDoc THIS_CLASS = InnerClassNameEUTest.class;
52     
53     public InnerClassNameEUTest( String JavaDoc name )
54     {
55         super( name );
56     }
57
58     
59     static boolean IS_JDK_12_COMPAT = true;
60     static {
61         try
62         {
63             Class.forName("java.lang.ThreadLocal");
64         }
65         catch (ThreadDeath JavaDoc td)
66         {
67             throw td;
68         }
69         catch (Throwable JavaDoc t)
70         {
71             IS_JDK_12_COMPAT = false;
72         }
73     }
74
75
76     //-------------------------------------------------------------------------
77
// Tests
78

79     /*
80      * These tests show off very interesting behavior:
81      * - JDK 1.1 always returns NULL for the "getDeclaringClass()"
82      * call.
83      * - JDK 1.2-1.4 correctly report the declared class for all classes
84      * except anonymous inner classes.
85      */

86     
87     
88     
89     public void testGetDeclaringClass1()
90     {
91         Class JavaDoc owner = THIS_CLASS.getDeclaringClass();
92         assertNull(
93             "Test class has a declaring class.",
94             owner );
95     }
96     
97     
98     private class MyClass1 {}
99     
100     public void testGetDeclaringClass2()
101     {
102         Class JavaDoc c = MyClass1.class;
103         Class JavaDoc owner = c.getDeclaringClass();
104         if (IS_JDK_12_COMPAT)
105         {
106             assertNotNull(
107                 "Inner class has no declaring class.",
108                 owner );
109             assertEquals(
110                 "Did not return expected owning class.",
111                 THIS_CLASS,
112                 owner
113                 );
114         }
115         else
116         {
117             assertNull(
118                 "Inner class's owner is not null.",
119                 owner );
120         }
121     }
122     
123     
124     private static class MyClass2 {}
125     
126     public void testGetDeclaringClass3()
127     {
128         Class JavaDoc c = MyClass2.class;
129         Class JavaDoc owner = c.getDeclaringClass();
130         if (IS_JDK_12_COMPAT)
131         {
132             assertNotNull(
133                 "Static inner class has no declaring class.",
134                 owner );
135             assertEquals(
136                 "Did not return expected owning class.",
137                 THIS_CLASS,
138                 owner
139                 );
140         }
141         else
142         {
143             assertNull(
144                 "Static inner class' owner is not null.",
145                 owner );
146         }
147     }
148     
149     
150     public void testGetDeclaringClass4()
151     {
152         /* The following test works on JDK 1.1.8 and JDK 1.4.0, but fails for
153            JDK 1.2.2 (all on Windows). It's all so confusing!!!
154         Object o = new MyClass2() {};
155         Class c = o.getClass();
156         Class owner = c.getDeclaringClass();
157         
158         // Not really what you expect, is it?
159         assertNull(
160             "Anonymous inner class has a declaring class.",
161             owner );
162         */

163     }
164     
165     
166     
167     
168     
169     //-------------------------------------------------------------------------
170
// Standard JUnit declarations
171

172     
173     public static Test suite()
174     {
175         TestSuite suite = new TestSuite( THIS_CLASS );
176         
177         return suite;
178     }
179     
180     public static void main( String JavaDoc[] args )
181     {
182         String JavaDoc[] name = { THIS_CLASS.getName() };
183         
184         // junit.textui.TestRunner.main( name );
185
// junit.swingui.TestRunner.main( name );
186

187         junit.textui.TestRunner.main( name );
188     }
189     
190     
191     /**
192      *
193      * @exception Exception thrown under any exceptional condition.
194      */

195     protected void setUp() throws Exception JavaDoc
196     {
197         super.setUp();
198         
199         // set ourself up
200
}
201     
202     
203     /**
204      *
205      * @exception Exception thrown under any exceptional condition.
206      */

207     protected void tearDown() throws Exception JavaDoc
208     {
209         // tear ourself down
210

211         
212         super.tearDown();
213     }
214 }
215
216
Popular Tags