Skip to content

Commit ebff3da

Browse files
committed
ArrayComparisonFailure serialization incompatibility fix:
Override getCause() to allow fallback to the deprecated fCause field. Run tests around possible forward incompatibility of the class from r4.11, 4.12.
1 parent 9523817 commit ebff3da

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

src/main/java/org/junit/internal/ArrayComparisonFailure.java

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public void addDimension(int index) {
4242
fIndices.add(0, index);
4343
}
4444

45+
@Override
46+
public synchronized Throwable getCause() {
47+
return super.getCause() == null ? fCause : super.getCause();
48+
}
49+
4550
@Override
4651
public String getMessage() {
4752
StringBuilder sb = new StringBuilder();

src/test/java/org/junit/internal/AllInternalTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
FailOnTimeoutTest.class,
1717
MethodSorterTest.class,
1818
StacktracePrintingMatcherTest.class,
19-
ThrowableCauseMatcherTest.class
19+
ThrowableCauseMatcherTest.class,
20+
ArrayComparisonFailureTest.class
2021
})
2122
public class AllInternalTests {
2223
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.junit.internal;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.ObjectInputStream;
8+
9+
import static org.junit.Assert.assertArrayEquals;
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
import static org.junit.Assert.fail;
13+
14+
public class ArrayComparisonFailureTest {
15+
16+
private static final String ARRAY_COMPARISON_FAILURE_411 = "arrayComparisonFailure_411";
17+
private static final String ARRAY_COMPARISON_FAILURE_412 = "arrayComparisonFailure_412";
18+
19+
/*
20+
Test compatibility of older versions of ArrayComparisonFailure
21+
Setup:
22+
- checkout prior versions of the codebase (r4.11, r4.12 in this case)
23+
- catch the exception resulting from:
24+
assertArrayEquals(new int[]{0, 1}, new int[]{0, 5});
25+
- serialize the resulting exception to a file, moving into the test/resources path
26+
Ex., for v4.11's resulting exception {@link org/junit/internal/arrayComparisonFailure_411}
27+
Current unit test:
28+
- deserialize the above files casting it to the current version of the class
29+
(catches any forward incompatibility with missing fields)
30+
- assert the results from existing methods: getCause(), toString() -> getMessage()
31+
(catches incompatible usages of fields)
32+
33+
This does not test if an instance of the current version of the class is able to deserialize to a previous ver.
34+
*/
35+
36+
@Test
37+
public void classShouldAccept411Version() throws Exception {
38+
assertFailureSerializableFromOthers(ARRAY_COMPARISON_FAILURE_411);
39+
}
40+
41+
@Test
42+
public void classShouldAccept412Version() throws Exception {
43+
assertFailureSerializableFromOthers(ARRAY_COMPARISON_FAILURE_412);
44+
}
45+
46+
private void assertFailureSerializableFromOthers(String failureFileName) throws IOException,
47+
ClassNotFoundException {
48+
try {
49+
assertArrayEquals(new int[]{0, 1}, new int[]{0, 5});
50+
fail();
51+
} catch (ArrayComparisonFailure e) {
52+
ArrayComparisonFailure arrayComparisonFailureFromFile = deserializeFailureFromFile(failureFileName);
53+
assertNotNull("ArrayComparisonFailure.getCause() should fallback to the deprecated fCause field"
54+
+ " for compatibility with older versions of junit4 that didn't use Throwable.initCause().",
55+
arrayComparisonFailureFromFile.getCause());
56+
assertEquals(e.getCause().toString(), arrayComparisonFailureFromFile.getCause().toString());
57+
assertEquals(e.toString(), arrayComparisonFailureFromFile.toString());
58+
}
59+
}
60+
61+
private ArrayComparisonFailure deserializeFailureFromFile(String fileName) throws IOException,
62+
ClassNotFoundException {
63+
InputStream resource = getClass().getResourceAsStream(fileName);
64+
ObjectInputStream objectInputStream = new ObjectInputStream(resource);
65+
return (ArrayComparisonFailure) objectInputStream.readObject();
66+
}
67+
68+
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)