Skip to content

Commit cd3e57a

Browse files
committed
Add tests to try to reproduce databind/2852 (but won't fail)
1 parent 2a96fda commit cd3e57a

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fasterxml.jackson.module.paramnames;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
9+
10+
public class ConstructorDetectorConfigTest
11+
extends ModuleTestBase
12+
{
13+
static class SingleArgImmutable {
14+
final String v;
15+
16+
public SingleArgImmutable(String value) { v = value; }
17+
}
18+
19+
private final ObjectMapper MAPPER = newMapper();
20+
21+
@Test
22+
public void testSimpleImmutable() throws Exception
23+
{
24+
ImmutableBean input = new ImmutableBean("value", 42);
25+
final String json = MAPPER.writeValueAsString(input);
26+
ImmutableBean result = MAPPER.readValue(json, ImmutableBean.class);
27+
assertEquals(input, result);
28+
}
29+
30+
// [databind#2852]
31+
@Test
32+
public void testSimpleArgNonAnnotatedProperties() throws Exception
33+
{
34+
final ObjectMapper mapper = mapperBuilder()
35+
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
36+
.build();
37+
SingleArgImmutable result = mapper.readValue("{\"value\": \"foo\"}",
38+
SingleArgImmutable.class);
39+
assertEquals("foo", result.v);
40+
}
41+
42+
// [databind#2852]
43+
@Test
44+
public void testSimpleArgNonAnnotatedDelegating() throws Exception
45+
{
46+
final ObjectMapper mapper = mapperBuilder()
47+
.constructorDetector(ConstructorDetector.USE_DELEGATING)
48+
.build();
49+
SingleArgImmutable result = mapper.readValue("\"bar\"",
50+
SingleArgImmutable.class);
51+
assertEquals("bar", result.v);
52+
}
53+
}
Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,47 @@
11
package com.fasterxml.jackson.module.paramnames;
22

3-
import com.fasterxml.jackson.annotation.JsonCreator;
4-
53
/**
64
* @author Lovro Pandzic
75
*/
8-
class ImmutableBean {
9-
6+
class ImmutableBean
7+
{
108
private final String name;
119
private final Integer value;
1210

1311
// needed because names are implicit (as of Jackson 2.4), not explicit
14-
@JsonCreator
12+
// 14-Oct-2020, tatu: As of 2.12, no longer needed (was fixed earlier)
13+
// @JsonCreator
1514
public ImmutableBean(String name, Integer value) {
16-
1715
this.name = name;
1816
this.value = value;
1917
}
2018

2119
public String getName() {
22-
2320
return name;
2421
}
2522

2623
public Integer getValue() {
27-
2824
return value;
2925
}
3026

3127
@Override
3228
public int hashCode() {
33-
34-
int result = name != null ? name.hashCode() : 0;
35-
result = 31 * result + (value != null ? value.hashCode() : 0);
36-
return result;
29+
return name.hashCode() + value.hashCode();
3730
}
3831

3932
@Override
4033
public boolean equals(Object o) {
41-
42-
if (this == o) {
43-
return true;
44-
}
34+
if (this == o) return true;
4535
if (o == null || getClass() != o.getClass()) {
4636
return false;
4737
}
4838

4939
final ImmutableBean that = (ImmutableBean) o;
50-
51-
if (name != null ? !name.equals(that.name) : that.name != null) {
52-
return false;
53-
}
54-
if (value != null ? !value.equals(that.value) : that.value != null) {
55-
return false;
56-
}
57-
58-
return true;
40+
return name.equals(that.name) && value.equals(that.value);
5941
}
6042

6143
@Override
6244
public String toString() {
63-
64-
return "ImmutableBean{" +
65-
"name='" + name + '\'' +
66-
", value=" + value +
67-
'}';
45+
return "ImmutableBean{name='" + name + "', value=" + value +'}';
6846
}
6947
}

parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/ModuleTestBase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Arrays;
44

55
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.json.JsonMapper;
67

78
public class ModuleTestBase
89
{
@@ -11,6 +12,11 @@ protected static ObjectMapper newMapper() {
1112
.registerModule(new ParameterNamesModule());
1213
}
1314

15+
protected static JsonMapper.Builder mapperBuilder() {
16+
return JsonMapper.builder()
17+
.addModule(new ParameterNamesModule());
18+
}
19+
1420
protected String quote(String value) {
1521
return "\"" + value + "\"";
1622
}

0 commit comments

Comments
 (0)