View Javadoc
1   package io.atlassian.fugue;
2   
3   import org.junit.Test;
4   
5   import java.io.IOException;
6   import java.io.NotSerializableException;
7   import java.util.Objects;
8   
9   import static io.atlassian.fugue.Serializer.toBytes;
10  import static io.atlassian.fugue.Serializer.toObject;
11  import static org.hamcrest.Matchers.equalTo;
12  import static org.hamcrest.Matchers.not;
13  import static org.junit.Assert.assertThat;
14  
15  public class TrySerializationTest {
16    @Test public void serializeFailure() throws IOException {
17      final Try<String> t = Try.failure(new EqualityException("Error message"));
18      assertThat(Serializer.toObject(toBytes(t)), equalTo(t));
19    }
20  
21    @Test public void serializeSuccess() throws IOException {
22      final Try<Integer> t = Try.successful(1);
23      assertThat(Serializer.toObject(toBytes(t)), equalTo(t));
24    }
25  
26    @Test public void serializeDelayedFailure() throws IOException {
27      final Try<String> t = Try.delayed(() -> Try.failure(new EqualityException("Delay This Error")));
28      final Try<String> deserialized = Serializer.toObject(toBytes(t));
29  
30      assertThat("Try.Delayed has not implemented .equals() so deserialized object should not be equal", deserialized, not(equalTo(t)));
31      assertThat("Evaluating a Try.Delayed into Either will have equal content", deserialized.toEither(), equalTo(t.toEither()));
32      assertThat("Evaluated Try.Delayed still not actually equal", deserialized, not(equalTo(t)));
33    }
34  
35    @Test public void serializeDelayedSuccess() throws IOException {
36      final Try<String> t = Try.delayed(() -> Try.successful("Delay This Message"));
37      final Try<String> deserialized = Serializer.toObject(toBytes(t));
38  
39      assertThat("Try.Delayed has not implemented .equals() so deserialized object should not be equal", deserialized, not(equalTo(t)));
40      assertThat("Evaluating a Try.Delayed into Either will have equal content", deserialized.toEither(), equalTo(t.toEither()));
41      assertThat("Evaluated Try.Delayed still not actually equal", deserialized, not(equalTo(t)));
42    }
43  
44    @Test(expected = NotSerializableException.class) public void serializeSuccessNonSerializable() throws IOException {
45      toObject(toBytes(Try.successful(Serializer.Unserializable.instance())));
46    }
47  
48    @Test(expected = NotSerializableException.class) public void serializeDelayedSuccessNonSerializable() throws IOException {
49      toObject(toBytes(Try.delayed(() -> Try.successful(Serializer.Unserializable.instance()))));
50    }
51  
52    private static class EqualityException extends RuntimeException {
53      private static final long serialVersionUID = 4644995396280333232L;
54  
55      EqualityException(String message) {
56        super(message);
57      }
58  
59      @Override public boolean equals(final Object o) {
60        if (this == o) {
61          return true;
62        }
63        if (o == null || getClass() != o.getClass()) {
64          return false;
65        }
66  
67        final EqualityException equalityException = (EqualityException) o;
68  
69        return Objects.equals(getMessage(), equalityException.getMessage());
70      }
71  
72      @Override public int hashCode() {
73        return getMessage().hashCode();
74      }
75    }
76  }