Skip to content

Commit 47f9bc6

Browse files
authored
Migrate /contextual tests to JUnit 5 (#4251)
1 parent c2c80aa commit 47f9bc6

File tree

6 files changed

+73
-9
lines changed

6 files changed

+73
-9
lines changed

src/test/java/com/fasterxml/jackson/databind/contextual/TestContextAttributeWithDeser.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import java.io.IOException;
44

5+
import org.junit.jupiter.api.Test;
6+
57
import com.fasterxml.jackson.core.*;
68
import com.fasterxml.jackson.databind.*;
79
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
810
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
911
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
1012

11-
public class TestContextAttributeWithDeser extends BaseMapTest
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
15+
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*;
16+
17+
public class TestContextAttributeWithDeser
1218
{
1319
final static String KEY = "foobar";
1420

@@ -46,8 +52,9 @@ static class TestPOJO
4652
/**********************************************************
4753
*/
4854

49-
final ObjectMapper MAPPER = sharedMapper();
55+
final ObjectMapper MAPPER = newJsonMapper();
5056

57+
@Test
5158
public void testSimplePerCall() throws Exception
5259
{
5360
final String INPUT = a2q("[{'value':'a'},{'value':'b'}]");
@@ -63,6 +70,7 @@ public void testSimplePerCall() throws Exception
6370
assertEquals("b/1", pojos2[1].value);
6471
}
6572

73+
@Test
6674
public void testSimpleDefaults() throws Exception
6775
{
6876
final String INPUT = a2q("{'value':'x'}");
@@ -78,6 +86,7 @@ public void testSimpleDefaults() throws Exception
7886
assertEquals("x/5", pojo2.value);
7987
}
8088

89+
@Test
8190
public void testHierarchic() throws Exception
8291
{
8392
final String INPUT = a2q("[{'value':'x'},{'value':'y'}]");
@@ -94,6 +103,7 @@ public void testHierarchic() throws Exception
94103
assertEquals("y/3", pojos2[1].value);
95104
}
96105

106+
@Test
97107
// [databind#3001]
98108
public void testDefaultsViaMapper() throws Exception
99109
{

src/test/java/com/fasterxml/jackson/databind/contextual/TestContextAttributeWithSer.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
import java.io.IOException;
44

5+
import org.junit.jupiter.api.Test;
6+
57
import com.fasterxml.jackson.core.JsonGenerator;
68
import com.fasterxml.jackson.databind.*;
9+
import com.fasterxml.jackson.databind.SerializerProvider;
710
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
811
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
912
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
1013

11-
public class TestContextAttributeWithSer extends BaseMapTest
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
16+
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*;
17+
18+
public class TestContextAttributeWithSer
1219
{
1320
final static String KEY = "foobar";
1421

@@ -48,8 +55,9 @@ static class TestPOJO
4855
/**********************************************************
4956
*/
5057

51-
final ObjectMapper MAPPER = sharedMapper();
58+
final ObjectMapper MAPPER = newJsonMapper();
5259

60+
@Test
5361
public void testSimplePerCall() throws Exception
5462
{
5563
final String EXP = a2q("[{'value':'0:a'},{'value':'1:b'}]");
@@ -62,6 +70,7 @@ public void testSimplePerCall() throws Exception
6270
assertEquals(EXP, w.writeValueAsString(INPUT));
6371
}
6472

73+
@Test
6574
public void testSimpleDefaults() throws Exception
6675
{
6776
final String EXP = a2q("{'value':'3:xyz'}");
@@ -75,6 +84,7 @@ public void testSimpleDefaults() throws Exception
7584
assertEquals(EXP, json2);
7685
}
7786

87+
@Test
7888
public void testHierarchic() throws Exception
7989
{
8090
final TestPOJO[] INPUT = new TestPOJO[] { new TestPOJO("a"), new TestPOJO("b") };
@@ -87,6 +97,7 @@ public void testHierarchic() throws Exception
8797
}
8898

8999
// [databind#3001]
100+
@Test
90101
public void testDefaultsViaMapper() throws Exception
91102
{
92103
final TestPOJO[] INPUT = new TestPOJO[] { new TestPOJO("a"), new TestPOJO("b") };

src/test/java/com/fasterxml/jackson/databind/contextual/TestContextualDeserialization.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.lang.annotation.Target;
88
import java.util.*;
99

10+
import org.junit.jupiter.api.Test;
11+
1012
import com.fasterxml.jackson.annotation.*;
1113
import com.fasterxml.jackson.core.JsonParser;
1214
import com.fasterxml.jackson.core.Version;
@@ -17,13 +19,18 @@
1719
import com.fasterxml.jackson.databind.json.JsonMapper;
1820
import com.fasterxml.jackson.databind.module.SimpleModule;
1921

22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
25+
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.a2q;
26+
2027
/**
2128
* Test cases to verify that it is possible to define deserializers
2229
* that can use contextual information (like field/method
2330
* annotations) for configuration.
2431
*/
2532
@SuppressWarnings("serial")
26-
public class TestContextualDeserialization extends BaseMapTest
33+
public class TestContextualDeserialization
2734
{
2835
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
2936
@Retention(RetentionPolicy.RUNTIME)
@@ -186,6 +193,7 @@ static class GenericBean {
186193
))
187194
.build();
188195

196+
@Test
189197
public void testSimple() throws Exception
190198
{
191199
ObjectMapper mapper = new ObjectMapper();
@@ -202,6 +210,7 @@ public void testSimple() throws Exception
202210
assertEquals("b=4", bean.b.value);
203211
}
204212

213+
@Test
205214
public void testSimpleWithAnnotations() throws Exception
206215
{
207216
ObjectMapper mapper = _mapperWithAnnotatedContextual();
@@ -215,6 +224,7 @@ public void testSimpleWithAnnotations() throws Exception
215224
assertEquals("NameB=y", bean.b.value);
216225
}
217226

227+
@Test
218228
public void testSimpleWithClassAnnotations() throws Exception
219229
{
220230
ObjectMapper mapper = _mapperWithAnnotatedContextual();
@@ -227,6 +237,7 @@ public void testSimpleWithClassAnnotations() throws Exception
227237
assertEquals("NameB=345", bean.b.value);
228238
}
229239

240+
@Test
230241
public void testAnnotatedCtor() throws Exception
231242
{
232243
ObjectMapper mapper = _mapperWithAnnotatedContextual();
@@ -239,6 +250,7 @@ public void testAnnotatedCtor() throws Exception
239250
assertEquals("CtorB=0", bean.b);
240251
}
241252

253+
@Test
242254
public void testAnnotatedArray() throws Exception
243255
{
244256
ObjectMapper mapper = _mapperWithAnnotatedContextual();
@@ -252,6 +264,7 @@ public void testAnnotatedArray() throws Exception
252264
assertEquals("array=b", bean.beans[1].value);
253265
}
254266

267+
@Test
255268
public void testAnnotatedList() throws Exception
256269
{
257270
ObjectMapper mapper = _mapperWithAnnotatedContextual();
@@ -266,6 +279,7 @@ public void testAnnotatedList() throws Exception
266279
assertEquals("list=z", bean.beans.get(2).value);
267280
}
268281

282+
@Test
269283
public void testAnnotatedMap() throws Exception
270284
{
271285
ObjectMapper mapper = _mapperWithAnnotatedContextual();
@@ -287,6 +301,7 @@ public void testAnnotatedMap() throws Exception
287301
}
288302

289303
// for [databind#165]
304+
@Test
290305
public void testContextualType() throws Exception {
291306
GenericBean bean = new ObjectMapper().readValue(a2q("{'stuff':{'1':'b'}}"),
292307
GenericBean.class);

src/test/java/com/fasterxml/jackson/databind/contextual/TestContextualKeyTypes.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.IOException;
44
import java.util.*;
55

6+
import org.junit.jupiter.api.Test;
7+
68
import com.fasterxml.jackson.core.JsonGenerator;
79
import com.fasterxml.jackson.core.Version;
810
import com.fasterxml.jackson.databind.*;
@@ -11,11 +13,14 @@
1113
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
1214
import com.fasterxml.jackson.databind.type.TypeFactory;
1315

16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
1419
/**
1520
* Tests to ensure that we can do contextual key serializers and
1621
* deserializers as well as value ser/deser.
1722
*/
18-
public class TestContextualKeyTypes extends BaseMapTest
23+
public class TestContextualKeyTypes
1924
{
2025
/*
2126
/**********************************************************
@@ -85,6 +90,7 @@ static class MapBean {
8590
/**********************************************************
8691
*/
8792

93+
@Test
8894
public void testSimpleKeySer() throws Exception
8995
{
9096
ObjectMapper mapper = new ObjectMapper();
@@ -104,6 +110,7 @@ public void testSimpleKeySer() throws Exception
104110
/**********************************************************
105111
*/
106112

113+
@Test
107114
public void testSimpleKeyDeser() throws Exception
108115
{
109116
ObjectMapper mapper = new ObjectMapper();

src/test/java/com/fasterxml/jackson/databind/contextual/TestContextualSerialization.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@
44
import java.lang.annotation.*;
55
import java.util.*;
66

7-
import com.fasterxml.jackson.annotation.*;
7+
import org.junit.jupiter.api.Test;
88

9+
import com.fasterxml.jackson.annotation.JacksonAnnotation;
910
import com.fasterxml.jackson.core.*;
1011
import com.fasterxml.jackson.databind.*;
1112
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
1213
import com.fasterxml.jackson.databind.module.SimpleModule;
1314
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
1415
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
1516

17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
19+
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*;
20+
1621
/**
1722
* Test cases to verify that it is possible to define serializers
1823
* that can use contextual information (like field/method
1924
* annotations) for configuration.
2025
*/
21-
public class TestContextualSerialization extends BaseMapTest
26+
public class TestContextualSerialization
2227
{
2328
// NOTE: important; MUST be considered a 'Jackson' annotation to be seen
2429
// (or recognized otherwise via AnnotationIntrospect.isHandled())
@@ -219,6 +224,7 @@ public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty
219224

220225
// Test to verify that contextual serializer can make use of property
221226
// (method, field) annotations.
227+
@Test
222228
public void testMethodAnnotations() throws Exception
223229
{
224230
ObjectMapper mapper = new ObjectMapper();
@@ -230,6 +236,7 @@ public void testMethodAnnotations() throws Exception
230236

231237
// Test to verify that contextual serializer can also use annotations
232238
// for enclosing class.
239+
@Test
233240
public void testClassAnnotations() throws Exception
234241
{
235242
ObjectMapper mapper = new ObjectMapper();
@@ -239,6 +246,7 @@ public void testClassAnnotations() throws Exception
239246
assertEquals("{\"value\":\"Voila->xyz\"}", mapper.writeValueAsString(new BeanWithClassConfig("xyz")));
240247
}
241248

249+
@Test
242250
public void testWrappedBean() throws Exception
243251
{
244252
ObjectMapper mapper = new ObjectMapper();
@@ -249,6 +257,7 @@ public void testWrappedBean() throws Exception
249257
}
250258

251259
// Serializer should get passed property context even if contained in an array.
260+
@Test
252261
public void testMethodAnnotationInArray() throws Exception
253262
{
254263
ObjectMapper mapper = new ObjectMapper();
@@ -260,6 +269,7 @@ public void testMethodAnnotationInArray() throws Exception
260269
}
261270

262271
// Serializer should get passed property context even if contained in a Collection.
272+
@Test
263273
public void testMethodAnnotationInList() throws Exception
264274
{
265275
ObjectMapper mapper = new ObjectMapper();
@@ -271,6 +281,7 @@ public void testMethodAnnotationInList() throws Exception
271281
}
272282

273283
// Serializer should get passed property context even if contained in a Collection.
284+
@Test
274285
public void testMethodAnnotationInMap() throws Exception
275286
{
276287
ObjectMapper mapper = new ObjectMapper();
@@ -282,13 +293,15 @@ public void testMethodAnnotationInMap() throws Exception
282293
assertEquals("{\"beans\":{\"first\":\"map->In Map\"}}", mapper.writeValueAsString(map));
283294
}
284295

296+
@Test
285297
public void testContextualViaAnnotation() throws Exception
286298
{
287299
ObjectMapper mapper = new ObjectMapper();
288300
AnnotatedContextualBean bean = new AnnotatedContextualBean("abc");
289301
assertEquals("{\"value\":\"prefix->abc\"}", mapper.writeValueAsString(bean));
290302
}
291303

304+
@Test
292305
public void testResolveOnContextual() throws Exception
293306
{
294307
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
@@ -302,6 +315,7 @@ public void testResolveOnContextual() throws Exception
302315
assertEquals(q("contextual=1,resolved=1"), mapper.writeValueAsString("foo"));
303316
}
304317

318+
@Test
305319
public void testContextualArrayElement() throws Exception
306320
{
307321
ObjectMapper mapper = newJsonMapper();
@@ -310,6 +324,7 @@ public void testContextualArrayElement() throws Exception
310324
}
311325

312326
// Test to verify aspects of [databind#2429]
327+
@Test
313328
public void testRootContextualization2429() throws Exception
314329
{
315330
ObjectMapper mapper = jsonMapperBuilder()

src/test/java/com/fasterxml/jackson/databind/contextual/TestContextualWithAnnDeserializer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
import java.lang.annotation.RetentionPolicy;
77
import java.lang.annotation.Target;
88

9+
import org.junit.jupiter.api.Test;
10+
911
import com.fasterxml.jackson.annotation.JacksonAnnotation;
1012
import com.fasterxml.jackson.core.JsonParser;
1113
import com.fasterxml.jackson.databind.*;
1214
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1315
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
1416

15-
public class TestContextualWithAnnDeserializer extends BaseMapTest
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
20+
public class TestContextualWithAnnDeserializer
1621
{
1722
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
1823
@Retention(RetentionPolicy.RUNTIME)
@@ -65,6 +70,7 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
6570
}
6671

6772
// ensure that direct associations also work
73+
@Test
6874
public void testAnnotatedContextual() throws Exception
6975
{
7076
ObjectMapper mapper = new ObjectMapper();

0 commit comments

Comments
 (0)