Skip to content

Commit

Permalink
Add tests for serialization of Result.assumptionFailureCount.
Browse files Browse the repository at this point in the history
  • Loading branch information
kcooney committed Jun 9, 2018
1 parent c4c8ebd commit eb75886
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 13 deletions.
18 changes: 13 additions & 5 deletions src/main/java/org/junit/runner/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,28 @@ private Result(SerializedForm serializedForm) {
}

/**
* @return the number of tests run
* Returns the number of tests run
*/
public int getRunCount() {
return count.get();
}

/**
* @return the number of tests that failed during the run
* Returns the number of tests that failed during the run
*/
public int getFailureCount() {
return failures.size();
}

/**
* @return the number of milliseconds it took to run the entire suite to run
* Returns the number of milliseconds it took to run the entire suite to run
*/
public long getRunTime() {
return runTime.get();
}

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

/**
* @return the number of tests skipped because of an assumption failure
* Returns the number of tests skipped because of an assumption failure
*
* @throws UnsupportedOperationException if the result was serialized in a version before JUnit 4.13
* @since 4.13
*/
public int getAssumptionFailureCount() {
if (assumptionFailureCount == null) {
throw new UnsupportedOperationException(
"Result was serialized from a version of JUnit that doesn't support this method");
}
return assumptionFailureCount.get();
}

Expand Down Expand Up @@ -197,6 +204,7 @@ public void serialize(ObjectOutputStream s) throws IOException {
fields.put("fFailures", fFailures);
fields.put("fRunTime", fRunTime);
fields.put("fStartTime", fStartTime);
fields.put("assumptionFailureCount", assumptionFailureCount);
s.writeFields();
}

Expand Down
95 changes: 87 additions & 8 deletions src/test/java/junit/tests/runner/ResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
Expand All @@ -10,25 +12,69 @@

import junit.framework.TestCase;
import junit.tests.framework.Success;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.tests.running.methods.AnnotationTest;

public class ResultTest extends TestCase {

private Result fromStream;

public void testRunFailureResultCanBeSerialised() throws Exception {
JUnitCore runner = new JUnitCore();
Result result = runner.run(AnnotationTest.FailureTest.class);
assertResultSerializable(result);
}

public void testRunFailureResultCanBeReserialised_v4_12() throws Exception {
JUnitCore runner = new JUnitCore();
Result result = runner.run(AnnotationTest.FailureTest.class);
assertResultReserializable(result, SerializationFormat.V4_12);
}

public void testRunAssumptionFailedResultCanBeSerialised() throws Exception {
JUnitCore runner = new JUnitCore();
Result result = runner.run(AssumptionFailedTest.class);
assertResultSerializable(result);
}

public void testRunAssumptionFailedResultCanBeReserialised_v4_12() throws Exception {
JUnitCore runner = new JUnitCore();
Result result = runner.run(AssumptionFailedTest.class);
assertResultReserializable(result, SerializationFormat.V4_12);
}

public void testRunAssumptionFailedResultCanBeReserialised_v4_13() throws Exception {
JUnitCore runner = new JUnitCore();
Result result = runner.run(AssumptionFailedTest.class);
assertResultReserializable(result, SerializationFormat.V4_13);
}

public void testRunSuccessResultCanBeSerialised() throws Exception {
JUnitCore runner = new JUnitCore();
Result result = runner.run(Success.class);
assertResultSerializable(result);
}

public void testRunSuccessResultCanBeReserialised_v4_12() throws Exception {
JUnitCore runner = new JUnitCore();
Result result = runner.run(Success.class);
assertResultReserializable(result, SerializationFormat.V4_12);
}

public void testRunSuccessResultCanBeReserialised_v4_13() throws Exception {
JUnitCore runner = new JUnitCore();
Result result = runner.run(Success.class);
assertResultReserializable(result, SerializationFormat.V4_13);
}

private enum SerializationFormat {
V4_12,
V4_13
}

private void assertResultSerializable(Result result) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
Expand All @@ -37,14 +83,26 @@ private void assertResultSerializable(Result result) throws IOException, ClassNo
byte[] bytes = byteArrayOutputStream.toByteArray();
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
Result fromStream = (Result) objectInputStream.readObject();
assertSerializedCorrectly(result, fromStream);

InputStream resource = getClass().getResourceAsStream(getName());
assertNotNull("Could not read resource " + getName(), resource);
objectInputStream = new ObjectInputStream(resource);
assertSerializedCorrectly(result, fromStream, SerializationFormat.V4_13);
}

private void assertResultReserializable(Result result, SerializationFormat resourceSerializationFormat)
throws IOException, ClassNotFoundException {
String resourceName = getName();
InputStream resource = getClass().getResourceAsStream(resourceName);
assertNotNull("Could not read resource " + resourceName, resource);
ObjectInputStream objectInputStream = new ObjectInputStream(resource);
fromStream = (Result) objectInputStream.readObject();

assertSerializedCorrectly(new ResultWithFixedRunTime(result), fromStream);

assertSerializedCorrectly(new ResultWithFixedRunTime(result),
fromStream, resourceSerializationFormat);
}

static public class AssumptionFailedTest {
@Test
public void assumptionFailed() throws Exception {
org.junit.Assume.assumeTrue(false);
}
}

/**
Expand Down Expand Up @@ -85,14 +143,35 @@ public List<Failure> getFailures() {
public int getIgnoreCount() {
return delegate.getIgnoreCount();
}

@Override
public int getAssumptionFailureCount() {
return delegate.getAssumptionFailureCount();
}
}

private void assertSerializedCorrectly(Result result, Result fromStream) {
private void assertSerializedCorrectly(
Result result, Result fromStream, SerializationFormat serializationFormat) {
assertNotNull(fromStream);

// Exceptions don't implement equals() so we need to compare field by field
assertEquals("failureCount", result.getFailureCount(), fromStream.getFailureCount());
assertEquals("ignoreCount", result.getIgnoreCount(), fromStream.getIgnoreCount());

if (serializationFormat == SerializationFormat.V4_13) {
// assumption failures are serialized
assertEquals("assumptionFailureCount",
result.getAssumptionFailureCount(),
fromStream.getAssumptionFailureCount());
} else {
// assumption failures were not serialized
try {
fromStream.getAssumptionFailureCount();
fail("UnsupportedOperationException expected");
} catch (UnsupportedOperationException expected) {
}
}

assertEquals("runTime", result.getRunTime(), fromStream.getRunTime());
assertEquals("failures", result.getFailures().size(), fromStream.getFailures().size());
int index = 0;
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit eb75886

Please sign in to comment.