|
1 | 1 | package com.fasterxml.jackson.datatype.guava;
|
2 | 2 |
|
3 |
| - |
4 | 3 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
5 | 4 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
6 | 5 | import com.fasterxml.jackson.annotation.JsonInclude;
|
7 | 6 | import com.fasterxml.jackson.core.type.TypeReference;
|
8 | 7 | import com.fasterxml.jackson.databind.ObjectMapper;
|
9 | 8 | import com.google.common.base.Optional;
|
10 | 9 |
|
11 |
| -public class TestOptional extends BaseTest { |
| 10 | +public class TestOptional extends BaseTest |
| 11 | +{ |
| 12 | + private final ObjectMapper MAPPER = mapperWithModule(); |
| 13 | + |
| 14 | + @JsonAutoDetect(fieldVisibility=Visibility.ANY) |
| 15 | + public static final class OptionalData{ |
| 16 | + private Optional<String> myString; |
| 17 | + } |
| 18 | + |
| 19 | + @JsonAutoDetect(fieldVisibility=Visibility.ANY) |
| 20 | + public static final class OptionalGenericData<T>{ |
| 21 | + private Optional<T> myData; |
| 22 | + } |
| 23 | + |
12 | 24 | public void testDeserAbsent() throws Exception {
|
13 |
| - Optional<?> value = mapperWithModule().readValue("null", new TypeReference<Optional<String>>() {}); |
| 25 | + Optional<?> value = MAPPER.readValue("null", new TypeReference<Optional<String>>() {}); |
14 | 26 | assertFalse(value.isPresent());
|
15 | 27 | }
|
16 | 28 |
|
17 | 29 | public void testDeserSimpleString() throws Exception{
|
18 |
| - Optional<?> value = mapperWithModule().readValue("\"simpleString\"", new TypeReference<Optional<String>>() {}); |
| 30 | + Optional<?> value = MAPPER.readValue("\"simpleString\"", new TypeReference<Optional<String>>() {}); |
19 | 31 | assertTrue(value.isPresent());
|
20 | 32 | assertEquals("simpleString", value.get());
|
21 | 33 | }
|
22 | 34 |
|
23 | 35 | public void testDeserInsideObject() throws Exception {
|
24 |
| - OptionalData data = mapperWithModule().readValue("{\"myString\":\"simpleString\"}", OptionalData.class); |
| 36 | + OptionalData data = MAPPER.readValue("{\"myString\":\"simpleString\"}", OptionalData.class); |
25 | 37 | assertTrue(data.myString.isPresent());
|
26 | 38 | assertEquals("simpleString", data.myString.get());
|
27 | 39 | }
|
28 | 40 |
|
29 | 41 | public void testDeserComplexObject() throws Exception {
|
30 | 42 | TypeReference<Optional<OptionalData>> type = new TypeReference<Optional<OptionalData>>() {};
|
31 |
| - Optional<OptionalData> data = mapperWithModule().readValue("{\"myString\":\"simpleString\"}", type); |
| 43 | + Optional<OptionalData> data = MAPPER.readValue("{\"myString\":\"simpleString\"}", type); |
32 | 44 | assertTrue(data.isPresent());
|
33 | 45 | assertTrue(data.get().myString.isPresent());
|
34 | 46 | assertEquals("simpleString", data.get().myString.get());
|
35 | 47 | }
|
36 | 48 |
|
37 | 49 | public void testDeserGeneric() throws Exception {
|
38 | 50 | TypeReference<Optional<OptionalGenericData<String>>> type = new TypeReference<Optional<OptionalGenericData<String>>>() {};
|
39 |
| - Optional<OptionalGenericData<String>> data = mapperWithModule().readValue("{\"myData\":\"simpleString\"}", type); |
| 51 | + Optional<OptionalGenericData<String>> data = MAPPER.readValue("{\"myData\":\"simpleString\"}", type); |
40 | 52 | assertTrue(data.isPresent());
|
41 | 53 | assertTrue(data.get().myData.isPresent());
|
42 | 54 | assertEquals("simpleString", data.get().myData.get());
|
43 | 55 | }
|
44 | 56 |
|
45 | 57 | public void testSerAbsent() throws Exception {
|
46 |
| - String value = mapperWithModule().writeValueAsString(Optional.absent()); |
| 58 | + String value = MAPPER.writeValueAsString(Optional.absent()); |
47 | 59 | assertEquals("null", value);
|
48 | 60 | }
|
49 | 61 |
|
50 | 62 | public void testSerSimpleString() throws Exception {
|
51 |
| - String value = mapperWithModule().writeValueAsString(Optional.of("simpleString")); |
| 63 | + String value = MAPPER.writeValueAsString(Optional.of("simpleString")); |
52 | 64 | assertEquals("\"simpleString\"", value);
|
53 | 65 | }
|
54 | 66 |
|
55 | 67 | public void testSerInsideObject() throws Exception {
|
56 | 68 | OptionalData data = new OptionalData();
|
57 | 69 | data.myString = Optional.of("simpleString");
|
58 |
| - String value = mapperWithModule().writeValueAsString(data); |
| 70 | + String value = MAPPER.writeValueAsString(data); |
59 | 71 | assertEquals("{\"myString\":\"simpleString\"}", value);
|
60 | 72 | }
|
61 | 73 |
|
62 | 74 | public void testSerComplexObject() throws Exception {
|
63 | 75 | OptionalData data = new OptionalData();
|
64 | 76 | data.myString = Optional.of("simpleString");
|
65 |
| - String value = mapperWithModule().writeValueAsString(Optional.of(data)); |
| 77 | + String value = MAPPER.writeValueAsString(Optional.of(data)); |
66 | 78 | assertEquals("{\"myString\":\"simpleString\"}", value);
|
67 | 79 | }
|
68 | 80 |
|
69 | 81 | public void testSerGeneric() throws Exception {
|
70 | 82 | OptionalGenericData<String> data = new OptionalGenericData<String>();
|
71 | 83 | data.myData = Optional.of("simpleString");
|
72 |
| - String value = mapperWithModule().writeValueAsString(Optional.of(data)); |
| 84 | + String value = MAPPER.writeValueAsString(Optional.of(data)); |
73 | 85 | assertEquals("{\"myData\":\"simpleString\"}", value);
|
74 | 86 | }
|
75 | 87 |
|
@@ -99,15 +111,5 @@ public void testWithTypingEnabled() throws Exception {
|
99 | 111 |
|
100 | 112 | final OptionalData deserializedMyData = objectMapper.readValue(json, OptionalData.class);
|
101 | 113 | assertEquals(myData.myString, deserializedMyData.myString);
|
102 |
| - } |
103 |
| - |
104 |
| - @JsonAutoDetect(fieldVisibility=Visibility.ANY) |
105 |
| - public static final class OptionalData{ |
106 |
| - private Optional<String> myString; |
107 |
| - } |
108 |
| - |
109 |
| - @JsonAutoDetect(fieldVisibility=Visibility.ANY) |
110 |
| - public static final class OptionalGenericData<T>{ |
111 |
| - private Optional<T> myData; |
112 | 114 | }
|
113 | 115 | }
|
0 commit comments