15
15
*/
16
16
package com .arpnetworking .commons .jackson .databind .module ;
17
17
18
+ import com .fasterxml .jackson .core .type .TypeReference ;
18
19
import com .fasterxml .jackson .databind .Module ;
20
+ import com .fasterxml .jackson .databind .ObjectMapper ;
19
21
import com .fasterxml .jackson .databind .introspect .AnnotationIntrospectorPair ;
22
+ import org .junit .Assert ;
20
23
import org .junit .Before ;
24
+ import org .junit .Ignore ;
21
25
import org .junit .Test ;
22
26
import org .mockito .Mock ;
23
27
import org .mockito .Mockito ;
24
28
import org .mockito .MockitoAnnotations ;
25
29
30
+ import java .io .IOException ;
31
+ import java .util .Objects ;
32
+
26
33
/**
27
34
* Test for the BuilderModule class.
28
35
*
@@ -42,6 +49,150 @@ public void test() {
42
49
Mockito .verify (_context ).insertAnnotationIntrospector (Mockito .any (AnnotationIntrospectorPair .class ));
43
50
}
44
51
52
+ @ Test
53
+ public void testSimpleRoundTrip () throws IOException {
54
+ final ObjectMapper objectMapper = new ObjectMapper ();
55
+ objectMapper .registerModule (new BuilderModule ());
56
+
57
+ final PojoWithBuilder pojo = new PojoWithBuilder .Builder ()
58
+ .setInt (1 )
59
+ .build ();
60
+ final String pojoAsJson = objectMapper .writeValueAsString (pojo );
61
+ final PojoWithBuilder actualPojo = objectMapper .readValue (pojoAsJson , PojoWithBuilder .class );
62
+
63
+ Assert .assertEquals (pojo , actualPojo );
64
+ }
65
+
66
+ @ Test
67
+ @ Ignore
68
+ public void testGenericRoundTrip () throws IOException {
69
+ final ObjectMapper objectMapper = new ObjectMapper ();
70
+ objectMapper .registerModule (new BuilderModule ());
71
+
72
+ final GenericPojoWithBuilder <PojoWithBuilder > pojo = new GenericPojoWithBuilder .Builder <PojoWithBuilder >()
73
+ .setVal (
74
+ new PojoWithBuilder .Builder ()
75
+ .setInt (1 )
76
+ .build ())
77
+ .build ();
78
+ final String pojoAsJson = objectMapper .writeValueAsString (pojo );
79
+ final GenericPojoWithBuilder <PojoWithBuilder > actualPojo = objectMapper .readValue (
80
+ pojoAsJson ,
81
+ TYPE_REFERENCE_GENERIC_POJO_WITH_BUILDER );
82
+ Assert .assertEquals (pojo , actualPojo );
83
+ }
84
+
45
85
@ Mock
46
86
private Module .SetupContext _context ;
87
+
88
+ private static final TypeReference <GenericPojoWithBuilder <PojoWithBuilder >> TYPE_REFERENCE_GENERIC_POJO_WITH_BUILDER =
89
+ new TypeReference <GenericPojoWithBuilder <PojoWithBuilder >>(){};
90
+
91
+ /**
92
+ * Test class.
93
+ */
94
+ public static class PojoWithBuilder {
95
+
96
+ protected PojoWithBuilder (final Builder builder ) {
97
+ _int = builder ._int ;
98
+ }
99
+
100
+ public int getInt () {
101
+ return _int ;
102
+ }
103
+
104
+ @ Override
105
+ public boolean equals (final Object other ) {
106
+ if (this == other ) {
107
+ return true ;
108
+ }
109
+ if (!(other instanceof PojoWithBuilder )) {
110
+ return false ;
111
+ }
112
+ final PojoWithBuilder otherPojo = (PojoWithBuilder ) other ;
113
+ return otherPojo ._int == this ._int ;
114
+ }
115
+
116
+ @ Override
117
+ public int hashCode () {
118
+ return _int ;
119
+ }
120
+
121
+ private final int _int ;
122
+
123
+ /**
124
+ * Builder for PojoWithBuilder.
125
+ */
126
+ public static class Builder implements com .arpnetworking .commons .builder .Builder <PojoWithBuilder > {
127
+
128
+ public Builder setInt (final int value ) {
129
+ _int = value ;
130
+ return this ;
131
+ }
132
+
133
+ @ Override
134
+ public PojoWithBuilder build () {
135
+ return new PojoWithBuilder (this );
136
+ }
137
+
138
+ private Integer _int ;
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Parameterized test class.
144
+ *
145
+ * @param <T> The value type.
146
+ */
147
+ public static class GenericPojoWithBuilder <T > {
148
+
149
+ protected GenericPojoWithBuilder (final Builder <T > builder ) {
150
+ _val = builder ._val ;
151
+ }
152
+
153
+ public T getVal () {
154
+ return _val ;
155
+ }
156
+
157
+ @ Override
158
+ public boolean equals (final Object other ) {
159
+ if (this == other ) {
160
+ return true ;
161
+ }
162
+ if (!(other instanceof GenericPojoWithBuilder )) {
163
+ return false ;
164
+ }
165
+ @ SuppressWarnings ("unchecked" )
166
+ final GenericPojoWithBuilder <T > otherPojo = (GenericPojoWithBuilder <T >) other ;
167
+ return Objects .equals (otherPojo ._val , this ._val );
168
+ }
169
+
170
+ @ Override
171
+ public int hashCode () {
172
+ return _val == null ? 0 : _val .hashCode ();
173
+ }
174
+
175
+ private final T _val ;
176
+
177
+ /**
178
+ * Builder for GenericPojoWithBuilder.
179
+ *
180
+ * @param <T> The value type.
181
+ */
182
+ public static class Builder <T >
183
+ implements com .arpnetworking .commons .builder .Builder <GenericPojoWithBuilder <T >> {
184
+
185
+ public Builder <T > setVal (final T value ) {
186
+ _val = value ;
187
+ return this ;
188
+ }
189
+
190
+ @ Override
191
+ public GenericPojoWithBuilder <T > build () {
192
+ return new GenericPojoWithBuilder <T >(this );
193
+ }
194
+
195
+ private T _val ;
196
+ }
197
+ }
47
198
}
0 commit comments