summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/junit
diff options
context:
space:
mode:
authorYohann Roussel <yroussel@google.com>2014-03-19 16:25:37 +0100
committerYohann Roussel <yroussel@google.com>2014-03-20 15:13:33 +0100
commit4eceb95409e844fdc33c9c706e1dc307bfd40303 (patch)
treeee9f4f3fc79f757c79081c336bce4f1782c6ccd8 /junit4/src/test/java/junit
parent3d2402901b1a6462e2cf47a6fd09711f327961c3 (diff)
downloadtoolchain_jack-4eceb95409e844fdc33c9c706e1dc307bfd40303.zip
toolchain_jack-4eceb95409e844fdc33c9c706e1dc307bfd40303.tar.gz
toolchain_jack-4eceb95409e844fdc33c9c706e1dc307bfd40303.tar.bz2
Initial Jack import.
Change-Id: I953cf0a520195a7187d791b2885848ad0d5a9b43
Diffstat (limited to 'junit4/src/test/java/junit')
-rw-r--r--junit4/src/test/java/junit/samples/AllTests.java22
-rw-r--r--junit4/src/test/java/junit/samples/ListTest.java63
-rw-r--r--junit4/src/test/java/junit/samples/SimpleTest.java68
-rw-r--r--junit4/src/test/java/junit/samples/money/IMoney.java45
-rw-r--r--junit4/src/test/java/junit/samples/money/Money.java78
-rw-r--r--junit4/src/test/java/junit/samples/money/MoneyBag.java124
-rw-r--r--junit4/src/test/java/junit/samples/money/MoneyTest.java143
-rw-r--r--junit4/src/test/java/junit/samples/money/package-info.java4
-rw-r--r--junit4/src/test/java/junit/samples/package-info.java4
-rw-r--r--junit4/src/test/java/junit/tests/AllTests.java23
-rw-r--r--junit4/src/test/java/junit/tests/WasRun.java15
-rw-r--r--junit4/src/test/java/junit/tests/extensions/ActiveTestTest.java64
-rw-r--r--junit4/src/test/java/junit/tests/extensions/AllTests.java23
-rw-r--r--junit4/src/test/java/junit/tests/extensions/ExtensionTest.java98
-rw-r--r--junit4/src/test/java/junit/tests/extensions/RepeatedTestTest.java63
-rw-r--r--junit4/src/test/java/junit/tests/extensions/package-info.java4
-rw-r--r--junit4/src/test/java/junit/tests/framework/AllTests.java32
-rw-r--r--junit4/src/test/java/junit/tests/framework/AssertTest.java171
-rw-r--r--junit4/src/test/java/junit/tests/framework/AssertionFailedErrorTest.java23
-rw-r--r--junit4/src/test/java/junit/tests/framework/ComparisonCompactorTest.java102
-rw-r--r--junit4/src/test/java/junit/tests/framework/ComparisonFailureTest.java47
-rw-r--r--junit4/src/test/java/junit/tests/framework/DoublePrecisionAssertTest.java55
-rw-r--r--junit4/src/test/java/junit/tests/framework/Failure.java14
-rw-r--r--junit4/src/test/java/junit/tests/framework/FloatAssertTest.java63
-rw-r--r--junit4/src/test/java/junit/tests/framework/InheritedTestCase.java9
-rw-r--r--junit4/src/test/java/junit/tests/framework/NoArgTestCaseTest.java9
-rw-r--r--junit4/src/test/java/junit/tests/framework/NoTestCaseClass.java10
-rw-r--r--junit4/src/test/java/junit/tests/framework/NoTestCases.java11
-rw-r--r--junit4/src/test/java/junit/tests/framework/NotPublicTestCase.java13
-rw-r--r--junit4/src/test/java/junit/tests/framework/NotVoidTestCase.java14
-rw-r--r--junit4/src/test/java/junit/tests/framework/OneTestCase.java15
-rw-r--r--junit4/src/test/java/junit/tests/framework/OverrideTestCase.java10
-rw-r--r--junit4/src/test/java/junit/tests/framework/Success.java17
-rw-r--r--junit4/src/test/java/junit/tests/framework/SuiteTest.java105
-rw-r--r--junit4/src/test/java/junit/tests/framework/TestCaseTest.java190
-rw-r--r--junit4/src/test/java/junit/tests/framework/TestImplementorTest.java54
-rw-r--r--junit4/src/test/java/junit/tests/framework/TestListenerTest.java73
-rw-r--r--junit4/src/test/java/junit/tests/framework/ThreeTestCases.java15
-rw-r--r--junit4/src/test/java/junit/tests/framework/package-info.java4
-rw-r--r--junit4/src/test/java/junit/tests/package-info.java4
-rw-r--r--junit4/src/test/java/junit/tests/runner/AllTests.java31
-rw-r--r--junit4/src/test/java/junit/tests/runner/BaseTestRunnerTest.java53
-rw-r--r--junit4/src/test/java/junit/tests/runner/ResultTest.java37
-rw-r--r--junit4/src/test/java/junit/tests/runner/StackFilterTest.java46
-rw-r--r--junit4/src/test/java/junit/tests/runner/TextFeedbackTest.java109
-rw-r--r--junit4/src/test/java/junit/tests/runner/TextRunnerSingleMethodTest.java39
-rw-r--r--junit4/src/test/java/junit/tests/runner/TextRunnerTest.java61
-rw-r--r--junit4/src/test/java/junit/tests/runner/package-info.java4
48 files changed, 2281 insertions, 0 deletions
diff --git a/junit4/src/test/java/junit/samples/AllTests.java b/junit4/src/test/java/junit/samples/AllTests.java
new file mode 100644
index 0000000..2fb7628
--- /dev/null
+++ b/junit4/src/test/java/junit/samples/AllTests.java
@@ -0,0 +1,22 @@
+package junit.samples;
+
+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("All JUnit Tests");
+ suite.addTest(ListTest.suite());
+ suite.addTest(new TestSuite(junit.samples.money.MoneyTest.class));
+ suite.addTest(junit.tests.AllTests.suite());
+ return suite;
+ }
+} \ No newline at end of file
diff --git a/junit4/src/test/java/junit/samples/ListTest.java b/junit4/src/test/java/junit/samples/ListTest.java
new file mode 100644
index 0000000..eac850d
--- /dev/null
+++ b/junit4/src/test/java/junit/samples/ListTest.java
@@ -0,0 +1,63 @@
+package junit.samples;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * A sample test case, testing {@link java.util.Vector}.
+ *
+ */
+public class ListTest extends TestCase {
+ protected List<Integer> fEmpty;
+ protected List<Integer> fFull;
+
+ public static void main (String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ @Override
+ protected void setUp() {
+ fEmpty= new ArrayList<Integer>();
+ fFull= new ArrayList<Integer>();
+ fFull.add(1);
+ fFull.add(2);
+ fFull.add(3);
+ }
+ public static Test suite() {
+ return new TestSuite(ListTest.class);
+ }
+ public void testCapacity() {
+ int size= fFull.size();
+ for (int i= 0; i < 100; i++)
+ fFull.add(new Integer(i));
+ assertTrue(fFull.size() == 100+size);
+ }
+ public void testContains() {
+ assertTrue(fFull.contains(1));
+ assertTrue(!fEmpty.contains(1));
+ }
+ public void testElementAt() {
+ int i= fFull.get(0);
+ assertTrue(i == 1);
+
+ try {
+ fFull.get(fFull.size());
+ } catch (IndexOutOfBoundsException e) {
+ return;
+ }
+ fail("Should raise an ArrayIndexOutOfBoundsException");
+ }
+ public void testRemoveAll() {
+ fFull.removeAll(fFull);
+ fEmpty.removeAll(fEmpty);
+ assertTrue(fFull.isEmpty());
+ assertTrue(fEmpty.isEmpty());
+ }
+ public void testRemoveElement() {
+ fFull.remove(new Integer(3));
+ assertTrue(!fFull.contains(3) );
+ }
+} \ No newline at end of file
diff --git a/junit4/src/test/java/junit/samples/SimpleTest.java b/junit4/src/test/java/junit/samples/SimpleTest.java
new file mode 100644
index 0000000..f745021
--- /dev/null
+++ b/junit4/src/test/java/junit/samples/SimpleTest.java
@@ -0,0 +1,68 @@
+package junit.samples;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Some simple tests.
+ *
+ */
+public class SimpleTest extends TestCase {
+ protected int fValue1;
+ protected int fValue2;
+
+ @Override
+ protected void setUp() {
+ fValue1= 2;
+ fValue2= 3;
+ }
+ public static Test suite() {
+
+ /*
+ * the type safe way
+ *
+ TestSuite suite= new TestSuite();
+ suite.addTest(
+ new SimpleTest("add") {
+ protected void runTest() { testAdd(); }
+ }
+ );
+
+ suite.addTest(
+ new SimpleTest("testDivideByZero") {
+ protected void runTest() { testDivideByZero(); }
+ }
+ );
+ return suite;
+ */
+
+ /*
+ * the dynamic way
+ */
+ return new TestSuite(SimpleTest.class);
+ }
+ public void testAdd() {
+ double result= fValue1 + fValue2;
+ // forced failure result == 5
+ assertTrue(result == 6);
+ }
+
+ public int unused;
+ public void testDivideByZero() {
+ int zero= 0;
+ int result= 8/zero;
+ unused= result; // avoid warning for not using result
+ }
+ public void testEquals() {
+ assertEquals(12, 12);
+ assertEquals(12L, 12L);
+ assertEquals(new Long(12), new Long(12));
+
+ assertEquals("Size", 12, 13);
+ assertEquals("Capacity", 12.0, 11.99, 0.0);
+ }
+ public static void main (String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+} \ No newline at end of file
diff --git a/junit4/src/test/java/junit/samples/money/IMoney.java b/junit4/src/test/java/junit/samples/money/IMoney.java
new file mode 100644
index 0000000..b8f9496
--- /dev/null
+++ b/junit4/src/test/java/junit/samples/money/IMoney.java
@@ -0,0 +1,45 @@
+package junit.samples.money;
+
+/**
+ * The common interface for simple Monies and MoneyBags
+ *
+ */
+public interface IMoney {
+ /**
+ * Adds a money to this money.
+ */
+ public abstract IMoney add(IMoney m);
+ /**
+ * Adds a simple Money to this money. This is a helper method for
+ * implementing double dispatch
+ */
+ public abstract IMoney addMoney(Money m);
+ /**
+ * Adds a MoneyBag to this money. This is a helper method for
+ * implementing double dispatch
+ */
+ public abstract IMoney addMoneyBag(MoneyBag s);
+ /**
+ * Tests whether this money is zero
+ */
+ public abstract boolean isZero();
+ /**
+ * Multiplies a money by the given factor.
+ */
+ public abstract IMoney multiply(int factor);
+ /**
+ * Negates this money.
+ */
+ public abstract IMoney negate();
+ /**
+ * Subtracts a money from this money.
+ */
+ public abstract IMoney subtract(IMoney m);
+ /**
+ * Append this to a MoneyBag m.
+ * appendTo() needs to be public because it is used
+ * polymorphically, but it should not be used by clients
+ * because it modifies the argument m.
+ */
+ public abstract void appendTo(MoneyBag m);
+} \ No newline at end of file
diff --git a/junit4/src/test/java/junit/samples/money/Money.java b/junit4/src/test/java/junit/samples/money/Money.java
new file mode 100644
index 0000000..3d8c2fd
--- /dev/null
+++ b/junit4/src/test/java/junit/samples/money/Money.java
@@ -0,0 +1,78 @@
+package junit.samples.money;
+
+/**
+ * A simple Money.
+ *
+ */
+public class Money implements IMoney {
+
+ private int fAmount;
+ private String fCurrency;
+
+ /**
+ * Constructs a money from the given amount and currency.
+ */
+ public Money(int amount, String currency) {
+ fAmount= amount;
+ fCurrency= currency;
+ }
+ /**
+ * Adds a money to this money. Forwards the request to the addMoney helper.
+ */
+ public IMoney add(IMoney m) {
+ return m.addMoney(this);
+ }
+ public IMoney addMoney(Money m) {
+ if (m.currency().equals(currency()) )
+ return new Money(amount()+m.amount(), currency());
+ return MoneyBag.create(this, m);
+ }
+ public IMoney addMoneyBag(MoneyBag s) {
+ return s.addMoney(this);
+ }
+ public int amount() {
+ return fAmount;
+ }
+ public String currency() {
+ return fCurrency;
+ }
+ @Override
+ public boolean equals(Object anObject) {
+ if (isZero())
+ if (anObject instanceof IMoney)
+ return ((IMoney)anObject).isZero();
+ if (anObject instanceof Money) {
+ Money aMoney= (Money)anObject;
+ return aMoney.currency().equals(currency())
+ && amount() == aMoney.amount();
+ }
+ return false;
+ }
+ @Override
+ public int hashCode() {
+ if (fAmount == 0)
+ return 0;
+ return fCurrency.hashCode()+fAmount;
+ }
+ public boolean isZero() {
+ return amount() == 0;
+ }
+ public IMoney multiply(int factor) {
+ return new Money(amount()*factor, currency());
+ }
+ public IMoney negate() {
+ return new Money(-amount(), currency());
+ }
+ public IMoney subtract(IMoney m) {
+ return add(m.negate());
+ }
+ @Override
+ public String toString() {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("["+amount()+" "+currency()+"]");
+ return buffer.toString();
+ }
+ public /*this makes no sense*/ void appendTo(MoneyBag m) {
+ m.appendMoney(this);
+ }
+} \ No newline at end of file
diff --git a/junit4/src/test/java/junit/samples/money/MoneyBag.java b/junit4/src/test/java/junit/samples/money/MoneyBag.java
new file mode 100644
index 0000000..edc73b5
--- /dev/null
+++ b/junit4/src/test/java/junit/samples/money/MoneyBag.java
@@ -0,0 +1,124 @@
+package junit.samples.money;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * A MoneyBag defers exchange rate conversions. For example adding
+ * 12 Swiss Francs to 14 US Dollars is represented as a bag
+ * containing the two Monies 12 CHF and 14 USD. Adding another
+ * 10 Swiss francs gives a bag with 22 CHF and 14 USD. Due to
+ * the deferred exchange rate conversion we can later value a
+ * MoneyBag with different exchange rates.
+ *
+ * A MoneyBag is represented as a list of Monies and provides
+ * different constructors to create a MoneyBag.
+ */
+public class MoneyBag implements IMoney {
+ private List<Money> fMonies= new ArrayList<Money>(5);
+
+ public static IMoney create(IMoney m1, IMoney m2) {
+ MoneyBag result= new MoneyBag();
+ m1.appendTo(result);
+ m2.appendTo(result);
+ return result.simplify();
+ }
+ public IMoney add(IMoney m) {
+ return m.addMoneyBag(this);
+ }
+ public IMoney addMoney(Money m) {
+ return MoneyBag.create(m, this);
+ }
+ public IMoney addMoneyBag(MoneyBag s) {
+ return MoneyBag.create(s, this);
+ }
+ void appendBag(MoneyBag aBag) {
+ for (Money each : aBag.fMonies)
+ appendMoney(each);
+ }
+ void appendMoney(Money aMoney) {
+ if (aMoney.isZero()) return;
+ IMoney old= findMoney(aMoney.currency());
+ if (old == null) {
+ fMonies.add(aMoney);
+ return;
+ }
+ fMonies.remove(old);
+ Money sum= (Money) old.add(aMoney);
+ if (sum.isZero())
+ return;
+ fMonies.add(sum);
+ }
+ @Override
+ public boolean equals(Object anObject) {
+ if (isZero())
+ if (anObject instanceof IMoney)
+ return ((IMoney)anObject).isZero();
+
+ if (anObject instanceof MoneyBag) {
+ MoneyBag aMoneyBag= (MoneyBag)anObject;
+ if (aMoneyBag.fMonies.size() != fMonies.size())
+ return false;
+
+ for (Money each : fMonies)
+ if (! aMoneyBag.contains(each))
+ return false;
+ return true;
+ }
+ return false;
+ }
+ private Money findMoney(String currency) {
+ for (Money each : fMonies)
+ if (each.currency().equals(currency))
+ return each;
+ return null;
+ }
+ private boolean contains(Money m) {
+ Money found= findMoney(m.currency());
+ if (found == null) return false;
+ return found.amount() == m.amount();
+ }
+ @Override
+ public int hashCode() {
+ int hash= 0;
+ for (Money each : fMonies)
+ hash^= each.hashCode();
+ return hash;
+ }
+ public boolean isZero() {
+ return fMonies.size() == 0;
+ }
+ public IMoney multiply(int factor) {
+ MoneyBag result= new MoneyBag();
+ if (factor != 0)
+ for (Money each : fMonies)
+ result.appendMoney((Money) each.multiply(factor));
+ return result;
+ }
+ public IMoney negate() {
+ MoneyBag result= new MoneyBag();
+ for (Money each : fMonies)
+ result.appendMoney((Money) each.negate());
+ return result;
+ }
+ private IMoney simplify() {
+ if (fMonies.size() == 1)
+ return fMonies.iterator().next();
+ return this;
+ }
+ public IMoney subtract(IMoney m) {
+ return add(m.negate());
+ }
+ @Override
+ public String toString() {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("{");
+ for (Money each : fMonies)
+ buffer.append(each);
+ buffer.append("}");
+ return buffer.toString();
+ }
+ public void appendTo(MoneyBag m) {
+ m.appendBag(this);
+ }
+} \ No newline at end of file
diff --git a/junit4/src/test/java/junit/samples/money/MoneyTest.java b/junit4/src/test/java/junit/samples/money/MoneyTest.java
new file mode 100644
index 0000000..043798d
--- /dev/null
+++ b/junit4/src/test/java/junit/samples/money/MoneyTest.java
@@ -0,0 +1,143 @@
+package junit.samples.money;
+
+import junit.framework.TestCase;
+
+public class MoneyTest extends TestCase {
+ private Money f12CHF;
+ private Money f14CHF;
+ private Money f7USD;
+ private Money f21USD;
+
+ private IMoney fMB1;
+ private IMoney fMB2;
+
+ public static void main(String args[]) {
+ junit.textui.TestRunner.run(MoneyTest.class);
+ }
+ @Override
+ protected void setUp() {
+ f12CHF= new Money(12, "CHF");
+ f14CHF= new Money(14, "CHF");
+ f7USD= new Money( 7, "USD");
+ f21USD= new Money(21, "USD");
+
+ fMB1= MoneyBag.create(f12CHF, f7USD);
+ fMB2= MoneyBag.create(f14CHF, f21USD);
+ }
+ public void testBagMultiply() {
+ // {[12 CHF][7 USD]} *2 == {[24 CHF][14 USD]}
+ IMoney expected= MoneyBag.create(new Money(24, "CHF"), new Money(14, "USD"));
+ assertEquals(expected, fMB1.multiply(2));
+ assertEquals(fMB1, fMB1.multiply(1));
+ assertTrue(fMB1.multiply(0).isZero());
+ }
+ public void testBagNegate() {
+ // {[12 CHF][7 USD]} negate == {[-12 CHF][-7 USD]}
+ IMoney expected= MoneyBag.create(new Money(-12, "CHF"), new Money(-7, "USD"));
+ assertEquals(expected, fMB1.negate());
+ }
+ public void testBagSimpleAdd() {
+ // {[12 CHF][7 USD]} + [14 CHF] == {[26 CHF][7 USD]}
+ IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
+ assertEquals(expected, fMB1.add(f14CHF));
+ }
+ public void testBagSubtract() {
+ // {[12 CHF][7 USD]} - {[14 CHF][21 USD] == {[-2 CHF][-14 USD]}
+ IMoney expected= MoneyBag.create(new Money(-2, "CHF"), new Money(-14, "USD"));
+ assertEquals(expected, fMB1.subtract(fMB2));
+ }
+ public void testBagSumAdd() {
+ // {[12 CHF][7 USD]} + {[14 CHF][21 USD]} == {[26 CHF][28 USD]}
+ IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(28, "USD"));
+ assertEquals(expected, fMB1.add(fMB2));
+ }
+ public void testIsZero() {
+ assertTrue(fMB1.subtract(fMB1).isZero());
+ assertTrue(MoneyBag.create(new Money (0, "CHF"), new Money (0, "USD")).isZero());
+ }
+ public void testMixedSimpleAdd() {
+ // [12 CHF] + [7 USD] == {[12 CHF][7 USD]}
+ IMoney expected= MoneyBag.create(f12CHF, f7USD);
+ assertEquals(expected, f12CHF.add(f7USD));
+ }
+ public void testBagNotEquals() {
+ IMoney bag= MoneyBag.create(f12CHF, f7USD);
+ assertFalse(bag.equals(new Money(12, "DEM").add(f7USD)));
+ }
+ public void testMoneyBagEquals() {
+ assertTrue(!fMB1.equals(null));
+
+ assertEquals(fMB1, fMB1);
+ IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
+ assertTrue(fMB1.equals(equal));
+ assertTrue(!fMB1.equals(f12CHF));
+ assertTrue(!f12CHF.equals(fMB1));
+ assertTrue(!fMB1.equals(fMB2));
+ }
+ public void testMoneyBagHash() {
+ IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
+ assertEquals(fMB1.hashCode(), equal.hashCode());
+ }
+ public void testMoneyEquals() {
+ assertTrue(!f12CHF.equals(null));
+ Money equalMoney= new Money(12, "CHF");
+ assertEquals(f12CHF, f12CHF);
+ assertEquals(f12CHF, equalMoney);
+ assertEquals(f12CHF.hashCode(), equalMoney.hashCode());
+ assertTrue(!f12CHF.equals(f14CHF));
+ }
+ public void testMoneyHash() {
+ assertTrue(!f12CHF.equals(null));
+ Money equal= new Money(12, "CHF");
+ assertEquals(f12CHF.hashCode(), equal.hashCode());
+ }
+ public void testSimplify() {
+ IMoney money= MoneyBag.create(new Money(26, "CHF"), new Money(28, "CHF"));
+ assertEquals(new Money(54, "CHF"), money);
+ }
+ public void testNormalize2() {
+ // {[12 CHF][7 USD]} - [12 CHF] == [7 USD]
+ Money expected= new Money(7, "USD");
+ assertEquals(expected, fMB1.subtract(f12CHF));
+ }
+ public void testNormalize3() {
+ // {[12 CHF][7 USD]} - {[12 CHF][3 USD]} == [4 USD]
+ IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
+ Money expected= new Money(4, "USD");
+ assertEquals(expected, fMB1.subtract(ms1));
+ }
+ public void testNormalize4() {
+ // [12 CHF] - {[12 CHF][3 USD]} == [-3 USD]
+ IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
+ Money expected= new Money(-3, "USD");
+ assertEquals(expected, f12CHF.subtract(ms1));
+ }
+ public void testPrint() {
+ assertEquals("[12 CHF]", f12CHF.toString());
+ }
+ public void testSimpleAdd() {
+ // [12 CHF] + [14 CHF] == [26 CHF]
+ Money expected= new Money(26, "CHF");
+ assertEquals(expected, f12CHF.add(f14CHF));
+ }
+ public void testSimpleBagAdd() {
+ // [14 CHF] + {[12 CHF][7 USD]} == {[26 CHF][7 USD]}
+ IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
+ assertEquals(expected, f14CHF.add(fMB1));
+ }
+ public void testSimpleMultiply() {
+ // [14 CHF] *2 == [28 CHF]
+ Money expected= new Money(28, "CHF");
+ assertEquals(expected, f14CHF.multiply(2));
+ }
+ public void testSimpleNegate() {
+ // [14 CHF] negate == [-14 CHF]
+ Money expected= new Money(-14, "CHF");
+ assertEquals(expected, f14CHF.negate());
+ }
+ public void testSimpleSubtract() {
+ // [14 CHF] - [12 CHF] == [2 CHF]
+ Money expected= new Money(2, "CHF");
+ assertEquals(expected, f14CHF.subtract(f12CHF));
+ }
+} \ No newline at end of file
diff --git a/junit4/src/test/java/junit/samples/money/package-info.java b/junit4/src/test/java/junit/samples/money/package-info.java
new file mode 100644
index 0000000..eb37dd8
--- /dev/null
+++ b/junit4/src/test/java/junit/samples/money/package-info.java
@@ -0,0 +1,4 @@
+/**
+ * Example "Money" for JUnit v3.x.
+ */
+package junit.samples.money; \ No newline at end of file
diff --git a/junit4/src/test/java/junit/samples/package-info.java b/junit4/src/test/java/junit/samples/package-info.java
new file mode 100644
index 0000000..7b9ea8f
--- /dev/null
+++ b/junit4/src/test/java/junit/samples/package-info.java
@@ -0,0 +1,4 @@
+/**
+ * JUnit v3.x examples.
+ */
+package junit.samples; \ No newline at end of file
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