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.Base64;
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.sameInstance;
13 import static org.junit.Assert.assertThat;
14
15 public class OptionSerializationTest {
16 @Test public void serializeSome() throws IOException {
17 final Option<Integer> opt = Option.some(1);
18 assertThat(Serializer.<Option<Integer>> toObject(toBytes(opt)), equalTo(opt));
19 }
20
21 @Test public void serializeNone() throws IOException {
22 final Option<Integer> opt = Option.none();
23 assertThat(Serializer.<Option<Integer>> toObject(toBytes(opt)), equalTo(opt));
24 }
25
26 @Test(expected = NotSerializableException.class) public void serializeSomeNonSerializable() throws IOException {
27 toObject(toBytes(Option.some(Serializer.Unserializable.instance())));
28 }
29
30 @Test public void deserializeAnonymousNoneReadResolve() throws IOException {
31 final byte[] serialized = Base64.getDecoder().decode(
32 "rO0ABXNyABtpby5hdGxhc3NpYW4uZnVndWUuT3B0aW9uJDHki4wrMT+ZGgIAAHhyABlpby5hdGxhc3NpYW4uZnVndWUuT3B0aW9ubO2YatZzMVECAAB4cA==");
33 assertThat(toObject(serialized), sameInstance(Option.none()));
34 }
35 }