Skip to content

Commit 95fc0f2

Browse files
committed
Add DEDUCTION-based polymorphism base tests for YAML to try to repro #404
1 parent 75cf419 commit 95fc0f2

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.fasterxml.jackson.dataformat.yaml.misc;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
6+
import com.fasterxml.jackson.databind.*;
7+
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
8+
9+
import static com.fasterxml.jackson.annotation.JsonSubTypes.Type;
10+
import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.DEDUCTION;
11+
12+
// for [databind#43], deduction-based polymorphism
13+
public class TestPolymorphicDeduction extends ModuleTestBase
14+
{
15+
@JsonTypeInfo(use = DEDUCTION)
16+
@JsonSubTypes( {@Type(LiveCat.class), @Type(DeadCat.class), @Type(Fleabag.class)})
17+
// A general supertype with no properties - used for tests involving {}
18+
interface Feline {}
19+
20+
@JsonTypeInfo(use = DEDUCTION)
21+
@JsonSubTypes( {@Type(LiveCat.class), @Type(DeadCat.class)})
22+
// A supertype containing common properties
23+
public static class Cat implements Feline {
24+
public String name;
25+
}
26+
27+
// Distinguished by its parent and a unique property
28+
static class DeadCat extends Cat {
29+
public String causeOfDeath;
30+
}
31+
32+
// Distinguished by its parent and a unique property
33+
static class LiveCat extends Cat {
34+
public boolean angry;
35+
}
36+
37+
// No distinguishing properties whatsoever
38+
static class Fleabag implements Feline {
39+
// NO OP
40+
}
41+
42+
/*
43+
/**********************************************************
44+
/* Mock data
45+
/**********************************************************
46+
*/
47+
48+
private static final String DEAD_CAT_DOC = "name: Felix\ncauseOfDeath: entropy\n";
49+
private static final String LIVE_CAT_DOC = "name: Felix\nangry: true\n";
50+
51+
/*
52+
/**********************************************************
53+
/* Test methods
54+
/**********************************************************
55+
*/
56+
57+
private final ObjectMapper MAPPER = newObjectMapper();
58+
59+
public void testSimpleInference() throws Exception
60+
{
61+
Cat cat = MAPPER.readValue(LIVE_CAT_DOC, Cat.class);
62+
assertTrue(cat instanceof LiveCat);
63+
assertSame(cat.getClass(), LiveCat.class);
64+
assertEquals("Felix", cat.name);
65+
assertTrue(((LiveCat)cat).angry);
66+
67+
cat = MAPPER.readValue(DEAD_CAT_DOC, Cat.class);
68+
assertTrue(cat instanceof DeadCat);
69+
assertSame(cat.getClass(), DeadCat.class);
70+
assertEquals("Felix", cat.name);
71+
assertEquals("entropy", ((DeadCat)cat).causeOfDeath);
72+
}
73+
74+
public void testSimpleInferenceOfEmptySubtypeDoesntMatchNull() throws Exception {
75+
Feline feline = MAPPER.readValue("null", Feline.class);
76+
assertNull(feline);
77+
}
78+
79+
public void testCaseInsensitiveInference() throws Exception
80+
{
81+
Cat cat = mapperBuilder()
82+
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
83+
.build()
84+
.readValue(DEAD_CAT_DOC.toUpperCase(), Cat.class);
85+
assertTrue(cat instanceof DeadCat);
86+
assertSame(cat.getClass(), DeadCat.class);
87+
assertEquals("FELIX", cat.name);
88+
assertEquals("ENTROPY", ((DeadCat)cat).causeOfDeath);
89+
}
90+
}

0 commit comments

Comments
 (0)