Skip to content

Commit d4151e0

Browse files
committed
Polish "Use Jackson configuration with JsonPath"
Polish contribution to use a factory method in `AbstractJsonMarshalTester` rather than additional constructor arguments. Also change the `JsonContent` tests so that the `Configuration` constructor is package private. This keeps JsonPath classes out of our public API, at the expense of limiting custom JsonPath configurations to just our code. See gh-16629
1 parent 756a7f1 commit d4151e0

File tree

8 files changed

+48
-119
lines changed

8 files changed

+48
-119
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/AbstractJsonMarshalTester.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@
2626
import java.io.StringReader;
2727
import java.lang.reflect.Field;
2828

29-
import com.jayway.jsonpath.Configuration;
3029
import org.assertj.core.api.Assertions;
3130

3231
import org.springframework.beans.factory.ObjectFactory;
@@ -73,8 +72,6 @@ public abstract class AbstractJsonMarshalTester<T> {
7372

7473
private ResolvableType type;
7574

76-
private Configuration configuration;
77-
7875
/**
7976
* Create a new uninitialized {@link AbstractJsonMarshalTester} instance.
8077
*/
@@ -88,22 +85,9 @@ protected AbstractJsonMarshalTester() {
8885
* @param type the type under test
8986
*/
9087
public AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type) {
91-
this(resourceLoadClass, type, Configuration.defaultConfiguration());
92-
}
93-
94-
/**
95-
* Create a new {@link AbstractJsonMarshalTester} instance.
96-
* @param resourceLoadClass the source class used when loading relative classpath
97-
* resources
98-
* @param type the type under test
99-
* @param configuration the json-path configuration
100-
*/
101-
public AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type,
102-
Configuration configuration) {
10388
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
10489
Assert.notNull(type, "Type must not be null");
105-
Assert.notNull(configuration, "Configuration must not be null");
106-
initialize(resourceLoadClass, type, configuration);
90+
initialize(resourceLoadClass, type);
10791
}
10892

