-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DEDUCTION-based polymorphism base tests for YAML to try to repro #…
- Loading branch information
1 parent
75cf419
commit 95fc0f2
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/misc/TestPolymorphicDeduction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.fasterxml.jackson.dataformat.yaml.misc; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; | ||
|
||
import static com.fasterxml.jackson.annotation.JsonSubTypes.Type; | ||
import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.DEDUCTION; | ||
|
||
// for [databind#43], deduction-based polymorphism | ||
public class TestPolymorphicDeduction extends ModuleTestBase | ||
{ | ||
@JsonTypeInfo(use = DEDUCTION) | ||
@JsonSubTypes( {@Type(LiveCat.class), @Type(DeadCat.class), @Type(Fleabag.class)}) | ||
// A general supertype with no properties - used for tests involving {} | ||
interface Feline {} | ||
|
||
@JsonTypeInfo(use = DEDUCTION) | ||
@JsonSubTypes( {@Type(LiveCat.class), @Type(DeadCat.class)}) | ||
// A supertype containing common properties | ||
public static class Cat implements Feline { | ||
public String name; | ||
} | ||
|
||
// Distinguished by its parent and a unique property | ||
static class DeadCat extends Cat { | ||
public String causeOfDeath; | ||
} | ||
|
||
// Distinguished by its parent and a unique property | ||
static class LiveCat extends Cat { | ||
public boolean angry; | ||
} | ||
|
||
// No distinguishing properties whatsoever | ||
static class Fleabag implements Feline { | ||
// NO OP | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Mock data | ||
/********************************************************** | ||
*/ | ||
|
||
private static final String DEAD_CAT_DOC = "name: Felix\ncauseOfDeath: entropy\n"; | ||
private static final String LIVE_CAT_DOC = "name: Felix\nangry: true\n"; | ||
|
||
/* | ||
/********************************************************** | ||
/* Test methods | ||
/********************************************************** | ||
*/ | ||
|
||
private final ObjectMapper MAPPER = newObjectMapper(); | ||
|
||
public void testSimpleInference() throws Exception | ||
{ | ||
Cat cat = MAPPER.readValue(LIVE_CAT_DOC, Cat.class); | ||
assertTrue(cat instanceof LiveCat); | ||
assertSame(cat.getClass(), LiveCat.class); | ||
assertEquals("Felix", cat.name); | ||
assertTrue(((LiveCat)cat).angry); | ||
|
||
cat = MAPPER.readValue(DEAD_CAT_DOC, Cat.class); | ||
assertTrue(cat instanceof DeadCat); | ||
assertSame(cat.getClass(), DeadCat.class); | ||
assertEquals("Felix", cat.name); | ||
assertEquals("entropy", ((DeadCat)cat).causeOfDeath); | ||
} | ||
|
||
public void testSimpleInferenceOfEmptySubtypeDoesntMatchNull() throws Exception { | ||
Feline feline = MAPPER.readValue("null", Feline.class); | ||
assertNull(feline); | ||
} | ||
|
||
public void testCaseInsensitiveInference() throws Exception | ||
{ | ||
Cat cat = mapperBuilder() | ||
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true) | ||
.build() | ||
.readValue(DEAD_CAT_DOC.toUpperCase(), Cat.class); | ||
assertTrue(cat instanceof DeadCat); | ||
assertSame(cat.getClass(), DeadCat.class); | ||
assertEquals("FELIX", cat.name); | ||
assertEquals("ENTROPY", ((DeadCat)cat).causeOfDeath); | ||
} | ||
} |