44import static org .hamcrest .CoreMatchers .is ;
55import static org .hamcrest .CoreMatchers .notNullValue ;
66import static org .hamcrest .MatcherAssert .assertThat ;
7+ import static org .hamcrest .core .IsNot .not ;
8+ import static org .junit .Assert .assertEquals ;
9+ import static org .junit .Assert .assertNotNull ;
710import static org .junit .Assume .assumeThat ;
11+
12+ import java .io .ByteArrayInputStream ;
13+ import java .io .ByteArrayOutputStream ;
14+ import java .io .IOException ;
15+ import java .io .InputStream ;
16+ import java .io .ObjectInputStream ;
17+ import java .io .ObjectOutputStream ;
18+ import java .io .Serializable ;
19+
20+ import org .hamcrest .BaseMatcher ;
21+ import org .hamcrest .Description ;
822import org .hamcrest .Matcher ;
923import org .hamcrest .StringDescription ;
1024import org .junit .experimental .theories .DataPoint ;
1125import org .junit .experimental .theories .Theories ;
1226import org .junit .experimental .theories .Theory ;
27+ import org .junit .rules .TestName ;
1328import org .junit .runner .RunWith ;
1429
1530@ RunWith (Theories .class )
@@ -23,6 +38,14 @@ public class AssumptionViolatedExceptionTest {
2338 @ DataPoint
2439 public static Matcher <Integer > NULL = null ;
2540
41+ @ Rule
42+ public TestName name = new TestName ();
43+
44+ private static final String MESSAGE = "Assumption message" ;
45+ private static Matcher <Integer > SERIALIZABLE_IS_THREE = new SerializableIsThreeMatcher <Integer >();
46+ private static final UnserializableClass UNSERIALIZABLE_VALUE = new UnserializableClass ();
47+ private static final Matcher <UnserializableClass > UNSERIALIZABLE_MATCHER = not (is (UNSERIALIZABLE_VALUE ));
48+
2649 @ Theory
2750 public void toStringReportsMatcher (Integer actual , Matcher <Integer > matcher ) {
2851 assumeThat (matcher , notNullValue ());
@@ -92,4 +115,93 @@ public void canSetCauseWithInstanceCreatedWithExplicitThrowableConstructor() {
92115 AssumptionViolatedException e = new AssumptionViolatedException ("invalid number" , cause );
93116 assertThat (e .getCause (), is (cause ));
94117 }
118+
119+ @ Test
120+ public void assumptionViolatedExceptionWithoutValueAndMatcherCanBeReserialized_v4_13 ()
121+ throws IOException , ClassNotFoundException {
122+ assertReserializable (new AssumptionViolatedException (MESSAGE ));
123+ }
124+
125+ @ Test
126+ public void assumptionViolatedExceptionWithValueAndMatcherCanBeReserialized_v4_13 ()
127+ throws IOException , ClassNotFoundException {
128+ assertReserializable (new AssumptionViolatedException (MESSAGE , TWO , SERIALIZABLE_IS_THREE ));
129+ }
130+
131+ @ Test
132+ public void unserializableValueAndMatcherCanBeSerialized () throws IOException , ClassNotFoundException {
133+ AssumptionViolatedException exception = new AssumptionViolatedException (MESSAGE ,
134+ UNSERIALIZABLE_VALUE , UNSERIALIZABLE_MATCHER );
135+
136+ assertCanBeSerialized (exception );
137+ }
138+
139+ @ Test
140+ public void nullValueAndMatcherCanBeSerialized () throws IOException , ClassNotFoundException {
141+ AssumptionViolatedException exception = new AssumptionViolatedException (MESSAGE );
142+
143+ assertCanBeSerialized (exception );
144+ }
145+
146+ @ Test
147+ public void serializableValueAndMatcherCanBeSerialized () throws IOException , ClassNotFoundException {
148+ AssumptionViolatedException exception = new AssumptionViolatedException (MESSAGE ,
149+ TWO , SERIALIZABLE_IS_THREE );
150+
151+ assertCanBeSerialized (exception );
152+ }
153+
154+ private void assertCanBeSerialized (AssumptionViolatedException exception )
155+ throws IOException , ClassNotFoundException {
156+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
157+ ObjectOutputStream oos = new ObjectOutputStream (baos );
158+ oos .writeObject (exception );
159+
160+ ByteArrayInputStream bais = new ByteArrayInputStream (baos .toByteArray ());
161+ ObjectInputStream ois = new ObjectInputStream (bais );
162+ AssumptionViolatedException fromStream = (AssumptionViolatedException ) ois .readObject ();
163+
164+ assertSerializedCorrectly (exception , fromStream );
165+ }
166+
167+ private void assertReserializable (AssumptionViolatedException expected )
168+ throws IOException , ClassNotFoundException {
169+ String resourceName = name .getMethodName ();
170+ InputStream resource = getClass ().getResourceAsStream (resourceName );
171+ assertNotNull ("Could not read resource " + resourceName , resource );
172+ ObjectInputStream objectInputStream = new ObjectInputStream (resource );
173+ AssumptionViolatedException fromStream = (AssumptionViolatedException ) objectInputStream .readObject ();
174+
175+ assertSerializedCorrectly (expected , fromStream );
176+ }
177+
178+ private void assertSerializedCorrectly (
179+ AssumptionViolatedException expected , AssumptionViolatedException fromStream ) {
180+ assertNotNull (fromStream );
181+
182+ // Exceptions don't implement equals() so we need to compare field by field
183+ assertEquals ("message" , expected .getMessage (), fromStream .getMessage ());
184+ assertEquals ("description" , StringDescription .asString (expected ), StringDescription .asString (fromStream ));
185+ // We don't check the stackTrace as that will be influenced by how the test was started
186+ // (e.g. by maven or directly from IDE)
187+ // We also don't check the cause as that should already be serialized correctly by the superclass
188+ }
189+
190+ private static class SerializableIsThreeMatcher <T > extends BaseMatcher <T > implements Serializable {
191+
192+ public boolean matches (Object item ) {
193+ return IS_THREE .matches (item );
194+ }
195+
196+ public void describeTo (Description description ) {
197+ IS_THREE .describeTo (description );
198+ }
199+ }
200+
201+ private static class UnserializableClass {
202+ @ Override
203+ public String toString () {
204+ return "I'm not serializable" ;
205+ }
206+ }
95207}
0 commit comments