Skip to content

Commit

Permalink
ArrayComparisonFailure serialization incompatibility fix:
Browse files Browse the repository at this point in the history
Override getCause() to allow fallback to the deprecated fCause field.
Run tests around possible forward incompatibility of the class from r4.11, 4.12.
  • Loading branch information
aishahalim committed Jun 13, 2016
1 parent 9523817 commit ebff3da
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/org/junit/internal/ArrayComparisonFailure.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/junit/internal/AllInternalTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
FailOnTimeoutTest.class,
MethodSorterTest.class,
StacktracePrintingMatcherTest.class,
ThrowableCauseMatcherTest.class
ThrowableCauseMatcherTest.class,
ArrayComparisonFailureTest.class
})
public class AllInternalTests {
}
68 changes: 68 additions & 0 deletions src/test/java/org/junit/internal/ArrayComparisonFailureTest.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
Binary file not shown.
Binary file not shown.

0 comments on commit ebff3da

Please sign in to comment.