1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .test .util ;
18
18
19
- import java .text .ParseException ;
20
19
import java .util .List ;
21
20
import java .util .Map ;
22
21
23
- import com .jayway .jsonpath .InvalidPathException ;
24
22
import com .jayway .jsonpath .JsonPath ;
25
23
import org .hamcrest .Matcher ;
26
24
@@ -71,18 +69,33 @@ public JsonPathExpectationsHelper(String expression, Object... args) {
71
69
* @param matcher the matcher with which to assert the result
72
70
*/
73
71
@ SuppressWarnings ("unchecked" )
74
- public <T > void assertValue (String content , Matcher <T > matcher ) throws ParseException {
72
+ public <T > void assertValue (String content , Matcher <T > matcher ) {
75
73
T value = (T ) evaluateJsonPath (content );
76
74
assertThat ("JSON path \" " + this .expression + "\" " , value , matcher );
77
75
}
78
76
77
+ /**
78
+ * An overloaded variant of {@link #assertValue(String, Matcher)} that also
79
+ * accepts a target type for the resulting value. This can be useful for
80
+ * matching numbers reliably for example coercing an integer into a double.
81
+ * @param content the JSON content
82
+ * @param matcher the matcher with which to assert the result
83
+ * @param targetType a the expected type of the resulting value
84
+ * @since 4.3.3
85
+ */
86
+ @ SuppressWarnings ("unchecked" )
87
+ public <T > void assertValue (String content , Matcher <T > matcher , Class <T > targetType ) {
88
+ T value = (T ) evaluateJsonPath (content , targetType );
89
+ assertThat ("JSON path \" " + this .expression + "\" " , value , matcher );
90
+ }
91
+
79
92
/**
80
93
* Evaluate the JSON path expression against the supplied {@code content}
81
94
* and assert that the result is equal to the expected value.
82
95
* @param content the JSON content
83
96
* @param expectedValue the expected value
84
97
*/
85
- public void assertValue (String content , Object expectedValue ) throws ParseException {
98
+ public void assertValue (String content , Object expectedValue ) {
86
99
Object actualValue = evaluateJsonPath (content );
87
100
if ((actualValue instanceof List ) && !(expectedValue instanceof List )) {
88
101
@ SuppressWarnings ("rawtypes" )
@@ -96,8 +109,9 @@ public void assertValue(String content, Object expectedValue) throws ParseExcept
96
109
actualValue = actualValueList .get (0 );
97
110
}
98
111
else if (actualValue != null && expectedValue != null ) {
99
- assertEquals ("At JSON path \" " + this .expression + "\" , type of value" ,
100
- expectedValue .getClass ().getName (), actualValue .getClass ().getName ());
112
+ if (!actualValue .getClass ().equals (expectedValue .getClass ())) {
113
+ actualValue = evaluateJsonPath (content , expectedValue .getClass ());
114
+ }
101
115
}
102
116
assertEquals ("JSON path \" " + this .expression + "\" " , expectedValue , actualValue );
103
117
}
@@ -108,7 +122,7 @@ else if (actualValue != null && expectedValue != null) {
108
122
* @param content the JSON content
109
123
* @since 4.2.1
110
124
*/
111
- public void assertValueIsString (String content ) throws ParseException {
125
+ public void assertValueIsString (String content ) {
112
126
Object value = assertExistsAndReturn (content );
113
127
assertThat (failureReason ("a string" , value ), value , instanceOf (String .class ));
114
128
}
@@ -119,7 +133,7 @@ public void assertValueIsString(String content) throws ParseException {
119
133
* @param content the JSON content
120
134
* @since 4.2.1
121
135
*/
122
- public void assertValueIsBoolean (String content ) throws ParseException {
136
+ public void assertValueIsBoolean (String content ) {
123
137
Object value = assertExistsAndReturn (content );
124
138
assertThat (failureReason ("a boolean" , value ), value , instanceOf (Boolean .class ));
125
139
}
@@ -130,7 +144,7 @@ public void assertValueIsBoolean(String content) throws ParseException {
130
144
* @param content the JSON content
131
145
* @since 4.2.1
132
146
*/
133
- public void assertValueIsNumber (String content ) throws ParseException {
147
+ public void assertValueIsNumber (String content ) {
134
148
Object value = assertExistsAndReturn (content );
135
149
assertThat (failureReason ("a number" , value ), value , instanceOf (Number .class ));
136
150
}
@@ -140,7 +154,7 @@ public void assertValueIsNumber(String content) throws ParseException {
140
154
* and assert that the resulting value is an array.
141
155
* @param content the JSON content
142
156
*/
143
- public void assertValueIsArray (String content ) throws ParseException {
157
+ public void assertValueIsArray (String content ) {
144
158
Object value = assertExistsAndReturn (content );
145
159
assertThat (failureReason ("an array" , value ), value , instanceOf (List .class ));
146
160
}
@@ -151,7 +165,7 @@ public void assertValueIsArray(String content) throws ParseException {
151
165
* @param content the JSON content
152
166
* @since 4.2.1
153
167
*/
154
- public void assertValueIsMap (String content ) throws ParseException {
168
+ public void assertValueIsMap (String content ) {
155
169
Object value = assertExistsAndReturn (content );
156
170
assertThat (failureReason ("a map" , value ), value , instanceOf (Map .class ));
157
171
}
@@ -164,7 +178,7 @@ public void assertValueIsMap(String content) throws ParseException {
164
178
* that the value at the given path is not <em>empty</em>.
165
179
* @param content the JSON content
166
180
*/
167
- public void exists (String content ) throws ParseException {
181
+ public void exists (String content ) {
168
182
assertExistsAndReturn (content );
169
183
}
170
184
@@ -176,7 +190,7 @@ public void exists(String content) throws ParseException {
176
190
* that the value at the given path is <em>empty</em>.
177
191
* @param content the JSON content
178
192
*/
179
- public void doesNotExist (String content ) throws ParseException {
193
+ public void doesNotExist (String content ) {
180
194
Object value ;
181
195
try {
182
196
value = evaluateJsonPath (content );
@@ -189,7 +203,7 @@ public void doesNotExist(String content) throws ParseException {
189
203
assertTrue (reason , ((List <?>) value ).isEmpty ());
190
204
}
191
205
else {
192
- assertTrue (reason , value == null );
206
+ assertTrue (reason , ( value == null ) );
193
207
}
194
208
}
195
209
@@ -200,7 +214,7 @@ public void doesNotExist(String content) throws ParseException {
200
214
* {@link ObjectUtils#isEmpty(Object)}.
201
215
* @param content the JSON content
202
216
*/
203
- public void assertValueIsEmpty (String content ) throws ParseException {
217
+ public void assertValueIsEmpty (String content ) {
204
218
Object value = evaluateJsonPath (content );
205
219
assertTrue (failureReason ("an empty value" , value ), ObjectUtils .isEmpty (value ));
206
220
}
@@ -212,33 +226,37 @@ public void assertValueIsEmpty(String content) throws ParseException {
212
226
* {@link ObjectUtils#isEmpty(Object)}.
213
227
* @param content the JSON content
214
228
*/
215
- public void assertValueIsNotEmpty (String content ) throws ParseException {
229
+ public void assertValueIsNotEmpty (String content ) {
216
230
Object value = evaluateJsonPath (content );
217
231
assertTrue (failureReason ("a non-empty value" , value ), !ObjectUtils .isEmpty (value ));
218
232
}
219
233
220
234
private String failureReason (String expectedDescription , Object value ) {
221
235
return String .format ("Expected %s at JSON path \" %s\" but found: %s" , expectedDescription , this .expression ,
222
- ObjectUtils .nullSafeToString (StringUtils .quoteIfString (value )));
236
+ ObjectUtils .nullSafeToString (StringUtils .quoteIfString (value )));
223
237
}
224
238
225
- private Object evaluateJsonPath (String content ) throws ParseException {
239
+ private Object evaluateJsonPath (String content ) {
226
240
String message = "No value at JSON path \" " + this .expression + "\" , exception: " ;
227
241
try {
228
242
return this .jsonPath .read (content );
229
243
}
230
- catch (InvalidPathException ex ) {
244
+ catch (Throwable ex ) {
231
245
throw new AssertionError (message + ex .getMessage ());
232
246
}
233
- catch (ArrayIndexOutOfBoundsException ex ) {
234
- throw new AssertionError (message + ex .getMessage ());
247
+ }
248
+
249
+ private Object evaluateJsonPath (String content , Class <?> targetType ) {
250
+ String message = "No value at JSON path \" " + this .expression + "\" , exception: " ;
251
+ try {
252
+ return JsonPath .parse (content ).read (this .expression , targetType );
235
253
}
236
- catch (IndexOutOfBoundsException ex ) {
254
+ catch (Throwable ex ) {
237
255
throw new AssertionError (message + ex .getMessage ());
238
256
}
239
257
}
240
258
241
- private Object assertExistsAndReturn (String content ) throws ParseException {
259
+ private Object assertExistsAndReturn (String content ) {
242
260
Object value = evaluateJsonPath (content );
243
261
String reason = "No value at JSON path \" " + this .expression + "\" " ;
244
262
assertTrue (reason , value != null );
0 commit comments