Skip to content

Commit ae10786

Browse files
committed
bacport test for 761
1 parent c94ec60 commit ae10786

File tree

1 file changed

+136
-17
lines changed

1 file changed

+136
-17
lines changed

src/test/java/com/fasterxml/jackson/databind/creators/TestBuilderSimple.java

Lines changed: 136 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonProperty;
55
import com.fasterxml.jackson.annotation.JsonSetter;
6+
import com.fasterxml.jackson.core.JsonProcessingException;
67
import com.fasterxml.jackson.databind.*;
78
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
89
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
@@ -157,8 +158,96 @@ public CreatorValue build() {
157158
return new CreatorValue(a, b, c);
158159
}
159160
}
160-
161-
/*
161+
162+
// for [databind#761]
163+
164+
@JsonDeserialize(builder=ValueInterfaceBuilder.class)
165+
interface ValueInterface {
166+
int getX();
167+
}
168+
169+
@JsonDeserialize(builder=ValueInterface2Builder.class)
170+
interface ValueInterface2 {
171+
int getX();
172+
}
173+
174+
static class ValueInterfaceImpl implements ValueInterface
175+
{
176+
final int _x;
177+
178+
protected ValueInterfaceImpl(int x) {
179+
_x = x+1;
180+
}
181+
182+
@Override
183+
public int getX() {
184+
return _x;
185+
}
186+
}
187+
188+
static class ValueInterface2Impl implements ValueInterface2
189+
{
190+
final int _x;
191+
192+
protected ValueInterface2Impl(int x) {
193+
_x = x+1;
194+
}
195+
196+
@Override
197+
public int getX() {
198+
return _x;
199+
}
200+
}
201+
202+
static class ValueInterfaceBuilder
203+
{
204+
public int x;
205+
206+
public ValueInterfaceBuilder withX(int x0) {
207+
this.x = x0;
208+
return this;
209+
}
210+
211+
public ValueInterface build() {
212+
return new ValueInterfaceImpl(x);
213+
}
214+
}
215+
216+
static class ValueInterface2Builder
217+
{
218+
public int x;
219+
220+
public ValueInterface2Builder withX(int x0) {
221+
this.x = x0;
222+
return this;
223+
}
224+
225+
// should also be ok: more specific type
226+
public ValueInterface2Impl build() {
227+
return new ValueInterface2Impl(x);
228+
}
229+
}
230+
231+
// for [databind#761]
232+
@JsonDeserialize(builder = ValueBuilderWrongBuildType.class)
233+
static class ValueClassWrongBuildType {
234+
}
235+
236+
static class ValueBuilderWrongBuildType
237+
{
238+
public int x;
239+
240+
public ValueBuilderWrongBuildType withX(int x0) {
241+
this.x = x0;
242+
return this;
243+
}
244+
245+
public ValueClassXY build() {
246+
return null;
247+
}
248+
}
249+
250+
/*
162251
/**********************************************************
163252
/* Unit tests
164253
/**********************************************************
@@ -168,25 +257,25 @@ public CreatorValue build() {
168257

169258
public void testSimple() throws Exception
170259
{
171-
String json = "{\"x\":1,\"y\":2}";
172-
Object o = mapper.readValue(json, ValueClassXY.class);
173-
assertNotNull(o);
174-
assertSame(ValueClassXY.class, o.getClass());
175-
ValueClassXY value = (ValueClassXY) o;
176-
// note: ctor adds one to both values
177-
assertEquals(value._x, 2);
178-
assertEquals(value._y, 3);
260+
String json = "{\"x\":1,\"y\":2}";
261+
Object o = mapper.readValue(json, ValueClassXY.class);
262+
assertNotNull(o);
263+
assertSame(ValueClassXY.class, o.getClass());
264+
ValueClassXY value = (ValueClassXY) o;
265+
// note: ctor adds one to both values
266+
assertEquals(value._x, 2);
267+
assertEquals(value._y, 3);
179268
}
180269

181270
public void testMultiAccess() throws Exception
182271
{
183-
String json = "{\"c\":3,\"a\":2,\"b\":-9}";
184-
ValueClassABC value = mapper.readValue(json, ValueClassABC.class);
185-
assertNotNull(value);
186-
// note: ctor adds one to both values
187-
assertEquals(value.a, 2);
188-
assertEquals(value.b, -9);
189-
assertEquals(value.c, 3);
272+
String json = "{\"c\":3,\"a\":2,\"b\":-9}";
273+
ValueClassABC value = mapper.readValue(json, ValueClassABC.class);
274+
assertNotNull(value);
275+
// note: ctor adds one to both values
276+
assertEquals(value.a, 2);
277+
assertEquals(value.b, -9);
278+
assertEquals(value.c, 3);
190279
}
191280

192281
// test for Immutable builder, to ensure return value is used
@@ -214,4 +303,34 @@ public void testWithCreator() throws Exception
214303
assertEquals(2, value.b);
215304
assertEquals(3, value.c);
216305
}
306+
307+
// for [databind#761]
308+
309+
public void testBuilderMethodReturnMoreGeneral() throws Exception
310+
{
311+
final String json = "{\"x\":1}";
312+
ValueInterface value = mapper.readValue(json, ValueInterface.class);
313+
assertEquals(2, value.getX());
314+
}
315+
316+
public void testBuilderMethodReturnMoreSpecific() throws Exception
317+
{
318+
final String json = "{\"x\":1}";
319+
ValueInterface2 value = mapper.readValue(json, ValueInterface2.class);
320+
assertEquals(2, value.getX());
321+
}
322+
323+
public void testBuilderMethodReturnInvalidType() throws Exception
324+
{
325+
final String json = "{\"x\":1}";
326+
try {
327+
mapper.readValue(json, ValueClassWrongBuildType.class);
328+
fail("Missing expected JsonProcessingException exception");
329+
} catch(JsonProcessingException e) {
330+
assertTrue(
331+
"Exception cause must be IllegalArgumentException",
332+
e.getCause() instanceof IllegalArgumentException);
333+
}
334+
}
335+
217336
}

0 commit comments

Comments
 (0)