diff --git a/src/main/java/org/junit/internal/ArrayComparisonFailure.java b/src/main/java/org/junit/internal/ArrayComparisonFailure.java index 0b24cb865cc2..d300e7e5866b 100644 --- a/src/main/java/org/junit/internal/ArrayComparisonFailure.java +++ b/src/main/java/org/junit/internal/ArrayComparisonFailure.java @@ -42,6 +42,11 @@ public void addDimension(int index) { fIndices.add(0, index); } + @Override + public synchronized Throwable getCause() { + return super.getCause() == null ? fCause : super.getCause(); + } + @Override public String getMessage() { StringBuilder sb = new StringBuilder(); diff --git a/src/test/java/org/junit/internal/AllInternalTests.java b/src/test/java/org/junit/internal/AllInternalTests.java index c6f7ce29cd96..04cef67c73ab 100644 --- a/src/test/java/org/junit/internal/AllInternalTests.java +++ b/src/test/java/org/junit/internal/AllInternalTests.java @@ -16,7 +16,8 @@ FailOnTimeoutTest.class, MethodSorterTest.class, StacktracePrintingMatcherTest.class, - ThrowableCauseMatcherTest.class + ThrowableCauseMatcherTest.class, + ArrayComparisonFailureTest.class }) public class AllInternalTests { } diff --git a/src/test/java/org/junit/internal/ArrayComparisonFailureTest.java b/src/test/java/org/junit/internal/ArrayComparisonFailureTest.java new file mode 100644 index 000000000000..93db64992fa3 --- /dev/null +++ b/src/test/java/org/junit/internal/ArrayComparisonFailureTest.java @@ -0,0 +1,68 @@ +package org.junit.internal; + +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +public class ArrayComparisonFailureTest { + + private static final String ARRAY_COMPARISON_FAILURE_411 = "arrayComparisonFailure_411"; + private static final String ARRAY_COMPARISON_FAILURE_412 = "arrayComparisonFailure_412"; + + /* + Test compatibility of older versions of ArrayComparisonFailure + Setup: + - checkout prior versions of the codebase (r4.11, r4.12 in this case) + - catch the exception resulting from: + assertArrayEquals(new int[]{0, 1}, new int[]{0, 5}); + - serialize the resulting exception to a file, moving into the test/resources path + Ex., for v4.11's resulting exception {@link org/junit/internal/arrayComparisonFailure_411} + Current unit test: + - deserialize the above files casting it to the current version of the class + (catches any forward incompatibility with missing fields) + - assert the results from existing methods: getCause(), toString() -> getMessage() + (catches incompatible usages of fields) + + This does not test if an instance of the current version of the class is able to deserialize to a previous ver. + */ + + @Test + public void classShouldAccept411Version() throws Exception { + assertFailureSerializableFromOthers(ARRAY_COMPARISON_FAILURE_411); + } + + @Test + public void classShouldAccept412Version() throws Exception { + assertFailureSerializableFromOthers(ARRAY_COMPARISON_FAILURE_412); + } + + private void assertFailureSerializableFromOthers(String failureFileName) throws IOException, + ClassNotFoundException { + try { + assertArrayEquals(new int[]{0, 1}, new int[]{0, 5}); + fail(); + } catch (ArrayComparisonFailure e) { + ArrayComparisonFailure arrayComparisonFailureFromFile = deserializeFailureFromFile(failureFileName); + assertNotNull("ArrayComparisonFailure.getCause() should fallback to the deprecated fCause field" + + " for compatibility with older versions of junit4 that didn't use Throwable.initCause().", + arrayComparisonFailureFromFile.getCause()); + assertEquals(e.getCause().toString(), arrayComparisonFailureFromFile.getCause().toString()); + assertEquals(e.toString(), arrayComparisonFailureFromFile.toString()); + } + } + + private ArrayComparisonFailure deserializeFailureFromFile(String fileName) throws IOException, + ClassNotFoundException { + InputStream resource = getClass().getResourceAsStream(fileName); + ObjectInputStream objectInputStream = new ObjectInputStream(resource); + return (ArrayComparisonFailure) objectInputStream.readObject(); + } + +} \ No newline at end of file diff --git a/src/test/resources/org/junit/internal/arrayComparisonFailure_411 b/src/test/resources/org/junit/internal/arrayComparisonFailure_411 new file mode 100644 index 000000000000..11fe0c51d7b1 Binary files /dev/null and b/src/test/resources/org/junit/internal/arrayComparisonFailure_411 differ diff --git a/src/test/resources/org/junit/internal/arrayComparisonFailure_412 b/src/test/resources/org/junit/internal/arrayComparisonFailure_412 new file mode 100644 index 000000000000..11fe0c51d7b1 Binary files /dev/null and b/src/test/resources/org/junit/internal/arrayComparisonFailure_412 differ