01: package com.reeltwo.jumble.fast;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.PrintStream;
05:
06: import junit.framework.Test;
07: import junit.framework.TestCase;
08: import junit.framework.TestResult;
09: import junit.framework.TestSuite;
10:
11: /**
12: * Tests the corresponding class
13: *
14: * @author Tin Pavlinic
15: * @version $Revision: 500 $
16: */
17: public class TimingTestSuiteTest extends TestCase {
18:
19: private static final int MED_DELAY = 500;
20: private static final int LONG_DELAY = 2000;
21:
22: /**
23: * <code>TimedTests</code> is a small set of tests that take varying
24: * times to execute.
25: */
26: public static class TimedTests extends TestCase {
27: // Warning, the declaration order of these tests is important to testGetOrder below
28: public final void testMedium() throws Exception {
29: System.out.println("Medium");
30: Thread.sleep(MED_DELAY);
31: }
32:
33: public final void testLong() throws Exception {
34: System.out.println("Long");
35: Thread.sleep(LONG_DELAY);
36: }
37:
38: public final void testShort() {
39: System.out.println("Short");
40: }
41: }
42:
43: private TimingTestSuite mSuite;
44:
45: private TestResult mResult;
46:
47: protected void setUp() throws Exception {
48: mSuite = new TimingTestSuite(getClass().getClassLoader(),
49: new String[] { TimedTests.class.getName() });
50: mResult = new TestResult();
51: PrintStream oldOut = System.out;
52: System.setOut(new PrintStream(new ByteArrayOutputStream()));
53: try {
54: mSuite.run(mResult);
55: } finally {
56: System.setOut(oldOut);
57: }
58: }
59:
60: protected void tearDown() {
61: mSuite = null;
62: mResult = null;
63: }
64:
65: public final void testGetOrder() {
66: TestOrder order = mSuite.getOrder(true);
67: assertEquals(3, order.getTestCount());
68: assertEquals(2, order.getTestIndex(0));
69: assertEquals(0, order.getTestIndex(1));
70: assertEquals(1, order.getTestIndex(2));
71: // A bit dangerous
72: assertTrue(MED_DELAY + LONG_DELAY <= mSuite.getTotalRuntime());
73: assertTrue(mSuite.getTotalRuntime() < (MED_DELAY + LONG_DELAY * 1.5));
74: }
75:
76: public final void testResult() {
77: assertEquals(3, mResult.runCount());
78: assertEquals(0, mResult.errorCount());
79: assertEquals(0, mResult.failureCount());
80: }
81:
82: public static Test suite() {
83: TestSuite suite = new TestSuite(TimingTestSuiteTest.class);
84: return suite;
85: }
86:
87: public static void main(String[] args) {
88: junit.textui.TestRunner.run(suite());
89: }
90: }
|