diff options
author | Pete Bentley <prb@google.com> | 2021-02-21 18:26:28 +0000 |
---|---|---|
committer | Pete Bentley <prb@google.com> | 2021-02-22 18:38:53 +0000 |
commit | aa1524950fb5fbffbb61d23c5442984cb731a1eb (patch) | |
tree | 7ce2c9779133dd053d314fdb25818acbbb4fa2be /src/main/java/org/junit/internal/SerializableMatcherDescription.java | |
parent | e938015d2d5a2157ed20eb622fcd9750a12bb4af (diff) | |
download | junit-aa1524950fb5fbffbb61d23c5442984cb731a1eb.tar.gz |
Upgrade external/junit to 4.13.2
Contains just the changes from 4.12 to 4.13.2 and undoes local
Android changes. Will re-patch those in in subsequent CLs.
Bug: 129054170
Test: m
Change-Id: I5ec909df6840f6c54cbab9c509c5addaee3f94e6
Diffstat (limited to 'src/main/java/org/junit/internal/SerializableMatcherDescription.java')
-rw-r--r-- | src/main/java/org/junit/internal/SerializableMatcherDescription.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main/java/org/junit/internal/SerializableMatcherDescription.java b/src/main/java/org/junit/internal/SerializableMatcherDescription.java new file mode 100644 index 0000000..e036557 --- /dev/null +++ b/src/main/java/org/junit/internal/SerializableMatcherDescription.java @@ -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); + } + } +} |