Browse Source

Made Filter more functional. Now it's only referred as Predicate.

pull/46/head
Kalle Stenflo 10 years ago
parent
commit
8b918b2238
  1. 28
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  2. 18
      json-path/src/main/java/com/jayway/jsonpath/Filter.java
  3. 21
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  4. 10
      json-path/src/main/java/com/jayway/jsonpath/Predicate.java
  5. 2
      json-path/src/main/java/com/jayway/jsonpath/ReadContext.java
  6. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  7. 15
      json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java
  8. 26
      json-path/src/main/java/com/jayway/jsonpath/internal/compiler/FilterPathToken.java
  9. 14
      json-path/src/test/java/com/jayway/jsonpath/BaseTest.java
  10. 216
      json-path/src/test/java/com/jayway/jsonpath/FilterTest.java
  11. 7
      json-path/src/test/java/com/jayway/jsonpath/ReturnTypeTest.java
  12. 162
      json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java

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

@ -192,9 +192,19 @@ public class Criteria implements Predicate {
},
MATCHES {
@Override
boolean eval(Object expected, Object actual, Configuration configuration) {
boolean eval(Object expected, final Object actual, final Configuration configuration) {
Predicate exp = (Predicate) expected;
return exp.apply(actual, configuration);
return exp.apply(new PredicateContext() {
@Override
public Object target() {
return actual;
}
@Override
public Configuration configuration() {
return configuration;
}
});
}
},
NOT_EMPTY {
@ -258,27 +268,27 @@ public class Criteria implements Predicate {
@Override
public boolean apply(Object model, Configuration configuration) {
public boolean apply(PredicateContext ctx) {
for (Criteria criteria : criteriaChain) {
if (!criteria.eval(model, configuration)) {
if (!criteria.eval(ctx)) {
return false;
}
}
return true;
}
private boolean eval(Object model, Configuration configuration) {
private boolean eval(PredicateContext ctx) {
if (CriteriaType.EXISTS == criteriaType) {
boolean exists = ((Boolean) expected);
try {
//path.evaluate(model, configuration.options(Option.THROW_ON_MISSING_PROPERTY)).getValue();
Configuration c = configuration;
Configuration c = ctx.configuration();
if(c.containsOption(Option.ALWAYS_RETURN_LIST) || c.containsOption(Option.SUPPRESS_EXCEPTIONS)){
c = c.options();
}
path.evaluate(model, c).getValue();
path.evaluate(ctx.target(), c).getValue();
return exists;
} catch (PathNotFoundException e) {
return !exists;
@ -286,8 +296,8 @@ public class Criteria implements Predicate {
} else {
try {
final Object actual = path.evaluate(model, configuration).getValue();
return criteriaType.eval(expected, actual, configuration);
final Object actual = path.evaluate(ctx.target(), ctx.configuration()).getValue();
return criteriaType.eval(expected, actual, ctx.configuration());
} catch (CompareException e) {
return false;
} catch (PathNotFoundException e) {

18
json-path/src/main/java/com/jayway/jsonpath/Filter.java

@ -8,28 +8,28 @@ import java.util.List;
*/
public class Filter implements Predicate {
private List<Criteria> criteriaList = new ArrayList<Criteria>();
private List<Predicate> criteriaList = new ArrayList<Predicate>();
private Filter(Criteria criteria) {
private Filter(Predicate criteria) {
this.criteriaList.add(criteria);
}
private Filter(List<Criteria> criteriaList) {
private Filter(List<Predicate> criteriaList) {
this.criteriaList = criteriaList;
}
public static Filter filter(Criteria criteria) {
public static Filter filter(Predicate criteria) {
return new Filter(criteria);
}
public static Filter filter(List<Criteria> criteriaList) {
public static Filter filter(List<Predicate> criteriaList) {
return new Filter(criteriaList);
}
@Override
public boolean apply(Object target, Configuration configuration) {
for (Criteria criteria : criteriaList) {
if (!criteria.apply(target, configuration)) {
public boolean apply(PredicateContext ctx) {
for (Predicate criteria : criteriaList) {
if (!criteria.apply(ctx)) {
return false;
}
}
@ -39,7 +39,7 @@ public class Filter implements Predicate {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Criteria crit : criteriaList) {
for (Predicate crit : criteriaList) {
sb.append(crit.toString());
}
return sb.toString();

21
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -94,7 +94,7 @@ public class JsonPath {
private final Path path;
private JsonPath(String jsonPath, Filter[] filters) {
private JsonPath(String jsonPath, Predicate[] filters) {
notNull(jsonPath, "path can not be null");
this.path = PathCompiler.compile(jsonPath, filters);
}
@ -359,7 +359,7 @@ public class JsonPath {
* @param filters filters to be applied to the filter place holders [?] in the path
* @return compiled JsonPath
*/
public static JsonPath compile(String jsonPath, Filter... filters) {
public static JsonPath compile(String jsonPath, Predicate... filters) {
notEmpty(jsonPath, "json can not be null or empty");
return new JsonPath(jsonPath, filters);
@ -382,7 +382,7 @@ public class JsonPath {
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(Object json, String jsonPath, Filter... filters) {
public static <T> T read(Object json, String jsonPath, Predicate... filters) {
//return compile(jsonPath, filters).read(json);
return new JsonReader().parse(json).read(jsonPath, filters);
}
@ -397,17 +397,10 @@ public class JsonPath {
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(String json, String jsonPath, Filter... filters) {
public static <T> T read(String json, String jsonPath, Predicate... filters) {
return new JsonReader().parse(json).read(jsonPath, filters);
}
//FIXME : remove this or not
public static <T> T read(String json, String jsonPath, Class<T> clazz, Filter... filters) {
Object res = new JsonReader().parse(json).read(jsonPath, filters);
return clazz.cast(res);
}
/**
* Creates a new JsonPath and applies it to the provided Json object
@ -419,7 +412,7 @@ public class JsonPath {
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(URL jsonURL, String jsonPath, Filter... filters) throws IOException {
public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException {
return new JsonReader().parse(jsonURL).read(jsonPath, filters);
}
@ -433,7 +426,7 @@ public class JsonPath {
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(File jsonFile, String jsonPath, Filter... filters) throws IOException {
public static <T> T read(File jsonFile, String jsonPath, Predicate... filters) throws IOException {
return new JsonReader().parse(jsonFile).read(jsonPath, filters);
}
@ -447,7 +440,7 @@ public class JsonPath {
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(InputStream jsonInputStream, String jsonPath, Filter... filters) throws IOException {
public static <T> T read(InputStream jsonInputStream, String jsonPath, Predicate... filters) throws IOException {
return new JsonReader().parse(jsonInputStream).read(jsonPath, filters);
}

10
json-path/src/main/java/com/jayway/jsonpath/Predicate.java

@ -5,5 +5,13 @@ package com.jayway.jsonpath;
*/
public interface Predicate {
boolean apply(Object target, Configuration configuration);
boolean apply(PredicateContext ctx);
public interface PredicateContext {
Object target();
Configuration configuration();
}
}

2
json-path/src/main/java/com/jayway/jsonpath/ReadContext.java

@ -31,7 +31,7 @@ public interface ReadContext {
* @param <T>
* @return result
*/
<T> T read(String path, Filter... filters);
<T> T read(String path, Predicate... filters);
/**
* Reads the given path from this context

4
json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

@ -1,9 +1,9 @@
package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.ReadContext;
import com.jayway.jsonpath.spi.http.HttpProviderFactory;
@ -87,7 +87,7 @@ public class JsonReader implements ParseContext, ReadContext {
}
@Override
public <T> T read(String path, Filter... filters) {
public <T> T read(String path, Predicate... filters) {
notEmpty(path, "path can not be null or empty");
return read(JsonPath.compile(path, filters));
}

15
json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java

@ -3,6 +3,7 @@ package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.compiler.ArrayPathToken;
import com.jayway.jsonpath.internal.compiler.FilterPathToken;
import com.jayway.jsonpath.internal.compiler.PathToken;
@ -37,11 +38,11 @@ public class PathCompiler {
public static Path compile(String path, Filter... filters) {
public static Path compile(String path, Predicate... filters) {
notEmpty(path, "Path may not be null empty");
path = path.trim();
LinkedList<Filter> filterList = new LinkedList<Filter>(asList(filters));
LinkedList<Predicate> filterList = new LinkedList<Predicate>(asList(filters));
if (!path.startsWith("$")) {
path = "$." + path;
@ -188,15 +189,15 @@ public class PathCompiler {
private int i;
private char current;
private final LinkedList<Filter> filterList;
private final LinkedList<Predicate> filterList;
private final String pathFragment;
PathComponentAnalyzer(String pathFragment, LinkedList<Filter> filterList) {
PathComponentAnalyzer(String pathFragment, LinkedList<Predicate> filterList) {
this.pathFragment = pathFragment;
this.filterList = filterList;
}
static PathToken analyze(String pathFragment, LinkedList<Filter> filterList) {
static PathToken analyze(String pathFragment, LinkedList<Predicate> filterList) {
return new PathComponentAnalyzer(pathFragment, filterList).analyze();
}
@ -210,7 +211,7 @@ public class PathCompiler {
else if (FILTER_PATTERN.matcher(pathFragment).matches()) {
final int criteriaCount = Utils.countMatches(pathFragment, "?");
List<Filter> filters = new ArrayList<Filter>(criteriaCount);
List<Predicate> filters = new ArrayList<Predicate>(criteriaCount);
for (int i = 0; i < criteriaCount; i++) {
filters.add(filterList.poll());
}
@ -250,7 +251,7 @@ public class PathCompiler {
StringBuilder pathBuffer = new StringBuilder();
StringBuilder operatorBuffer = new StringBuilder();
StringBuilder valueBuffer = new StringBuilder();
List<Criteria> criteria = new ArrayList<Criteria>();
List<Predicate> criteria = new ArrayList<Predicate>();
int bracketCount = 0;

26
json-path/src/main/java/com/jayway/jsonpath/internal/compiler/FilterPathToken.java

@ -1,8 +1,8 @@
package com.jayway.jsonpath.internal.compiler;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.Predicate;
import java.util.Collection;
@ -22,13 +22,13 @@ public class FilterPathToken extends PathToken {
"[?,?,?,?,?]"
};
private final Collection<Filter> filters;
private final Collection<Predicate> filters;
public FilterPathToken(Filter filter) {
public FilterPathToken(Predicate filter) {
this.filters = asList(filter);
}
public FilterPathToken(Collection<Filter> filters) {
public FilterPathToken(Collection<Predicate> filters) {
this.filters = filters;
}
@ -48,11 +48,23 @@ public class FilterPathToken extends PathToken {
}
}
public boolean accept(Object obj, Configuration configuration) {
public boolean accept(final Object obj, final Configuration configuration) {
boolean accept = true;
for (Filter filter : filters) {
if (!filter.apply (obj, configuration)) {
Predicate.PredicateContext ctx = new Predicate.PredicateContext() {
@Override
public Object target() {
return obj;
}
@Override
public Configuration configuration() {
return configuration;
}
};
for (Predicate filter : filters) {
if (!filter.apply (ctx)) {
accept = false;
break;
}

14
json-path/src/test/java/com/jayway/jsonpath/BaseTest.java

@ -49,4 +49,18 @@ public class BaseTest {
" \"foo\" : \"bar\",\n" +
" \"@id\" : \"ID\"\n" +
"}";
public Predicate.PredicateContext createPredicateContext(final Object check) {
return new Predicate.PredicateContext() {
@Override
public Object target() {
return check;
}
@Override
public Configuration configuration() {
return Configuration.defaultConfiguration();
}
};
}
}

216
json-path/src/test/java/com/jayway/jsonpath/old/Filter2Test.java → json-path/src/test/java/com/jayway/jsonpath/FilterTest.java

@ -1,7 +1,5 @@
package com.jayway.jsonpath.old;
package com.jayway.jsonpath;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Predicate;
import org.junit.Test;
import java.util.List;
@ -12,7 +10,7 @@ import static com.jayway.jsonpath.Criteria.where;
import static com.jayway.jsonpath.Filter.filter;
import static org.assertj.core.api.Assertions.assertThat;
public class Filter2Test {
public class FilterTest extends BaseTest {
Configuration conf = Configuration.defaultConfiguration();
Object json = conf.getProvider().parse(
@ -38,40 +36,40 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void int_eq_evals() {
assertThat(filter(where("int-key").eq(1)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-key").eq(666)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-key").eq(1)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("int-key").eq(666)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void long_eq_evals() {
assertThat(filter(where("long-key").eq(3000000000L)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("long-key").eq(666L)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("long-key").eq(3000000000L)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("long-key").eq(666L)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void double_eq_evals() {
assertThat(filter(where("double-key").eq(10.1D)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("double-key").eq(10.10D)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("double-key").eq(10.11D)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("double-key").eq(10.1D)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("double-key").eq(10.10D)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("double-key").eq(10.11D)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void string_eq_evals() {
assertThat(filter(where("string-key").eq("string")).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").eq("666")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").eq("string")).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-key").eq("666")).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void boolean_eq_evals() {
assertThat(filter(where("boolean-key").eq(true)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("boolean-key").eq(false)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("boolean-key").eq(true)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("boolean-key").eq(false)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void null_eq_evals() {
assertThat(filter(where("null-key").eq(null)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("null-key").eq("666")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").eq(null)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("null-key").eq(null)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("null-key").eq("666")).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("string-key").eq(null)).apply(createPredicateContext(json))).isEqualTo(false);
}
//----------------------------------------------------------------------------
@ -81,40 +79,40 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void int_ne_evals() {
assertThat(filter(where("int-key").ne(1)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-key").ne(666)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-key").ne(1)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("int-key").ne(666)).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void long_ne_evals() {
assertThat(filter(where("long-key").ne(3000000000L)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("long-key").ne(666L)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("long-key").ne(3000000000L)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("long-key").ne(666L)).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void double_ne_evals() {
assertThat(filter(where("double-key").ne(10.1D)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("double-key").ne(10.10D)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("double-key").ne(10.11D)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("double-key").ne(10.1D)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("double-key").ne(10.10D)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("double-key").ne(10.11D)).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void string_ne_evals() {
assertThat(filter(where("string-key").ne("string")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").ne("666")).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").ne("string")).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("string-key").ne("666")).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void boolean_ne_evals() {
assertThat(filter(where("boolean-key").ne(true)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("boolean-key").ne(false)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("boolean-key").ne(true)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("boolean-key").ne(false)).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void null_ne_evals() {
assertThat(filter(where("null-key").ne(null)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("null-key").ne("666")).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").ne(null)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("null-key").ne(null)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("null-key").ne("666")).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-key").ne(null)).apply(createPredicateContext(json))).isEqualTo(true);
}
//----------------------------------------------------------------------------
@ -124,26 +122,26 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void int_lt_evals() {
assertThat(filter(where("int-key").lt(10)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-key").lt(0)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-key").lt(10)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("int-key").lt(0)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void long_lt_evals() {
assertThat(filter(where("long-key").lt(4000000000L)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("long-key").lt(666L)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("long-key").lt(4000000000L)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("long-key").lt(666L)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void double_lt_evals() {
assertThat(filter(where("double-key").lt(100.1D)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("double-key").lt(1.1D)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("double-key").lt(100.1D)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("double-key").lt(1.1D)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void string_lt_evals() {
assertThat(filter(where("char-key").lt("x")).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("char-key").lt("a")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("char-key").lt("x")).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("char-key").lt("a")).apply(createPredicateContext(json))).isEqualTo(false);
}
//----------------------------------------------------------------------------
@ -153,23 +151,23 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void int_lte_evals() {
assertThat(filter(where("int-key").lte(10)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-key").lte(1)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-key").lte(0)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-key").lte(10)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("int-key").lte(1)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("int-key").lte(0)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void long_lte_evals() {
assertThat(filter(where("long-key").lte(4000000000L)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("long-key").lte(3000000000L)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("long-key").lte(666L)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("long-key").lte(4000000000L)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("long-key").lte(3000000000L)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("long-key").lte(666L)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void double_lte_evals() {
assertThat(filter(where("double-key").lte(100.1D)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("double-key").lte(10.1D)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("double-key").lte(1.1D)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("double-key").lte(100.1D)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("double-key").lte(10.1D)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("double-key").lte(1.1D)).apply(createPredicateContext(json))).isEqualTo(false);
}
//----------------------------------------------------------------------------
@ -179,26 +177,26 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void int_gt_evals() {
assertThat(filter(where("int-key").gt(10)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-key").gt(0)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-key").gt(10)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("int-key").gt(0)).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void long_gt_evals() {
assertThat(filter(where("long-key").gt(4000000000L)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("long-key").gt(666L)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("long-key").gt(4000000000L)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("long-key").gt(666L)).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void double_gt_evals() {
assertThat(filter(where("double-key").gt(100.1D)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("double-key").gt(1.1D)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("double-key").gt(100.1D)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("double-key").gt(1.1D)).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void string_gt_evals() {
assertThat(filter(where("char-key").gt("x")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("char-key").gt("a")).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("char-key").gt("x")).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("char-key").gt("a")).apply(createPredicateContext(json))).isEqualTo(true);
}
//----------------------------------------------------------------------------
@ -208,23 +206,23 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void int_gte_evals() {
assertThat(filter(where("int-key").gte(10)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-key").gte(1)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-key").gte(0)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-key").gte(10)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("int-key").gte(1)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("int-key").gte(0)).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void long_gte_evals() {
assertThat(filter(where("long-key").gte(4000000000L)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("long-key").gte(3000000000L)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("long-key").gte(666L)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("long-key").gte(4000000000L)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("long-key").gte(3000000000L)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("long-key").gte(666L)).apply(createPredicateContext(json))).isEqualTo(true);
}
@Test
public void double_gte_evals() {
assertThat(filter(where("double-key").gte(100.1D)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("double-key").gte(10.1D)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("double-key").gte(1.1D)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("double-key").gte(100.1D)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("double-key").gte(10.1D)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("double-key").gte(1.1D)).apply(createPredicateContext(json))).isEqualTo(true);
}
//----------------------------------------------------------------------------
@ -234,10 +232,10 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void string_regex_evals() {
assertThat(filter(where("string-key").regex(Pattern.compile("^string$"))).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").regex(Pattern.compile("^tring$"))).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("null-key").regex(Pattern.compile("^string$"))).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-key").regex(Pattern.compile("^string$"))).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").regex(Pattern.compile("^string$"))).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-key").regex(Pattern.compile("^tring$"))).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("null-key").regex(Pattern.compile("^string$"))).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("int-key").regex(Pattern.compile("^string$"))).apply(createPredicateContext(json))).isEqualTo(false);
}
//----------------------------------------------------------------------------
@ -247,11 +245,11 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void string_in_evals() {
assertThat(filter(where("string-key").in("a", null, "string")).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").in("a", null)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("null-key").in("a", null)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("null-key").in("a", "b")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-arr").in("a")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").in("a", null, "string")).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-key").in("a", null)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("null-key").in("a", null)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("null-key").in("a", "b")).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("string-arr").in("a")).apply(createPredicateContext(json))).isEqualTo(false);
}
//----------------------------------------------------------------------------
@ -261,11 +259,11 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void string_nin_evals() {
assertThat(filter(where("string-key").nin("a", null, "string")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").nin("a", null)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("null-key").nin("a", null)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("null-key").nin("a", "b")).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-arr").nin("a")).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").nin("a", null, "string")).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("string-key").nin("a", null)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("null-key").nin("a", null)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("null-key").nin("a", "b")).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-arr").nin("a")).apply(createPredicateContext(json))).isEqualTo(true);
}
@ -276,17 +274,17 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void int_all_evals() {
assertThat(filter(where("int-arr").all(0,1)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-arr").all(0,7)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-arr").all(0,1)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("int-arr").all(0,7)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void string_all_evals() {
assertThat(filter(where("string-arr").all("a","b")).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-arr").all("a","x")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-arr").all("a","b")).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-arr").all("a","x")).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void not_array_all_evals() {
assertThat(filter(where("string-key").all("a","b")).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").all("a","b")).apply(createPredicateContext(json))).isEqualTo(false);
}
//----------------------------------------------------------------------------
@ -296,24 +294,24 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void array_size_evals() {
assertThat(filter(where("string-arr").size(5)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-arr").size(7)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-arr").size(5)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-arr").size(7)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void string_size_evals() {
assertThat(filter(where("string-key").size(6)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").size(7)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").size(6)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-key").size(7)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void other_size_evals() {
assertThat(filter(where("int-key").size(6)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-key").size(6)).apply(createPredicateContext(json))).isEqualTo(false);
}
@Test
public void null_size_evals() {
assertThat(filter(where("null-key").size(6)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("null-key").size(6)).apply(createPredicateContext(json))).isEqualTo(false);
}
//----------------------------------------------------------------------------
@ -323,11 +321,11 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void exists_evals() {
assertThat(filter(where("string-key").exists(true)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").exists(false)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").exists(true)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-key").exists(false)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("missing-key").exists(true)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("missing-key").exists(false)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("missing-key").exists(true)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("missing-key").exists(false)).apply(createPredicateContext(json))).isEqualTo(true);
}
//----------------------------------------------------------------------------
@ -337,15 +335,15 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void type_evals() {
assertThat(filter(where("string-key").type(String.class)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").type(Integer.class)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").type(String.class)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-key").type(Integer.class)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("int-key").type(String.class)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-key").type(Integer.class)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-key").type(String.class)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("int-key").type(Integer.class)).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("null-key").type(String.class)).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("null-key").type(String.class)).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("int-arr").type(List.class)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("int-arr").type(List.class)).apply(createPredicateContext(json))).isEqualTo(true);
}
//----------------------------------------------------------------------------
@ -355,13 +353,13 @@ public class Filter2Test {
//----------------------------------------------------------------------------
@Test
public void not_empty_evals() {
assertThat(filter(where("string-key").notEmpty()).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key-empty").notEmpty()).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("string-key").notEmpty()).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("string-key-empty").notEmpty()).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("int-arr").notEmpty()).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("arr-empty").notEmpty()).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("int-arr").notEmpty()).apply(createPredicateContext(json))).isEqualTo(true);
assertThat(filter(where("arr-empty").notEmpty()).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("null-key").notEmpty()).apply(json, conf)).isEqualTo(false);
assertThat(filter(where("null-key").notEmpty()).apply(createPredicateContext(json))).isEqualTo(false);
}
@ -375,8 +373,8 @@ public class Filter2Test {
public void matches_evals() {
Predicate p = new Predicate() {
@Override
public boolean apply(Object target, Configuration configuration) {
Map<String, Object> t = (Map<String, Object>) target;
public boolean apply(PredicateContext ctx) {
Map<String, Object> t = (Map<String, Object>) ctx.target();
Object o = t.get("int-key");
@ -385,6 +383,6 @@ public class Filter2Test {
return i == 1;
}
};
assertThat(filter(where("string-key").eq("string").and("$").matches(p)).apply(json, conf)).isEqualTo(true);
assertThat(filter(where("string-key").eq("string").and("$").matches(p)).apply(createPredicateContext(json))).isEqualTo(true);
}
}

7
json-path/src/test/java/com/jayway/jsonpath/ReturnTypeTest.java

@ -1,11 +1,8 @@
package com.jayway.jsonpath;
import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
@ -46,10 +43,12 @@ public class ReturnTypeTest extends BaseTest {
@Test
public void assert_maps_can_be_read() {
assertThat(JsonPath.read(JSON_DOCUMENT, "$.store.book[0]", Map.class))
/*
assertThat(JsonPath.parse(JSON_DOCUMENT).read("$.store.book[0]", Map.class)
.containsEntry("category", "reference")
.containsEntry("author", "Nigel Rees")
.containsEntry("title", "Sayings of the Century")
.containsEntry("display-price", 8.95D);
*/
}
}

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

@ -1,5 +1,6 @@
package com.jayway.jsonpath.old;
import com.jayway.jsonpath.BaseTest;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.InvalidCriteriaException;
@ -21,7 +22,7 @@ import static junit.framework.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class FilterTest {
public class FilterTest extends BaseTest {
public final static String DOCUMENT =
"{ \"store\": {\n" +
@ -68,38 +69,40 @@ public class FilterTest {
//-------------------------------------------------
@Test
public void is_filters_evaluates() throws Exception {
Map<String, Object> check = new HashMap<String, Object>();
final Map<String, Object> check = new HashMap<String, Object>();
check.put("foo", "foo");
check.put("bar", null);
assertTrue(filter(where("bar").is(null)).apply(check, conf));
assertTrue(filter(where("foo").is("foo")).apply(check, conf));
assertFalse(filter(where("foo").is("xxx")).apply(check, conf));
assertFalse(filter(where("bar").is("xxx")).apply(check, conf));
assertTrue(filter(where("bar").is(null)).apply(createPredicateContext(check)));
assertTrue(filter(where("foo").is("foo")).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").is("xxx")).apply(createPredicateContext(check)));
assertFalse(filter(where("bar").is("xxx")).apply(createPredicateContext(check)));
}
@Test
public void ne_filters_evaluates() throws Exception {
Map<String, Object> check = new HashMap<String, Object>();
final Map<String, Object> check = new HashMap<String, Object>();
check.put("foo", "foo");
check.put("bar", null);
assertTrue(filter(where("foo").ne(null)).apply(check, conf));
assertTrue(filter(where("foo").ne("not foo")).apply(check, conf));
assertFalse(filter(where("foo").ne("foo")).apply(check, conf));
assertFalse(filter(where("bar").ne(null)).apply(check, conf));
assertTrue(filter(where("foo").ne(null)).apply(createPredicateContext(check)));
assertTrue(filter(where("foo").ne("not foo")).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").ne("foo")).apply(createPredicateContext(check)));
assertFalse(filter(where("bar").ne(null)).apply(createPredicateContext(check)));
}
@Test
public void gt_filters_evaluates() throws Exception {
Map<String, Object> check = new HashMap<String, Object>();
final Map<String, Object> check = new HashMap<String, Object>();
check.put("foo", 12.5D);
check.put("foo_null", null);
assertTrue(filter(where("foo").gt(12D)).apply(check, conf));
assertFalse(filter(where("foo").gt(null)).apply(check, conf));
assertFalse(filter(where("foo").gt(20D)).apply(check, conf));
assertFalse(filter(where("foo_null").gt(20D)).apply(check, conf));
assertTrue(filter(where("foo").gt(12D)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").gt(null)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").gt(20D)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo_null").gt(20D)).apply(createPredicateContext(check)));
}
@Test
@ -108,11 +111,11 @@ public class FilterTest {
check.put("foo", 12.5D);
check.put("foo_null", null);
assertTrue(filter(where("foo").gte(12D)).apply(check, conf));
assertTrue(filter(where("foo").gte(12.5D)).apply(check, conf));
assertFalse(filter(where("foo").gte(null)).apply(check, conf));
assertFalse(filter(where("foo").gte(20D)).apply(check, conf));
assertFalse(filter(where("foo_null").gte(20D)).apply(check, conf));
assertTrue(filter(where("foo").gte(12D)).apply(createPredicateContext(check)));
assertTrue(filter(where("foo").gte(12.5D)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").gte(null)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").gte(20D)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo_null").gte(20D)).apply(createPredicateContext(check)));
}
@Test
@ -121,10 +124,10 @@ public class FilterTest {
check.put("foo", 10.5D);
check.put("foo_null", null);
//assertTrue(filter(where("foo").lt(12D)).apply(check, conf));
assertFalse(filter(where("foo").lt(null)).apply(check, conf));
//assertFalse(filter(where("foo").lt(5D)).apply(check, conf));
//assertFalse(filter(where("foo_null").lt(5D)).apply(check, conf));
//assertTrue(filter(where("foo").lt(12D)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").lt(null)).apply(createPredicateContext(check)));
//assertFalse(filter(where("foo").lt(5D)).apply(createPredicateContext(check)));
//assertFalse(filter(where("foo_null").lt(5D)).apply(createPredicateContext(check)));
}
@Test
@ -133,10 +136,10 @@ public class FilterTest {
check.put("foo", 12.5D);
check.put("foo_null", null);
assertTrue(filter(where("foo").lte(13D)).apply(check, conf));
assertFalse(filter(where("foo").lte(null)).apply(check, conf));
assertFalse(filter(where("foo").lte(5D)).apply(check, conf));
assertFalse(filter(where("foo_null").lte(5D)).apply(check, conf));
assertTrue(filter(where("foo").lte(13D)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").lte(null)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").lte(5D)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo_null").lte(5D)).apply(createPredicateContext(check)));
}
@Test
@ -145,15 +148,15 @@ public class FilterTest {
check.put("item", 3);
check.put("null_item", null);
assertTrue(filter(where("item").in(1, 2, 3)).apply(check, conf));
assertTrue(filter(where("item").in(asList(1, 2, 3))).apply(check, conf));
assertFalse(filter(where("item").in(4, 5, 6)).apply(check, conf));
assertFalse(filter(where("item").in(asList(4, 5, 6))).apply(check, conf));
assertFalse(filter(where("item").in(asList('A'))).apply(check, conf));
assertFalse(filter(where("item").in(asList((Object) null))).apply(check, conf));
assertTrue(filter(where("item").in(1, 2, 3)).apply(createPredicateContext(check)));
assertTrue(filter(where("item").in(asList(1, 2, 3))).apply(createPredicateContext(check)));
assertFalse(filter(where("item").in(4, 5, 6)).apply(createPredicateContext(check)));
assertFalse(filter(where("item").in(asList(4, 5, 6))).apply(createPredicateContext(check)));
assertFalse(filter(where("item").in(asList('A'))).apply(createPredicateContext(check)));
assertFalse(filter(where("item").in(asList((Object) null))).apply(createPredicateContext(check)));
assertTrue(filter(where("null_item").in((Object) null)).apply(check, conf));
assertFalse(filter(where("null_item").in(1, 2, 3)).apply(check, conf));
assertTrue(filter(where("null_item").in((Object) null)).apply(createPredicateContext(check)));
assertFalse(filter(where("null_item").in(1, 2, 3)).apply(createPredicateContext(check)));
}
@Test
@ -162,14 +165,14 @@ public class FilterTest {
check.put("item", 3);
check.put("null_item", null);
assertTrue(filter(where("item").nin(4, 5)).apply(check, conf));
assertTrue(filter(where("item").nin(asList(4, 5))).apply(check, conf));
assertTrue(filter(where("item").nin(asList('A'))).apply(check, conf));
assertTrue(filter(where("null_item").nin(1, 2, 3)).apply(check, conf));
assertTrue(filter(where("item").nin(asList((Object) null))).apply(check, conf));
assertTrue(filter(where("item").nin(4, 5)).apply(createPredicateContext(check)));
assertTrue(filter(where("item").nin(asList(4, 5))).apply(createPredicateContext(check)));
assertTrue(filter(where("item").nin(asList('A'))).apply(createPredicateContext(check)));
assertTrue(filter(where("null_item").nin(1, 2, 3)).apply(createPredicateContext(check)));
assertTrue(filter(where("item").nin(asList((Object) null))).apply(createPredicateContext(check)));
assertFalse(filter(where("item").nin(3)).apply(check, conf));
assertFalse(filter(where("item").nin(asList(3))).apply(check, conf));
assertFalse(filter(where("item").nin(3)).apply(createPredicateContext(check)));
assertFalse(filter(where("item").nin(asList(3))).apply(createPredicateContext(check)));
}
@Test
@ -177,8 +180,8 @@ public class FilterTest {
Map<String, Object> check = new HashMap<String, Object>();
check.put("items", asList(1, 2, 3));
assertTrue(filter(where("items").all(1, 2, 3)).apply(check, conf));
assertFalse(filter(where("items").all(1, 2, 3, 4)).apply(check, conf));
assertTrue(filter(where("items").all(1, 2, 3)).apply(createPredicateContext(check)));
assertFalse(filter(where("items").all(1, 2, 3, 4)).apply(createPredicateContext(check)));
}
@Test
@ -187,9 +190,9 @@ public class FilterTest {
check.put("items", asList(1, 2, 3));
check.put("items_empty", Collections.emptyList());
assertTrue(filter(where("items").size(3)).apply(check, conf));
assertTrue(filter(where("items_empty").size(0)).apply(check, conf));
assertFalse(filter(where("items").size(2)).apply(check, conf));
assertTrue(filter(where("items").size(3)).apply(createPredicateContext(check)));
assertTrue(filter(where("items_empty").size(0)).apply(createPredicateContext(check)));
assertFalse(filter(where("items").size(2)).apply(createPredicateContext(check)));
}
@Test
@ -199,14 +202,14 @@ public class FilterTest {
check.put("foo", "foo");
check.put("foo_null", null);
assertTrue(filter(where("foo").exists(true)).apply(check, conf));
assertFalse(filter(where("foo").exists(false)).apply(check, conf));
assertTrue(filter(where("foo").exists(true)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").exists(false)).apply(createPredicateContext(check)));
assertTrue(filter(where("foo_null").exists(true)).apply(check, conf));
assertFalse(filter(where("foo_null").exists(false)).apply(check, conf));
assertTrue(filter(where("foo_null").exists(true)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo_null").exists(false)).apply(createPredicateContext(check)));
assertTrue(filter(where("bar").exists(false)).apply(check, conf));
assertFalse(filter(where("bar").exists(true)).apply(check, conf));
assertTrue(filter(where("bar").exists(false)).apply(createPredicateContext(check)));
assertFalse(filter(where("bar").exists(true)).apply(createPredicateContext(check)));
}
@Test
@ -218,18 +221,18 @@ public class FilterTest {
check.put("long", 1L);
check.put("double", 1.12D);
assertFalse(filter(where("string_null").type(String.class)).apply(check, conf));
assertTrue(filter(where("string").type(String.class)).apply(check, conf));
assertFalse(filter(where("string").type(Number.class)).apply(check, conf));
assertFalse(filter(where("string_null").type(String.class)).apply(createPredicateContext(check)));
assertTrue(filter(where("string").type(String.class)).apply(createPredicateContext(check)));
assertFalse(filter(where("string").type(Number.class)).apply(createPredicateContext(check)));
assertTrue(filter(where("int").type(Integer.class)).apply(check, conf));
assertFalse(filter(where("int").type(Long.class)).apply(check, conf));
assertTrue(filter(where("int").type(Integer.class)).apply(createPredicateContext(check)));
assertFalse(filter(where("int").type(Long.class)).apply(createPredicateContext(check)));
assertTrue(filter(where("long").type(Long.class)).apply(check, conf));
assertFalse(filter(where("long").type(Integer.class)).apply(check, conf));
assertTrue(filter(where("long").type(Long.class)).apply(createPredicateContext(check)));
assertFalse(filter(where("long").type(Integer.class)).apply(createPredicateContext(check)));
assertTrue(filter(where("double").type(Double.class)).apply(check, conf));
assertFalse(filter(where("double").type(Integer.class)).apply(check, conf));
assertTrue(filter(where("double").type(Double.class)).apply(createPredicateContext(check)));
assertFalse(filter(where("double").type(Integer.class)).apply(createPredicateContext(check)));
}
@Test
@ -238,10 +241,10 @@ public class FilterTest {
check.put("name", "kalle");
check.put("name_null", null);
assertFalse(filter(where("name_null").regex(Pattern.compile(".alle"))).apply(check, conf));
assertTrue(filter(where("name").regex(Pattern.compile(".alle"))).apply(check, conf));
assertFalse(filter(where("name").regex(Pattern.compile("KALLE"))).apply(check, conf));
assertTrue(filter(where("name").regex(Pattern.compile("KALLE", Pattern.CASE_INSENSITIVE))).apply(check, conf));
assertFalse(filter(where("name_null").regex(Pattern.compile(".alle"))).apply(createPredicateContext(check)));
assertTrue(filter(where("name").regex(Pattern.compile(".alle"))).apply(createPredicateContext(check)));
assertFalse(filter(where("name").regex(Pattern.compile("KALLE"))).apply(createPredicateContext(check)));
assertTrue(filter(where("name").regex(Pattern.compile("KALLE", Pattern.CASE_INSENSITIVE))).apply(createPredicateContext(check)));
}
@ -308,10 +311,11 @@ public class FilterTest {
Filter shouldMarch = filter(where("string").is("foo").and("int").lt(11));
Filter shouldNotMarch = filter(where("string").is("foo").and("int").gt(11));
assertTrue(shouldMarch.apply(check, conf));
assertFalse(shouldNotMarch.apply(check, conf));
assertTrue(shouldMarch.apply(createPredicateContext(check)));
assertFalse(shouldNotMarch.apply(createPredicateContext(check)));
}
/*
@Test
public void filters_can_be_extended_with_new_criteria() throws Exception {
@ -325,11 +329,11 @@ public class FilterTest {
Filter filter = filter(where("string").is("foo").and("int").lt(11));
assertTrue(filter.apply(check, conf));
assertTrue(filter.apply(createPredicateContext(check)));
filter.addCriteria(where("long").ne(1L));
assertFalse(filter.apply(check, conf));
assertFalse(filter.apply(createPredicateContext(check)));
}
*/
@ -347,21 +351,21 @@ public class FilterTest {
Filter filter = filter(where("string").is("foo"));
assertTrue(filter.apply(check, conf));
assertTrue(filter.apply(createPredicateContext(check)));
Criteria criteria = where("string").is("not eq");
filter.addCriteria(criteria);
assertFalse(filter.apply(check, conf));
assertFalse(filter.apply(createPredicateContext(check)));
filter = filter(where("string").is("foo").and("string").is("not eq"));
assertFalse(filter.apply(check, conf));
assertFalse(filter.apply(createPredicateContext(check)));
filter = filter(where("string").is("foo").and("string").is("foo"));
assertTrue(filter.apply(check, conf));
assertTrue(filter.apply(createPredicateContext(check)));
}
*/
@ -399,7 +403,7 @@ public class FilterTest {
Filter customFilter = new Filter.FilterAdapter<Map<String, Object>>() {
@Override
public boolean apply(check, confMap<String, Object> map) {
public boolean apply(createPredicateContext(check)Map<String, Object> map) {
if (map.getValue("name").equals("rootGrandChild_A")) {
return true;
}
@ -428,7 +432,7 @@ public class FilterTest {
Filter customFilter = new Filter.FilterAdapter() {
@Override
public boolean apply(check, confObject o, Configuration configuration) {
public boolean apply(createPredicateContext(check)Object o, Configuration configuration) {
return 1 == (Integer) o;
}
};
@ -445,7 +449,7 @@ public class FilterTest {
Object doc = JsonProviderFactory.createProvider().parse(DOCUMENT);
assertFalse(filter(where("$.store.bicycle.color").ne("red")).apply(doc, conf));
assertFalse(filter(where("$.store.bicycle.color").ne("red")).apply(createPredicateContext(doc)));
/*
assertFalse(filter(where("store..price").gt(10)).apply(doc, conf));
assertFalse(filter(where("$.store..price").gte(100)).apply(doc, conf));

Loading…
Cancel
Save