Skip to content

Commit 1d71a73

Browse files
Copilotpjfanning
andcommitted
Refactor CustomTypeIdResolverGenericTest to Java 8 compatible style
- Replaced var with explicit types - Replaced text blocks with string concatenation - Replaced records with regular POJO classes (Bar, Qux, Box, GenericBox) - Replaced sealed interface with regular interface - Replaced pattern matching switch with instanceof checks - Replaced Map.of() with HashMap - Changed method names from void style to public void style for compatibility Co-authored-by: pjfanning <11783444+pjfanning@users.noreply.github.com>
1 parent 28c0b67 commit 1d71a73

File tree

1 file changed

+146
-89
lines changed

1 file changed

+146
-89
lines changed

src/test/java/tools/jackson/databind/jsontype/CustomTypeIdResolverGenericTest.java

Lines changed: 146 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import com.fasterxml.jackson.annotation.JsonTypeInfo;
66
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
7+
import java.util.Collections;
8+
import java.util.HashMap;
79
import java.util.Map;
810
import org.junit.jupiter.api.Test;
911
import tools.jackson.core.type.TypeReference;
@@ -21,122 +23,176 @@ public class CustomTypeIdResolverGenericTest extends DatabindTestUtil {
2123
.build();
2224

2325
@Test
24-
void root() throws Exception {
25-
//given
26-
var bar = new Bar("test");
27-
var expected = """
28-
{
29-
"@type" : "BAR",
30-
"any" : "test"
31-
}""";
32-
33-
//when
34-
var actual = mapper.writeValueAsString(bar);
35-
36-
//then
26+
public void testRoot() throws Exception {
27+
// given
28+
Bar bar = new Bar("test");
29+
String expected = "{\n" +
30+
" \"@type\" : \"BAR\",\n" +
31+
" \"any\" : \"test\"\n" +
32+
"}";
33+
34+
// when
35+
String actual = mapper.writeValueAsString(bar);
36+
37+
// then
3738
assertEquals(expected, actual);
3839
}
3940

4041
@Test
41-
void nestedMap() throws Exception {
42-
//given
43-
var map = Map.of("bar", new Bar("test"));
44-
var expected = """
45-
{
46-
"bar" : {
47-
"@type" : "BAR",
48-
"any" : "test"
49-
}
50-
}""";
51-
52-
//when
53-
var actual = mapper.writeValueAsString(map);
54-
55-
//then
42+
public void testNestedMap() throws Exception {
43+
// given
44+
Map<String, Foo> map = new HashMap<>();
45+
map.put("bar", new Bar("test"));
46+
String expected = "{\n" +
47+
" \"bar\" : {\n" +
48+
" \"@type\" : \"BAR\",\n" +
49+
" \"any\" : \"test\"\n" +
50+
" }\n" +
51+
"}";
52+
53+
// when
54+
String actual = mapper.writeValueAsString(map);
55+
56+
// then
5657
assertEquals(expected, actual);
5758
}
5859

5960
@Test
60-
void nestedRecord() throws Exception {
61-
//given
62-
record Box(Foo value) {
63-
64-
}
65-
var box = new Box(new Qux(1));
66-
var expected = """
67-
{
68-
"value" : {
69-
"@type" : "QUX",
70-
"any" : 1
71-
}
72-
}""";
73-
74-
//when
75-
var actual = mapper.writeValueAsString(box);
61+
public void testNestedPojo() throws Exception {
62+
// given
63+
Box box = new Box(new Qux(1));
64+
String expected = "{\n" +
65+
" \"value\" : {\n" +
66+
" \"@type\" : \"QUX\",\n" +
67+
" \"any\" : 1\n" +
68+
" }\n" +
69+
"}";
70+
71+
// when
72+
String actual = mapper.writeValueAsString(box);
73+
74+
// then
75+
assertEquals(expected, actual);
76+
}
7677

77-
//then
78+
@Test
79+
public void testNestedGenericPojo() throws Exception {
80+
// given
81+
GenericBox<Foo> box = new GenericBox<>(new Qux(1));
82+
String expected = "{\n" +
83+
" \"value\" : {\n" +
84+
" \"@type\" : \"QUX\",\n" +
85+
" \"any\" : 1\n" +
86+
" }\n" +
87+
"}";
88+
89+
// when
90+
String actual = mapper.writeValueAsString(box);
91+
92+
// then
7893
assertEquals(expected, actual);
7994
}
8095

8196
@Test
82-
void nestedGenericRecord() throws Exception {
83-
//given
84-
record Box<T>(T value) {
97+
public void testNestedGenericPojoExplicitWriter() throws Exception {
98+
// given
99+
GenericBox<Foo> box = new GenericBox<>(new Qux(1));
100+
String expected = "{\n" +
101+
" \"value\" : {\n" +
102+
" \"@type\" : \"QUX\",\n" +
103+
" \"any\" : 1\n" +
104+
" }\n" +
105+
"}";
106+
107+
// when
108+
String actual = mapper.writer().forType(new TypeReference<GenericBox<Foo>>() {
109+
}).writeValueAsString(box);
110+
111+
// then
112+
assertEquals(expected, actual);
113+
}
114+
115+
// Test classes
116+
117+
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "@type")
118+
@JsonTypeIdResolver(FooIdResolver.class)
119+
public interface Foo {
120+
}
121+
122+
public static class Bar implements Foo {
123+
private String any;
124+
125+
public Bar() {
126+
}
85127

128+
public Bar(String any) {
129+
this.any = any;
86130
}
87-
var box = new Box<>(new Qux(1));
88-
var expected = """
89-
{
90-
"value" : {
91-
"@type" : "QUX",
92-
"any" : 1
93-
}
94-
}""";
95131

96-
//when
97-
var actual = mapper.writeValueAsString(box);
132+
public String getAny() {
133+
return any;
134+
}
98135

99-
//then
100-
assertEquals(expected, actual);
136+
public void setAny(String any) {
137+
this.any = any;
138+
}
101139
}
102140

103-
@Test
104-
void nestedGenericRecordExplicitWriter() throws Exception {
105-
//given
106-
record Box<T>(T value) {
141+
public static class Qux implements Foo {
142+
private int any;
107143

144+
public Qux() {
108145
}
109-
var box = new Box<>(new Qux(1));
110-
var expected = """
111-
{
112-
"value" : {
113-
"@type" : "QUX",
114-
"any" : 1
115-
}
116-
}""";
117146

118-
var writer = mapper.writer().forType(new TypeReference<Box<Foo>>() {
119-
});
147+
public Qux(int any) {
148+
this.any = any;
149+
}
120150

121-
//when
122-
var actual = writer.writeValueAsString(box);
151+
public int getAny() {
152+
return any;
153+
}
123154

124-
//then
125-
assertEquals(expected, actual);
155+
public void setAny(int any) {
156+
this.any = any;
157+
}
126158
}
127159

128-
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "@type")
129-
@JsonTypeIdResolver(FooIdResolver.class)
130-
public sealed interface Foo permits Bar, Qux {
160+
public static class Box {
161+
private Foo value;
131162

132-
}
163+
public Box() {
164+
}
133165

134-
record Bar(String any) implements Foo {
166+
public Box(Foo value) {
167+
this.value = value;
168+
}
135169

170+
public Foo getValue() {
171+
return value;
172+
}
173+
174+
public void setValue(Foo value) {
175+
this.value = value;
176+
}
136177
}
137178

138-
record Qux(int any) implements Foo {
179+
public static class GenericBox<T> {
180+
private T value;
139181

182+
public GenericBox() {
183+
}
184+
185+
public GenericBox(T value) {
186+
this.value = value;
187+
}
188+
189+
public T getValue() {
190+
return value;
191+
}
192+
193+
public void setValue(T value) {
194+
this.value = value;
195+
}
140196
}
141197

142198
public static class FooIdResolver extends TypeIdResolverBase {
@@ -148,11 +204,12 @@ public String idFromValue(DatabindContext ctxt, Object value) {
148204

149205
@Override
150206
public String idFromValueAndType(DatabindContext ctxt, Object value, Class<?> suggestedType) {
151-
return switch (value) {
152-
case Bar _ -> "BAR";
153-
case Qux _ -> "QUX";
154-
default -> throw new IllegalStateException("Unexpected value: " + value);
155-
};
207+
if (value instanceof Bar) {
208+
return "BAR";
209+
} else if (value instanceof Qux) {
210+
return "QUX";
211+
}
212+
throw new IllegalStateException("Unexpected value: " + value);
156213
}
157214

158215
@Override

0 commit comments

Comments
 (0)