Browse Source

Improved test for Filter serialization.

pull/139/head
Kalle Stenflo 9 years ago
parent
commit
e1d5329a8c
  1. 3
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  2. 140
      json-path/src/test/java/com/jayway/jsonpath/FilterTest.java
  3. 12
      json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

3
json-path/src/main/java/com/jayway/jsonpath/Criteria.java

@ -451,7 +451,6 @@ public class Criteria implements Predicate {
} else {
return (value == null);
}
} catch (PathNotFoundException e) {
return !exists;
}
@ -841,7 +840,7 @@ public class Criteria implements Predicate {
@Override
public String toString() {
return getClass().getSimpleName() + " " + value;
return value;
}
}

140
json-path/src/test/java/com/jayway/jsonpath/FilterTest.java

@ -11,8 +11,6 @@ import java.util.regex.Pattern;
import static com.jayway.jsonpath.Criteria.where;
import static com.jayway.jsonpath.Filter.filter;
import static com.jayway.jsonpath.Filter.parse;
import static java.lang.System.out;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
public class FilterTest extends BaseTest {
@ -473,74 +471,116 @@ public class FilterTest extends BaseTest {
@Test
public void a_gte_filter_can_be_serialized() {
System.out.println(filter(where("a").gte(1)).toString());
String filter = filter(where("a").gte(1)).toString();
String parsed = parse("[?(@['a'] >= 1)]").toString();
assertThat(filter(where("a").gte(1)).toString()).isEqualTo(parse("[?(@['a'] >= 1)]").toString());
assertThat(filter).isEqualTo(parse(parsed).toString());
}
@Test
public void a_lte_filter_can_be_serialized() {
assertThat(filter(where("a").lte(1)).toString()).isEqualTo("[?(@['a'] <= 1)]");
String filter = filter(where("a").lte(1)).toString();
String parsed = parse("[?(@['a'] <= 1)]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_eq_filter_can_be_serialized() {
assertThat(filter(where("a").eq(1)).toString()).isEqualTo("[?(@['a'] == 1)]");
String filter = filter(where("a").eq(1)).toString();
String parsed = parse("[?(@['a'] == 1)]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_ne_filter_can_be_serialized() {
assertThat(filter(where("a").ne(1)).toString()).isEqualTo("[?(@['a'] != 1)]");
String filter = filter(where("a").ne(1)).toString();
String parsed = parse("[?(@['a'] != 1)]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_lt_filter_can_be_serialized() {
assertThat(filter(where("a").lt(1)).toString()).isEqualTo("[?(@['a'] < 1)]");
String filter = filter(where("a").lt(1)).toString();
String parsed = parse("[?(@['a'] < 1)]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_gt_filter_can_be_serialized() {
assertThat(filter(where("a").gt(1)).toString()).isEqualTo("[?(@['a'] > 1)]");
}
@Test
public void a_regex_filter_can_be_serialized() {
assertThat(filter(where("a").regex(Pattern.compile("/.*?/i"))).toString()).isEqualTo("[?(@['a'] =~ /.*?/i)]");
String filter = filter(where("a").gt(1)).toString();
String parsed = parse("[?(@['a'] > 1)]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_nin_filter_can_be_serialized() {
assertThat(filter(where("a").nin(1)).toString()).isEqualTo("[?(@['a'] ¦NIN¦ [1])]");
String filter = filter(where("a").nin(1)).toString();
String parsed = parse("[?(@['a'] ¦NIN¦ [1])]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_in_filter_can_be_serialized() {
assertThat(filter(where("a").in("a")).toString()).isEqualTo("[?(@['a'] ¦IN¦ ['a'])]");
String filter = filter(where("a").in("a")).toString();
String parsed = parse("[?(@['a'] ¦IN¦ ['a'])]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_contains_filter_can_be_serialized() {
assertThat(filter(where("a").contains("a")).toString()).isEqualTo("[?(@['a'] ¦CONTAINS¦ 'a')]");
String filter = filter(where("a").contains("a")).toString();
String parsed = parse("[?(@['a'] ¦CONTAINS¦ 'a')]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_all_filter_can_be_serialized() {
assertThat(filter(where("a").all("a", "b")).toString()).isEqualTo("[?(@['a'] ¦ALL¦ ['a','b'])]");
String filter = filter(where("a").all("a", "b")).toString();
String parsed = parse("[?(@['a'] ¦ALL¦ ['a','b'])]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_size_filter_can_be_serialized() {
assertThat(filter(where("a").size(5)).toString()).isEqualTo("[?(@['a'] ¦SIZE¦ 5)]");
String filter = filter(where("a").size(5)).toString();
String parsed = parse("[?(@['a'] ¦SIZE¦ 5)]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_exists_filter_can_be_serialized() {
assertThat(filter(where("a").exists(true)).toString()).isEqualTo("[?(@['a'])]");
String filter = filter(where("a").exists(true)).toString();
String parsed = parse("[?(@['a'])]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_not_exists_filter_can_be_serialized() {
assertThat(filter(where("a").exists(false)).toString()).isEqualTo("[?(!@['a'])]");
String filter = filter(where("a").exists(false)).toString();
String parsed = parse("[?(!@['a'])]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
@ -557,22 +597,43 @@ public class FilterTest extends BaseTest {
@Test
public void a_not_empty_filter_can_be_serialized() {
assertThat(filter(where("a").notEmpty()).toString()).isEqualTo("[?(@['a'] ¦NOT_EMPTY¦)]");
String filter = filter(where("a").notEmpty()).toString();
String parsed = parse("[?(@['a'] ¦NOT_EMPTY¦)]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void and_filter_can_be_serialized() {
assertThat(filter(where("a").eq(1).and("b").eq(2)).toString()).isEqualTo("[?(@['a'] == 1 && @['b'] == 2)]");
String filter = filter(where("a").eq(1).and("b").eq(2)).toString();
String parsed = parse("[?(@['b'] == 2 && @['a'] == 1)]").toString(); //FIXME: criteria are reversed
assertThat(filter).isEqualTo(parsed);
}
@Test
public void in_string_filter_can_be_serialized() {
assertThat(filter(where("a").in("1","2")).toString()).isEqualTo("[?(@['a'] ¦IN¦ ['1','2'])]");
String filter = filter(where("a").in("1","2")).toString();
String parsed = parse("[?(@['a'] ¦IN¦ ['1','2'])]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_deep_path_filter_can_be_serialized() {
assertThat(filter(where("a.b.c").in("1","2")).toString()).isEqualTo("[?(@['a']['b']['c'] ¦IN¦ ['1','2'])]");
String filter = filter(where("a.b.c").in("1", "2")).toString();
String parsed = parse("[?(@['a']['b']['c'] ¦IN¦ ['1','2'])]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_regex_filter_can_be_serialized() {
assertThat(filter(where("a").regex(Pattern.compile("/.*?/i"))).toString()).isEqualTo("[?(@['a'] =~ /.*?/i)]");
}
@Test
@ -585,9 +646,13 @@ public class FilterTest extends BaseTest {
Filter a = filter(where("a").eq(1));
Filter b = filter(where("b").eq(2));
Filter c = a.and(b);
Filter c = b.and(a);
String filter = c.toString();
String parsed = parse("[?(@['a'] == 1 && @['b'] == 2)]").toString();
assertThat(c.toString()).isEqualTo("[?(@['a'] == 1 && @['b'] == 2)]");
assertThat(filter).isEqualTo(parsed);
}
@Test
@ -595,26 +660,11 @@ public class FilterTest extends BaseTest {
Filter a = filter(where("a").eq(1));
Filter b = filter(where("b").eq(2));
Filter c = a.or(b);
assertThat(c.toString()).isEqualTo("[?(@['a'] == 1 || @['b'] == 2)]");
}
@Test
public void a_____() {
// :2
// 1:2
// -2:
//2:
Filter c = b.or(a);
out.println(asList(":2".split(":"))); //[, 2]
out.println(asList("1:2".split(":"))); //[1, 2]
out.println(asList("-2:".split(":"))); //[-2]
out.println(asList("2:".split(":"))); //[2]
out.println(asList(":2".split(":")).get(0).equals("")); //true
String filter = c.toString();
String parsed = parse("[?(@['a'] == 1 || @['b'] == 2)]").toString();
assertThat(filter).isEqualTo(parsed);
}
}

12
json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

@ -765,4 +765,16 @@ public class IssuesTest extends BaseTest {
}
};
}
@Test
public void issue_131() {
String json = "[1, 2, {\"d\": { \"random\":null, \"date\": \"1234\"} , \"l\": \"filler\"}]";
//String json = "[1, 2, {\"d\": { \"random\":1, \"date\": \"1234\"} , \"l\": \"filler\"}]";
Object read = JsonPath.read(json, "$[2]['d'][?(@.random)]['date']");
System.out.println(read);
}
}

Loading…
Cancel
Save