Skip to content

Commit eb75886

Browse files
committed
Add tests for serialization of Result.assumptionFailureCount.
1 parent c4c8ebd commit eb75886

7 files changed

+100
-13
lines changed

src/main/java/org/junit/runner/Result.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,28 @@ private Result(SerializedForm serializedForm) {
5555
}
5656

5757
/**
58-
* @return the number of tests run
58+
* Returns the number of tests run
5959
*/
6060
public int getRunCount() {
6161
return count.get();
6262
}
6363

6464
/**
65-
* @return the number of tests that failed during the run
65+
* Returns the number of tests that failed during the run
6666
*/
6767
public int getFailureCount() {
6868
return failures.size();
6969
}
7070

7171
/**
72-
* @return the number of milliseconds it took to run the entire suite to run
72+
* Returns the number of milliseconds it took to run the entire suite to run
7373
*/
7474
public long getRunTime() {
7575
return runTime.get();
7676
}
7777

7878
/**
79-
* @return the {@link Failure}s describing tests that failed and the problems they encountered
79+
* Returns the {@link Failure}s describing tests that failed and the problems they encountered
8080
*/
8181
public List<Failure> getFailures() {
8282
return failures;
@@ -90,9 +90,16 @@ public int getIgnoreCount() {
9090
}
9191

9292
/**
93-
* @return the number of tests skipped because of an assumption failure
93+
* Returns the number of tests skipped because of an assumption failure
94+
*
95+
* @throws UnsupportedOperationException if the result was serialized in a version before JUnit 4.13
96+
* @since 4.13
9497
*/
9598
public int getAssumptionFailureCount() {
99+
if (assumptionFailureCount == null) {
100+
throw new UnsupportedOperationException(
101+
"Result was serialized from a version of JUnit that doesn't support this method");
102+
}
96103
return assumptionFailureCount.get();
97104
}
98105

@@ -197,6 +204,7 @@ public void serialize(ObjectOutputStream s) throws IOException {
197204
fields.put("fFailures", fFailures);
198205
fields.put("fRunTime", fRunTime);
199206
fields.put("fStartTime", fStartTime);
207+
fields.put("assumptionFailureCount", assumptionFailureCount);
200208
s.writeFields();
201209
}
202210

src/test/java/junit/tests/runner/ResultTest.java

+87-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
5+
import java.io.File;
6+
import java.io.FileOutputStream;
57
import java.io.IOException;
68
import java.io.InputStream;
79
import java.io.ObjectInputStream;
@@ -10,25 +12,69 @@
1012

1113
import junit.framework.TestCase;
1214
import junit.tests.framework.Success;
15+
import org.junit.Test;
1316
import org.junit.runner.JUnitCore;
1417
import org.junit.runner.Result;
1518
import org.junit.runner.notification.Failure;
1619
import org.junit.tests.running.methods.AnnotationTest;
1720

1821
public class ResultTest extends TestCase {
1922

23+
private Result fromStream;
24+
2025
public void testRunFailureResultCanBeSerialised() throws Exception {
2126
JUnitCore runner = new JUnitCore();
2227
Result result = runner.run(AnnotationTest.FailureTest.class);
2328
assertResultSerializable(result);
2429
}
2530

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+
2655
public void testRunSuccessResultCanBeSerialised() throws Exception {
2756
JUnitCore runner = new JUnitCore();
2857
Result result = runner.run(Success.class);
2958
assertResultSerializable(result);
3059
}
3160

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+
3278
private void assertResultSerializable(Result result) throws IOException, ClassNotFoundException {
3379
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
3480
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
@@ -37,14 +83,26 @@ private void assertResultSerializable(Result result) throws IOException, ClassNo
3783
byte[] bytes = byteArrayOutputStream.toByteArray();
3884
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
3985
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);
4595
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+
}
48106
}
49107

50108
/**
@@ -85,14 +143,35 @@ public List<Failure> getFailures() {
85143
public int getIgnoreCount() {
86144
return delegate.getIgnoreCount();
87145
}
146+
147+
@Override
148+
public int getAssumptionFailureCount() {
149+
return delegate.getAssumptionFailureCount();
150+
}
88151
}
89152

90-
private void assertSerializedCorrectly(Result result, Result fromStream) {
153+
private void assertSerializedCorrectly(
154+
Result result, Result fromStream, SerializationFormat serializationFormat) {
91155
assertNotNull(fromStream);
92156

93157
// Exceptions don't implement equals() so we need to compare field by field
94158
assertEquals("failureCount", result.getFailureCount(), fromStream.getFailureCount());
95159
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+
96175
assertEquals("runTime", result.getRunTime(), fromStream.getRunTime());
97176
assertEquals("failures", result.getFailures().size(), fromStream.getFailures().size());
98177
int index = 0;
Binary file not shown.

0 commit comments

Comments
 (0)