10993
/**
@@ -113,23 +97,9 @@ public AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type
11397
* @param type the type under test
11498
*/
11599
protected final void initialize(Class<?> resourceLoadClass, ResolvableType type) {
116-
initialize(resourceLoadClass, type, Configuration.defaultConfiguration());
117-
}
118-
119-
/**
120-
* Initialize the marshal tester for use.
121-
* @param resourceLoadClass the source class used when loading relative classpath
122-
* resources
123-
* @param type the type under test
124-
* @param configuration the json-path configuration
125-
*/
126-
protected final void initialize(Class<?> resourceLoadClass, ResolvableType type,
127-
Configuration configuration) {
128-
if (this.resourceLoadClass == null && this.type == null
129-
&& this.configuration == null) {
100+
if (this.resourceLoadClass == null && this.type == null) {
130101
this.resourceLoadClass = resourceLoadClass;
131102
this.type = type;
132-
this.configuration = configuration;
133103
}
134104
}
135105

@@ -159,8 +129,18 @@ public JsonContent<T> write(T value) throws IOException {
159129
verify();
160130
Assert.notNull(value, "Value must not be null");
161131
String json = writeObject(value, this.type);
162-
return new JsonContent<>(this.resourceLoadClass, this.type, json,
163-
this.configuration);
132+
return getJsonContent(json);
133+
}
134+
135+
/**
136+
* Factory method used to get a {@link JsonContent} instance from a source JSON
137+
* string.
138+
* @param json the source JSON
139+
* @return a new {@link JsonContent} instance
140+
* @since 2.1.5
141+
*/
142+
protected JsonContent<T> getJsonContent(String json) {
143+
return new JsonContent<>(getResourceLoadClass(), getType(), json);
164144
}
165145

166146
/**

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/BasicJsonTester.java

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,8 +20,6 @@
2020
import java.io.InputStream;
2121
import java.nio.charset.Charset;
2222

23-
import com.jayway.jsonpath.Configuration;
24-
2523
import org.springframework.core.io.Resource;
2624
import org.springframework.util.Assert;
2725

@@ -51,8 +49,6 @@ public class BasicJsonTester {
5149

5250
private JsonLoader loader;
5351

54-
private Configuration configuration;
55-
5652
/**
5753
* Create a new uninitialized {@link BasicJsonTester} instance.
5854
*/
@@ -74,21 +70,8 @@ public BasicJsonTester(Class<?> resourceLoadClass) {
7470
* @since 1.4.1
7571
*/
7672
public BasicJsonTester(Class<?> resourceLoadClass, Charset charset) {
77-
this(resourceLoadClass, charset, Configuration.defaultConfiguration());
78-
}
79-
80-
/**
81-
* Create a new {@link BasicJsonTester} instance.
82-
* @param resourceLoadClass the source class used to load resources
83-
* @param charset the charset used to load resources
84-
* @param configuration the json-path configuration
85-
*/
86-
public BasicJsonTester(Class<?> resourceLoadClass, Charset charset,
87-
Configuration configuration) {
8873
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
89-
Assert.notNull(configuration, "Configuration must not be null");
9074
this.loader = new JsonLoader(resourceLoadClass, charset);
91-
this.configuration = configuration;
9275
}
9376

9477
/**
@@ -109,22 +92,8 @@ protected final void initialize(Class<?> resourceLoadClass) {
10992
* @since 1.4.1
11093
*/
11194
protected final void initialize(Class<?> resourceLoadClass, Charset charset) {
112-
initialize(resourceLoadClass, charset, Configuration.defaultConfiguration());
113-
}
114-
115-
/**
116-
* Initialize the marshal tester for use.
117-
* @param resourceLoadClass the source class used when loading relative classpath
118-
* resources
119-
* @param charset the charset used when loading relative classpath resources
120-
* @param configuration the json-path configuration
121-
* @since
122-
*/
123-
protected final void initialize(Class<?> resourceLoadClass, Charset charset,
124-
Configuration configuration) {
125-
if (this.loader == null && this.configuration == null) {
95+
if (this.loader == null) {
12696
this.loader = new JsonLoader(resourceLoadClass, charset);
127-
this.configuration = configuration;
12897
}
12998
}
13099

@@ -196,8 +165,7 @@ private void verify() {
196165
}
197166

198167
private JsonContent<Object> getJsonContent(String json) {
199-
return new JsonContent<>(this.loader.getResourceLoadClass(), null, json,
200-
this.configuration);
168+
return new JsonContent<>(this.loader.getResourceLoadClass(), null, json);
201169
}
202170

203171
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/GsonTester.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.io.Reader;
2121

2222
import com.google.gson.Gson;
23-
import com.jayway.jsonpath.Configuration;
2423

2524
import org.springframework.beans.factory.ObjectFactory;
2625
import org.springframework.core.ResolvableType;
@@ -75,20 +74,7 @@ protected GsonTester(Gson gson) {
7574
* @see #initFields(Object, Gson)
7675
*/
7776
public GsonTester(Class<?> resourceLoadClass, ResolvableType type, Gson gson) {
78-
this(resourceLoadClass, type, gson, Configuration.defaultConfiguration());
79-
}
80-
81-
/**
82-
* Create a new {@link GsonTester} instance.
83-
* @param resourceLoadClass the source class used to load resources
84-
* @param type the type under test
85-
* @param gson the Gson instance
86-
* @param configuration the json-path configuration
87-
* @see #initFields(Object, Gson)
88-
*/
89-
public GsonTester(Class<?> resourceLoadClass, ResolvableType type, Gson gson,
90-
Configuration configuration) {
91-
super(resourceLoadClass, type, configuration);
77+
super(resourceLoadClass, type);
9278
Assert.notNull(gson, "Gson must not be null");
9379
this.gson = gson;
9480
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JacksonTester.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,6 +59,7 @@
5959
* @param <T> the type under test
6060
* @author Phillip Webb
6161
* @author Madhura Bhave
62+
* @author Diego Berrueta
6263
* @since 1.4.0
6364
*/
6465
public class JacksonTester<T> extends AbstractJsonMarshalTester<T> {
@@ -89,14 +90,20 @@ public JacksonTester(Class<?> resourceLoadClass, ResolvableType type,
8990

9091
public JacksonTester(Class<?> resourceLoadClass, ResolvableType type,
9192
ObjectMapper objectMapper, Class<?> view) {
92-
super(resourceLoadClass, type, Configuration.builder()
93-
.jsonProvider(new JacksonJsonProvider(objectMapper))
94-
.mappingProvider(new JacksonMappingProvider(objectMapper)).build());
93+
super(resourceLoadClass, type);
9594
Assert.notNull(objectMapper, "ObjectMapper must not be null");
9695
this.objectMapper = objectMapper;
9796
this.view = view;
9897
}
9998

99+
@Override
100+
protected JsonContent<T> getJsonContent(String json) {
101+
Configuration configuration = Configuration.builder()
102+
.jsonProvider(new JacksonJsonProvider(this.objectMapper))
103+
.mappingProvider(new JacksonMappingProvider(this.objectMapper)).build();
104+
return new JsonContent<>(getResourceLoadClass(), getType(), json, configuration);
105+
}
106+
100107
@Override
101108
protected T readObject(InputStream inputStream, ResolvableType type)
102109
throws IOException {

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContent.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
*
3030
* @param <T> the source type that created the content
3131
* @author Phillip Webb
32+
* @author Diego Berrueta
3233
* @since 1.4.0
3334
*/
3435
public final class JsonContent<T> implements AssertProvider<JsonContentAssert> {
@@ -56,9 +57,9 @@ public JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json)
5657
* @param resourceLoadClass the source class used to load resources
5758
* @param type the type under test (or {@code null} if not known)
5859
* @param json the actual JSON content
59-
* @param configuration the json-path configuration
60+
* @param configuration the JsonPath configuration
6061
*/
61-
public JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json,
62+
JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json,
6263
Configuration configuration) {
6364
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
6465
Assert.notNull(json, "JSON must not be null");

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*
4747
* @author Phillip Webb
4848
* @author Andy Wilkinson
49+
* @author Diego Berrueta
4950
* @since 1.4.0
5051
*/
5152
public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> {
@@ -84,8 +85,8 @@ public JsonContentAssert(Class<?> resourceLoadClass, Charset charset,
8485
* @param json the actual JSON content
8586
* @param configuration the json-path configuration
8687
*/
87-
public JsonContentAssert(Class<?> resourceLoadClass, Charset charset,
88-
CharSequence json, Configuration configuration) {
88+
JsonContentAssert(Class<?> resourceLoadClass, Charset charset, CharSequence json,
89+
Configuration configuration) {
8990
super(json, JsonContentAssert.class);
9091
this.configuration = configuration;
9192
this.loader = new JsonLoader(resourceLoadClass, charset);

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonbTester.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,6 @@
2121

2222
import javax.json.bind.Jsonb;
2323

24-
import com.jayway.jsonpath.Configuration;
25-
2624
import org.springframework.beans.factory.ObjectFactory;
2725
import org.springframework.core.ResolvableType;
2826
import org.springframework.util.Assert;
@@ -76,20 +74,7 @@ protected JsonbTester(Jsonb jsonb) {
7674
* @see #initFields(Object, Jsonb)
7775
*/
7876
public JsonbTester(Class<?> resourceLoadClass, ResolvableType type, Jsonb jsonb) {
79-
this(resourceLoadClass, type, jsonb, Configuration.defaultConfiguration());
80-
}
81-
82-
/**
83-
* Create a new {@link JsonbTester} instance.
84-
* @param resourceLoadClass the source class used to load resources
85-
* @param type the type under test
86-
* @param jsonb the Jsonb instance
87-
* @param configuration the json-path configuration
88-
* @see #initFields(Object, Jsonb)
89-
*/
90-
public JsonbTester(Class<?> resourceLoadClass, ResolvableType type, Jsonb jsonb,
91-
Configuration configuration) {
92-
super(resourceLoadClass, type, configuration);
77+
super(resourceLoadClass, type);
9378
Assert.notNull(jsonb, "Jsonb must not be null");
9479
this.jsonb = jsonb;
9580
}

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/JacksonTesterIntegrationTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
*
3737
* @author Phillip Webb
3838
* @author Madhura Bhave
39+
* @author Diego Berrueta
3940
*/
4041
public class JacksonTesterIntegrationTests {
4142

@@ -91,14 +92,14 @@ public void stringLiteral() throws Exception {
9192
.isEqualTo(stringWithSpecialCharacters);
9293
}
9394

94-
// This test confirms that the handling of special characters is symmetrical between
95-
// the serialisation (via the JacksonTester) and the parsing (via json-path). By
96-
// default json-path uses SimpleJson as its parser, which has a slightly different
97-
// behaviour to Jackson and breaks the symmetry. However JacksonTester
98-
// configures json-path to use Jackson for evaluating the path expressions and
99-
// restores the symmetry.
10095
@Test
10196
public void parseSpecialCharactersTest() throws Exception {
97+
// Confirms that the handling of special characters is symmetrical between
98+
// the serialization (via the JacksonTester) and the parsing (via json-path). By
99+
// default json-path uses SimpleJson as its parser, which has a slightly different
100+
// behavior to Jackson and breaks the symmetry. JacksonTester
101+
// configures json-path to use Jackson for evaluating the path expressions and
102+
// restores the symmetry. See gh-15727
102103
String stringWithSpecialCharacters = "\u0006\u007F";
103104
assertThat(this.stringJson.write(stringWithSpecialCharacters))
104105
.extractingJsonPathStringValue("@")

0 commit comments

Comments
 (0)