Skip to content

Commit 790c487

Browse files
authored
Migrate tests in view test package to JUnit 5 using DatabindTestUtil (#4217)
1 parent 5f961f5 commit 790c487

7 files changed

+99
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fasterxml.jackson.databind;
2+
3+
import com.fasterxml.jackson.databind.json.JsonMapper;
4+
import java.io.IOException;
5+
import java.util.LinkedHashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* Class containing test utility methods.
10+
* The methods are migrated from {@link BaseMapTest} and {@link BaseTest},
11+
* as part of JUnit 5 migration.
12+
*
13+
* @since 2.17
14+
*/
15+
public class DatabindTestUtil
16+
{
17+
18+
/*
19+
/**********************************************************
20+
/* Helper methods, serialization
21+
/**********************************************************
22+
*/
23+
24+
@SuppressWarnings("unchecked")
25+
public static Map<String,Object> writeAndMap(ObjectMapper m, Object value)
26+
throws IOException
27+
{
28+
String str = m.writeValueAsString(value);
29+
return (Map<String,Object>) m.readValue(str, LinkedHashMap.class);
30+
}
31+
32+
/*
33+
/**********************************************************
34+
/* Construction
35+
/**********************************************************
36+
*/
37+
38+
public static ObjectMapper newJsonMapper() {
39+
return new JsonMapper();
40+
}
41+
42+
public static JsonMapper.Builder jsonMapperBuilder() {
43+
return JsonMapper.builder();
44+
}
45+
46+
/*
47+
/**********************************************************
48+
/* Encoding or String representations
49+
/**********************************************************
50+
*/
51+
52+
public static String a2q(String json) {
53+
return json.replace("'", "\"");
54+
}
55+
}

src/test/java/com/fasterxml/jackson/databind/views/DefaultViewTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.fasterxml.jackson.databind.views;
22

3+
import static com.fasterxml.jackson.databind.DatabindTestUtil.a2q;
4+
import static org.junit.Assert.assertEquals;
35
import java.io.IOException;
46

57
import com.fasterxml.jackson.annotation.*;
68

79
import com.fasterxml.jackson.databind.*;
10+
import org.junit.jupiter.api.Test;
811

912
// for [databind#507], supporting default views
10-
public class DefaultViewTest extends BaseMapTest
13+
public class DefaultViewTest
1114
{
1215
// Classes that represent views
1316
static class ViewA { }
@@ -32,6 +35,7 @@ static class Defaulting {
3235

3336
private final ObjectMapper MAPPER = new ObjectMapper();
3437

38+
@Test
3539
public void testDeserialization() throws IOException
3640
{
3741
final String JSON = a2q("{'a':1,'b':2}");
@@ -56,6 +60,7 @@ public void testDeserialization() throws IOException
5660
assertEquals(result.b, 2);
5761
}
5862

63+
@Test
5964
public void testSerialization() throws IOException
6065
{
6166
assertEquals(a2q("{'a':3,'b':5}"),

src/test/java/com/fasterxml/jackson/databind/views/TestViewDeserialization.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.fasterxml.jackson.databind.views;
22

3+
import static com.fasterxml.jackson.databind.DatabindTestUtil.a2q;
4+
import static com.fasterxml.jackson.databind.DatabindTestUtil.jsonMapperBuilder;
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertNull;
37
import com.fasterxml.jackson.annotation.JsonCreator;
48
import com.fasterxml.jackson.annotation.JsonProperty;
59
import com.fasterxml.jackson.annotation.JsonView;
610

711
import com.fasterxml.jackson.databind.*;
12+
import org.junit.jupiter.api.Test;
813

9-
public class TestViewDeserialization extends BaseMapTest
14+
public class TestViewDeserialization
1015
{
1116
// Classes that represent views
1217
static class ViewA { }
@@ -61,6 +66,7 @@ public ViewsAndCreatorBean(@JsonProperty("a") int a,
6166

6267
private final ObjectMapper mapper = new ObjectMapper();
6368

69+
@Test
6470
public void testSimple() throws Exception
6571
{
6672
// by default, should have it all...
@@ -95,6 +101,7 @@ public void testSimple() throws Exception
95101
assertEquals(2, bean.b);
96102
}
97103

104+
@Test
98105
public void testWithoutDefaultInclusion() throws Exception
99106
{
100107
// without active view, all included still:
@@ -116,6 +123,7 @@ public void testWithoutDefaultInclusion() throws Exception
116123
assertEquals(2, bean.b);
117124
}
118125

126+
@Test
119127
public void testWithCreatorAndViews() throws Exception
120128
{
121129
ViewsAndCreatorBean result;

src/test/java/com/fasterxml/jackson/databind/views/TestViewSerialization.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package com.fasterxml.jackson.databind.views;
22

3+
import static com.fasterxml.jackson.databind.DatabindTestUtil.jsonMapperBuilder;
4+
import static com.fasterxml.jackson.databind.DatabindTestUtil.newJsonMapper;
5+
import static com.fasterxml.jackson.databind.DatabindTestUtil.writeAndMap;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNull;
38
import java.io.*;
49
import java.util.*;
510

611
import com.fasterxml.jackson.annotation.*;
712

813
import com.fasterxml.jackson.databind.*;
14+
import org.junit.jupiter.api.Test;
915

1016
/**
1117
* Unit tests for verifying JSON view functionality: ability to declaratively
1218
* suppress subset of properties from being serialized.
1319
*/
1420
public class TestViewSerialization
15-
extends BaseMapTest
1621
{
1722
// Classes that represent views
1823
static class ViewA { }
@@ -75,9 +80,10 @@ public static class Foo {
7580
/**********************************************************
7681
*/
7782

78-
private final ObjectMapper MAPPER = objectMapper();
83+
private final ObjectMapper MAPPER = newJsonMapper();
7984

8085
@SuppressWarnings("unchecked")
86+
@Test
8187
public void testSimple() throws IOException
8288
{
8389
StringWriter sw = new StringWriter();
@@ -128,6 +134,7 @@ public void testSimple() throws IOException
128134
* a view.
129135
*/
130136
@SuppressWarnings("unchecked")
137+
@Test
131138
public void testDefaultExclusion() throws IOException
132139
{
133140
MixedBean bean = new MixedBean();
@@ -163,12 +170,14 @@ public void testDefaultExclusion() throws IOException
163170
* As per [JACKSON-261], @JsonView annotation should imply that associated
164171
* method/field does indicate a property.
165172
*/
173+
@Test
166174
public void testImplicitAutoDetection() throws Exception
167175
{
168176
assertEquals("{\"a\":1}",
169177
MAPPER.writeValueAsString(new ImplicitBean()));
170178
}
171179

180+
@Test
172181
public void testVisibility() throws Exception
173182
{
174183
VisibilityBean bean = new VisibilityBean();
@@ -179,6 +188,7 @@ public void testVisibility() throws Exception
179188
}
180189

181190
// [JACKSON-868]
191+
@Test
182192
public void test868() throws IOException
183193
{
184194
ObjectMapper mapper = new ObjectMapper();

src/test/java/com/fasterxml/jackson/databind/views/TestViewsSerialization2.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.fasterxml.jackson.databind.views;
22

3+
import static com.fasterxml.jackson.databind.DatabindTestUtil.jsonMapperBuilder;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertTrue;
36
import java.io.*;
47

58
import com.fasterxml.jackson.annotation.*;
69

710
import com.fasterxml.jackson.databind.*;
11+
import org.junit.jupiter.api.Test;
812

9-
public class TestViewsSerialization2 extends BaseMapTest
13+
public class TestViewsSerialization2
1014
{
1115
/*
1216
/************************************************************************
@@ -121,13 +125,15 @@ public void setNameHidden( String nameHidden )
121125
/************************************************************************
122126
*/
123127

128+
@Test
124129
public void testDataBindingUsage( ) throws Exception
125130
{
126131
ObjectMapper mapper = createMapper();
127132
String result = serializeWithObjectMapper(new ComplexTestData( ), Views.View.class, mapper);
128133
assertEquals(-1, result.indexOf( "nameHidden" ));
129134
}
130135

136+
@Test
131137
public void testDataBindingUsageWithoutView( ) throws Exception
132138
{
133139
ObjectMapper mapper = createMapper();

src/test/java/com/fasterxml/jackson/databind/views/ViewsWithCreatorTest.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.fasterxml.jackson.databind.views;
22

3+
import static com.fasterxml.jackson.databind.DatabindTestUtil.newJsonMapper;
4+
import static org.junit.Assert.assertEquals;
35
import com.fasterxml.jackson.annotation.*;
46

5-
import com.fasterxml.jackson.databind.BaseMapTest;
67
import com.fasterxml.jackson.databind.ObjectMapper;
78
import com.fasterxml.jackson.databind.ObjectReader;
9+
import org.junit.jupiter.api.Test;
810

911
public class ViewsWithCreatorTest
10-
extends BaseMapTest
1112
{
1213
static class View { }
1314
static class View1 extends View { }
@@ -74,6 +75,7 @@ public String toString() {
7475
private final ObjectMapper MAPPER = newJsonMapper();
7576

7677
// [databind#1172]
78+
@Test
7779
public void testWithJsonCreator() throws Exception
7880
{
7981
ObjectReader reader = MAPPER.readerFor(ObjWithCreator.class).withView(View1.class);
@@ -85,6 +87,7 @@ public void testWithJsonCreator() throws Exception
8587
}
8688

8789
// [databind#1172]
90+
@Test
8891
public void testWithoutJsonCreator() throws Exception
8992
{
9093
ObjectReader reader = MAPPER.readerFor(ObjWithoutCreator.class).withView(View1.class);

src/test/java/com/fasterxml/jackson/databind/views/ViewsWithSchemaTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.fasterxml.jackson.databind.views;
22

3+
import static org.junit.Assert.assertEquals;
34
import java.util.*;
45

56
import com.fasterxml.jackson.annotation.*;
67
import com.fasterxml.jackson.databind.*;
78
import com.fasterxml.jackson.databind.jsonFormatVisitors.*;
9+
import org.junit.jupiter.api.Test;
810

9-
public class ViewsWithSchemaTest extends BaseMapTest
11+
public class ViewsWithSchemaTest
1012
{
1113
interface ViewBC { }
1214
interface ViewAB { }
@@ -52,6 +54,7 @@ public void optionalProperty(String name,
5254

5355
private final ObjectMapper MAPPER = new ObjectMapper();
5456

57+
@Test
5558
public void testSchemaWithViews() throws Exception
5659
{
5760
ListingVisitor v = new ListingVisitor();
@@ -65,6 +68,7 @@ public void testSchemaWithViews() throws Exception
6568
assertEquals(Arrays.asList("a", "b"), v.names);
6669
}
6770

71+
@Test
6872
public void testSchemaWithoutViews() throws Exception
6973
{
7074
ListingVisitor v = new ListingVisitor();

0 commit comments

Comments
 (0)