001: package com.clarkware.junitperf;
002:
003: import junit.framework.*;
004:
005: /**
006: * The <code>TestFactoryTest</code> is a <code>TestCase</code>
007: * for the <code>TestFactory</code> and <code>TestMethodFactory</code>
008: * classes.
009: *
010: * @author <a href="mailto:mike@clarkware.com">Mike Clark</a>
011: * @author <a href="http://www.clarkware.com">Clarkware Consulting, Inc.</a>
012: *
013: * @see junit.framework.TestCase
014: */
015:
016: public class TestFactoryTest extends TestCase {
017:
018: private TestSuite _allMethodsTestSuite;
019: private TestSuite _oneMethodTestSuite;
020:
021: private Class _testClass;
022:
023: public TestFactoryTest(String name) {
024: super (name);
025:
026: _testClass = com.clarkware.junitperf.MockTestFactoryTest.class;
027:
028: _allMethodsTestSuite = new TestSuite(_testClass);
029:
030: _oneMethodTestSuite = new TestSuite();
031: _oneMethodTestSuite.addTest(new MockTestFactoryTest(
032: "testSuccess"));
033: }
034:
035: public void testAllTestMethods() {
036:
037: TestFactory testFactory = new TestFactory(_testClass);
038:
039: assertEquals(2, testFactory.countTestCases());
040:
041: assertEqualsTestSuite(_allMethodsTestSuite, testFactory
042: .getTest());
043:
044: TestResult result = new TestResult();
045: testFactory.run(result);
046:
047: assertEquals(2, result.runCount());
048: assertEquals(0, result.errorCount());
049: assertEquals(1, result.failureCount());
050: }
051:
052: public void testAllTestMethodsSameSuiteForSameThread() {
053:
054: TestFactory testFactory = new TestFactory(_testClass);
055:
056: Test test1 = testFactory.getTest();
057: Test test2 = testFactory.getTest();
058:
059: assertSame(test1, test2);
060:
061: assertEqualsTestSuite(_allMethodsTestSuite, test1);
062: assertEqualsTestSuite(_allMethodsTestSuite, test2);
063: }
064:
065: public void testAllTestMethodsDifferentSuiteForDifferentThread() {
066:
067: TestFactory testFactory = new TestFactory(_testClass);
068:
069: MockRunnable runner1 = new MockRunnable(testFactory);
070: MockRunnable runner2 = new MockRunnable(testFactory);
071:
072: Thread thread1 = new Thread(runner1);
073: Thread thread2 = new Thread(runner2);
074:
075: thread1.start();
076: thread2.start();
077:
078: sleep();
079:
080: Test test1 = runner1.getTest();
081: Test test2 = runner2.getTest();
082:
083: assertTrue(!(test1 == test2));
084:
085: assertEqualsTestSuite(_allMethodsTestSuite, test1);
086: assertEqualsTestSuite(_allMethodsTestSuite, test2);
087: }
088:
089: public void testOneTestMethodSuccess() {
090:
091: TestMethodFactory testFactory = new TestMethodFactory(
092: _testClass, "testSuccess");
093:
094: assertEquals(1, testFactory.countTestCases());
095:
096: assertEqualsTestSuite(_oneMethodTestSuite, testFactory
097: .getTest());
098:
099: TestResult result = new TestResult();
100: testFactory.run(result);
101:
102: assertEquals(1, result.runCount());
103: assertEquals(0, result.errorCount());
104: assertEquals(0, result.failureCount());
105: }
106:
107: public void testOneTestMethodFailure() {
108:
109: TestMethodFactory testFactory = new TestMethodFactory(
110: _testClass, "testFailure");
111:
112: assertEquals(1, testFactory.countTestCases());
113:
114: assertEqualsTestSuite(_oneMethodTestSuite, testFactory
115: .getTest());
116:
117: TestResult result = new TestResult();
118: testFactory.run(result);
119:
120: assertEquals(1, result.runCount());
121: assertEquals(0, result.errorCount());
122: assertEquals(1, result.failureCount());
123: }
124:
125: public void testOneTestMethodNoSuchMethod() {
126:
127: TestMethodFactory testFactory = new TestMethodFactory(
128: _testClass, "testFoo");
129:
130: TestResult result = new TestResult();
131: testFactory.run(result);
132:
133: assertEquals(1, result.runCount());
134: assertEquals(0, result.errorCount());
135: assertEquals(1, result.failureCount());
136: }
137:
138: public void testOneTestMethodSameSuiteForSameThread() {
139:
140: TestFactory testFactory = new TestMethodFactory(_testClass,
141: "testSuccess");
142:
143: Test test1 = testFactory.getTest();
144: Test test2 = testFactory.getTest();
145:
146: assertSame(test1, test2);
147:
148: assertEqualsTestSuite(_oneMethodTestSuite, test1);
149: assertEqualsTestSuite(_oneMethodTestSuite, test2);
150: }
151:
152: public void testOneTestMethodDifferentSuiteForDifferentThread() {
153:
154: TestFactory testFactory = new TestMethodFactory(_testClass,
155: "testSuccess");
156:
157: MockRunnable runner1 = new MockRunnable(testFactory);
158: MockRunnable runner2 = new MockRunnable(testFactory);
159:
160: Thread thread1 = new Thread(runner1);
161: Thread thread2 = new Thread(runner2);
162:
163: thread1.start();
164: thread2.start();
165:
166: sleep();
167:
168: Test test1 = runner1.getTest();
169: Test test2 = runner2.getTest();
170:
171: assertTrue(!(test1 == test2));
172:
173: assertEqualsTestSuite(_oneMethodTestSuite, test1);
174: assertEqualsTestSuite(_oneMethodTestSuite, test2);
175: }
176:
177: public void testClassNotATestCase() {
178:
179: try {
180:
181: TestFactory testFactory = new TestFactory(
182: java.lang.String.class);
183: fail("Class not assignable to TestCase!");
184:
185: testFactory = new TestMethodFactory(java.lang.String.class,
186: "");
187: fail("Class not assignable to TestCase!");
188:
189: } catch (IllegalArgumentException success) {
190: }
191: }
192:
193: protected void sleep() {
194: try {
195: Thread.sleep(250);
196: } catch (InterruptedException ignored) {
197: }
198: }
199:
200: protected void assertEqualsTestSuite(Test t1, Test t2) {
201:
202: assertTrue(t1 instanceof TestSuite);
203: assertTrue(t2 instanceof TestSuite);
204:
205: TestSuite suite1 = (TestSuite) t1;
206: TestSuite suite2 = (TestSuite) t2;
207:
208: assertEquals(suite1.countTestCases(), suite2.countTestCases());
209: assertEquals(suite1.getName(), suite2.getName());
210: }
211:
212: private static class MockRunnable implements Runnable {
213:
214: private TestFactory _factory;
215: private Test _test;
216:
217: public MockRunnable(TestFactory factory) {
218: _factory = factory;
219: }
220:
221: public void run() {
222: _test = _factory.getTest();
223: }
224:
225: public Test getTest() {
226: return _test;
227: }
228: }
229:
230: public static Test suite() {
231: return new TestSuite(TestFactoryTest.class);
232: }
233:
234: public static void main(String args[]) {
235: junit.textui.TestRunner.run(suite());
236: }
237: }
|