Skip to content

Commit 9700cd6

Browse files
committed
Moar test refactoring
1 parent a2753b3 commit 9700cd6

File tree

3 files changed

+65
-63
lines changed

3 files changed

+65
-63
lines changed

src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeIdTest1288.java renamed to src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeCustomResolverTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
1414

1515
@SuppressWarnings("hiding")
16-
public class ExternalTypeIdTest1288 extends BaseMapTest
16+
public class ExternalTypeCustomResolverTest extends BaseMapTest
1717
{
18+
// [databind#1288]
1819
public static class ClassesWithoutBuilder {
1920

2021
public static class CreditCardDetails implements PaymentDetails {
@@ -497,7 +498,8 @@ public Id getMechanism () {
497498
}
498499
}
499500

500-
public void testVisibleExternalTypeId1288() throws Exception
501+
// [databind#1288]
502+
public void testVisibleExternalTypeIdCustomResolver() throws Exception
501503
{
502504
// given
503505
final String asJson1 = "{\"form_of_payment\":\"INDIVIDUAL_CREDIT_CARD\", \"payment_details\":{\"card_holder_first_name\":\"John\", \"card_holder_last_name\":\"Doe\", \"number\":\"XXXXXXXXXXXXXXXX\", \"expiry_date\":\"MM/YY\","

src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId999Test.java

-55
This file was deleted.

src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeIdWithCreatorTest.java

+61-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.fasterxml.jackson.databind.jsontype.ext;
22

3-
import com.fasterxml.jackson.annotation.JsonCreator;
4-
import com.fasterxml.jackson.annotation.JsonSubTypes;
5-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6-
3+
import com.fasterxml.jackson.annotation.*;
4+
import com.fasterxml.jackson.core.type.TypeReference;
75
import com.fasterxml.jackson.databind.*;
86

9-
// [databind#1198]
107
public class ExternalTypeIdWithCreatorTest extends BaseMapTest
118
{
9+
// [databind#1198]
10+
1211
public enum Attacks { KICK, PUNCH }
1312

1413
static class Character {
@@ -47,8 +46,46 @@ public Punch(String side) {
4746
}
4847
}
4948

50-
final ObjectMapper MAPPER = new ObjectMapper();
49+
// [databind#999]
50+
51+
public static interface Payload999 { }
52+
53+
@JsonTypeName("foo")
54+
public static class FooPayload999 implements Payload999 { }
55+
56+
@JsonTypeName("bar")
57+
public static class BarPayload999 implements Payload999 { }
58+
59+
public static class Message<P extends Payload999>
60+
{
61+
final String type;
62+
63+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
64+
visible = true,
65+
include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
66+
@JsonSubTypes({
67+
@JsonSubTypes.Type(FooPayload999.class),
68+
@JsonSubTypes.Type(BarPayload999.class) })
69+
private final P payload;
70+
71+
@JsonCreator
72+
public Message(@JsonProperty("type") String type,
73+
@JsonProperty("payload") P payload)
74+
{
75+
this.type = type;
76+
this.payload = payload;
77+
}
78+
}
79+
80+
/*
81+
/**********************************************************************
82+
/* Test methods
83+
/**********************************************************************
84+
*/
85+
86+
private final ObjectMapper MAPPER = new ObjectMapper();
5187

88+
// [databind#1198]
5289
public void testFails() throws Exception {
5390
String json = "{ \"name\": \"foo\", \"attack\":\"right\" } }";
5491

@@ -59,6 +96,7 @@ public void testFails() throws Exception {
5996
assertEquals("foo", character.name);
6097
}
6198

99+
// [databind#1198]
62100
public void testWorks() throws Exception {
63101
String json = "{ \"name\": \"foo\", \"preferredAttack\": \"KICK\", \"attack\":\"right\" } }";
64102

@@ -68,4 +106,21 @@ public void testWorks() throws Exception {
68106
assertNotNull(character.attack);
69107
assertEquals("foo", character.name);
70108
}
109+
110+
// [databind#999]
111+
public void testExternalTypeId() throws Exception
112+
{
113+
TypeReference<Message<FooPayload999>> type = new TypeReference<Message<FooPayload999>>() { };
114+
115+
Message<?> msg = MAPPER.readValue(aposToQuotes("{ 'type':'foo', 'payload': {} }"), type);
116+
assertNotNull(msg);
117+
assertNotNull(msg.payload);
118+
assertEquals("foo", msg.type);
119+
120+
// and then with different order
121+
msg = MAPPER.readValue(aposToQuotes("{'payload': {}, 'type':'foo' }"), type);
122+
assertNotNull(msg);
123+
assertNotNull(msg.payload);
124+
assertEquals("foo", msg.type);
125+
}
71126
}

0 commit comments

Comments
 (0)