Browse Source

Added internal logging support.

pull/32/head
Kalle Stenflo 11 years ago
parent
commit
77e18924f9
  1. 10
      json-path-assert/pom.xml
  2. 29
      json-path/pom.xml
  3. 34
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  4. 111
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  5. 50
      json-path/src/main/java/com/jayway/jsonpath/internal/Log.java
  6. 5
      json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java
  7. 190
      json-path/src/test/java/com/jayway/jsonpath/FilterTest.java
  8. 17
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  9. 22
      json-path/src/test/java/com/jayway/jsonpath/LogTest.java
  10. 2
      json-path/src/test/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilterTest.java
  11. 15
      json-path/src/test/resources/logback-test.xml
  12. 102
      pom.xml

10
json-path-assert/pom.xml

@ -31,11 +31,6 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
@ -46,10 +41,5 @@
<artifactId>hamcrest-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

29
json-path/pom.xml

@ -26,8 +26,6 @@
<name>json-path</name>
<url>http://code.google.com/p/json-path/</url>
<dependencies>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
@ -41,33 +39,6 @@
<artifactId>jackson-mapper-asl</artifactId>
<optional>true</optional>
</dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

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

@ -14,11 +14,13 @@
*/
package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.JsonProvider;
import org.apache.commons.lang3.Validate;
import java.util.*;
import java.util.regex.Pattern;
import static java.util.Arrays.asList;
import static org.apache.commons.lang3.Validate.notEmpty;
import static org.apache.commons.lang3.Validate.notNull;
/**
@ -40,6 +42,7 @@ public class Criteria {
EXISTS,
TYPE,
REGEX,
NOT_EMPTY,
OR
}
@ -58,14 +61,14 @@ public class Criteria {
private Criteria(String key) {
notEmpty(key, "key can not be null or empty");
Validate.notEmpty(key, "key can not be null or empty");
this.criteriaChain = new ArrayList<Criteria>();
this.criteriaChain.add(this);
this.key = JsonPath.compile(key);
}
private Criteria(List<Criteria> criteriaChain, String key) {
notEmpty(key, "key can not be null or empty");
Validate.notEmpty(key, "key can not be null or empty");
this.criteriaChain = criteriaChain;
this.criteriaChain.add(this);
this.key = JsonPath.compile(key);
@ -226,6 +229,20 @@ public class Criteria {
return (act.size() == exp);
} else if (CriteriaType.NOT_EMPTY.equals(key)) {
if(actualVal == null){
return false;
}
boolean empty = false;
if (actualVal instanceof Collection) {
empty = ((List) actualVal).isEmpty();
} else if(actualVal instanceof String){
empty = ((String) actualVal).isEmpty();
}
return !empty;
} else if (CriteriaType.EXISTS.equals(key)) {
final boolean exp = (Boolean) expectedVal;
@ -506,6 +523,17 @@ public class Criteria {
return this;
}
/**
* The <code>notEmpty</code> operator checks that an array is not empty.
*
* @return
*/
public Criteria notEmpty() {
checkFilterCanBeApplied(CriteriaType.NOT_EMPTY);
criteria.put(CriteriaType.NOT_EMPTY, null);
return this;
}
/**
* Check for existence (or lack thereof) of a field.
*

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

@ -16,6 +16,7 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.IOUtils;
import com.jayway.jsonpath.internal.Log;
import com.jayway.jsonpath.internal.PathToken;
import com.jayway.jsonpath.internal.PathTokenizer;
import com.jayway.jsonpath.internal.filter.PathTokenFilter;
@ -98,37 +99,44 @@ import static org.apache.commons.lang3.Validate.*;
*/
public class JsonPath {
private static Pattern DEFINITE_PATH_PATTERN = Pattern.compile(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*");
private static Pattern DEFINITE_PATH_PATTERN = Pattern.compile(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?]|\\[\\s?:|>|\\(|<|=|\\+).*");
private static Pattern INVALID_PATH_PATTERN = Pattern.compile("[^\\?\\+=\\-\\*/!]\\(");
private PathTokenizer tokenizer;
private LinkedList<Filter> filters;
public JsonPath(String jsonPath, Filter[] filters) {
if (jsonPath == null ||
jsonPath.trim().length() == 0 ||
jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) {
private JsonPath(String jsonPath, Filter[] filters) {
notNull(jsonPath, "path can not be null");
jsonPath = jsonPath.trim();
notEmpty(jsonPath, "path can not be empty");
if (INVALID_PATH_PATTERN.matcher(jsonPath).matches()) {
throw new InvalidPathException("Invalid path");
}
int filterCountInPath = StringUtils.countMatches(jsonPath, "[?]");
isTrue(filterCountInPath == filters.length, "Filters in path ([?]) does not match provided filters.");
this.tokenizer = new PathTokenizer(jsonPath);
//System.out.println(tokenizer.toString());
if(Log.isDebugEnabled()){
Log.debug("New JsonPath:\n{}", this.tokenizer.toString());
}
this.filters = new LinkedList<Filter>();
this.filters.addAll(asList(filters));
}
PathTokenizer getTokenizer(){
PathTokenizer getTokenizer() {
return this.tokenizer;
}
public JsonPath copy(){
public JsonPath copy() {
return new JsonPath(tokenizer.getPath(), filters.toArray(new Filter[0]));
}
@ -199,7 +207,7 @@ public class JsonPath {
* the {@link JsonProvider}
*
* @param jsonObject a container Object
* @param <T> expected return type
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
@ -213,8 +221,8 @@ public class JsonPath {
* the {@link JsonProvider}
*
* @param jsonProvider JsonProvider to use
* @param jsonObject a container Object
* @param <T> expected return type
* @param jsonObject a container Object
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
@ -222,9 +230,9 @@ public class JsonPath {
notNull(jsonProvider, "jsonProvider can not be null");
notNull(jsonObject, "json can not be null");
if(this.getPath().equals("$")){
if (this.getPath().equals("$")) {
//This path only references the whole object. No need to do any work here...
return (T)jsonObject;
return (T) jsonObject;
}
if (!jsonProvider.isContainer(jsonObject)) {
@ -265,8 +273,8 @@ public class JsonPath {
* Applies this JsonPath to the provided json string
*
* @param jsonProvider JsonProvider to use
* @param json a json string
* @param <T> expected return type
* @param json a json string
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
@ -294,8 +302,8 @@ public class JsonPath {
* Applies this JsonPath to the provided json URL
*
* @param jsonProvider JsonProvider to use
* @param jsonURL url to read from
* @param <T> expected return type
* @param jsonURL url to read from
* @param <T> expected return type
* @return list of objects matched by the given path
* @throws IOException
*/
@ -331,8 +339,8 @@ public class JsonPath {
* Applies this JsonPath to the provided json file
*
* @param jsonProvider JsonProvider to use
* @param jsonFile file to read from
* @param <T> expected return type
* @param jsonFile file to read from
* @param <T> expected return type
* @return list of objects matched by the given path
* @throws IOException
*/
@ -373,7 +381,7 @@ public class JsonPath {
/**
* Applies this JsonPath to the provided json input stream
*
* @param jsonProvider JsonProvider to use
* @param jsonProvider JsonProvider to use
* @param jsonInputStream input stream to read from
* @param <T> expected return type
* @return list of objects matched by the given path
@ -401,7 +409,7 @@ public class JsonPath {
* Compiles a JsonPath
*
* @param jsonPath to compile
* @param filters filters to be applied to the filter place holders [?] in the path
* @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) {
@ -422,7 +430,7 @@ public class JsonPath {
*
* @param json a json string
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@ -436,10 +444,10 @@ public class JsonPath {
* Creates a new JsonPath and applies it to the provided Json string
*
* @param jsonProvider JsonProvider to use
* @param json a json string
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @param json a json string
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
@ -456,7 +464,7 @@ public class JsonPath {
*
* @param json a json object
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@ -464,14 +472,15 @@ public class JsonPath {
public static <T> T read(Object json, String jsonPath, Filter... filters) {
return read(JsonProviderFactory.createProvider(), json, jsonPath, filters);
}
/**
* Creates a new JsonPath and applies it to the provided Json object
*
* @param jsonProvider JsonProvider to use
* @param json a json object
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @param json a json object
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
@ -489,7 +498,7 @@ public class JsonPath {
*
* @param jsonURL url pointing to json doc
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@ -502,10 +511,10 @@ public class JsonPath {
* Creates a new JsonPath and applies it to the provided Json object
*
* @param jsonProvider JsonProvider to use
* @param jsonURL url pointing to json doc
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @param jsonURL url pointing to json doc
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
@ -520,9 +529,9 @@ public class JsonPath {
/**
* Creates a new JsonPath and applies it to the provided Json object
*
* @param jsonFile json file
* @param jsonFile json file
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@ -535,10 +544,10 @@ public class JsonPath {
* Creates a new JsonPath and applies it to the provided Json object
*
* @param jsonProvider JsonProvider to use
* @param jsonFile json file
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @param jsonFile json file
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
@ -553,10 +562,10 @@ public class JsonPath {
/**
* Creates a new JsonPath and applies it to the provided Json object
*
* @param jsonInputStream json input stream
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @param jsonInputStream json input stream
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
@ -567,11 +576,11 @@ public class JsonPath {
/**
* Creates a new JsonPath and applies it to the provided Json object
*
* @param jsonProvider JsonProvider to use
* @param jsonInputStream json input stream
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @param jsonProvider JsonProvider to use
* @param jsonInputStream json input stream
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})

50
json-path/src/main/java/com/jayway/jsonpath/internal/Log.java

@ -0,0 +1,50 @@
package com.jayway.jsonpath.internal;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* User: kalle
* Date: 8/28/13
* Time: 10:23 AM
*/
public final class Log {
private Log() {
}
private static boolean enabled = true;
public static void enableDebug(){
enabled = true;
}
public static boolean isDebugEnabled(){
return enabled;
}
public static void debug(String msg, Object... args){
if(enabled){
int argCount = StringUtils.countMatches(msg, "{}");
if(!(argCount == args.length)){
throw new RuntimeException("Invalid debug statement.");
}
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
String cls = stackTraceElements[2].getClassName();
msg = msg.replaceFirst(Pattern.quote("{}"), "%s");
msg = String.format(msg, args);
System.out.println("DEBUG [" + Thread.currentThread().getName() + "] " + cls + " " + msg);
}
}
}

5
json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java

@ -198,10 +198,7 @@ public class ExpressionEvalTest {
result = JsonPath.read(DOCUMENT, "$.characters[?(@.offspring)]");
assertEquals(4, result.size());
}
}

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

@ -1,7 +1,5 @@
package com.jayway.jsonpath;
import net.minidev.json.parser.JSONParser;
import org.junit.Test;
import java.util.Collections;
@ -258,7 +256,7 @@ public class FilterTest {
check.put("int", 10);
check.put("long", 1L);
check.put("double", 1.12D);
Filter shouldMarch = filter(where("string").is("foo").and("int").lt(11));
Filter shouldNotMarch = filter(where("string").is("foo").and("int").gt(11));
@ -318,107 +316,141 @@ public class FilterTest {
@Test
public void arrays_of_maps_can_be_filtered() throws Exception {
public void arrays_of_maps_can_be_filtered() throws Exception {
Map<String, Object> rootGrandChild_A = new HashMap<String, Object>();
rootGrandChild_A.put("name", "rootGrandChild_A");
Map<String, Object> rootGrandChild_A = new HashMap<String, Object>();
rootGrandChild_A.put("name", "rootGrandChild_A");
Map<String, Object> rootGrandChild_B = new HashMap<String, Object>();
rootGrandChild_B.put("name", "rootGrandChild_B");
Map<String, Object> rootGrandChild_B = new HashMap<String, Object>();
rootGrandChild_B.put("name", "rootGrandChild_B");
Map<String, Object> rootGrandChild_C = new HashMap<String, Object>();
rootGrandChild_C.put("name", "rootGrandChild_C");
Map<String, Object> rootGrandChild_C = new HashMap<String, Object>();
rootGrandChild_C.put("name", "rootGrandChild_C");
Map<String, Object> rootChild_A = new HashMap<String, Object>();
rootChild_A.put("name", "rootChild_A");
rootChild_A.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C));
Map<String, Object> rootChild_A = new HashMap<String, Object>();
rootChild_A.put("name", "rootChild_A");
rootChild_A.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C));
Map<String, Object> rootChild_B = new HashMap<String, Object>();
rootChild_B.put("name", "rootChild_B");
rootChild_B.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C));
Map<String, Object> rootChild_B = new HashMap<String, Object>();
rootChild_B.put("name", "rootChild_B");
rootChild_B.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C));
Map<String, Object> rootChild_C = new HashMap<String, Object>();
rootChild_C.put("name", "rootChild_C");
rootChild_C.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C));
Map<String, Object> rootChild_C = new HashMap<String, Object>();
rootChild_C.put("name", "rootChild_C");
rootChild_C.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C));
Map<String, Object> root = new HashMap<String, Object>();
root.put("children", asList(rootChild_A, rootChild_B, rootChild_C));
Map<String, Object> root = new HashMap<String, Object>();
root.put("children", asList(rootChild_A, rootChild_B, rootChild_C));
Filter customFilter = new Filter.FilterAdapter<Map<String, Object>>() {
@Override
public boolean accept(Map<String, Object> map) {
if(map.get("name").equals("rootGrandChild_A")){
return true;
}
return false;
Filter customFilter = new Filter.FilterAdapter<Map<String, Object>>() {
@Override
public boolean accept(Map<String, Object> map) {
if (map.get("name").equals("rootGrandChild_A")) {
return true;
}
};
return false;
}
};
Filter rootChildFilter = filter(where("name").regex(Pattern.compile("rootChild_[A|B]")));
Filter rootGrandChildFilter = filter(where("name").regex(Pattern.compile("rootGrandChild_[A|B]")));
Filter rootChildFilter = filter(where("name").regex(Pattern.compile("rootChild_[A|B]")));
Filter rootGrandChildFilter = filter(where("name").regex(Pattern.compile("rootGrandChild_[A|B]")));
List read = JsonPath.read(root, "children[?].children[?][?]", rootChildFilter, rootGrandChildFilter, customFilter);
List read = JsonPath.read(root, "children[?].children[?][?]", rootChildFilter, rootGrandChildFilter, customFilter);
System.out.println(read.size());
}
System.out.println(read.size());
}
@Test
public void arrays_of_objects_can_be_filtered() throws Exception {
Map<String, Object> doc = new HashMap<String, Object>();
doc.put("items", asList(1, 2, 3));
@Test
public void arrays_of_objects_can_be_filtered() throws Exception {
Map<String, Object> doc = new HashMap<String, Object>();
doc.put("items", asList(1, 2, 3));
Filter customFilter = new Filter.FilterAdapter(){
@Override
public boolean accept(Object o) {
return 1 == (Integer)o;
}
};
Filter customFilter = new Filter.FilterAdapter() {
@Override
public boolean accept(Object o) {
return 1 == (Integer) o;
}
};
List<Integer> res = JsonPath.read(doc, "$.items[?]", customFilter);
List<Integer> res = JsonPath.read(doc, "$.items[?]", customFilter);
assertEquals(1, res.get(0).intValue());
}
@Test
public void filters_can_contain_json_path_expressions() throws Exception {
Object doc = JsonModel.model(DOCUMENT).getJsonObject();
assertTrue(filter(where("$.store..price").gt(10)).accept(doc));
assertFalse(filter(where("$.store..price").gte(100)).accept(doc));
assertTrue(filter(where("$.store..category").ne("fiction")).accept(doc));
assertFalse(filter(where("$.store.bicycle.color").ne("red")).accept(doc));
assertTrue(filter(where("$.store.bicycle.color").ne("blue")).accept(doc));
assertTrue(filter(where("$.store..color").exists(true)).accept(doc));
assertFalse(filter(where("$.store..flavor").exists(true)).accept(doc));
assertTrue(filter(where("$.store..color").regex(Pattern.compile("^r.d$"))).accept(doc));
assertTrue(filter(where("$.store..color").type(String.class)).accept(doc));
assertTrue(filter(where("$.store..price").is(12.99)).accept(doc));
assertFalse(filter(where("$.store..price").is(13.99)).accept(doc));
}
@Test
public void collection_based_filters_cannot_be_applied_to_multi_level_expressions(){
try{
assertEquals(1, res.get(0).intValue());
}
@Test
public void filters_can_contain_json_path_expressions() throws Exception {
Object doc = JsonModel.model(DOCUMENT).getJsonObject();
assertTrue(filter(where("$.store..price").gt(10)).accept(doc));
assertFalse(filter(where("$.store..price").gte(100)).accept(doc));
assertTrue(filter(where("$.store..category").ne("fiction")).accept(doc));
assertFalse(filter(where("$.store.bicycle.color").ne("red")).accept(doc));
assertTrue(filter(where("$.store.bicycle.color").ne("blue")).accept(doc));
assertTrue(filter(where("$.store..color").exists(true)).accept(doc));
assertFalse(filter(where("$.store..flavor").exists(true)).accept(doc));
assertTrue(filter(where("$.store..color").regex(Pattern.compile("^r.d$"))).accept(doc));
assertTrue(filter(where("$.store..color").type(String.class)).accept(doc));
assertTrue(filter(where("$.store..price").is(12.99)).accept(doc));
assertFalse(filter(where("$.store..price").is(13.99)).accept(doc));
}
@Test
public void not_empty_filter_evaluates() {
String json = "{\n" +
" \"fields\": [\n" +
" {\n" +
" \"errors\": [], \n" +
" \"name\": \"\", \n" +
" \"empty\": true \n" +
" }, \n" +
" {\n" +
" \"errors\": [], \n" +
" \"name\": \"foo\"\n" +
" }, \n" +
" {\n" +
" \"errors\": [\n" +
" \"first\", \n" +
" \"second\"\n" +
" ], \n" +
" \"name\": \"invalid\"\n" +
" }\n" +
" ]\n" +
"}\n";
Object doc = JsonModel.model(json).getJsonObject();
List<Map<String, Object>> result = JsonPath.read(doc, "$.fields[?]", filter(where("errors").notEmpty()));
assertEquals(1, result.size());
System.out.println(result);
result = JsonPath.read(doc, "$.fields[?]", filter(where("name").notEmpty()));
assertEquals(2, result.size());
System.out.println(result);
}
@Test
public void collection_based_filters_cannot_be_applied_to_multi_level_expressions() {
try {
where("$.store.*").size(4);
fail("This should have thrown an exception");
} catch(IllegalArgumentException e){
}
try{
} catch (IllegalArgumentException e) {
}
try {
where("$.store.*").in("foo");
fail("This should have thrown an exception");
} catch(IllegalArgumentException e){
}
} catch (IllegalArgumentException e) {
}
}
}

17
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -77,6 +77,23 @@ public class JsonPathTest {
private final static String ARRAY_EXPAND = "[{\"parent\": \"ONE\", \"child\": {\"name\": \"NAME_ONE\"}}, [{\"parent\": \"TWO\", \"child\": {\"name\": \"NAME_TWO\"}}]]";
@Test
public void bracket_notation_with_dots() {
String json = "{\n" +
" \"store\": {\n" +
" \"book\": [\n" +
" {\n" +
" \"author.name\": \"Nigel Rees\", \n" +
" \"category\": \"reference\", \n" +
" \"price\": 8.95, \n" +
" \"title\": \"Sayings of the Century\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
assertEquals("Nigel Rees", JsonPath.read(json, "$.store.book[0]['author.name']"));
}
@Test
public void null_object_in_path() {

22
json-path/src/test/java/com/jayway/jsonpath/LogTest.java

@ -0,0 +1,22 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.Log;
import org.junit.Test;
/**
* User: kalle
* Date: 8/28/13
* Time: 10:40 AM
*/
public class LogTest {
@Test
public void logger_expands_templates() {
Log.enableDebug();
Log.debug("foo \n{}", "bar");
}
}

2
json-path/src/test/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilterTest.java

@ -14,6 +14,8 @@ public class ArrayEvalFilterTest {
@Test
public void condition_statements_can_be_parsed() {
assertEquals(new ArrayEvalFilter.ConditionStatement("@.length", ">", "0"), ArrayEvalFilter.createConditionStatement("[?(@.length>0)]"));
//int array
assertEquals(new ArrayEvalFilter.ConditionStatement("@", "==", "5"), ArrayEvalFilter.createConditionStatement("[?(@==5)]"));
assertEquals(new ArrayEvalFilter.ConditionStatement("@", "==", "5"), ArrayEvalFilter.createConditionStatement("[?(@ == 5)]"));

15
json-path/src/test/resources/logback-test.xml

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextName>myAppName</contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5level [%t] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.jayway.jsonpath" level="TRACE"/>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>

102
pom.xml

@ -55,6 +55,15 @@
<properties>
<scm.branch>master</scm.branch>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.5</slf4j.version>
<logback.version>1.0.13</logback.version>
<junit.version>4.10</junit.version>
<commons-io.version>2.4</commons-io.version>
<commons-lang.version>3.1</commons-lang.version>
<hamcrest.version>1.3</hamcrest.version>
<jackson.version>1.9.11</jackson.version>
<json-smart.version>1.2</json-smart.version>
</properties>
<scm>
<url>http://github.com/jayway/JsonPath/tree/${scm.branch}</url>
@ -65,7 +74,7 @@
<mailingLists>
<mailingList>
<name>JsonPath mailing-list</name>
<archive>http://groups.google.com/group/json-path/topics</archive>
<archive>https://groups.google.com/forum/#!forum/jsonpath</archive>
</mailingList>
</mailingLists>
@ -183,48 +192,109 @@
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>1.2</version>
<version>${json-smart.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<optional>true</optional>
<version>1.9.11</version>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<version>${hamcrest.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<version>${hamcrest.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--
==================================
Global dependencies
==================================
-->
<!--
==================================
Global test dependencies
==================================
-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading…
Cancel
Save