Skip to content

Commit

Permalink
More testing for #65
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 8, 2019
1 parent 153a169 commit 5eb057a
Showing 1 changed file with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ public CustomValueBean(int v) {
}

static class CustomValueReader extends ValueReader {
public CustomValueReader() {
private final int delta;

public CustomValueReader(int d) {
super(CustomValue.class);
delta = d;
}

@Override
public Object read(JSONReader reader, JsonParser p) throws IOException {
return new CustomValue(p.getIntValue(), true);
return new CustomValue(p.getIntValue() + delta, true);
}

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

static class ABCValueReader extends ValueReader {
public ABCValueReader() {
super(ABC.class);
}

@Override
public Object read(JSONReader reader, JsonParser p) throws IOException {
final String str = p.getText();
if ("n/a".equals(str)) {
return ABC.DEF;
}
return ABC.valueOf(str);
}
}

static class CustomReaders extends ReaderWriterProvider {
final int delta;

public CustomReaders(int d) {
delta = d;
}

@Override
public ValueReader findBeanReader(JSONReader readContext, Class<?> type) {
if (type.equals(CustomValue.class)) {
return new CustomValueReader();
return new CustomValueReader(delta);
}
return null;
}

@Override
public ValueReader findEnumReader(JSONReader readContext, Class<?> type) {
if (type.equals(ABC.class)) {
return new ABCValueReader();
}
return null;
}
}

enum ABC {
A, B, C, DEF;
}

/*
/**********************************************************************
/* Test methdods
/**********************************************************************
*/

public void testSimpleCustomReader() throws Exception
public void testCustomBeanReader() throws Exception
{
// First: without handler, will fail to map
try {
Expand All @@ -78,13 +114,44 @@ public void testSimpleCustomReader() throws Exception

// then with custom, should be fine
JSON json = JSON.std
.with(new CustomReaders());
.with(new CustomReaders(0));
CustomValue v = json.beanFrom(CustomValue.class, "123");
assertEquals(124, v.value);

// similarly with wrapper
CustomValueBean bean = json.beanFrom(CustomValueBean.class,
aposToQuotes("{ 'custom' : 137 }"));
assertEquals(138, bean.custom.value);

// but also ensure we can change registered handler(s)
JSON json2 = json.with(new CustomReaders(100));
v = json2.beanFrom(CustomValue.class, "123");
assertEquals(224, v.value);
}

public void testCustomEnumReader() throws Exception
{
// First: without handler, will fail to map
try {
JSON.std.beanFrom(ABC.class, quote("n/a"));
fail("Should not pass");
} catch (JSONObjectException e) {
verifyException(e, "Failed to find Enum of type");
}

// then with custom, should be fine
JSON json = JSON.std
.with(new CustomReaders(0));
ABC v = json.beanFrom(ABC.class, quote("n/a"));
assertEquals(ABC.DEF, v);

// but if we remove, again error
JSON json2 = json.with((ReaderWriterProvider) null);
try {
json2.beanFrom(ABC.class, quote("n/a"));
fail("Should not pass");
} catch (JSONObjectException e) {
verifyException(e, "Failed to find Enum of type");
}
}
}

0 comments on commit 5eb057a

Please sign in to comment.