-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
324b2d7
commit 69416ee
Showing
3 changed files
with
131 additions
and
21 deletions.
There are no files selected for viewing
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
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
93 changes: 93 additions & 0 deletions
93
src/test/java/com/fasterxml/jackson/databind/jsontype/TestBaseTypeAsDefault.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,93 @@ | ||
package com.fasterxml.jackson.databind.jsontype; | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
|
||
public class TestBaseTypeAsDefault extends BaseMapTest { | ||
|
||
private ObjectMapper objectMapper; | ||
|
||
@Override | ||
public void setUp() { | ||
objectMapper = new ObjectMapper(); | ||
objectMapper.enable(DeserializationFeature.USE_BASE_TYPE_AS_DEFAULT); | ||
} | ||
|
||
public void testPositiveForParent() throws IOException { | ||
Object o = objectMapper.readerFor(Parent.class).readValue("{}"); | ||
|
||
assertEquals(o.getClass(), Parent.class); | ||
} | ||
|
||
public void testPositiveForChild() throws IOException { | ||
Object o = objectMapper.readerFor(Child.class).readValue("{}"); | ||
|
||
assertEquals(o.getClass(), Child.class); | ||
} | ||
|
||
public void testNegativeForParent() throws IOException { | ||
objectMapper.disable(DeserializationFeature.USE_BASE_TYPE_AS_DEFAULT); | ||
|
||
try { | ||
Object o = objectMapper.readerFor(Parent.class).readValue("{}"); | ||
} catch (JsonMappingException ex) { | ||
assertTrue(ex.getMessage().contains("missing type id property '@class'")); | ||
} | ||
} | ||
|
||
public void testNegativeForChild() throws IOException { | ||
objectMapper.disable(DeserializationFeature.USE_BASE_TYPE_AS_DEFAULT); | ||
|
||
try { | ||
Object o = objectMapper.readerFor(Child.class).readValue("{}"); | ||
} catch (JsonMappingException ex) { | ||
assertTrue(ex.getMessage().contains("missing type id property '@class'")); | ||
} | ||
} | ||
|
||
public void testNegativeConversionForAbstract() throws IOException { | ||
try { | ||
Object o = objectMapper.readerFor(AbstractParentWithDefault.class).readValue("{}"); | ||
} catch (JsonMappingException ex) { | ||
assertTrue(ex.getMessage().contains("missing property '@class'")); | ||
} | ||
} | ||
|
||
public void testPositiveWithTypeSpecification() throws IOException { | ||
Object o = objectMapper.readerFor(Parent.class) | ||
.readValue("{\"@class\":\"com.fasterxml.jackson.databind.jsontype.TestBaseTypeAsDefault$Child\"}"); | ||
|
||
assertEquals(o.getClass(), Child.class); | ||
} | ||
|
||
public void testPositiveWithManualDefault() throws IOException { | ||
Object o = objectMapper.readerFor(ChildOfAbstract.class).readValue("{}"); | ||
|
||
assertEquals(o.getClass(), ChildOfChild.class); | ||
} | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class") | ||
static class Parent { | ||
} | ||
|
||
|
||
static class Child extends Parent { | ||
} | ||
|
||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class", defaultImpl = ChildOfChild.class) | ||
static abstract class AbstractParentWithDefault { | ||
} | ||
|
||
|
||
static class ChildOfAbstract extends AbstractParentWithDefault { | ||
} | ||
|
||
static class ChildOfChild extends ChildOfAbstract { | ||
} | ||
} |