-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix serialization of AssumptionViolatedException (#1654)
Added serializable descriptions of values and matchers and use them in writeObject() serialization of AssumptionViolatedException. Fixes #1192
- Loading branch information
Showing
6 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/org/junit/internal/SerializableMatcherDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.junit.internal; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.hamcrest.BaseMatcher; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.StringDescription; | ||
|
||
/** | ||
* This class exists solely to provide a serializable description of a matcher to be serialized as a field in | ||
* {@link AssumptionViolatedException}. Being a {@link Throwable}, it is required to be {@link Serializable}, but most | ||
* implementations of {@link Matcher} are not. This class works around that limitation as | ||
* {@link AssumptionViolatedException} only every uses the description of the {@link Matcher}, while still retaining | ||
* backwards compatibility with classes compiled against its class signature before 4.14 and/or deserialization of | ||
* previously serialized instances. | ||
*/ | ||
class SerializableMatcherDescription<T> extends BaseMatcher<T> implements Serializable { | ||
|
||
private final String matcherDescription; | ||
|
||
private SerializableMatcherDescription(Matcher<T> matcher) { | ||
matcherDescription = StringDescription.asString(matcher); | ||
} | ||
|
||
public boolean matches(Object o) { | ||
throw new UnsupportedOperationException("This Matcher implementation only captures the description"); | ||
} | ||
|
||
public void describeTo(Description description) { | ||
description.appendText(matcherDescription); | ||
} | ||
|
||
/** | ||
* Factory method that checks to see if the matcher is already serializable. | ||
* @param matcher the matcher to make serializable | ||
* @return The provided matcher if it is null or already serializable, | ||
* the SerializableMatcherDescription representation of it if it is not. | ||
*/ | ||
static <T> Matcher<T> asSerializableMatcher(Matcher<T> matcher) { | ||
if (matcher == null || matcher instanceof Serializable) { | ||
return matcher; | ||
} else { | ||
return new SerializableMatcherDescription<T>(matcher); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/org/junit/internal/SerializableValueDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.junit.internal; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* This class exists solely to provide a serializable description of a value to be serialized as a field in | ||
* {@link AssumptionViolatedException}. Being a {@link Throwable}, it is required to be {@link Serializable}, but a | ||
* value of type Object provides no guarantee to be serializable. This class works around that limitation as | ||
* {@link AssumptionViolatedException} only every uses the string representation of the value, while still retaining | ||
* backwards compatibility with classes compiled against its class signature before 4.14 and/or deserialization of | ||
* previously serialized instances. | ||
*/ | ||
class SerializableValueDescription implements Serializable { | ||
private final String value; | ||
|
||
private SerializableValueDescription(Object value) { | ||
this.value = String.valueOf(value); | ||
} | ||
|
||
/** | ||
* Factory method that checks to see if the value is already serializable. | ||
* @param value the value to make serializable | ||
* @return The provided value if it is null or already serializable, | ||
* the SerializableValueDescription representation of it if it is not. | ||
*/ | ||
static Object asSerializableValue(Object value) { | ||
if (value == null || value instanceof Serializable) { | ||
return value; | ||
} else { | ||
return new SerializableValueDescription(value); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+3.71 KB
...resources/org/junit/assumptionViolatedExceptionWithValueAndMatcherCanBeReserialized_v4_13
Binary file not shown.
Binary file added
BIN
+3.55 KB
...ources/org/junit/assumptionViolatedExceptionWithoutValueAndMatcherCanBeReserialized_v4_13
Binary file not shown.