KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > SubTestTestCaseUTest


1 /*
2  * @(#)SubTestTestCaseUTest.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;
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 import junit.framework.TestResult;
34 import junit.framework.AssertionFailedError;
35
36 import java.io.IOException JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38
39
40 /**
41  * Tests the SubTestTestCase class.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @since July 26, 2002
45  * @version $Date: 2003/02/10 22:52:23 $
46  */

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

52     private static final Class JavaDoc THIS_CLASS = SubTestTestCaseUTest.class;
53 // private static final IJUnitDocumentor LOG = (new JUnitLog(THIS_CLASS)).getDocumentor();
54

55     public SubTestTestCaseUTest( String JavaDoc name )
56     {
57         super( name );
58     }
59
60     
61
62
63     //-------------------------------------------------------------------------
64
// Tests
65

66     
67     public static class MyTestCase1 extends TestCase
68     {
69         public MyTestCase1( String JavaDoc name )
70         {
71             super( name );
72         }
73         
74         public void testFail()
75         {
76             fail("this should fail.");
77         }
78         
79         public static Test suite()
80         {
81             return new TestSuite( MyTestCase1.class );
82         }
83     }
84     
85     
86     public static class MyTestCase2 extends SubTestTestCase
87     {
88         public MyTestCase2( String JavaDoc name )
89         {
90             super( name );
91         }
92         
93         public void testFailTwice()
94         {
95             addSubTest( MyTestCase1.suite() );
96             fail( "this should fail too." );
97         }
98         
99         public static Test suite()
100         {
101             return new TestSuite( MyTestCase2.class );
102         }
103     }
104     
105     
106     public static class MyTestCase3 extends SubTestTestCase
107     {
108         boolean enteredOnce = false;
109         long createdTime = System.currentTimeMillis();
110         
111         public MyTestCase3( String JavaDoc name )
112         {
113             super( name );
114         }
115         
116         public void testReentryTwice()
117         {
118             try
119             {
120                 System.out.println("Entering Created Time="+createdTime);
121                 if (!this.enteredOnce)
122                 {
123                     this.enteredOnce = true;
124                     System.out.println("Running again Created Time="+createdTime);
125                     run( new TestResult() );
126                     System.out.println("Back from Created Time="+createdTime);
127                 }
128                 addSubTest( MyTestCase1.suite() );
129                 System.out.println("Leaving Created Time="+createdTime);
130             }
131             catch (RuntimeException JavaDoc re)
132             {
133                 re.printStackTrace();
134                 throw re;
135             }
136             catch (Error JavaDoc e)
137             {
138                 e.printStackTrace();
139                 throw e;
140             }
141         }
142         
143         public static Test suite()
144         {
145             return new TestSuite( MyTestCase3.class );
146         }
147     }
148     
149     
150     public void testSameResult1()
151     {
152         // ensure the same TestResult object is used for both test instances.
153
try
154         {
155             Test t = MyTestCase2.suite();
156             TestResult tr = new TestResult();
157             
158             t.run( tr );
159             
160             assertEquals(
161                 "Should have 2 failures now.",
162                 2,
163                 tr.failureCount() );
164             assertEquals(
165                 "Should have no errors now.",
166                 0,
167                 tr.errorCount() );
168             assertEquals(
169                 "Should have 2 tests now.",
170                 2,
171                 tr.runCount() );
172         }
173         catch (RuntimeException JavaDoc re)
174         {
175             re.printStackTrace();
176             throw re;
177         }
178         catch (Error JavaDoc e)
179         {
180             e.printStackTrace();
181             throw e;
182         }
183     }
184     
185     
186     public void testReentry1()
187     {
188         try
189         {
190             // ensure the same TestResult object is used for both test instances.
191

192             Test t = MyTestCase3.suite();
193             TestResult tr = new TestResult();
194             
195             // this should:
196
// 1. execute the testReentryTwice() method w/ tr
197
// 2. execute the testReentryTwice() method w/ a new TestResult
198
// 3. add a new fail test
199
// 4. return from testReentryTwice() w/ a new TestResult
200
// 5. add a new fail test
201
// 6. execute 2 fail tests.
202
// for a total of 2 fail tests and 1 testReentryTwice() for tr.
203
t.run( tr );
204             
205             assertEquals(
206                 "Should have 2 failures.",
207                 2,
208                 tr.failureCount() );
209             assertEquals(
210                 "Should have no errors now.",
211                 0,
212                 tr.errorCount() );
213             assertEquals(
214                 "Should have 3 tests now.",
215                 3,
216                 tr.runCount() );
217         }
218         catch (RuntimeException JavaDoc re)
219         {
220             re.printStackTrace();
221             throw re;
222         }
223         catch (Error JavaDoc e)
224         {
225             e.printStackTrace();
226             throw e;
227         }
228     }
229     
230     
231     
232     //-------------------------------------------------------------------------
233
// Helpers
234

235     
236     
237     //-------------------------------------------------------------------------
238
// Standard JUnit declarations
239

240     
241     public static Test suite()
242     {
243         try
244         {
245             TestSuite suite = new TestSuite( THIS_CLASS );
246             
247             return suite;
248         }
249         catch (RuntimeException JavaDoc re)
250         {
251             re.printStackTrace();
252             throw re;
253         }
254         catch (Error JavaDoc e)
255         {
256             e.printStackTrace();
257             throw e;
258         }
259     }
260     
261     public static void main( String JavaDoc[] args )
262     {
263         String JavaDoc[] name = { THIS_CLASS.getName() };
264         
265         // junit.textui.TestRunner.main( name );
266
// junit.swingui.TestRunner.main( name );
267

268         junit.textui.TestRunner.main( name );
269     }
270     
271     
272     /**
273      *
274      * @exception Exception thrown under any exceptional condition.
275      */

276     protected void setUp() throws Exception JavaDoc
277     {
278         super.setUp();
279         
280         // set ourself up
281
}
282     
283     
284     /**
285      *
286      * @exception Exception thrown under any exceptional condition.
287      */

288     protected void tearDown() throws Exception JavaDoc
289     {
290         // tear ourself down
291

292         
293         super.tearDown();
294     }
295 }
296
297
Popular Tags