Skip to content

Commit 5eb057a

Browse files
committed
More testing for #65
1 parent 153a169 commit 5eb057a

File tree

1 file changed

+72
-5
lines changed

1 file changed

+72
-5
lines changed

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/impl/CustomValueHandlersTest.java

+72-5
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ public CustomValueBean(int v) {
3131
}
3232

3333
static class CustomValueReader extends ValueReader {
34-
public CustomValueReader() {
34+
private final int delta;
35+
36+
public CustomValueReader(int d) {
3537
super(CustomValue.class);
38+
delta = d;
3639
}
3740

3841
@Override
3942
public Object read(JSONReader reader, JsonParser p) throws IOException {
40-
return new CustomValue(p.getIntValue(), true);
43+
return new CustomValue(p.getIntValue() + delta, true);
4144
}
4245

4346
// Base class impl should be fine, although we'd use this for optimal
@@ -49,23 +52,56 @@ public Object readNext(JSONReader reader, JsonParser p) throws IOException {
4952
*/
5053
}
5154

55+
static class ABCValueReader extends ValueReader {
56+
public ABCValueReader() {
57+
super(ABC.class);
58+
}
59+
60+
@Override
61+
public Object read(JSONReader reader, JsonParser p) throws IOException {
62+
final String str = p.getText();
63+
if ("n/a".equals(str)) {
64+
return ABC.DEF;
65+
}
66+
return ABC.valueOf(str);
67+
}
68+
}
69+
5270
static class CustomReaders extends ReaderWriterProvider {
71+
final int delta;
72+
73+
public CustomReaders(int d) {
74+
delta = d;
75+
}
76+
5377
@Override
5478
public ValueReader findBeanReader(JSONReader readContext, Class<?> type) {
5579
if (type.equals(CustomValue.class)) {
56-
return new CustomValueReader();
80+
return new CustomValueReader(delta);
81+
}
82+
return null;
83+
}
84+
85+
@Override
86+
public ValueReader findEnumReader(JSONReader readContext, Class<?> type) {
87+
if (type.equals(ABC.class)) {
88+
return new ABCValueReader();
5789
}
5890
return null;
5991
}
6092
}
6193

94+
enum ABC {
95+
A, B, C, DEF;
96+
}
97+
6298
/*
6399
/**********************************************************************
64100
/* Test methdods
65101
/**********************************************************************
66102
*/
67103

68-
public void testSimpleCustomReader() throws Exception
104+
public void testCustomBeanReader() throws Exception
69105
{
70106
// First: without handler, will fail to map
71107
try {
@@ -78,13 +114,44 @@ public void testSimpleCustomReader() throws Exception
78114

79115
// then with custom, should be fine
80116
JSON json = JSON.std
81-
.with(new CustomReaders());
117+
.with(new CustomReaders(0));
82118
CustomValue v = json.beanFrom(CustomValue.class, "123");
83119
assertEquals(124, v.value);
84120

85121
// similarly with wrapper
86122
CustomValueBean bean = json.beanFrom(CustomValueBean.class,
87123
aposToQuotes("{ 'custom' : 137 }"));
88124
assertEquals(138, bean.custom.value);
125+
126+
// but also ensure we can change registered handler(s)
127+
JSON json2 = json.with(new CustomReaders(100));
128+
v = json2.beanFrom(CustomValue.class, "123");
129+
assertEquals(224, v.value);
130+
}
131+
132+
public void testCustomEnumReader() throws Exception
133+
{
134+
// First: without handler, will fail to map
135+
try {
136+
JSON.std.beanFrom(ABC.class, quote("n/a"));
137+
fail("Should not pass");
138+
} catch (JSONObjectException e) {
139+
verifyException(e, "Failed to find Enum of type");
140+
}
141+
142+
// then with custom, should be fine
143+
JSON json = JSON.std
144+
.with(new CustomReaders(0));
145+
ABC v = json.beanFrom(ABC.class, quote("n/a"));
146+
assertEquals(ABC.DEF, v);
147+
148+
// but if we remove, again error
149+
JSON json2 = json.with((ReaderWriterProvider) null);
150+
try {
151+
json2.beanFrom(ABC.class, quote("n/a"));
152+
fail("Should not pass");
153+
} catch (JSONObjectException e) {
154+
verifyException(e, "Failed to find Enum of type");
155+
}
89156
}
90157
}

0 commit comments

Comments
 (0)