Skip to content

Commit 32040e3

Browse files
committed
Merge branch '2.1.x'
Closes gh-16649
2 parents 17aaf26 + 7302974 commit 32040e3

File tree

10 files changed

+203
-15
lines changed

10 files changed

+203
-15
lines changed

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

Lines changed: 13 additions & 2 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.
@@ -129,7 +129,18 @@ public JsonContent<T> write(T value) throws IOException {
129129
verify();
130130
Assert.notNull(value, "Value must not be null");
131131
String json = writeObject(value, this.type);
132-
return new JsonContent<>(this.resourceLoadClass, this.type, json);
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);
133144
}
134145

135146
/**

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

Lines changed: 1 addition & 1 deletion
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.

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

Lines changed: 1 addition & 1 deletion
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.

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

Lines changed: 13 additions & 1 deletion
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.
@@ -24,6 +24,9 @@
2424
import com.fasterxml.jackson.databind.ObjectMapper;
2525
import com.fasterxml.jackson.databind.ObjectReader;
2626
import com.fasterxml.jackson.databind.ObjectWriter;
27+
import com.jayway.jsonpath.Configuration;
28+
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
29+
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
2730

2831
import org.springframework.beans.factory.ObjectFactory;
2932
import org.springframework.core.ResolvableType;
@@ -56,6 +59,7 @@
5659
* @param <T> the type under test
5760
* @author Phillip Webb
5861
* @author Madhura Bhave
62+
* @author Diego Berrueta
5963
* @since 1.4.0
6064
*/
6165
public class JacksonTester<T> extends AbstractJsonMarshalTester<T> {
@@ -92,6 +96,14 @@ public JacksonTester(Class<?> resourceLoadClass, ResolvableType type,
9296
this.view = view;
9397
}
9498

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+
95107
@Override
96108
protected T readObject(InputStream inputStream, ResolvableType type)
97109
throws IOException {

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.test.json;
1818

19+
import com.jayway.jsonpath.Configuration;
1920
import org.assertj.core.api.AssertProvider;
2021

2122
import org.springframework.core.ResolvableType;
@@ -28,6 +29,7 @@
2829
*
2930
* @param <T> the source type that created the content
3031
* @author Phillip Webb
32+
* @author Diego Berrueta
3133
* @since 1.4.0
3234
*/
3335
public final class JsonContent<T> implements AssertProvider<JsonContentAssert> {
@@ -38,18 +40,34 @@ public final class JsonContent<T> implements AssertProvider<JsonContentAssert> {
3840

3941
private final String json;
4042

43+
private final Configuration configuration;
44+
4145
/**
4246
* Create a new {@link JsonContent} instance.
4347
* @param resourceLoadClass the source class used to load resources
4448
* @param type the type under test (or {@code null} if not known)
4549
* @param json the actual JSON content
4650
*/
4751
public JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json) {
52+
this(resourceLoadClass, type, json, Configuration.defaultConfiguration());
53+
}
54+
55+
/**
56+
* Create a new {@link JsonContent} instance.
57+
* @param resourceLoadClass the source class used to load resources
58+
* @param type the type under test (or {@code null} if not known)
59+
* @param json the actual JSON content
60+
* @param configuration the JsonPath configuration
61+
*/
62+
JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json,
63+
Configuration configuration) {
4864
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
4965
Assert.notNull(json, "JSON must not be null");
66+
Assert.notNull(configuration, "Configuration must not be null");
5067
this.resourceLoadClass = resourceLoadClass;
5168
this.type = type;
5269
this.json = json;
70+
this.configuration = configuration;
5371
}
5472

5573
/**
@@ -61,7 +79,8 @@ public JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json)
6179
@Override
6280
@Deprecated
6381
public JsonContentAssert assertThat() {
64-
return new JsonContentAssert(this.resourceLoadClass, this.json);
82+
return new JsonContentAssert(this.resourceLoadClass, null, this.json,
83+
this.configuration);
6584
}
6685

6786
/**

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25+
import com.jayway.jsonpath.Configuration;
2526
import com.jayway.jsonpath.JsonPath;
2627
import org.assertj.core.api.AbstractAssert;
2728
import org.assertj.core.api.AbstractBooleanAssert;
@@ -45,13 +46,16 @@
4546
*
4647
* @author Phillip Webb
4748
* @author Andy Wilkinson
49+
* @author Diego Berrueta
4850
* @author Camille Vienot
4951
* @since 1.4.0
5052
*/
5153
public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> {
5254

5355
private final JsonLoader loader;
5456

57+
private final Configuration configuration;
58+
5559
/**
5660
* Create a new {@link JsonContentAssert} instance that will load resources as UTF-8.
5761
* @param resourceLoadClass the source class used to load resources
@@ -71,7 +75,21 @@ public JsonContentAssert(Class<?> resourceLoadClass, CharSequence json) {
7175
*/
7276
public JsonContentAssert(Class<?> resourceLoadClass, Charset charset,
7377
CharSequence json) {
78+
this(resourceLoadClass, charset, json, Configuration.defaultConfiguration());
79+
}
80+
81+
/**
82+
* Create a new {@link JsonContentAssert} instance that will load resources in the
83+
* given {@code charset}.
84+
* @param resourceLoadClass the source class used to load resources
85+
* @param charset the charset of the JSON resources
86+
* @param json the actual JSON content
87+
* @param configuration the json-path configuration
88+
*/
89+
JsonContentAssert(Class<?> resourceLoadClass, Charset charset, CharSequence json,
90+
Configuration configuration) {
7491
super(json, JsonContentAssert.class);
92+
this.configuration = configuration;
7593
this.loader = new JsonLoader(resourceLoadClass, charset);
7694
}
7795

@@ -1110,7 +1128,8 @@ private boolean isEmpty() {
11101128
public Object getValue(boolean required) {
11111129
try {
11121130
CharSequence json = JsonContentAssert.this.actual;
1113-
return this.jsonPath.read((json != null) ? json.toString() : null);
1131+
return this.jsonPath.read((json != null) ? json.toString() : null,
1132+
JsonContentAssert.this.configuration);
11141133
}
11151134
catch (Exception ex) {
11161135
if (required) {

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

Lines changed: 1 addition & 1 deletion
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.json;
18+
19+
import java.util.LinkedHashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import com.google.gson.Gson;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Integration tests for {@link GsonTester}. Shows typical usage.
31+
*
32+
* @author Andy Wilkinson
33+
* @author Diego Berrueta
34+
*/
35+
public class GsonTesterIntegrationTests {
36+
37+
private GsonTester<ExampleObject> simpleJson;
38+
39+
private GsonTester<List<ExampleObject>> listJson;
40+
41+
private GsonTester<Map<String, Integer>> mapJson;
42+
43+
private GsonTester<String> stringJson;
44+
45+
private Gson gson;
46+
47+
private static final String JSON = "{\"name\":\"Spring\",\"age\":123}";
48+
49+
@Before
50+
public void setup() {
51+
this.gson = new Gson();
52+
GsonTester.initFields(this, this.gson);
53+
}
54+
55+
@Test
56+
public void typicalTest() throws Exception {
57+
String example = JSON;
58+
assertThat(this.simpleJson.parse(example).getObject().getName())
59+
.isEqualTo("Spring");
60+
}
61+
62+
@Test
63+
public void typicalListTest() throws Exception {
64+
String example = "[" + JSON + "]";
65+
assertThat(this.listJson.parse(example)).asList().hasSize(1);
66+
assertThat(this.listJson.parse(example).getObject().get(0).getName())
67+
.isEqualTo("Spring");
68+
}
69+
70+
@Test
71+
public void typicalMapTest() throws Exception {
72+
Map<String, Integer> map = new LinkedHashMap<>();
73+
map.put("a", 1);
74+
map.put("b", 2);
75+
assertThat(this.mapJson.write(map)).extractingJsonPathNumberValue("@.a")
76+
.isEqualTo(1);
77+
}
78+
79+
@Test
80+
public void stringLiteral() throws Exception {
81+
String stringWithSpecialCharacters = "myString";
82+
assertThat(this.stringJson.write(stringWithSpecialCharacters))
83+
.extractingJsonPathStringValue("@")
84+
.isEqualTo(stringWithSpecialCharacters);
85+
}
86+
87+
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* @author Phillip Webb
3838
* @author Madhura Bhave
39+
* @author Diego Berrueta
3940
*/
4041
public class JacksonTesterIntegrationTests {
4142

@@ -47,6 +48,8 @@ public class JacksonTesterIntegrationTests {
4748

4849
private JacksonTester<Map<String, Integer>> mapJson;
4950

51+
private JacksonTester<String> stringJson;
52+
5053
private ObjectMapper objectMapper;
5154

5255
private static final String JSON = "{\"name\":\"Spring\",\"age\":123}";
@@ -81,6 +84,28 @@ public void typicalMapTest() throws Exception {
8184
.isEqualTo(1);
8285
}
8386

87+
@Test
88+
public void stringLiteral() throws Exception {
89+
String stringWithSpecialCharacters = "myString";
90+
assertThat(this.stringJson.write(stringWithSpecialCharacters))
91+
.extractingJsonPathStringValue("@")
92+
.isEqualTo(stringWithSpecialCharacters);
93+
}
94+
95+
@Test
96+
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
103+
String stringWithSpecialCharacters = "\u0006\u007F";
104+
assertThat(this.stringJson.write(stringWithSpecialCharacters))
105+
.extractingJsonPathStringValue("@")
106+
.isEqualTo(stringWithSpecialCharacters);
107+
}
108+
84109
@Test
85110
public void writeWithView() throws Exception {
86111
this.objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);

0 commit comments

Comments
 (0)