From eb75886b2be7edf709441962bf3272e4319823c5 Mon Sep 17 00:00:00 2001 From: Kevin Cooney Date: Tue, 5 Jun 2018 08:45:08 -0700 Subject: [PATCH] Add tests for serialization of Result.assumptionFailureCount. --- src/main/java/org/junit/runner/Result.java | 18 +++- .../java/junit/tests/runner/ResultTest.java | 95 ++++++++++++++++-- ...umptionFailedResultCanBeReserialised_v4_12 | Bin 0 -> 766 bytes ...umptionFailedResultCanBeReserialised_v4_13 | Bin 0 -> 784 bytes ...stRunFailureResultCanBeReserialised_v4_12} | Bin ...stRunSuccessResultCanBeReserialised_v4_12} | Bin ...estRunSuccessResultCanBeReserialised_v4_13 | Bin 0 -> 784 bytes 7 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 src/test/resources/junit/tests/runner/testRunAssumptionFailedResultCanBeReserialised_v4_12 create mode 100644 src/test/resources/junit/tests/runner/testRunAssumptionFailedResultCanBeReserialised_v4_13 rename src/test/resources/junit/tests/runner/{testRunFailureResultCanBeSerialised => testRunFailureResultCanBeReserialised_v4_12} (100%) rename src/test/resources/junit/tests/runner/{testRunSuccessResultCanBeSerialised => testRunSuccessResultCanBeReserialised_v4_12} (100%) create mode 100644 src/test/resources/junit/tests/runner/testRunSuccessResultCanBeReserialised_v4_13 diff --git a/src/main/java/org/junit/runner/Result.java b/src/main/java/org/junit/runner/Result.java index 479c89689447..4b5f4a406238 100644 --- a/src/main/java/org/junit/runner/Result.java +++ b/src/main/java/org/junit/runner/Result.java @@ -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 getFailures() { return failures; @@ -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(); } @@ -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(); } diff --git a/src/test/java/junit/tests/runner/ResultTest.java b/src/test/java/junit/tests/runner/ResultTest.java index 9f09fafcb216..7a6042996268 100644 --- a/src/test/java/junit/tests/runner/ResultTest.java +++ b/src/test/java/junit/tests/runner/ResultTest.java @@ -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; @@ -10,6 +12,7 @@ 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; @@ -17,18 +20,61 @@ 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); @@ -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); + } } /** @@ -85,14 +143,35 @@ public List 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; diff --git a/src/test/resources/junit/tests/runner/testRunAssumptionFailedResultCanBeReserialised_v4_12 b/src/test/resources/junit/tests/runner/testRunAssumptionFailedResultCanBeReserialised_v4_12 new file mode 100644 index 0000000000000000000000000000000000000000..5eb4ef97839d352bd3db3e3c7e03ef995591c055 GIT binary patch literal 766 zcmaixy=xRf7{=edFLHh!Moui!1;Gzyn{-!5NC;e56_TKxFxi{k%rQHA&b$*hK?D&j z)G`(U{Rbqie}j!xN-M!y1S?DD<8phcoMPr>_I;n{H_x8@g0@z$GB%PUL8eg?Wu#<^ zY%y&F?%1p4P(I=sHR7)fD=5!LS(KcVohPf=KPJX1COkroo z#721A5fDc}Y621n$T@45rswAcl{tB6ie0AOoP1cic=Y34 z10XnBlUudPS}6oeT@-qG`#`39s+9bYjU4t-`upeW_1+8rXIjGEIPjKrx%Yp8TKEqA z`=I;r_)8NS2}CJEe=eT7&4$!246+L0T4->uh#&2aE#IJW;Vjm+t4h%W2U)#3T{`|s zKQ&zpEp6GuGoQCyu9YLc?DwnM)g6E9OPk2qiqg~XpZ`o|9g(lPe!t?H=F}Fw+Vxp4 GRQ~`uFz+w` literal 0 HcmV?d00001 diff --git a/src/test/resources/junit/tests/runner/testRunAssumptionFailedResultCanBeReserialised_v4_13 b/src/test/resources/junit/tests/runner/testRunAssumptionFailedResultCanBeReserialised_v4_13 new file mode 100644 index 0000000000000000000000000000000000000000..6fa2a060b86b025f8f2b440eea674e81244c23ac GIT binary patch literal 784 zcmaixJ!=#}7{{NzH3sV&fOE(8^CBSc|Qt^K!Yp*f_<`v$N0h|NrK{r@x@BB-|Tm!7xUuP{mRU zL1ny7mFBoKsO+cE$^F<4>_an!`Fx_K5V|uuDN|^hFXn?nR1+sZV#X8L z>1GqWJHkYoS=R_3oBVBG$`TF$j;)UZM zM-6~rsGNDnd0M-hwcSd^In7)qYH4FnWZSY5>;)Y<{^RWT&sPt+Fa0lSaeLxKTGsFG z{{?E{+vLx~`FAIun$Sog$`E=pIn`}8t#+xA8iXsM!QCf$yfw0XeKWS)S8Lm4DaoFL zGzZ@oPri`%O&3T@S@v+@=Ph?xzVj8s4ATTfzZrg83 zpk3lzh_#`&2@Ay(u?b7XHWG=BmqARF($xf6Ej9?_TGI7e3q*L3jmZH?H8P&&waB%U zRA5T5u2`O~`C}-MmQ)Th=oNl!A6`Q{gT-R1r4agaIvG=Fo39qbQq)tDA2E}u+1X(e zyypxg&Op)_NP>anl7Z)|FTa1}@p~KF5V{A1Yie`p`*}`8nLO3iE|u@jJ}qB4`T3y* z5P-_TdyZ*wcN^S#%{k3oCTeB-P~>~E7VLZ+Y2x H)(hqzUWM=G literal 0 HcmV?d00001