Skip to content

Commit e07aaa2

Browse files
vjkoskelaBrandonArp
authored andcommitted
Integration tests for builder deserialization with Jackson. The generic test fails due to a bug in Jackson - FasterXML/jackson-databind#921 - and is currently disabled. (#16)
1 parent 9028d22 commit e07aaa2

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

src/test/java/com/arpnetworking/commons/jackson/databind/module/BuilderModuleTest.java

+151
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@
1515
*/
1616
package com.arpnetworking.commons.jackson.databind.module;
1717

18+
import com.fasterxml.jackson.core.type.TypeReference;
1819
import com.fasterxml.jackson.databind.Module;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
1921
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
22+
import org.junit.Assert;
2023
import org.junit.Before;
24+
import org.junit.Ignore;
2125
import org.junit.Test;
2226
import org.mockito.Mock;
2327
import org.mockito.Mockito;
2428
import org.mockito.MockitoAnnotations;
2529

30+
import java.io.IOException;
31+
import java.util.Objects;
32+
2633
/**
2734
* Test for the BuilderModule class.
2835
*
@@ -42,6 +49,150 @@ public void test() {
4249
Mockito.verify(_context).insertAnnotationIntrospector(Mockito.any(AnnotationIntrospectorPair.class));
4350
}
4451

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+
4585
@Mock
4686
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+
}
47198
}

0 commit comments

Comments
 (0)