1 package com.atlassian.vcache.marshallers;
2
3 import com.atlassian.vcache.MarshallerException;
4 import org.junit.Test;
5
6 import java.util.Optional;
7
8 import static org.hamcrest.Matchers.is;
9 import static org.hamcrest.Matchers.notNullValue;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertThat;
12
13 public class OptionalMarshallerTest {
14
15
16
17
18
19 private OptionalMarshaller<String> marshaller = new OptionalMarshaller<>(new StringMarshaller());
20
21 @Test
22 public void emptyValue() throws MarshallerException {
23 final byte[] raw = marshaller.marshall(Optional.empty());
24
25 assertArrayEquals(new byte[] {0}, raw);
26
27 final Optional<String> revived = marshaller.unmarshall(raw);
28
29 assertThat(revived, notNullValue());
30 assertThat(revived, is(Optional.empty()));
31 }
32
33 @Test
34 public void withValue() throws MarshallerException {
35 final byte[] raw = marshaller.marshall(Optional.of("claira"));
36
37 assertArrayEquals(new byte[] {1, 99, 108, 97, 105, 114, 97}, raw);
38
39 final Optional<String> revived = marshaller.unmarshall(raw);
40
41 assertThat(revived, notNullValue());
42 assertThat(revived, is(Optional.of("claira")));
43 }
44
45 @Test
46 public void withValue_empty() throws MarshallerException {
47 final byte[] raw = marshaller.marshall(Optional.of(""));
48
49 assertArrayEquals(new byte[] {1}, raw);
50
51 final Optional<String> revived = marshaller.unmarshall(raw);
52
53 assertThat(revived, notNullValue());
54 assertThat(revived, is(Optional.of("")));
55 }
56 }