Skip to content

Commit 801317e

Browse files
committed
Re-package test classes a bit
1 parent 2eafcdf commit 801317e

15 files changed

+200
-328
lines changed

release-notes/VERSION

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Project: jackson-databind
88

99
#938: Regression: `StackOverflowError` with recursive types that contain `Map.Entry`
1010
(reported by jloisel@github)
11-
#941: Deserialization from "{}" to ObjectNode field causes "out of END_OBJECT token" error
12-
(reported by Sadayuki F)
1311
#940: Add missing `hashCode()` implementations for `JsonNode` types that did not have them
1412
(contributed by Sergio M)
13+
#941: Deserialization from "{}" to ObjectNode field causes "out of END_OBJECT token" error
14+
(reported by Sadayuki F)
1515

1616
2.6.2 (14-Sep-2015)
1717

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
2+
package com.fasterxml.jackson.databind.module;
3+
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.util.*;
7+
8+
import org.junit.Ignore;
9+
import org.junit.Test;
10+
11+
import com.fasterxml.jackson.core.*;
12+
import com.fasterxml.jackson.core.type.TypeReference;
13+
14+
import com.fasterxml.jackson.databind.*;
15+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
16+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
17+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
18+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
19+
import com.fasterxml.jackson.databind.node.ObjectNode;
20+
21+
@SuppressWarnings("serial")
22+
public class TestCustomEnumKeyDeserializer extends BaseMapTest
23+
{
24+
@JsonSerialize(using = TestEnumSerializer.class, keyUsing = TestEnumKeySerializer.class)
25+
@JsonDeserialize(using = TestEnumDeserializer.class, keyUsing = TestEnumKeyDeserializer.class)
26+
public enum TestEnumMixin { }
27+
28+
enum KeyEnum {
29+
replacements,
30+
rootDirectory,
31+
licenseString
32+
}
33+
34+
enum TestEnum {
35+
RED("red"),
36+
GREEN("green");
37+
38+
private final String code;
39+
40+
TestEnum(String code) {
41+
this.code = code;
42+
}
43+
44+
public static TestEnum lookup(String lower) {
45+
for (TestEnum item : values()) {
46+
if (item.code().equals(lower)) {
47+
return item;
48+
}
49+
}
50+
throw new IllegalArgumentException("Invalid code " + lower);
51+
}
52+
53+
public String code() {
54+
return code;
55+
}
56+
}
57+
58+
static class TestEnumSerializer extends JsonSerializer<TestEnum> {
59+
@Override
60+
public void serialize(TestEnum languageCode, JsonGenerator g, SerializerProvider serializerProvider) throws IOException {
61+
g.writeString(languageCode.code());
62+
}
63+
64+
@Override
65+
public Class<TestEnum> handledType() {
66+
return TestEnum.class;
67+
}
68+
}
69+
70+
static class TestEnumKeyDeserializer extends KeyDeserializer {
71+
@Override
72+
public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException {
73+
try {
74+
return TestEnum.lookup(key);
75+
} catch (IllegalArgumentException e) {
76+
throw ctxt.weirdKeyException(TestEnum.class, key, "Unknown code");
77+
}
78+
}
79+
}
80+
81+
static class TestEnumDeserializer extends StdDeserializer<TestEnum> {
82+
public TestEnumDeserializer() {
83+
super(TestEnum.class);
84+
}
85+
86+
@Override
87+
public TestEnum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
88+
String code = jp.getText();
89+
try {
90+
return TestEnum.lookup(code);
91+
} catch (IllegalArgumentException e) {
92+
throw new InvalidFormatException("Undefined ISO-639 language code", jp.getCurrentLocation(), code, TestEnum.class);
93+
}
94+
}
95+
}
96+
97+
static class TestEnumKeySerializer extends JsonSerializer<TestEnum> {
98+
@Override
99+
public void serialize(TestEnum test, JsonGenerator g, SerializerProvider serializerProvider) throws IOException {
100+
g.writeFieldName(test.code());
101+
}
102+
103+
@Override
104+
public Class<TestEnum> handledType() {
105+
return TestEnum.class;
106+
}
107+
}
108+
109+
static class TestEnumModule extends SimpleModule {
110+
111+
public TestEnumModule() {
112+
super(Version.unknownVersion());
113+
}
114+
115+
@Override
116+
public void setupModule(SetupContext context) {
117+
context.setMixInAnnotations(TestEnum.class, TestEnumMixin.class);
118+
SimpleSerializers keySerializers = new SimpleSerializers();
119+
keySerializers.addSerializer(new TestEnumKeySerializer());
120+
context.addKeySerializers(keySerializers);
121+
}
122+
123+
public static ObjectMapper setupObjectMapper(ObjectMapper mapper) {
124+
final TestEnumModule module = new TestEnumModule();
125+
mapper.registerModule(module);
126+
return mapper;
127+
}
128+
}
129+
130+
static class Bean {
131+
private File rootDirectory;
132+
private String licenseString;
133+
private Map<TestEnum, Map<String, String>> replacements;
134+
135+
public File getRootDirectory() {
136+
return rootDirectory;
137+
}
138+
139+
public void setRootDirectory(File rootDirectory) {
140+
this.rootDirectory = rootDirectory;
141+
}
142+
143+
public String getLicenseString() {
144+
return licenseString;
145+
}
146+
147+
public void setLicenseString(String licenseString) {
148+
this.licenseString = licenseString;
149+
}
150+
151+
public Map<TestEnum, Map<String, String>> getReplacements() {
152+
return replacements;
153+
}
154+
155+
public void setReplacements(Map<TestEnum, Map<String, String>> replacements) {
156+
this.replacements = replacements;
157+
}
158+
}
159+
160+
/*
161+
/**********************************************************
162+
/* Test methods
163+
/**********************************************************
164+
*/
165+
166+
// Test passing with the fix
167+
@Test
168+
public void testWithEnumKeys() throws Exception {
169+
ObjectMapper plainObjectMapper = new ObjectMapper();
170+
JsonNode tree = plainObjectMapper.readTree(aposToQuotes("{'red' : [ 'a', 'b']}"));
171+
172+
ObjectMapper fancyObjectMapper = TestEnumModule.setupObjectMapper(new ObjectMapper());
173+
// this line is might throw with Jackson 2.6.2.
174+
Map<TestEnum, Set<String>> map = fancyObjectMapper.convertValue(tree, new TypeReference<Map<TestEnum, Set<String>>>() {
175+
});
176+
assertNotNull(map);
177+
}
178+
179+
// and another still failing
180+
// NOTE: temporarily named as non-test to ignore it; JsonIgnore doesn't work for some reason
181+
// public void testWithTree749() throws Exception
182+
public void withTree749() throws Exception
183+
{
184+
Map<KeyEnum, Object> inputMap = new LinkedHashMap<KeyEnum, Object>();
185+
Map<TestEnum, Map<String, String>> replacements = new LinkedHashMap<TestEnum, Map<String, String>>();
186+
Map<String, String> reps = new LinkedHashMap<String, String>();
187+
reps.put("1", "one");
188+
replacements.put(TestEnum.GREEN, reps);
189+
inputMap.put(KeyEnum.replacements, replacements);
190+
ObjectMapper mapper = TestEnumModule.setupObjectMapper(new ObjectMapper());
191+
JsonNode tree = mapper.valueToTree(inputMap);
192+
ObjectNode ob = (ObjectNode) tree;
193+
JsonNode inner = ob.get("replacements");
194+
String firstFieldName = inner.fieldNames().next();
195+
assertEquals("green", firstFieldName);
196+
}
197+
}

src/test/java/com/fasterxml/jackson/databind/module/TestCustomKeyDeserializer.java

-56
This file was deleted.

src/test/java/com/fasterxml/jackson/databind/module/customenumkey/Bean.java

-39
This file was deleted.

src/test/java/com/fasterxml/jackson/databind/module/customenumkey/KeyEnum.java

-10
This file was deleted.

src/test/java/com/fasterxml/jackson/databind/module/customenumkey/ModuleVersion.java

-28
This file was deleted.

src/test/java/com/fasterxml/jackson/databind/module/customenumkey/TestEnum.java

-42
This file was deleted.

0 commit comments

Comments
 (0)