diff options
Diffstat (limited to 'junit4/src/test/java/junit/tests')
39 files changed, 1730 insertions, 0 deletions
diff --git a/junit4/src/test/java/junit/tests/AllTests.java b/junit4/src/test/java/junit/tests/AllTests.java new file mode 100644 index 0000000..00bbe56 --- /dev/null +++ b/junit4/src/test/java/junit/tests/AllTests.java @@ -0,0 +1,23 @@ +package junit.tests; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * TestSuite that runs all the JUnit tests + * + */ +public class AllTests { + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() { + TestSuite suite= new TestSuite("Framework Tests"); + suite.addTest(junit.tests.framework.AllTests.suite()); + suite.addTest(junit.tests.runner.AllTests.suite()); + suite.addTest(junit.tests.extensions.AllTests.suite()); + return suite; + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/WasRun.java b/junit4/src/test/java/junit/tests/WasRun.java new file mode 100644 index 0000000..98b83bb --- /dev/null +++ b/junit4/src/test/java/junit/tests/WasRun.java @@ -0,0 +1,15 @@ +package junit.tests; + +import junit.framework.TestCase; + +/** + * A helper test case for testing whether the testing method + * is run. + */ +public class WasRun extends TestCase { + public boolean fWasRun= false; + @Override + protected void runTest() { + fWasRun= true; + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/extensions/ActiveTestTest.java b/junit4/src/test/java/junit/tests/extensions/ActiveTestTest.java new file mode 100644 index 0000000..48987dd --- /dev/null +++ b/junit4/src/test/java/junit/tests/extensions/ActiveTestTest.java @@ -0,0 +1,64 @@ +package junit.tests.extensions; + +import junit.extensions.ActiveTestSuite; +import junit.extensions.RepeatedTest; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestResult; + +/** + * Testing the ActiveTest support + */ + +public class ActiveTestTest extends TestCase { + + public static class SuccessTest extends TestCase { + @Override + public void runTest() { + } + } + + public void testActiveTest() { + Test test= createActiveTestSuite(); + TestResult result= new TestResult(); + test.run(result); + assertEquals(100, result.runCount()); + assertEquals(0, result.failureCount()); + assertEquals(0, result.errorCount()); + } + + public void testActiveRepeatedTest() { + Test test= new RepeatedTest(createActiveTestSuite(), 5); + TestResult result= new TestResult(); + test.run(result); + assertEquals(500, result.runCount()); + assertEquals(0, result.failureCount()); + assertEquals(0, result.errorCount()); + } + + public void testActiveRepeatedTest0() { + Test test= new RepeatedTest(createActiveTestSuite(), 0); + TestResult result= new TestResult(); + test.run(result); + assertEquals(0, result.runCount()); + assertEquals(0, result.failureCount()); + assertEquals(0, result.errorCount()); + } + + public void testActiveRepeatedTest1() { + Test test= new RepeatedTest(createActiveTestSuite(), 1); + TestResult result= new TestResult(); + test.run(result); + assertEquals(100, result.runCount()); + assertEquals(0, result.failureCount()); + assertEquals(0, result.errorCount()); + } + + ActiveTestSuite createActiveTestSuite() { + ActiveTestSuite suite= new ActiveTestSuite(); + for (int i= 0; i < 100; i++) + suite.addTest(new SuccessTest()); + return suite; + } + +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/extensions/AllTests.java b/junit4/src/test/java/junit/tests/extensions/AllTests.java new file mode 100644 index 0000000..92de10b --- /dev/null +++ b/junit4/src/test/java/junit/tests/extensions/AllTests.java @@ -0,0 +1,23 @@ +package junit.tests.extensions; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * TestSuite that runs all the extension tests + * + */ +public class AllTests { + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() { // Collect tests manually because we have to test class collection code + TestSuite suite= new TestSuite("Framework Tests"); + suite.addTestSuite(ExtensionTest.class); + suite.addTestSuite(ActiveTestTest.class); + suite.addTestSuite(RepeatedTestTest.class); + return suite; + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/extensions/ExtensionTest.java b/junit4/src/test/java/junit/tests/extensions/ExtensionTest.java new file mode 100644 index 0000000..d8a5c09 --- /dev/null +++ b/junit4/src/test/java/junit/tests/extensions/ExtensionTest.java @@ -0,0 +1,98 @@ +package junit.tests.extensions; + +import junit.extensions.TestSetup; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestResult; +import junit.framework.TestSuite; +import junit.tests.WasRun; + +/** + * A test case testing the extensions to the testing framework. + * + */ +public class ExtensionTest extends TestCase { + static class TornDown extends TestSetup { + boolean fTornDown= false; + + TornDown(Test test) { + super(test); + } + @Override + protected void tearDown() { + fTornDown= true; + } + } + public void testRunningErrorInTestSetup() { + TestCase test= new TestCase("failure") { + @Override + public void runTest() { + fail(); + } + }; + + TestSetup wrapper= new TestSetup(test); + + TestResult result= new TestResult(); + wrapper.run(result); + assertTrue(!result.wasSuccessful()); + } + public void testRunningErrorsInTestSetup() { + TestCase failure= new TestCase("failure") { + @Override + public void runTest() { + fail(); + } + }; + + TestCase error= new TestCase("error") { + @Override + public void runTest() { + throw new Error(); + } + }; + + TestSuite suite= new TestSuite(); + suite.addTest(failure); + suite.addTest(error); + + TestSetup wrapper= new TestSetup(suite); + + TestResult result= new TestResult(); + wrapper.run(result); + + assertEquals(1, result.failureCount()); + assertEquals(1, result.errorCount()); + } + public void testSetupErrorDontTearDown() { + WasRun test= new WasRun(); + + TornDown wrapper= new TornDown(test) { + @Override + public void setUp() { + fail(); + } + }; + + TestResult result= new TestResult(); + wrapper.run(result); + + assertTrue(!wrapper.fTornDown); + } + public void testSetupErrorInTestSetup() { + WasRun test= new WasRun(); + + TestSetup wrapper= new TestSetup(test) { + @Override + public void setUp() { + fail(); + } + }; + + TestResult result= new TestResult(); + wrapper.run(result); + + assertTrue(!test.fWasRun); + assertTrue(!result.wasSuccessful()); + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/extensions/RepeatedTestTest.java b/junit4/src/test/java/junit/tests/extensions/RepeatedTestTest.java new file mode 100644 index 0000000..9e53bfa --- /dev/null +++ b/junit4/src/test/java/junit/tests/extensions/RepeatedTestTest.java @@ -0,0 +1,63 @@ +package junit.tests.extensions; + +import junit.extensions.RepeatedTest; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestResult; +import junit.framework.TestSuite; + +/** + * Testing the RepeatedTest support. + */ + +public class RepeatedTestTest extends TestCase { + private TestSuite fSuite; + + public static class SuccessTest extends TestCase { + + @Override + public void runTest() { + } + } + + public RepeatedTestTest(String name) { + super(name); + fSuite= new TestSuite(); + fSuite.addTest(new SuccessTest()); + fSuite.addTest(new SuccessTest()); + } + + public void testRepeatedOnce() { + Test test= new RepeatedTest(fSuite, 1); + assertEquals(2, test.countTestCases()); + TestResult result= new TestResult(); + test.run(result); + assertEquals(2, result.runCount()); + } + + public void testRepeatedMoreThanOnce() { + Test test= new RepeatedTest(fSuite, 3); + assertEquals(6, test.countTestCases()); + TestResult result= new TestResult(); + test.run(result); + assertEquals(6, result.runCount()); + } + + public void testRepeatedZero() { + Test test= new RepeatedTest(fSuite, 0); + assertEquals(0, test.countTestCases()); + TestResult result= new TestResult(); + test.run(result); + assertEquals(0, result.runCount()); + } + + public void testRepeatedNegative() { + try { + new RepeatedTest(fSuite, -1); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains(">=")); + return; + } + fail("Should throw an IllegalArgumentException"); + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/extensions/package-info.java b/junit4/src/test/java/junit/tests/extensions/package-info.java new file mode 100644 index 0000000..acc0194 --- /dev/null +++ b/junit4/src/test/java/junit/tests/extensions/package-info.java @@ -0,0 +1,4 @@ +/** + * Tests for the JUnit v3.x extension functionality. + */ +package junit.tests.extensions;
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/AllTests.java b/junit4/src/test/java/junit/tests/framework/AllTests.java new file mode 100644 index 0000000..6ec9891 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/AllTests.java @@ -0,0 +1,32 @@ +package junit.tests.framework; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * TestSuite that runs all the sample tests + * + */ +public class AllTests { + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() { + TestSuite suite= new TestSuite("Framework Tests"); + suite.addTestSuite(TestCaseTest.class); + suite.addTest(SuiteTest.suite()); // Tests suite building, so can't use automatic test extraction + suite.addTestSuite(TestListenerTest.class); + suite.addTestSuite(AssertionFailedErrorTest.class); + suite.addTestSuite(AssertTest.class); + suite.addTestSuite(TestImplementorTest.class); + suite.addTestSuite(NoArgTestCaseTest.class); + suite.addTestSuite(ComparisonCompactorTest.class); + suite.addTestSuite(ComparisonFailureTest.class); + suite.addTestSuite(DoublePrecisionAssertTest.class); + suite.addTestSuite(FloatAssertTest.class); + return suite; + } + +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/AssertTest.java b/junit4/src/test/java/junit/tests/framework/AssertTest.java new file mode 100644 index 0000000..de33ff6 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/AssertTest.java @@ -0,0 +1,171 @@ +package junit.tests.framework; + +import junit.framework.AssertionFailedError; +import junit.framework.ComparisonFailure; +import junit.framework.TestCase; + +public class AssertTest extends TestCase { + + /* In the tests that follow, we can't use standard formatting + * for exception tests: + * try { + * somethingThatShouldThrow(); + * fail(); + * catch (AssertionFailedError e) { + * } + * because fail() would never be reported. + */ + public void testFail() { + // Also, we are testing fail, so we can't rely on fail() working. + // We have to throw the exception manually. + try { + fail(); + } catch (AssertionFailedError e) { + return; + } + throw new AssertionFailedError(); + } + + public void testAssertionFailedErrorToStringWithNoMessage() { + // Also, we are testing fail, so we can't rely on fail() working. + // We have to throw the exception manually. + try { + fail(); + } catch (AssertionFailedError e) { + assertEquals("junit.framework.AssertionFailedError", e.toString()); + return; + } + throw new AssertionFailedError(); + } + + public void testAssertionFailedErrorToStringWithMessage() { + // Also, we are testing fail, so we can't rely on fail() working. + // We have to throw the exception manually. + try { + fail("woops!"); + } catch (AssertionFailedError e) { + assertEquals("junit.framework.AssertionFailedError: woops!", e.toString()); + return; + } + throw new AssertionFailedError(); + } + + public void testAssertEquals() { + Object o= new Object(); + assertEquals(o, o); + try { + assertEquals(new Object(), new Object()); + } catch (AssertionFailedError e) { + return; + } + fail(); + } + + public void testAssertEqualsNull() { + assertEquals((Object) null, (Object) null); + } + + public void testAssertStringEquals() { + assertEquals("a", "a"); + } + + public void testAssertNullNotEqualsString() { + try { + assertEquals(null, "foo"); + fail(); + } catch (ComparisonFailure e) { + } + } + + public void testAssertStringNotEqualsNull() { + try { + assertEquals("foo", null); + fail(); + } catch (ComparisonFailure e) { + e.getMessage(); // why no assertion? + } + } + + public void testAssertNullNotEqualsNull() { + try { + assertEquals(null, new Object()); + } catch (AssertionFailedError e) { + e.getMessage(); // why no assertion? + return; + } + fail(); + } + + public void testAssertNull() { + assertNull(null); + try { + assertNull(new Object()); + } catch (AssertionFailedError e) { + return; + } + fail(); + } + + public void testAssertNotNull() { + assertNotNull(new Object()); + try { + assertNotNull(null); + } catch (AssertionFailedError e) { + return; + } + fail(); + } + + public void testAssertTrue() { + assertTrue(true); + try { + assertTrue(false); + } catch (AssertionFailedError e) { + return; + } + fail(); + } + + public void testAssertFalse() { + assertFalse(false); + try { + assertFalse(true); + } catch (AssertionFailedError e) { + return; + } + fail(); + } + + public void testAssertSame() { + Object o= new Object(); + assertSame(o, o); + try { + assertSame(new Integer(1), new Integer(1)); + } catch (AssertionFailedError e) { + return; + } + fail(); + } + + public void testAssertNotSame() { + assertNotSame(new Integer(1), null); + assertNotSame(null, new Integer(1)); + assertNotSame(new Integer(1), new Integer(1)); + try { + Integer obj= new Integer(1); + assertNotSame(obj, obj); + } catch (AssertionFailedError e) { + return; + } + fail(); + } + + public void testAssertNotSameFailsNull() { + try { + assertNotSame(null, null); + } catch (AssertionFailedError e) { + return; + } + fail(); + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/AssertionFailedErrorTest.java b/junit4/src/test/java/junit/tests/framework/AssertionFailedErrorTest.java new file mode 100644 index 0000000..3dd6b1f --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/AssertionFailedErrorTest.java @@ -0,0 +1,23 @@ +package junit.tests.framework; + +import junit.framework.AssertionFailedError; +import junit.framework.TestCase; + +public class AssertionFailedErrorTest extends TestCase { + private static final String ARBITRARY_MESSAGE= "arbitrary message"; + + public void testCreateErrorWithoutMessage() throws Exception { + AssertionFailedError error= new AssertionFailedError(); + assertNull(error.getMessage()); + } + + public void testCreateErrorWithMessage() throws Exception { + AssertionFailedError error= new AssertionFailedError(ARBITRARY_MESSAGE); + assertEquals(ARBITRARY_MESSAGE, error.getMessage()); + } + + public void testCreateErrorWithoutMessageInsteadOfNull() throws Exception { + AssertionFailedError error= new AssertionFailedError(null); + assertEquals("", error.getMessage()); + } +} diff --git a/junit4/src/test/java/junit/tests/framework/ComparisonCompactorTest.java b/junit4/src/test/java/junit/tests/framework/ComparisonCompactorTest.java new file mode 100644 index 0000000..6edaefe --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/ComparisonCompactorTest.java @@ -0,0 +1,102 @@ +package junit.tests.framework; + +import junit.framework.ComparisonCompactor; +import junit.framework.TestCase; + +public class ComparisonCompactorTest extends TestCase { + + public void testMessage() { + String failure= new ComparisonCompactor(0, "b", "c").compact("a"); + assertTrue("a expected:<[b]> but was:<[c]>".equals(failure)); + } + + public void testStartSame() { + String failure= new ComparisonCompactor(1, "ba", "bc").compact(null); + assertEquals("expected:<b[a]> but was:<b[c]>", failure); + } + + public void testEndSame() { + String failure= new ComparisonCompactor(1, "ab", "cb").compact(null); + assertEquals("expected:<[a]b> but was:<[c]b>", failure); + } + + public void testSame() { + String failure= new ComparisonCompactor(1, "ab", "ab").compact(null); + assertEquals("expected:<ab> but was:<ab>", failure); + } + + public void testNoContextStartAndEndSame() { + String failure= new ComparisonCompactor(0, "abc", "adc").compact(null); + assertEquals("expected:<...[b]...> but was:<...[d]...>", failure); + } + + public void testStartAndEndContext() { + String failure= new ComparisonCompactor(1, "abc", "adc").compact(null); + assertEquals("expected:<a[b]c> but was:<a[d]c>", failure); + } + + public void testStartAndEndContextWithEllipses() { + String failure= new ComparisonCompactor(1, "abcde", "abfde").compact(null); + assertEquals("expected:<...b[c]d...> but was:<...b[f]d...>", failure); + } + + public void testComparisonErrorStartSameComplete() { + String failure= new ComparisonCompactor(2, "ab", "abc").compact(null); + assertEquals("expected:<ab[]> but was:<ab[c]>", failure); + } + + public void testComparisonErrorEndSameComplete() { + String failure= new ComparisonCompactor(0, "bc", "abc").compact(null); + assertEquals("expected:<[]...> but was:<[a]...>", failure); + } + + public void testComparisonErrorEndSameCompleteContext() { + String failure= new ComparisonCompactor(2, "bc", "abc").compact(null); + assertEquals("expected:<[]bc> but was:<[a]bc>", failure); + } + + public void testComparisonErrorOverlapingMatches() { + String failure= new ComparisonCompactor(0, "abc", "abbc").compact(null); + assertEquals("expected:<...[]...> but was:<...[b]...>", failure); + } + + public void testComparisonErrorOverlapingMatchesContext() { + String failure= new ComparisonCompactor(2, "abc", "abbc").compact(null); + assertEquals("expected:<ab[]c> but was:<ab[b]c>", failure); + } + + public void testComparisonErrorOverlapingMatches2() { + String failure= new ComparisonCompactor(0, "abcdde", "abcde").compact(null); + assertEquals("expected:<...[d]...> but was:<...[]...>", failure); + } + + public void testComparisonErrorOverlapingMatches2Context() { + String failure= new ComparisonCompactor(2, "abcdde", "abcde").compact(null); + assertEquals("expected:<...cd[d]e> but was:<...cd[]e>", failure); + } + + public void testComparisonErrorWithActualNull() { + String failure= new ComparisonCompactor(0, "a", null).compact(null); + assertEquals("expected:<a> but was:<null>", failure); + } + + public void testComparisonErrorWithActualNullContext() { + String failure= new ComparisonCompactor(2, "a", null).compact(null); + assertEquals("expected:<a> but was:<null>", failure); + } + + public void testComparisonErrorWithExpectedNull() { + String failure= new ComparisonCompactor(0, null, "a").compact(null); + assertEquals("expected:<null> but was:<a>", failure); + } + + public void testComparisonErrorWithExpectedNullContext() { + String failure= new ComparisonCompactor(2, null, "a").compact(null); + assertEquals("expected:<null> but was:<a>", failure); + } + + public void testBug609972() { + String failure= new ComparisonCompactor(10, "S&P500", "0").compact(null); + assertEquals("expected:<[S&P50]0> but was:<[]0>", failure); + } +} diff --git a/junit4/src/test/java/junit/tests/framework/ComparisonFailureTest.java b/junit4/src/test/java/junit/tests/framework/ComparisonFailureTest.java new file mode 100644 index 0000000..8a1e5f2 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/ComparisonFailureTest.java @@ -0,0 +1,47 @@ +package junit.tests.framework; + +import junit.framework.ComparisonFailure; +import junit.framework.TestCase; + +public class ComparisonFailureTest extends TestCase { + + // Most of the tests are in ComparisonCompactorTest + public void testConnection() { + ComparisonFailure failure= new ComparisonFailure("warning", "Mary had a little lamb", "Mary had the little lamb"); + assertEquals("warning expected:<Mary had [a] little lamb> but was:<Mary had [the] little lamb>", failure.getMessage()); + } + + // This is like an instanceof test. + public void testThrowing() { + try { + assertEquals("a", "b"); + } catch (ComparisonFailure e) { + return; + } + fail(); + } + + public void testExceptionToStringWithMessage() { + try { + assertEquals("woops!", "a", "b"); + } catch (ComparisonFailure e) { + if (!e.toString().startsWith("junit.framework.ComparisonFailure: woops! expected:<")) { + fail("Unexpected message: " + e); + } + return; + } + fail(); + } + + public void testExceptionToStringWithoutMessage() { + try { + assertEquals("a", "b"); + } catch (ComparisonFailure e) { + if (!e.toString().startsWith("junit.framework.ComparisonFailure: expected:<")) { + fail("Unexpected message: " + e); + } + return; + } + fail(); + } +} diff --git a/junit4/src/test/java/junit/tests/framework/DoublePrecisionAssertTest.java b/junit4/src/test/java/junit/tests/framework/DoublePrecisionAssertTest.java new file mode 100644 index 0000000..9df3560 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/DoublePrecisionAssertTest.java @@ -0,0 +1,55 @@ +package junit.tests.framework; + +import junit.framework.AssertionFailedError; +import junit.framework.TestCase; + +public class DoublePrecisionAssertTest extends TestCase { + + /** + * Test for the special Double.NaN value. + */ + public void testAssertEqualsNaNFails() { + try { + assertEquals(1.234, Double.NaN, 0.0); + fail(); + } catch (AssertionFailedError e) { + } + } + + public void testAssertNaNEqualsFails() { + try { + assertEquals(Double.NaN, 1.234, 0.0); + fail(); + } catch (AssertionFailedError e) { + } + } + + public void testAssertNaNEqualsNaN() { + assertEquals(Double.NaN, Double.NaN, 0.0); + } + + public void testAssertPosInfinityNotEqualsNegInfinity() { + try { + assertEquals(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0); + fail(); + } catch (AssertionFailedError e) { + } + } + + public void testAssertPosInfinityNotEquals() { + try { + assertEquals(Double.POSITIVE_INFINITY, 1.23, 0.0); + fail(); + } catch (AssertionFailedError e) { + } + } + + public void testAssertPosInfinityEqualsInfinity() { + assertEquals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 0.0); + } + + public void testAssertNegInfinityEqualsInfinity() { + assertEquals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0); + } + +} diff --git a/junit4/src/test/java/junit/tests/framework/Failure.java b/junit4/src/test/java/junit/tests/framework/Failure.java new file mode 100644 index 0000000..4dd9c8c --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/Failure.java @@ -0,0 +1,14 @@ +package junit.tests.framework; + +import junit.framework.TestCase; + +/** + * A test case testing the testing framework. + * + */ +public class Failure extends TestCase { + @Override + public void runTest() { + fail(); + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/FloatAssertTest.java b/junit4/src/test/java/junit/tests/framework/FloatAssertTest.java new file mode 100644 index 0000000..2d22549 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/FloatAssertTest.java @@ -0,0 +1,63 @@ +package junit.tests.framework; + +import junit.framework.AssertionFailedError; +import junit.framework.TestCase; + +public class FloatAssertTest extends TestCase { + + /** + * Test for the special Double.NaN value. + */ + public void testAssertEqualsNaNFails() { + try { + assertEquals(1.234f, Float.NaN, 0.0); + fail(); + } catch (AssertionFailedError e) { + } + } + + public void testAssertNaNEqualsFails() { + try { + assertEquals(Float.NaN, 1.234f, 0.0); + fail(); + } catch (AssertionFailedError e) { + } + } + + public void testAssertNaNEqualsNaN() { + assertEquals(Float.NaN, Float.NaN, 0.0); + } + + public void testAssertPosInfinityNotEqualsNegInfinity() { + try { + assertEquals(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.0); + fail(); + } catch (AssertionFailedError e) { + } + } + + public void testAssertPosInfinityNotEquals() { + try { + assertEquals(Float.POSITIVE_INFINITY, 1.23f, 0.0); + fail(); + } catch (AssertionFailedError e) { + } + } + + public void testAssertPosInfinityEqualsInfinity() { + assertEquals(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, 0.0); + } + + public void testAssertNegInfinityEqualsInfinity() { + assertEquals(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.0); + } + + public void testAllInfinities() { + try { + assertEquals(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY); + fail(); + } catch (AssertionFailedError e) { + } + } + +} diff --git a/junit4/src/test/java/junit/tests/framework/InheritedTestCase.java b/junit4/src/test/java/junit/tests/framework/InheritedTestCase.java new file mode 100644 index 0000000..f272d77 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/InheritedTestCase.java @@ -0,0 +1,9 @@ +package junit.tests.framework; + +/** + * Test class used in SuiteTest + */ +public class InheritedTestCase extends OneTestCase { + public void test2() { + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/NoArgTestCaseTest.java b/junit4/src/test/java/junit/tests/framework/NoArgTestCaseTest.java new file mode 100644 index 0000000..70281e6 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/NoArgTestCaseTest.java @@ -0,0 +1,9 @@ + +package junit.tests.framework; + +import junit.framework.TestCase; + +public class NoArgTestCaseTest extends TestCase { + public void testNothing() { // If this compiles, the no arg ctor is there + } +} diff --git a/junit4/src/test/java/junit/tests/framework/NoTestCaseClass.java b/junit4/src/test/java/junit/tests/framework/NoTestCaseClass.java new file mode 100644 index 0000000..5fea04d --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/NoTestCaseClass.java @@ -0,0 +1,10 @@ +package junit.tests.framework; + +/** + * Test class used in SuiteTest + */ + +public class NoTestCaseClass extends Object { + public void testSuccess() { + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/NoTestCases.java b/junit4/src/test/java/junit/tests/framework/NoTestCases.java new file mode 100644 index 0000000..fec60d0 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/NoTestCases.java @@ -0,0 +1,11 @@ +package junit.tests.framework; + +/** + * Test class used in SuiteTest + */ +import junit.framework.TestCase; + +public class NoTestCases extends TestCase { + public void noTestCase() { + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/NotPublicTestCase.java b/junit4/src/test/java/junit/tests/framework/NotPublicTestCase.java new file mode 100644 index 0000000..085f985 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/NotPublicTestCase.java @@ -0,0 +1,13 @@ +package junit.tests.framework; + +/** + * Test class used in SuiteTest + */ +import junit.framework.TestCase; + +public class NotPublicTestCase extends TestCase { + protected void testNotPublic() { + } + public void testPublic() { + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/NotVoidTestCase.java b/junit4/src/test/java/junit/tests/framework/NotVoidTestCase.java new file mode 100644 index 0000000..eca1a63 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/NotVoidTestCase.java @@ -0,0 +1,14 @@ +package junit.tests.framework; + +/** + * Test class used in SuiteTest + */ +import junit.framework.TestCase; + +public class NotVoidTestCase extends TestCase { + public int testNotVoid() { + return 1; + } + public void testVoid() { + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/OneTestCase.java b/junit4/src/test/java/junit/tests/framework/OneTestCase.java new file mode 100644 index 0000000..b4ca560 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/OneTestCase.java @@ -0,0 +1,15 @@ +package junit.tests.framework; + +/** + * Test class used in SuiteTest + */ +import junit.framework.TestCase; + +public class OneTestCase extends TestCase { + public void noTestCase() { + } + public void testCase() { + } + public void testCase(int arg) { + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/OverrideTestCase.java b/junit4/src/test/java/junit/tests/framework/OverrideTestCase.java new file mode 100644 index 0000000..043ef81 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/OverrideTestCase.java @@ -0,0 +1,10 @@ +package junit.tests.framework; + +/** + * Test class used in SuiteTest + */ +public class OverrideTestCase extends OneTestCase { + @Override + public void testCase() { + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/Success.java b/junit4/src/test/java/junit/tests/framework/Success.java new file mode 100644 index 0000000..ed4d1b6 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/Success.java @@ -0,0 +1,17 @@ +package junit.tests.framework; + +import junit.framework.TestCase; + +/** + * A test case testing the testing framework. + * + */ +public class Success extends TestCase { + + @Override + public void runTest() { + } + + public void testSuccess() { + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/SuiteTest.java b/junit4/src/test/java/junit/tests/framework/SuiteTest.java new file mode 100644 index 0000000..3953f4f --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/SuiteTest.java @@ -0,0 +1,105 @@ +package junit.tests.framework; + +import java.util.Collections; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestResult; +import junit.framework.TestSuite; + +/** + * A fixture for testing the "auto" test suite feature. + * + */ +public class SuiteTest extends TestCase { + protected TestResult fResult; + public SuiteTest(String name) { + super(name); + } + @Override + protected void setUp() { + fResult= new TestResult(); + } + public static Test suite() { + TestSuite suite= new TestSuite("Suite Tests"); + // build the suite manually, because some of the suites are testing + // the functionality that automatically builds suites + suite.addTest(new SuiteTest("testNoTestCases")); + suite.addTest(new SuiteTest("testOneTestCase")); + suite.addTest(new SuiteTest("testNotPublicTestCase")); + suite.addTest(new SuiteTest("testNotVoidTestCase")); + suite.addTest(new SuiteTest("testNotExistingTestCase")); + suite.addTest(new SuiteTest("testInheritedTests")); + suite.addTest(new SuiteTest("testOneTestCaseEclipseSeesSameStructureAs381")); + suite.addTest(new SuiteTest("testNoTestCaseClass")); + suite.addTest(new SuiteTest("testShadowedTests")); + suite.addTest(new SuiteTest("testAddTestSuite")); + suite.addTest(new SuiteTest("testCreateSuiteFromArray")); + + return suite; + } + public void testInheritedTests() { + TestSuite suite= new TestSuite(InheritedTestCase.class); + suite.run(fResult); + assertTrue(fResult.wasSuccessful()); + assertEquals(2, fResult.runCount()); + } + public void testNoTestCaseClass() { + Test t= new TestSuite(NoTestCaseClass.class); + t.run(fResult); + assertEquals(1, fResult.runCount()); // warning test + assertTrue(! fResult.wasSuccessful()); + } + public void testNoTestCases() { + Test t= new TestSuite(NoTestCases.class); + t.run(fResult); + assertTrue(fResult.runCount() == 1); // warning test + assertTrue(fResult.failureCount() == 1); + assertTrue(! fResult.wasSuccessful()); + } + public void testNotExistingTestCase() { + Test t= new SuiteTest("notExistingMethod"); + t.run(fResult); + assertTrue(fResult.runCount() == 1); + assertTrue(fResult.failureCount() == 1); + assertTrue(fResult.errorCount() == 0); + } + public void testNotPublicTestCase() { + TestSuite suite= new TestSuite(NotPublicTestCase.class); + // 1 public test case + 1 warning for the non-public test case + assertEquals(2, suite.countTestCases()); + } + public void testNotVoidTestCase() { + TestSuite suite= new TestSuite(NotVoidTestCase.class); + assertTrue(suite.countTestCases() == 1); + } + public void testOneTestCase() { + TestSuite t= new TestSuite(OneTestCase.class); + t.run(fResult); + assertTrue(fResult.runCount() == 1); + assertTrue(fResult.failureCount() == 0); + assertTrue(fResult.errorCount() == 0); + assertTrue(fResult.wasSuccessful()); + } + public void testOneTestCaseEclipseSeesSameStructureAs381() { + TestSuite t= new TestSuite(ThreeTestCases.class); + assertEquals(3, Collections.list(t.tests()).size()); + } + public void testShadowedTests() { + TestSuite suite= new TestSuite(OverrideTestCase.class); + suite.run(fResult); + assertEquals(1, fResult.runCount()); + } + public void testAddTestSuite() { + TestSuite suite= new TestSuite(); + suite.addTestSuite(OneTestCase.class); + suite.run(fResult); + assertEquals(1, fResult.runCount()); + } + public void testCreateSuiteFromArray() { + TestSuite suite = new TestSuite(OneTestCase.class, DoublePrecisionAssertTest.class); + assertEquals(2, suite.testCount()); + assertEquals("junit.tests.framework.DoublePrecisionAssertTest" , ((TestSuite)suite.testAt(1)).getName()); + assertEquals("junit.tests.framework.OneTestCase" , ((TestSuite)suite.testAt(0)).getName()); + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/TestCaseTest.java b/junit4/src/test/java/junit/tests/framework/TestCaseTest.java new file mode 100644 index 0000000..91b91e6 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/TestCaseTest.java @@ -0,0 +1,190 @@ +package junit.tests.framework; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestFailure; +import junit.framework.TestResult; +import junit.framework.TestSuite; +import junit.tests.WasRun; + +/** + * A test case testing the testing framework. + * + */ +public class TestCaseTest extends TestCase { + + static class TornDown extends TestCase { + boolean fTornDown= false; + + @Override + protected void tearDown() { + fTornDown= true; + } + @Override + protected void runTest() { + throw new Error("running"); + } + } + + public void testCaseToString() { + // This test wins the award for twisted snake tail eating while + // writing self tests. And you thought those weird anonymous + // inner classes were bad... + assertEquals("testCaseToString(junit.tests.framework.TestCaseTest)", toString()); + } + public void testError() { + TestCase error= new TestCase("error") { + @Override + protected void runTest() { + throw new Error(); + } + }; + verifyError(error); + } + public void testRunAndTearDownFails() { + TornDown fails= new TornDown() { + @Override + protected void tearDown() { + super.tearDown(); + throw new Error(); + } + @Override + protected void runTest() { + throw new Error(); + } + }; + verifyError(fails); + assertTrue(fails.fTornDown); + } + public void testSetupFails() { + TestCase fails= new TestCase("success") { + @Override + protected void setUp() { + throw new Error(); + } + @Override + protected void runTest() { + } + }; + verifyError(fails); + } + public void testSuccess() { + TestCase success= new TestCase("success") { + @Override + protected void runTest() { + } + }; + verifySuccess(success); + } + public void testFailure() { + TestCase failure= new TestCase("failure") { + @Override + protected void runTest() { + fail(); + } + }; + verifyFailure(failure); + } + + public void testTearDownAfterError() { + TornDown fails= new TornDown(); + verifyError(fails); + assertTrue(fails.fTornDown); + } + + public void testTearDownFails() { + TestCase fails= new TestCase("success") { + @Override + protected void tearDown() { + throw new Error(); + } + @Override + protected void runTest() { + } + }; + verifyError(fails); + } + public void testTearDownSetupFails() { + TornDown fails= new TornDown() { + @Override + protected void setUp() { + throw new Error(); + } + }; + verifyError(fails); + assertTrue(!fails.fTornDown); + } + public void testWasRun() { + WasRun test= new WasRun(); + test.run(); + assertTrue(test.fWasRun); + } + public void testExceptionRunningAndTearDown() { + // With 1.4, we should + // wrap the exception thrown while running with the exception thrown + // while tearing down + Test t= new TornDown() { + @Override + public void tearDown() { + throw new Error("tearingDown"); + } + }; + TestResult result= new TestResult(); + t.run(result); + TestFailure failure= result.errors().nextElement(); + assertEquals("running", failure.thrownException().getMessage()); + } + + public void testErrorTearingDownDoesntMaskErrorRunning() { + final Exception running= new Exception("Running"); + TestCase t= new TestCase() { + @Override + protected void runTest() throws Throwable { + throw running; + } + @Override + protected void tearDown() throws Exception { + throw new Error("Tearing down"); + } + }; + try { + t.runBare(); + } catch (Throwable thrown) { + assertSame(running, thrown); + } + } + + public void testNoArgTestCasePasses() { + Test t= new TestSuite(NoArgTestCaseTest.class); + TestResult result= new TestResult(); + t.run(result); + assertTrue(result.runCount() == 1); + assertTrue(result.failureCount() == 0); + assertTrue(result.errorCount() == 0); + } + + public void testNamelessTestCase() { + TestCase t= new TestCase() {}; + TestResult result = t.run(); + assertEquals(1, result.failureCount()); + } + + void verifyError(TestCase test) { + TestResult result= test.run(); + assertTrue(result.runCount() == 1); + assertTrue(result.failureCount() == 0); + assertTrue(result.errorCount() == 1); + } + void verifyFailure(TestCase test) { + TestResult result= test.run(); + assertTrue(result.runCount() == 1); + assertTrue(result.failureCount() == 1); + assertTrue(result.errorCount() == 0); + } + void verifySuccess(TestCase test) { + TestResult result= test.run(); + assertTrue(result.runCount() == 1); + assertTrue(result.failureCount() == 0); + assertTrue(result.errorCount() == 0); + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/TestImplementorTest.java b/junit4/src/test/java/junit/tests/framework/TestImplementorTest.java new file mode 100644 index 0000000..a5f0962 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/TestImplementorTest.java @@ -0,0 +1,54 @@ +package junit.tests.framework; + +import junit.framework.Protectable; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestResult; + +/** + * Test an implementor of junit.framework.Test other than TestCase or TestSuite + */ +public class TestImplementorTest extends TestCase { + public static class DoubleTestCase implements Test { + private TestCase fTestCase; + + public DoubleTestCase(TestCase testCase) { + fTestCase= testCase; + } + + public int countTestCases() { + return 2; + } + + public void run(TestResult result) { + result.startTest(this); + Protectable p= new Protectable() { + public void protect() throws Throwable { + fTestCase.runBare(); + fTestCase.runBare(); + } + }; + result.runProtected(this, p); + result.endTest(this); + } + } + + private DoubleTestCase fTest; + + public TestImplementorTest() { + TestCase testCase= new TestCase() { + @Override + public void runTest() { + } + }; + fTest= new DoubleTestCase(testCase); + } + + public void testSuccessfulRun() { + TestResult result= new TestResult(); + fTest.run(result); + assertEquals(fTest.countTestCases(), result.runCount()); + assertEquals(0, result.errorCount()); + assertEquals(0, result.failureCount()); + } +} diff --git a/junit4/src/test/java/junit/tests/framework/TestListenerTest.java b/junit4/src/test/java/junit/tests/framework/TestListenerTest.java new file mode 100644 index 0000000..1da2af5 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/TestListenerTest.java @@ -0,0 +1,73 @@ +package junit.tests.framework; + +/** + * Test class used in SuiteTest + */ +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestListener; +import junit.framework.TestResult; + +public class TestListenerTest extends TestCase implements TestListener { + private TestResult fResult; + private int fStartCount; + private int fEndCount; + private int fFailureCount; + private int fErrorCount; + + public void addError(Test test, Throwable t) { + fErrorCount++; + } + public void addFailure(Test test, AssertionFailedError t) { + fFailureCount++; + } + public void endTest(Test test) { + fEndCount++; + } + @Override + protected void setUp() { + fResult= new TestResult(); + fResult.addListener(this); + + fStartCount= 0; + fEndCount= 0; + fFailureCount= 0; + fErrorCount= 0; + } + public void startTest(Test test) { + fStartCount++; + } + public void testError() { + TestCase test= new TestCase("noop") { + @Override + public void runTest() { + throw new Error(); + } + }; + test.run(fResult); + assertEquals(1, fErrorCount); + assertEquals(1, fEndCount); + } + public void testFailure() { + TestCase test= new TestCase("noop") { + @Override + public void runTest() { + fail(); + } + }; + test.run(fResult); + assertEquals(1, fFailureCount); + assertEquals(1, fEndCount); + } + public void testStartStop() { + TestCase test= new TestCase("noop") { + @Override + public void runTest() { + } + }; + test.run(fResult); + assertEquals(1, fStartCount); + assertEquals(1, fEndCount); + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/ThreeTestCases.java b/junit4/src/test/java/junit/tests/framework/ThreeTestCases.java new file mode 100644 index 0000000..c58bece --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/ThreeTestCases.java @@ -0,0 +1,15 @@ +package junit.tests.framework; + +/** + * Test class used in SuiteTest + */ +import junit.framework.TestCase; + +public class ThreeTestCases extends TestCase { + public void testCase() { + } + public void testCase2() { + } + public void testCase3thisTimeItsPersonal() { + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/framework/package-info.java b/junit4/src/test/java/junit/tests/framework/package-info.java new file mode 100644 index 0000000..9cc0024 --- /dev/null +++ b/junit4/src/test/java/junit/tests/framework/package-info.java @@ -0,0 +1,4 @@ +/** + * Tests the JUnit v3.x core classes. + */ +package junit.tests.framework;
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/package-info.java b/junit4/src/test/java/junit/tests/package-info.java new file mode 100644 index 0000000..d23121b --- /dev/null +++ b/junit4/src/test/java/junit/tests/package-info.java @@ -0,0 +1,4 @@ +/** + * Tests the JUnit v3.x framework. + */ +package junit.tests;
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/runner/AllTests.java b/junit4/src/test/java/junit/tests/runner/AllTests.java new file mode 100644 index 0000000..a0aa116 --- /dev/null +++ b/junit4/src/test/java/junit/tests/runner/AllTests.java @@ -0,0 +1,31 @@ +package junit.tests.runner; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * TestSuite that runs all the sample tests + * + */ +public class AllTests { + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() { // Collect tests manually because we have to test class collection code + TestSuite suite= new TestSuite("Framework Tests"); + suite.addTestSuite(StackFilterTest.class); + suite.addTestSuite(ResultTest.class); + suite.addTestSuite(BaseTestRunnerTest.class); + suite.addTestSuite(TextFeedbackTest.class); + suite.addTestSuite(TextRunnerSingleMethodTest.class); + suite.addTestSuite(TextRunnerTest.class); + return suite; + } + + static boolean isJDK11() { + String version= System.getProperty("java.version"); + return version.startsWith("1.1"); + } +} diff --git a/junit4/src/test/java/junit/tests/runner/BaseTestRunnerTest.java b/junit4/src/test/java/junit/tests/runner/BaseTestRunnerTest.java new file mode 100644 index 0000000..8943da9 --- /dev/null +++ b/junit4/src/test/java/junit/tests/runner/BaseTestRunnerTest.java @@ -0,0 +1,53 @@ + +package junit.tests.runner; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.runner.BaseTestRunner; + +public class BaseTestRunnerTest extends TestCase { + public static class MockRunner extends BaseTestRunner { + private boolean fRunFailed = false; + + @Override + protected void runFailed(String message) { + fRunFailed = true; + } + + @Override + public void testEnded(String testName) { + } + + @Override + public void testFailed(int status, Test test, Throwable t) { + } + + @Override + public void testStarted(String testName) { + } + } + + public static class NonStatic { + public Test suite() { + return null; + } + } + + public void testInvokeNonStaticSuite() { + BaseTestRunner runner= new MockRunner(); + runner.getTest("junit.tests.runner.BaseTestRunnerTest$NonStatic"); // Used to throw NullPointerException + } + + public static class DoesntExtendTestCase { + public static Test suite() { + return new TestSuite(); + } + } + + public void testInvokeSuiteOnNonSubclassOfTestCase() { + MockRunner runner= new MockRunner(); + runner.getTest(DoesntExtendTestCase.class.getName()); + assertFalse(runner.fRunFailed); + } +} diff --git a/junit4/src/test/java/junit/tests/runner/ResultTest.java b/junit4/src/test/java/junit/tests/runner/ResultTest.java new file mode 100644 index 0000000..ba3b509 --- /dev/null +++ b/junit4/src/test/java/junit/tests/runner/ResultTest.java @@ -0,0 +1,37 @@ +package junit.tests.runner; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import junit.framework.TestCase; +import junit.tests.framework.Success; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.tests.running.methods.AnnotationTest; + +public class ResultTest extends TestCase { + + public void testRunFailureResultCanBeSerialised() throws Exception { + JUnitCore runner = new JUnitCore(); + Result result = runner.run(AnnotationTest.FailureTest.class); + assertResultSerializable(result); + } + + public void testRunSuccessResultCanBeSerialised() throws Exception { + JUnitCore runner = new JUnitCore(); + Result result = runner.run(Success.class); + assertResultSerializable(result); + } + + private void assertResultSerializable(Result result) throws IOException, ClassNotFoundException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + new ObjectOutputStream(byteArrayOutputStream).writeObject(result); + byte[] bytes = byteArrayOutputStream.toByteArray(); + ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes)); + Result fromStream = (Result) objectInputStream.readObject(); + assertNotNull(fromStream); + } +} diff --git a/junit4/src/test/java/junit/tests/runner/StackFilterTest.java b/junit4/src/test/java/junit/tests/runner/StackFilterTest.java new file mode 100644 index 0000000..e70ea23 --- /dev/null +++ b/junit4/src/test/java/junit/tests/runner/StackFilterTest.java @@ -0,0 +1,46 @@ +package junit.tests.runner; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import junit.framework.TestCase; +import junit.runner.BaseTestRunner; + +public class StackFilterTest extends TestCase { + String fFiltered; + String fUnfiltered; + + @Override + protected void setUp() { + StringWriter swin= new StringWriter(); + PrintWriter pwin= new PrintWriter(swin); + pwin.println("junit.framework.AssertionFailedError"); + pwin.println(" at junit.framework.Assert.fail(Assert.java:144)"); + pwin.println(" at junit.framework.Assert.assert(Assert.java:19)"); + pwin.println(" at junit.framework.Assert.assert(Assert.java:26)"); + pwin.println(" at MyTest.f(MyTest.java:13)"); + pwin.println(" at MyTest.testStackTrace(MyTest.java:8)"); + pwin.println(" at java.lang.reflect.Method.invoke(Native Method)"); + pwin.println(" at junit.framework.TestCase.runTest(TestCase.java:156)"); + pwin.println(" at junit.framework.TestCase.runBare(TestCase.java:130)"); + pwin.println(" at junit.framework.TestResult$1.protect(TestResult.java:100)"); + pwin.println(" at junit.framework.TestResult.runProtected(TestResult.java:118)"); + pwin.println(" at junit.framework.TestResult.run(TestResult.java:103)"); + pwin.println(" at junit.framework.TestCase.run(TestCase.java:121)"); + pwin.println(" at junit.framework.TestSuite.runTest(TestSuite.java:157)"); + pwin.println(" at junit.framework.TestSuite.run(TestSuite.java, Compiled Code)"); + pwin.println(" at junit.swingui.TestRunner$17.run(TestRunner.java:669)"); + fUnfiltered= swin.toString(); + + StringWriter swout= new StringWriter(); + PrintWriter pwout= new PrintWriter(swout); + pwout.println("junit.framework.AssertionFailedError"); + pwout.println(" at MyTest.f(MyTest.java:13)"); + pwout.println(" at MyTest.testStackTrace(MyTest.java:8)"); + fFiltered= swout.toString(); + } + + public void testFilter() { + assertEquals(fFiltered, BaseTestRunner.getFilteredTrace(fUnfiltered)); + } +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/runner/TextFeedbackTest.java b/junit4/src/test/java/junit/tests/runner/TextFeedbackTest.java new file mode 100644 index 0000000..fa2dbb2 --- /dev/null +++ b/junit4/src/test/java/junit/tests/runner/TextFeedbackTest.java @@ -0,0 +1,109 @@ + +package junit.tests.runner; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.io.PrintStream; + +import junit.framework.AssertionFailedError; +import junit.framework.TestCase; +import junit.framework.TestResult; +import junit.framework.TestSuite; +import junit.textui.ResultPrinter; +import junit.textui.TestRunner; + +public class TextFeedbackTest extends TestCase { + OutputStream output; + TestRunner runner; + + static class TestResultPrinter extends ResultPrinter { + TestResultPrinter(PrintStream writer) { + super(writer); + } + + /* Spoof printing time so the tests are deterministic + */ + @Override + protected String elapsedTimeAsString(long runTime) { + return "0"; + } + } + + public static void main(String[] args) { + TestRunner.run(TextFeedbackTest.class); + } + + @Override + public void setUp() { + output= new ByteArrayOutputStream(); + runner= new TestRunner(new TestResultPrinter(new PrintStream(output))); + } + + public void testEmptySuite() { + String expected= expected(new String[]{"", "Time: 0", "", "OK (0 tests)", ""}); + runner.doRun(new TestSuite()); + assertEquals(expected, output.toString()); + } + + + public void testOneTest() { + String expected= expected(new String[]{".", "Time: 0", "", "OK (1 test)", ""}); + TestSuite suite = new TestSuite(); + suite.addTest(new TestCase() { @Override + public void runTest() {}}); + runner.doRun(suite); + assertEquals(expected, output.toString()); + } + + public void testTwoTests() { + String expected= expected(new String[]{"..", "Time: 0", "", "OK (2 tests)", ""}); + TestSuite suite = new TestSuite(); + suite.addTest(new TestCase() { @Override + public void runTest() {}}); + suite.addTest(new TestCase() { @Override + public void runTest() {}}); + runner.doRun(suite); + assertEquals(expected, output.toString()); + } + + public void testFailure() { + String expected= expected(new String[]{".F", "Time: 0", "Failures here", "", "FAILURES!!!", "Tests run: 1, Failures: 1, Errors: 0", ""}); + ResultPrinter printer= new TestResultPrinter(new PrintStream(output)) { + @Override + public void printFailures(TestResult result) { + getWriter().println("Failures here"); + } + }; + runner.setPrinter(printer); + TestSuite suite = new TestSuite(); + suite.addTest(new TestCase() { @Override + public void runTest() {throw new AssertionFailedError();}}); + runner.doRun(suite); + assertEquals(expected, output.toString()); + } + + public void testError() { + String expected= expected(new String[]{".E", "Time: 0", "Errors here", "", "FAILURES!!!", "Tests run: 1, Failures: 0, Errors: 1", ""}); + ResultPrinter printer= new TestResultPrinter(new PrintStream(output)) { + @Override + public void printErrors(TestResult result) { + getWriter().println("Errors here"); + } + }; + runner.setPrinter(printer); + TestSuite suite = new TestSuite(); + suite.addTest(new TestCase() { @Override + public void runTest() throws Exception {throw new Exception();}}); + runner.doRun(suite); + assertEquals(expected, output.toString()); + } + + private String expected(String[] lines) { + OutputStream expected= new ByteArrayOutputStream(); + PrintStream expectedWriter= new PrintStream(expected); + for (int i= 0; i < lines.length; i++) + expectedWriter.println(lines[i]); + return expected.toString(); + } + +} diff --git a/junit4/src/test/java/junit/tests/runner/TextRunnerSingleMethodTest.java b/junit4/src/test/java/junit/tests/runner/TextRunnerSingleMethodTest.java new file mode 100644 index 0000000..1034fdd --- /dev/null +++ b/junit4/src/test/java/junit/tests/runner/TextRunnerSingleMethodTest.java @@ -0,0 +1,39 @@ +package junit.tests.runner; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import junit.framework.TestCase; +import junit.textui.ResultPrinter; +import junit.textui.TestRunner; + +/** + * Test invoking a single test method of a TestCase. + */ +public class TextRunnerSingleMethodTest extends TestCase { + + static boolean fgWasInvoked; + + public static class InvocationTest extends TestCase { + + public void testWasInvoked() { + TextRunnerSingleMethodTest.fgWasInvoked= true; + } + + public void testNotInvoked() { + fail("Shouldn't get here."); + } + } + + public void testSingle() throws Exception { + TestRunner t= new TestRunner(); + t.setPrinter(new ResultPrinter(new PrintStream(new ByteArrayOutputStream()))); + String[] args= { + "-m", "junit.tests.runner.TextRunnerSingleMethodTest$InvocationTest.testWasInvoked" + }; + fgWasInvoked= false; + t.start(args); + assertTrue(fgWasInvoked); + } + +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/runner/TextRunnerTest.java b/junit4/src/test/java/junit/tests/runner/TextRunnerTest.java new file mode 100644 index 0000000..d7b5941 --- /dev/null +++ b/junit4/src/test/java/junit/tests/runner/TextRunnerTest.java @@ -0,0 +1,61 @@ +package junit.tests.runner; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; + +import junit.framework.TestCase; +import junit.framework.TestResult; +import junit.framework.TestSuite; + +public class TextRunnerTest extends TestCase { + + public void testFailure() throws Exception { + execTest("junit.tests.framework.Failure", false); + } + + public void testSuccess() throws Exception { + execTest("junit.tests.framework.Success", true); + } + + public void testError() throws Exception { + execTest("junit.tests.BogusDude", false); + } + + void execTest(String testClass, boolean success) throws Exception { + String java= System.getProperty("java.home")+File.separator+"bin"+File.separator+"java"; + String cp= System.getProperty("java.class.path"); + //use -classpath for JDK 1.1.7 compatibility + String [] cmd= { java, "-classpath", cp, "junit.textui.TestRunner", testClass}; + Process p= Runtime.getRuntime().exec(cmd); + InputStream i= p.getInputStream(); + while((i.read()) != -1) + ; //System.out.write(b); + assertTrue((p.waitFor() == 0) == success); + if (success) + assertTrue(p.exitValue() == 0); + else + assertFalse(p.exitValue() == 0); + } + + public void testRunReturnsResult() { + PrintStream oldOut= System.out; + System.setOut(new PrintStream ( + new OutputStream() { + @Override + public void write(int arg0) throws IOException { + } + } + )); + try { + TestResult result= junit.textui.TestRunner.run(new TestSuite()); + assertTrue(result.wasSuccessful()); + } finally { + System.setOut(oldOut); + } + } + + +}
\ No newline at end of file diff --git a/junit4/src/test/java/junit/tests/runner/package-info.java b/junit4/src/test/java/junit/tests/runner/package-info.java new file mode 100644 index 0000000..fc44e8a --- /dev/null +++ b/junit4/src/test/java/junit/tests/runner/package-info.java @@ -0,0 +1,4 @@ +/** + * Tests for the JUnit v3.x runner functionality. + */ +package junit.tests.runner;
\ No newline at end of file |