Skip to content

Commit

Permalink
minor test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 24, 2020
1 parent 65f1e89 commit a018ddf
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ static class NestedImpl implements Nested {
public String value;
}

private final ObjectMapper MAPPER = newObjectMapper();

@Test
public void testPolymorphicType() throws Exception
{
// first, with value
String YAML = "nested:\n"
+" type: single\n"
+" value: whatever";
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Wrapper top = mapper.readValue(YAML, Wrapper.class);
Wrapper top = MAPPER.readValue(YAML, Wrapper.class);
assertNotNull(top);
assertEquals(NestedImpl.class, top.nested.getClass());
assertEquals("whatever", ((NestedImpl) top.nested).value);

// then without value
YAML = "nested:\n"
+" type: single";
top = mapper.readValue(YAML, Wrapper.class);
top = MAPPER.readValue(YAML, Wrapper.class);
assertNotNull(top);
assertEquals(NestedImpl.class, top.nested.getClass());
assertNull("whatever", ((NestedImpl) top.nested).value);
Expand All @@ -49,26 +50,27 @@ public void testNativePolymorphicType() throws Exception {
String YAML = "nested: !single\n"
+" value: foobar\n"
;
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Wrapper top = mapper.readValue(YAML, Wrapper.class);
Wrapper top = MAPPER.readValue(YAML, Wrapper.class);
assertNotNull(top);
assertNotNull(top.nested);
assertEquals(NestedImpl.class, top.nested.getClass());
assertEquals("foobar", ((NestedImpl) top.nested).value);

YAML = "nested: !single { }\n";
top = mapper.readValue(YAML, Wrapper.class);
top = MAPPER.readValue("nested: !single { }\n", Wrapper.class);
assertNotNull(top);
assertNotNull(top.nested);
assertEquals(NestedImpl.class, top.nested.getClass());
}

@Test
public void testNativePolymorphicTypeFromEmpty() throws Exception {
// no value specified, empty

// And third possibility; trickier, since YAML contains empty String,
// and not Object; so we need to allow coercion
ObjectReader r = mapper.readerFor(Wrapper.class)
ObjectReader r = MAPPER.readerFor(Wrapper.class)
.with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
YAML = "nested: !single\n";
top = r.readValue(YAML);
Wrapper top = r.readValue("nested: !single\n");
assertNotNull(top);

// and as a result, get null
Expand Down

0 comments on commit a018ddf

Please sign in to comment.