-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathPathCompilerTest.java
More file actions
333 lines (278 loc) · 11.3 KB
/
PathCompilerTest.java
File metadata and controls
333 lines (278 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.ParseContextImpl;
import org.junit.Ignore;
import org.junit.Test;
import java.util.List;
import static com.jayway.jsonpath.internal.path.PathCompiler.compile;
import static org.assertj.core.api.Assertions.assertThat;
public class PathCompilerTest {
@Ignore("Backward compatibility <= 2.0.0")
@Test(expected = InvalidPathException.class)
public void a_path_must_start_with_$_or_at() {
compile("x");
}
@Ignore("Backward compatibility <= 2.0.0")
@Test(expected = InvalidPathException.class)
public void a_square_bracket_may_not_follow_a_period() {
compile("$.[");
}
@Test(expected = InvalidPathException.class)
public void a_root_path_must_be_followed_by_period_or_bracket() {
compile("$X");
}
@Test
public void a_root_path_can_be_compiled() {
assertThat(compile("$").toString()).isEqualTo("$");
assertThat(compile("@").toString()).isEqualTo("@");
}
@Test(expected = InvalidPathException.class)
public void a_path_may_not_end_with_period() {
compile("$.");
}
@Test(expected = InvalidPathException.class)
public void a_path_may_not_end_with_period_2() {
compile("$.prop.");
}
@Test(expected = InvalidPathException.class)
public void a_path_may_not_end_with_scan() {
compile("$..");
}
@Test(expected = InvalidPathException.class)
public void a_path_may_not_end_with_scan_2() {
compile("$.prop..");
}
@Test
public void a_property_token_can_be_compiled() {
assertThat(compile("$.prop").toString()).isEqualTo("$['prop']");
assertThat(compile("$.1prop").toString()).isEqualTo("$['1prop']");
assertThat(compile("$.@prop").toString()).isEqualTo("$['@prop']");
}
@Test
public void a_bracket_notation_property_token_can_be_compiled() {
assertThat(compile("$['prop']").toString()).isEqualTo("$['prop']");
assertThat(compile("$['1prop']").toString()).isEqualTo("$['1prop']");
assertThat(compile("$['@prop']").toString()).isEqualTo("$['@prop']");
assertThat(compile("$[ '@prop' ]").toString()).isEqualTo("$['@prop']");
assertThat(compile("$[\"prop\"]").toString()).isEqualTo("$[\"prop\"]");
}
@Test
public void a_multi_property_token_can_be_compiled() {
assertThat(compile("$['prop0', 'prop1']").toString()).isEqualTo("$['prop0','prop1']");
assertThat(compile("$[ 'prop0' , 'prop1' ]").toString()).isEqualTo("$['prop0','prop1']");
}
@Test
public void a_property_chain_can_be_compiled() {
assertThat(compile("$.abc").toString()).isEqualTo("$['abc']");
assertThat(compile("$.aaa.bbb").toString()).isEqualTo("$['aaa']['bbb']");
assertThat(compile("$.aaa.bbb.ccc").toString()).isEqualTo("$['aaa']['bbb']['ccc']");
}
@Test(expected = InvalidPathException.class)
public void a_property_may_not_contain_blanks() {
assertThat(compile("$.foo bar").toString());
}
@Test
public void a_wildcard_can_be_compiled() {
assertThat(compile("$.*").toString()).isEqualTo("$[*]");
assertThat(compile("$[*]").toString()).isEqualTo("$[*]");
assertThat(compile("$[ * ]").toString()).isEqualTo("$[*]");
}
@Test
public void a_wildcard_can_follow_a_property() {
assertThat(compile("$.prop[*]").toString()).isEqualTo("$['prop'][*]");
assertThat(compile("$['prop'][*]").toString()).isEqualTo("$['prop'][*]");
}
@Test
public void an_array_index_path_can_be_compiled() {
assertThat(compile("$[1]").toString()).isEqualTo("$[1]");
assertThat(compile("$[1,2,3]").toString()).isEqualTo("$[1,2,3]");
assertThat(compile("$[ 1 , 2 , 3 ]").toString()).isEqualTo("$[1,2,3]");
}
@Test
public void an_array_slice_path_can_be_compiled() {
assertThat(compile("$[-1:]").toString()).isEqualTo("$[-1:]");
assertThat(compile("$[1:2]").toString()).isEqualTo("$[1:2]");
assertThat(compile("$[:2]").toString()).isEqualTo("$[:2]");
}
@Test
public void an_inline_criteria_can_be_parsed() {
assertThat(compile("$[?(@.foo == 'bar')]").toString()).isEqualTo("$[?]");
assertThat(compile("$[?(@.foo == \"bar\")]").toString()).isEqualTo("$[?]");
}
@Test
public void a_placeholder_criteria_can_be_parsed() {
Predicate p = new Predicate() {
@Override
public boolean apply(PredicateContext ctx) {
return false;
}
};
assertThat(compile("$[?]", p).toString()).isEqualTo("$[?]");
assertThat(compile("$[?,?]", p, p).toString()).isEqualTo("$[?,?]");
assertThat(compile("$[?,?,?]", p, p, p).toString()).isEqualTo("$[?,?,?]");
}
@Test
public void a_scan_token_can_be_parsed() {
assertThat(compile("$..['prop']..[*]").toString()).isEqualTo("$..['prop']..[*]");
}
@Test
public void issue_predicate_can_have_escaped_backslash_in_prop() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"message\": \"it\\\\\",\n"
+ " \"id\": 2\n"
+ " }\n"
+ " ]\n"
+ "}";
// message: it\ -> (after json escaping) -> "it\\" -> (after java escaping) -> "\"it\\\\\""
List<String> result = JsonPath.read(json, "$.logs[?(@.message == 'it\\\\')].message");
assertThat(result).containsExactly("it\\");
}
@Test
public void issue_predicate_can_have_bracket_in_regex() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"message\": \"(it\",\n"
+ " \"id\": 2\n"
+ " }\n"
+ " ]\n"
+ "}";
List<String> result = JsonPath.read(json, "$.logs[?(@.message =~ /\\(it/)].message");
assertThat(result).containsExactly("(it");
}
@Test
public void issue_predicate_can_have_and_in_regex() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"message\": \"it\",\n"
+ " \"id\": 2\n"
+ " }\n"
+ " ]\n"
+ "}";
List<String> result = JsonPath.read(json, "$.logs[?(@.message =~ /&&|it/)].message");
assertThat(result).containsExactly("it");
}
@Test
public void issue_predicate_can_have_and_in_prop() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"message\": \"&& it\",\n"
+ " \"id\": 2\n"
+ " }\n"
+ " ]\n"
+ "}";
List<String> result = JsonPath.read(json, "$.logs[?(@.message == '&& it')].message");
assertThat(result).containsExactly("&& it");
}
@Test
public void issue_predicate_brackets_must_change_priorities() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"id\": 2\n"
+ " }\n"
+ " ]\n"
+ "}";
List<String> result = JsonPath.read(json, "$.logs[?(@.message && (@.id == 1 || @.id == 2))].id");
assertThat(result).isEmpty();
result = JsonPath.read(json, "$.logs[?((@.id == 2 || @.id == 1) && @.message)].id");
assertThat(result).isEmpty();
}
@Test
public void issue_predicate_or_has_lower_priority_than_and() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"id\": 2\n"
+ " }\n"
+ " ]\n"
+ "}";
List<String> result = JsonPath.read(json, "$.logs[?(@.x && @.y || @.id)]");
assertThat(result).hasSize(1);
}
@Test
public void issue_predicate_can_have_double_quotes() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"message\": \"\\\"it\\\"\",\n"
+ " }\n"
+ " ]\n"
+ "}";
List<String> result = JsonPath.read(json, "$.logs[?(@.message == '\"it\"')].message");
assertThat(result).containsExactly("\"it\"");
}
@Test
public void issue_predicate_can_have_single_quotes() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"message\": \"'it'\",\n"
+ " }\n"
+ " ]\n"
+ "}";
DocumentContext parse = JsonPath.parse(json);
JsonPath compile = JsonPath.compile("$.logs[?(@.message == \"'it'\")].message");
List<String> result = parse.read(compile);
assertThat(result).containsExactly("'it'");
}
@Test
public void issue_predicate_can_have_single_quotes_escaped() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"message\": \"'it'\",\n"
+ " }\n"
+ " ]\n"
+ "}";
DocumentContext parse = JsonPath.parse(json);
JsonPath compile = JsonPath.compile("$.logs[?(@.message == '\\'it\\'')].message");
List<String> result = parse.read(compile);
assertThat(result).containsExactly("'it'");
}
@Test
public void issue_predicate_can_have_square_bracket_in_prop() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"message\": \"] it\",\n"
+ " \"id\": 2\n"
+ " }\n"
+ " ]\n"
+ "}";
List<String> result = JsonPath.read(json, "$.logs[?(@.message == '] it')].message");
assertThat(result).containsExactly("] it");
}
@Test
public void a_function_can_be_compiled() {
assertThat(compile("$.aaa.foo()").toString()).isEqualTo("$['aaa'].foo()");
assertThat(compile("$.aaa.foo(5)").toString()).isEqualTo("$['aaa'].foo(...)");
assertThat(compile("$.aaa.foo($.bar)").toString()).isEqualTo("$['aaa'].foo(...)");
assertThat(compile("$.aaa.foo(5,10,15)").toString()).isEqualTo("$['aaa'].foo(...)");
}
@Test(expected = InvalidPathException.class)
public void array_indexes_must_be_separated_by_commas() {
compile("$[0, 1, 2 4]");
}
@Test(expected = InvalidPathException.class)
public void trailing_comma_after_list_is_not_accepted() {
compile("$['1','2',]");
}
@Test(expected = InvalidPathException.class)
public void accept_only_a_single_comma_between_indexes() {
compile("$['1', ,'3']");
}
@Test(expected = InvalidPathException.class)
public void property_must_be_separated_by_commas() {
compile("$['aaa'}'bbb']");
}
@Test
public void function_parameter_with_closing_paren() {
String json = "{}";
String result = JsonPath.read(json, "$.concat(\"Bob\", $.concat(\" Joe\", \" Alex\"))");
assertThat(result).isEqualTo("Bob Joe Alex");
}
}