Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture non Serializable inputs to ValueWrapper as transient field #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/org/opentest4j/ValueWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static ValueWrapper create(Object value, String stringRepresentation) {
private final Class<?> type;
private final String stringRepresentation;
private final int identityHashCode;
private final transient Object ephemeralValue;

/**
* Reads and stores the supplied value's runtime type, string representation, and
Expand All @@ -88,6 +89,7 @@ private ValueWrapper(Object value, String stringRepresentation) {
this.type = value != null ? value.getClass() : null;
this.stringRepresentation = stringRepresentation == null ? safeValueToString(value) : stringRepresentation;
this.identityHashCode = System.identityHashCode(value);
this.ephemeralValue = value;
}

private ValueWrapper(Object value) {
Expand Down Expand Up @@ -145,6 +147,18 @@ public int getIdentityHashCode() {
return this.identityHashCode;
}

/**
* Returns the value supplied to {@link #create(Object)}.
*
* <p>if {@code ValueWrapper}
* has been been serialized this will return {@code null}`
*
* @since 1.2
*/
public Object getEphemeralValue() {
return this.ephemeralValue;
}

/**
* Returns the value's string representation along with its type and
* identity hash code.
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/opentest4j/AssertionFailedErrorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,23 @@ public void deserializationOfAssertionFailedErrorWorksForVersion_1_0_0() throws
assertEquals("bar", error.getActual().getValue());
}

@Test
public void ephemeralValueIsOmittedFromSerialization() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, wouldn't this make more sense if value itself was Serializable, maybe add another variant with that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original intent was to verify the behavior of ephemeralValue through serialization, which should return null when given a non-serializable value.

I'm not opposed to adding more test but there are already a handful that exercise AssertionFailedError and ValueWrapper with serializable values.

class NonSerializable {
public final String guid = "8675309";
}

AssertionFailedError error = serializeAndDeserialize(
new AssertionFailedError("a message", new NonSerializable(), new NonSerializable()));
assertEquals("a message", error.getMessage());
assertTrue(error.isExpectedDefined());
assertNull(error.getExpected().getValue());
assertNull(error.getExpected().getEphemeralValue());
assertTrue(error.isActualDefined());
assertNull(error.getActual().getValue());
assertNull(error.getActual().getEphemeralValue());
}

private Object deserializeClasspathResource(String name) throws Exception {
InputStream inputStream = getClass().getResourceAsStream(name);
try {
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/opentest4j/ValueWrapperTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.Serializable;
Expand All @@ -43,6 +44,7 @@ public void wrapsNull() {
assertEquals("null", wrapper.getStringRepresentation());
assertEquals(0, wrapper.getIdentityHashCode());
assertEquals("null", wrapper.toString());
assertNull(wrapper.getEphemeralValue());
}

@Test
Expand All @@ -55,6 +57,8 @@ public void wrapsSerializableValue() {
assertNotEquals(0, wrapper.getIdentityHashCode());
assertTrue(wrapper.toString().startsWith("1.2 (java.lang.Double@"));
assertTrue(wrapper.toString().endsWith(")"));
assertEquals(1.2D, wrapper.getEphemeralValue());
assertSame(wrapper.getValue(), wrapper.getEphemeralValue());
}

@Test
Expand Down Expand Up @@ -98,6 +102,7 @@ public String toString() {
assertNull(wrapper.getValue());
assertEquals("someString", wrapper.getStringRepresentation());
assertNotEquals(0, wrapper.getIdentityHashCode());
assertEquals(value, wrapper.getEphemeralValue());

String toString = wrapper.toString();
assertTrue(toString, toString.startsWith("someString (" + NonSerializable.class.getName() + "@"));
Expand All @@ -120,6 +125,7 @@ public String toString() {

assertEquals(BrokenToString.class, wrapper.getType());
assertEquals(value, wrapper.getValue());
assertEquals(value, wrapper.getEphemeralValue());
String representation = wrapper.getStringRepresentation();
assertTrue(representation, representation.contains(RuntimeException.class.getName()));
assertTrue(representation, representation.contains("toStringFailure"));
Expand Down