001: /*
002: * @(#)TestClassCreatorUTest.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.parser;
028:
029: import junit.framework.Test;
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032: import junit.framework.AssertionFailedError;
033:
034: import java.io.IOException;
035: import java.lang.reflect.Method;
036:
037: /**
038: * Tests the TestClassCreator class.
039: *
040: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
041: * @since November 4, 2002
042: * @version $Date: 2003/02/10 22:52:24 $
043: */
044: public class TestClassCreatorUTest extends TestCase {
045: //-------------------------------------------------------------------------
046: // Standard JUnit Class-specific declarations
047:
048: private static final Class THIS_CLASS = TestClassCreatorUTest.class;
049:
050: public TestClassCreatorUTest(String name) {
051: super (name);
052: }
053:
054: //-------------------------------------------------------------------------
055: // Tests
056:
057: public void testConstructor1() {
058: try {
059: new TestClassCreator(null);
060: } catch (IllegalArgumentException e) {
061: // test exception?
062: }
063: }
064:
065: public void testConstructor2() {
066: new TestClassCreator(new JUnitOrigCreator());
067: }
068:
069: public void testWarnings1() {
070: TestClassCreator tcc = new TestClassCreator(
071: new JUnitOrigCreator());
072:
073: String s[] = tcc.getWarnings();
074:
075: assertNotNull("Returned null warnings array.", s);
076: assertEquals("Returned warnings array with elements.", 0,
077: s.length);
078: }
079:
080: public void testWarnings2() {
081: TestClassCreator tcc = new TestClassCreator(
082: new JUnitOrigCreator());
083:
084: tcc.warning("a");
085: String s[] = tcc.getWarnings();
086:
087: assertNotNull("Returned null warnings array.", s);
088: assertEquals(
089: "Returned warnings array with incorrect element count.",
090: 1, s.length);
091: assertEquals(
092: "Did not return warnings array with correct entry.",
093: "a", s[0]);
094: }
095:
096: public void testWarnings3() {
097: TestClassCreator tcc = new TestClassCreator(
098: new JUnitOrigCreator());
099:
100: tcc.warning("a");
101: tcc.warning("b");
102: String s[] = tcc.getWarnings();
103:
104: assertNotNull("Returned null warnings array.", s);
105: assertEquals(
106: "Returned warnings array with incorrect element count.",
107: 2, s.length);
108: assertEquals(
109: "Did not return warnings array with correct entry.",
110: "a", s[0]);
111: assertEquals(
112: "Did not return warnings array with correct entry.",
113: "b", s[1]);
114: }
115:
116: public void testClearWarnings1() {
117: TestClassCreator tcc = new TestClassCreator(
118: new JUnitOrigCreator());
119:
120: tcc.clearWarnings();
121: String s[] = tcc.getWarnings();
122:
123: assertNotNull("Returned null warnings array.", s);
124: assertEquals("Returned warnings array with elements.", 0,
125: s.length);
126: }
127:
128: public void testClearWarnings2() {
129: TestClassCreator tcc = new TestClassCreator(
130: new JUnitOrigCreator());
131:
132: tcc.warning("a");
133: tcc.clearWarnings();
134: String s[] = tcc.getWarnings();
135:
136: assertNotNull("Returned null warnings array.", s);
137: assertEquals("Returned warnings array with elements.", 0,
138: s.length);
139: }
140:
141: public void testClearWarnings3() {
142: TestClassCreator tcc = new TestClassCreator(
143: new JUnitOrigCreator());
144:
145: tcc.warning("a");
146: tcc.warning("b");
147: tcc.clearWarnings();
148: String s[] = tcc.getWarnings();
149:
150: assertNotNull("Returned null warnings array.", s);
151: assertEquals("Returned warnings array with elements.", 0,
152: s.length);
153: }
154:
155: public void testCreateWarningTests1() {
156: TestClassCreator tcc = new TestClassCreator(
157: new JUnitOrigCreator());
158:
159: tcc.warning("a");
160: tcc.warning("b");
161: Test t[] = tcc.createWarningTests(new TestClassParser(
162: THIS_CLASS));
163:
164: assertNotNull("Returned null warning test list.", t);
165: assertEquals(
166: "Returned warnings test list with incorrect element count.",
167: 2, t.length);
168: // need to test failures!!!
169:
170: // warnings should not have been cleared
171: String s[] = tcc.getWarnings();
172:
173: assertNotNull("Returned null warnings array.", s);
174: assertEquals(
175: "Returned warnings array with incorrect element count.",
176: 2, s.length);
177: assertEquals(
178: "Did not return warnings array with correct entry.",
179: "a", s[0]);
180: assertEquals(
181: "Did not return warnings array with correct entry.",
182: "b", s[1]);
183: }
184:
185: /*
186:
187:
188: public static class TesterNoTestMethods implements Test
189: {
190: public int countTestCases() { return 0; }
191: public void run( junit.framework.TestResult tr ) {}
192: }
193:
194:
195: public static class TesterOneTestMethod implements Test
196: {
197: public int countTestCases() { return 0; }
198: public void run( junit.framework.TestResult tr ) {}
199:
200: public void testMyTest() {}
201: }
202:
203:
204: private class StaticClass {}
205: public class InnerClass {}
206:
207: public void testGetTestMethods1()
208: {
209: TestClassParser tcp = new TestClassParser( String.class );
210: Method m[] = tcp.getTestMethods();
211: assertNotNull(
212: "Must never return null.",
213: m );
214: assertEquals(
215: "String should have no test methods.",
216: 0,
217: m.length );
218: assertTrue(
219: "Must never return the same array, but rather a copy.",
220: m != tcp.getTestMethods() );
221: }
222:
223: public void testGetTestMethods2()
224: {
225: TestClassParser tcp = new TestClassParser( Runnable.class );
226: Method m[] = tcp.getTestMethods();
227: assertNotNull(
228: "Must never return null.",
229: m );
230: assertEquals(
231: "Runnable should have no test methods.",
232: 0,
233: m.length );
234: assertTrue(
235: "Must never return the same array, but rather a copy.",
236: m != tcp.getTestMethods() );
237: }
238:
239: public void testGetTestMethods3()
240: {
241: TestClassParser tcp = new TestClassParser( StaticClass.class );
242: Method m[] = tcp.getTestMethods();
243: assertNotNull(
244: "Must never return null.",
245: m );
246: assertEquals(
247: "Runnable should have no test methods.",
248: 0,
249: m.length );
250: assertTrue(
251: "Must never return the same array, but rather a copy.",
252: m != tcp.getTestMethods() );
253: }
254:
255: public void testGetTestMethods4()
256: {
257: TestClassParser tcp = new TestClassParser( InnerClass.class );
258: Method m[] = tcp.getTestMethods();
259: assertNotNull(
260: "Must never return null.",
261: m );
262: assertEquals(
263: "Runnable should have no test methods.",
264: 0,
265: m.length );
266: assertTrue(
267: "Must never return the same array, but rather a copy.",
268: m != tcp.getTestMethods() );
269: }
270:
271: public void testGetTestMethods5()
272: {
273: TestClassParser tcp = new TestClassParser( TesterNoTestMethods.class );
274: Method m[] = tcp.getTestMethods();
275: assertNotNull(
276: "Must never return null.",
277: m );
278: assertEquals(
279: "Runnable should have no test methods.",
280: 0,
281: m.length );
282: assertTrue(
283: "Must never return the same array, but rather a copy.",
284: m != tcp.getTestMethods() );
285: }
286:
287: public void testGetTestMethods6()
288: {
289: TestClassParser tcp = new TestClassParser( TesterOneTestMethod.class );
290: Method m[] = tcp.getTestMethods();
291: assertNotNull(
292: "Must never return null.",
293: m );
294: assertEquals(
295: "Runnable should have one test method.",
296: 1,
297: m.length );
298: assertTrue(
299: "Must never return the same array, but rather a copy.",
300: m != tcp.getTestMethods() );
301: }
302:
303:
304: */
305:
306: //-------------------------------------------------------------------------
307: // Standard JUnit declarations
308:
309: public static Test suite() {
310: TestSuite suite = new TestSuite(THIS_CLASS);
311:
312: return suite;
313: }
314:
315: public static void main(String[] args) {
316: String[] name = { THIS_CLASS.getName() };
317:
318: // junit.textui.TestRunner.main( name );
319: // junit.swingui.TestRunner.main( name );
320:
321: junit.textui.TestRunner.main(name);
322: }
323:
324: /**
325: *
326: * @exception Exception thrown under any exceptional condition.
327: */
328: protected void setUp() throws Exception {
329: super .setUp();
330:
331: // set ourself up
332: }
333:
334: /**
335: *
336: * @exception Exception thrown under any exceptional condition.
337: */
338: protected void tearDown() throws Exception {
339: // tear ourself down
340:
341: super.tearDown();
342: }
343: }
|