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
+ }
0 commit comments