001: /*
002: * @(#)SubTestTestCaseUTest.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.junit.v1;
028:
029: //import net.sourceforge.groboutils.testing.junitlog.v1.*;
030: import junit.framework.Test;
031: import junit.framework.TestCase;
032: import junit.framework.TestSuite;
033: import junit.framework.TestResult;
034: import junit.framework.AssertionFailedError;
035:
036: import java.io.IOException;
037: import java.lang.reflect.Method;
038:
039: /**
040: * Tests the SubTestTestCase class.
041: *
042: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
043: * @since July 26, 2002
044: * @version $Date: 2003/02/10 22:52:23 $
045: */
046: public class SubTestTestCaseUTest extends TestCase {
047: //-------------------------------------------------------------------------
048: // Standard JUnit Class-specific declarations
049:
050: private static final Class THIS_CLASS = SubTestTestCaseUTest.class;
051:
052: // private static final IJUnitDocumentor LOG = (new JUnitLog(THIS_CLASS)).getDocumentor();
053:
054: public SubTestTestCaseUTest(String name) {
055: super (name);
056: }
057:
058: //-------------------------------------------------------------------------
059: // Tests
060:
061: public static class MyTestCase1 extends TestCase {
062: public MyTestCase1(String name) {
063: super (name);
064: }
065:
066: public void testFail() {
067: fail("this should fail.");
068: }
069:
070: public static Test suite() {
071: return new TestSuite(MyTestCase1.class);
072: }
073: }
074:
075: public static class MyTestCase2 extends SubTestTestCase {
076: public MyTestCase2(String name) {
077: super (name);
078: }
079:
080: public void testFailTwice() {
081: addSubTest(MyTestCase1.suite());
082: fail("this should fail too.");
083: }
084:
085: public static Test suite() {
086: return new TestSuite(MyTestCase2.class);
087: }
088: }
089:
090: public static class MyTestCase3 extends SubTestTestCase {
091: boolean enteredOnce = false;
092: long createdTime = System.currentTimeMillis();
093:
094: public MyTestCase3(String name) {
095: super (name);
096: }
097:
098: public void testReentryTwice() {
099: try {
100: System.out.println("Entering Created Time="
101: + createdTime);
102: if (!this .enteredOnce) {
103: this .enteredOnce = true;
104: System.out.println("Running again Created Time="
105: + createdTime);
106: run(new TestResult());
107: System.out.println("Back from Created Time="
108: + createdTime);
109: }
110: addSubTest(MyTestCase1.suite());
111: System.out.println("Leaving Created Time="
112: + createdTime);
113: } catch (RuntimeException re) {
114: re.printStackTrace();
115: throw re;
116: } catch (Error e) {
117: e.printStackTrace();
118: throw e;
119: }
120: }
121:
122: public static Test suite() {
123: return new TestSuite(MyTestCase3.class);
124: }
125: }
126:
127: public void testSameResult1() {
128: // ensure the same TestResult object is used for both test instances.
129: try {
130: Test t = MyTestCase2.suite();
131: TestResult tr = new TestResult();
132:
133: t.run(tr);
134:
135: assertEquals("Should have 2 failures now.", 2, tr
136: .failureCount());
137: assertEquals("Should have no errors now.", 0, tr
138: .errorCount());
139: assertEquals("Should have 2 tests now.", 2, tr.runCount());
140: } catch (RuntimeException re) {
141: re.printStackTrace();
142: throw re;
143: } catch (Error e) {
144: e.printStackTrace();
145: throw e;
146: }
147: }
148:
149: public void testReentry1() {
150: try {
151: // ensure the same TestResult object is used for both test instances.
152:
153: Test t = MyTestCase3.suite();
154: TestResult tr = new TestResult();
155:
156: // this should:
157: // 1. execute the testReentryTwice() method w/ tr
158: // 2. execute the testReentryTwice() method w/ a new TestResult
159: // 3. add a new fail test
160: // 4. return from testReentryTwice() w/ a new TestResult
161: // 5. add a new fail test
162: // 6. execute 2 fail tests.
163: // for a total of 2 fail tests and 1 testReentryTwice() for tr.
164: t.run(tr);
165:
166: assertEquals("Should have 2 failures.", 2, tr
167: .failureCount());
168: assertEquals("Should have no errors now.", 0, tr
169: .errorCount());
170: assertEquals("Should have 3 tests now.", 3, tr.runCount());
171: } catch (RuntimeException re) {
172: re.printStackTrace();
173: throw re;
174: } catch (Error e) {
175: e.printStackTrace();
176: throw e;
177: }
178: }
179:
180: //-------------------------------------------------------------------------
181: // Helpers
182:
183: //-------------------------------------------------------------------------
184: // Standard JUnit declarations
185:
186: public static Test suite() {
187: try {
188: TestSuite suite = new TestSuite(THIS_CLASS);
189:
190: return suite;
191: } catch (RuntimeException re) {
192: re.printStackTrace();
193: throw re;
194: } catch (Error e) {
195: e.printStackTrace();
196: throw e;
197: }
198: }
199:
200: public static void main(String[] args) {
201: String[] name = { THIS_CLASS.getName() };
202:
203: // junit.textui.TestRunner.main( name );
204: // junit.swingui.TestRunner.main( name );
205:
206: junit.textui.TestRunner.main(name);
207: }
208:
209: /**
210: *
211: * @exception Exception thrown under any exceptional condition.
212: */
213: protected void setUp() throws Exception {
214: super .setUp();
215:
216: // set ourself up
217: }
218:
219: /**
220: *
221: * @exception Exception thrown under any exceptional condition.
222: */
223: protected void tearDown() throws Exception {
224: // tear ourself down
225:
226: super.tearDown();
227: }
228: }
|