2
2
3
3
import java .io .ByteArrayInputStream ;
4
4
import java .io .ByteArrayOutputStream ;
5
+ import java .io .File ;
6
+ import java .io .FileOutputStream ;
5
7
import java .io .IOException ;
6
8
import java .io .InputStream ;
7
9
import java .io .ObjectInputStream ;
10
12
11
13
import junit .framework .TestCase ;
12
14
import junit .tests .framework .Success ;
15
+ import org .junit .Test ;
13
16
import org .junit .runner .JUnitCore ;
14
17
import org .junit .runner .Result ;
15
18
import org .junit .runner .notification .Failure ;
16
19
import org .junit .tests .running .methods .AnnotationTest ;
17
20
18
21
public class ResultTest extends TestCase {
19
22
23
+ private Result fromStream ;
24
+
20
25
public void testRunFailureResultCanBeSerialised () throws Exception {
21
26
JUnitCore runner = new JUnitCore ();
22
27
Result result = runner .run (AnnotationTest .FailureTest .class );
23
28
assertResultSerializable (result );
24
29
}
25
30
31
+ public void testRunFailureResultCanBeReserialised_v4_12 () throws Exception {
32
+ JUnitCore runner = new JUnitCore ();
33
+ Result result = runner .run (AnnotationTest .FailureTest .class );
34
+ assertResultReserializable (result , SerializationFormat .V4_12 );
35
+ }
36
+
37
+ public void testRunAssumptionFailedResultCanBeSerialised () throws Exception {
38
+ JUnitCore runner = new JUnitCore ();
39
+ Result result = runner .run (AssumptionFailedTest .class );
40
+ assertResultSerializable (result );
41
+ }
42
+
43
+ public void testRunAssumptionFailedResultCanBeReserialised_v4_12 () throws Exception {
44
+ JUnitCore runner = new JUnitCore ();
45
+ Result result = runner .run (AssumptionFailedTest .class );
46
+ assertResultReserializable (result , SerializationFormat .V4_12 );
47
+ }
48
+
49
+ public void testRunAssumptionFailedResultCanBeReserialised_v4_13 () throws Exception {
50
+ JUnitCore runner = new JUnitCore ();
51
+ Result result = runner .run (AssumptionFailedTest .class );
52
+ assertResultReserializable (result , SerializationFormat .V4_13 );
53
+ }
54
+
26
55
public void testRunSuccessResultCanBeSerialised () throws Exception {
27
56
JUnitCore runner = new JUnitCore ();
28
57
Result result = runner .run (Success .class );
29
58
assertResultSerializable (result );
30
59
}
31
60
61
+ public void testRunSuccessResultCanBeReserialised_v4_12 () throws Exception {
62
+ JUnitCore runner = new JUnitCore ();
63
+ Result result = runner .run (Success .class );
64
+ assertResultReserializable (result , SerializationFormat .V4_12 );
65
+ }
66
+
67
+ public void testRunSuccessResultCanBeReserialised_v4_13 () throws Exception {
68
+ JUnitCore runner = new JUnitCore ();
69
+ Result result = runner .run (Success .class );
70
+ assertResultReserializable (result , SerializationFormat .V4_13 );
71
+ }
72
+
73
+ private enum SerializationFormat {
74
+ V4_12 ,
75
+ V4_13
76
+ }
77
+
32
78
private void assertResultSerializable (Result result ) throws IOException , ClassNotFoundException {
33
79
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
34
80
ObjectOutputStream objectOutputStream = new ObjectOutputStream (byteArrayOutputStream );
@@ -37,14 +83,26 @@ private void assertResultSerializable(Result result) throws IOException, ClassNo
37
83
byte [] bytes = byteArrayOutputStream .toByteArray ();
38
84
ObjectInputStream objectInputStream = new ObjectInputStream (new ByteArrayInputStream (bytes ));
39
85
Result fromStream = (Result ) objectInputStream .readObject ();
40
- assertSerializedCorrectly (result , fromStream );
41
-
42
- InputStream resource = getClass ().getResourceAsStream (getName ());
43
- assertNotNull ("Could not read resource " + getName (), resource );
44
- objectInputStream = new ObjectInputStream (resource );
86
+ assertSerializedCorrectly (result , fromStream , SerializationFormat .V4_13 );
87
+ }
88
+
89
+ private void assertResultReserializable (Result result , SerializationFormat resourceSerializationFormat )
90
+ throws IOException , ClassNotFoundException {
91
+ String resourceName = getName ();
92
+ InputStream resource = getClass ().getResourceAsStream (resourceName );
93
+ assertNotNull ("Could not read resource " + resourceName , resource );
94
+ ObjectInputStream objectInputStream = new ObjectInputStream (resource );
45
95
fromStream = (Result ) objectInputStream .readObject ();
46
-
47
- assertSerializedCorrectly (new ResultWithFixedRunTime (result ), fromStream );
96
+
97
+ assertSerializedCorrectly (new ResultWithFixedRunTime (result ),
98
+ fromStream , resourceSerializationFormat );
99
+ }
100
+
101
+ static public class AssumptionFailedTest {
102
+ @ Test
103
+ public void assumptionFailed () throws Exception {
104
+ org .junit .Assume .assumeTrue (false );
105
+ }
48
106
}
49
107
50
108
/**
@@ -85,14 +143,35 @@ public List<Failure> getFailures() {
85
143
public int getIgnoreCount () {
86
144
return delegate .getIgnoreCount ();
87
145
}
146
+
147
+ @ Override
148
+ public int getAssumptionFailureCount () {
149
+ return delegate .getAssumptionFailureCount ();
150
+ }
88
151
}
89
152
90
- private void assertSerializedCorrectly (Result result , Result fromStream ) {
153
+ private void assertSerializedCorrectly (
154
+ Result result , Result fromStream , SerializationFormat serializationFormat ) {
91
155
assertNotNull (fromStream );
92
156
93
157
// Exceptions don't implement equals() so we need to compare field by field
94
158
assertEquals ("failureCount" , result .getFailureCount (), fromStream .getFailureCount ());
95
159
assertEquals ("ignoreCount" , result .getIgnoreCount (), fromStream .getIgnoreCount ());
160
+
161
+ if (serializationFormat == SerializationFormat .V4_13 ) {
162
+ // assumption failures are serialized
163
+ assertEquals ("assumptionFailureCount" ,
164
+ result .getAssumptionFailureCount (),
165
+ fromStream .getAssumptionFailureCount ());
166
+ } else {
167
+ // assumption failures were not serialized
168
+ try {
169
+ fromStream .getAssumptionFailureCount ();
170
+ fail ("UnsupportedOperationException expected" );
171
+ } catch (UnsupportedOperationException expected ) {
172
+ }
173
+ }
174
+
96
175
assertEquals ("runTime" , result .getRunTime (), fromStream .getRunTime ());
97
176
assertEquals ("failures" , result .getFailures ().size (), fromStream .getFailures ().size ());
98
177
int index = 0 ;
0 commit comments