forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithoutParamNamesModule5314Test.java
More file actions
104 lines (89 loc) · 3.52 KB
/
Copy pathWithoutParamNamesModule5314Test.java
File metadata and controls
104 lines (89 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package tools.jackson.databind.deser;
import java.beans.ConstructorProperties;
import org.junit.jupiter.api.Test;
import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.exc.InvalidDefinitionException;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.testutil.DatabindTestUtil;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
public class WithoutParamNamesModule5314Test
extends DatabindTestUtil
{
// Constructor can (and will) be auto-detected if (and only if!)
// Implicit Parameter Names are detected (see
// {@link MapperFeature#DETECT_PARAMETER_NAMES})
static class Bean178
{
final String hiddenName;
final int hiddenAge;
public Bean178(String openName, int openAge) {
hiddenName = openName;
hiddenAge = openAge;
}
}
// [databind#6229]: same shape as Bean178 above, but with `@ConstructorProperties`
// added -- specifically to validate Jackson 2.x compatibility, `@ConstructorProperties`
// must keep resolving names regardless of `DETECT_PARAMETER_NAMES`.
static class CtorPropsBean6229
{
final String hiddenName;
final int hiddenAge;
@ConstructorProperties({"openName", "openAge"})
public CtorPropsBean6229(String openName, int openAge) {
hiddenName = openName;
hiddenAge = openAge;
}
}
private final String JSON = a2q("{'openName':'stu','openAge':22}");
@Test
public void testWorksByDefault()
{
// Passes... by default
_runTestSuccess(JsonMapper.builder()
.build());
// Passes... when enabled
_runTestSuccess(JsonMapper.builder()
.enable(MapperFeature.DETECT_PARAMETER_NAMES).build());
// Fails when...disabled
_runTestFailure(JsonMapper.builder()
.disable(MapperFeature.DETECT_PARAMETER_NAMES).build());
// Fails when...used with Jackson2Defaults
_runTestFailure(JsonMapper
.builderWithJackson2Defaults().build());
}
// [databind#6229]: `CtorPropsBean178` (see above) must deserialize
// successfully under all three configs below.
@Test
public void testConstructorPropertiesIgnoresDetectParameterNames()
{
_runCtorPropsSuccess(JsonMapper.builder()
.enable(MapperFeature.DETECT_PARAMETER_NAMES).build());
_runCtorPropsSuccess(JsonMapper.builder()
.disable(MapperFeature.DETECT_PARAMETER_NAMES).build());
_runCtorPropsSuccess(JsonMapper
.builderWithJackson2Defaults().build());
}
private void _runTestSuccess(JsonMapper mapper)
{
Bean178 bean = mapper.readValue(JSON, Bean178.class);
assertEquals("stu", bean.hiddenName);
assertEquals(22, bean.hiddenAge);
}
private void _runCtorPropsSuccess(JsonMapper mapper)
{
CtorPropsBean6229 bean = mapper.readValue(JSON, CtorPropsBean6229.class);
assertEquals("stu", bean.hiddenName);
assertEquals(22, bean.hiddenAge);
}
private void _runTestFailure(JsonMapper mapper) {
try {
mapper.readValue(JSON, Bean178.class);
fail("Should have thrown an exception");
} catch (InvalidDefinitionException e) {
assertThat(e.getMessage())
.contains("Cannot construct instance of")
.contains("no Creators, like default constructor, exist");
}
}
}