001: package com.reeltwo.jumble.fast;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.PrintStream;
005:
006: import com.reeltwo.jumble.mutation.Mutater;
007:
008: import junit.framework.Test;
009: import junit.framework.TestCase;
010: import junit.framework.TestResult;
011: import junit.framework.TestSuite;
012:
013: /**
014: * Tests the corresponding class.
015: *
016: * @author Sean A. Irvine
017: * @version $Revision: 500 $
018: */
019: public class JumbleTestSuiteTest extends TestCase {
020:
021: private static final int MED_DELAY = 500;
022: private static final int LONG_DELAY = 2000;
023:
024: /**
025: * <code>TimedTests</code> is a small set of tests that take varying
026: * times to execute.
027: */
028: public static class TimedTests extends TestCase {
029: // Warning, the declaration order of these tests is important to testGetOrder below
030: public final void testMedium() throws Exception {
031: System.out.println("Medium");
032: Thread.sleep(MED_DELAY);
033: }
034:
035: public final void testLong() throws Exception {
036: System.out.println("Long");
037: Thread.sleep(LONG_DELAY);
038: }
039:
040: public final void testShort() {
041: System.out.println("Short");
042: }
043: }
044:
045: public JumbleTestSuiteTest(String name) {
046: super (name);
047: }
048:
049: public static Test suite() {
050: TestSuite suite = new TestSuite(JumbleTestSuiteTest.class);
051: return suite;
052: }
053:
054: public static void main(String[] args) {
055: junit.textui.TestRunner.run(suite());
056: }
057:
058: public void testTestClass() {
059: assertTrue(JumbleTestSuite.run(
060: getClass().getClassLoader(),
061: new TestOrder(new Class[] { Mutater.class },
062: new long[] { 0 }), new FailedTestMap(), null,
063: null, 0, false).startsWith("PASS"));
064: }
065:
066: public void testX5T() {
067: assertTrue(JumbleTestSuite.run(
068: getClass().getClassLoader(),
069: new TestOrder(new Class[] { jumble.X5T.class },
070: new long[] { 0 }), null, null, null, 0, false)
071: .startsWith("PASS"));
072: }
073:
074: public void testX5TF() {
075: assertEquals("FAIL", JumbleTestSuite.run(getClass()
076: .getClassLoader(), new TestOrder(
077: new Class[] { jumble.X5TF.class }, new long[] { 0 }),
078: null, null, null, 0, false));
079: }
080:
081: public void testX5TY() {
082: assertTrue(JumbleTestSuite.run(
083: getClass().getClassLoader(),
084: new TestOrder(new Class[] { jumble.X5TY.class },
085: new long[] { 0, 1 }), new FailedTestMap(),
086: null, null, 0, false).startsWith("PASS"));
087: }
088:
089: public void testNULL() {
090: try {
091: JumbleTestSuite.run(getClass().getClassLoader(),
092: (TestOrder) null, null, null, null, 0, false);
093: fail("Took null");
094: } catch (NullPointerException e) {
095: // ok
096: }
097: }
098:
099: // This test shows a problem with the FlatTestSuite not having deterministic test ordering
100: // Seems to be in the way JUnit creates suites using reflection.. we should ensure that
101: // FlatTestSuite creates suites with tests in a known order, otherwise the TestOrder is serving
102: // no purpose.
103: // public void testX5TQ() {
104: // assertEquals("FAIL",
105: // JumbleTestSuite.run(ClassLoader.getSystemClassLoader(),
106: // new TestOrder(new Class[] {com.reeltwo.jumble.X5TQ.class },
107: // new long[] {0, 1, 2}), null, null, null, 0, false));
108: // }
109:
110: public final void testOrder() throws Exception {
111: PrintStream oldOut = System.out;
112:
113: // first run the tests to get timing information (throw away output)
114: TimingTestSuite timingSuite = new TimingTestSuite(getClass()
115: .getClassLoader(), new String[] { TimedTests.class
116: .getName() });
117: System.setOut(new PrintStream(new ByteArrayOutputStream()));
118: try {
119: timingSuite.run(new TestResult());
120: } finally {
121: System.setOut(oldOut);
122: }
123:
124: // The timed tests write to standard out so the easiest way
125: // to check the order is to hijack the output and read it
126: ByteArrayOutputStream ba = new ByteArrayOutputStream();
127: PrintStream out = new PrintStream(ba);
128:
129: System.setErr(out);
130: String s;
131: try {
132: s = JumbleTestSuite.run(getClass().getClassLoader(),
133: timingSuite.getOrder(true), null, null, null, 0,
134: true);
135: } finally {
136: System.setErr(oldOut);
137: }
138: assertTrue(s.startsWith("FAIL"));
139:
140: String errout = ba.toString();
141: int si = errout.indexOf("Short");
142: int mi = errout.indexOf("Medium");
143: int li = errout.indexOf("Long");
144: assertTrue(si >= 0);
145: assertTrue(mi >= 0);
146: assertTrue(li >= 0);
147: assertTrue((si < mi) && (mi < li));
148: }
149: }
|