Browse Source

use java scripting to eval path criteria

pull/1/merge
kalle 14 years ago
parent
commit
91df636d12
  1. 16
      json-path-assert/pom.xml
  2. 66
      json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
  3. 7
      json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java
  4. 31
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
  5. 115
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  6. 14
      json-path/pom.xml
  7. 84
      json-path/src/main/java/com/jayway/jsonassert/JsonAssert.java
  8. 4
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  9. 29
      json-path/src/main/java/com/jayway/jsonpath/PathItem.java
  10. 6
      json-path/src/main/java/com/jayway/jsonpath/PathUtil.java
  11. 73
      json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java
  12. 25
      json-path/src/test/java/com/jayway/ScriptTest.java
  13. 72
      json-path/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  14. 12
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  15. 22
      json-path/src/test/java/com/jayway/jsonpath/PathUtilTest.java
  16. 28
      pom.xml

16
json-path-assert/pom.xml

@ -19,5 +19,21 @@
<artifactId>json-path</artifactId> <artifactId>json-path</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

66
json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java

@ -0,0 +1,66 @@
package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JsonAsserterImpl;
import org.json.simple.parser.JSONParser;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.ParseException;
/**
* User: kalle stenflo
* Date: 1/24/11
* Time: 9:31 PM
*/
public class JsonAssert {
private static final JSONParser JSON_PARSER = new JSONParser();
/**
* Creates a JSONAsserter
*
* @param json the JSON document to create a JSONAsserter for
* @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed
*/
public static JsonAsserter with(String json) throws ParseException {
try {
return new JsonAsserterImpl(JSON_PARSER.parse(json));
} catch (org.json.simple.parser.ParseException e) {
throw new ParseException(json, e.getPosition());
}
}
/**
* Creates a JSONAsserter
*
* @param reader the reader of the json document
* @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed
*/
public static JsonAsserter with(Reader reader) throws ParseException, IOException {
try {
return new JsonAsserterImpl(JSON_PARSER.parse(reader));
} catch (org.json.simple.parser.ParseException e) {
throw new ParseException(e.toString(), e.getPosition());
} finally {
reader.close();
}
}
/**
* Creates a JSONAsserter
*
* @param is the input stream
* @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed
*/
public static JsonAsserter with(InputStream is) throws ParseException, IOException {
Reader reader = new InputStreamReader(is);
return with(reader);
}
}

7
json-path/src/main/java/com/jayway/jsonassert/JsonAsserter.java → json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java

@ -9,13 +9,6 @@ import org.hamcrest.Matcher;
*/ */
public interface JsonAsserter { public interface JsonAsserter {
/**
* Gives access to the {@link JsonPath} used to base the assertions on
*
* @return the underlying reader
*/
JsonPath reader();
/** /**
* Asserts that object specified by path satisfies the condition specified by matcher. * Asserts that object specified by path satisfies the condition specified by matcher.
* If not, an AssertionError is thrown with information about the matcher * If not, an AssertionError is thrown with information about the matcher

31
json-path/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java → json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java

@ -2,10 +2,14 @@ package com.jayway.jsonassert.impl;
import com.jayway.jsonassert.JsonAsserter; import com.jayway.jsonassert.JsonAsserter;
import com.jayway.jsonassert.JsonPath; import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathUtil;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert; import org.hamcrest.MatcherAssert;
import java.text.ParseException;
import java.util.List;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
/** /**
@ -15,31 +19,34 @@ import static org.hamcrest.Matchers.*;
*/ */
public class JsonAsserterImpl implements JsonAsserter { public class JsonAsserterImpl implements JsonAsserter {
private final JsonPath reader;
private final Object jsonObject;
/** /**
* Instantiates a new JSONAsserter * Instantiates a new JSONAsserter
* *
* @param reader initialized with the JSON document to be asserted upon * @param jsonObject the object to make asserts on
*/ */
public JsonAsserterImpl(JsonPath reader) { public JsonAsserterImpl(Object jsonObject) {
this.reader = reader; this.jsonObject = jsonObject;
} }
/**
* {@inheritDoc}
*/
public JsonPath reader() {
return reader;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> JsonAsserter assertThat(String path, Matcher<T> matcher) { public <T> JsonAsserter assertThat(String path, Matcher<T> matcher) {
MatcherAssert.assertThat((T) reader.get(path), matcher);
if(PathUtil.isPathDefinite(path)){
MatcherAssert.assertThat(JsonPath.<T>readOne(jsonObject, path), matcher);
}
else {
MatcherAssert.assertThat((T) JsonPath.<T>read(jsonObject, path), matcher);
}
return this; return this;
} }

115
json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java

@ -0,0 +1,115 @@
package com.jayway.jsonassert;
import org.junit.Test;
import static com.jayway.jsonassert.JsonAssert.with;
import static org.hamcrest.Matchers.*;
/**
* User: kalle stenflo
* Date: 1/21/11
* Time: 4:04 PM
*/
public class JsonAssertTest {
public final static String JSON =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n" +
" }\n" +
" }\n" +
"}";
@Test
public void a_path_can_be_asserted_with_matcher() throws Exception {
with(JSON).assertThat("$.store.bicycle.color", equalTo("red"))
.assertThat("$.store.bicycle.price", equalTo(19.95D));
}
@Test
public void list_content_can_be_asserted_with_matcher() throws Exception {
with(JSON).assertThat("$..book[*].author", hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
}
@Test
public void map_content_can_be_asserted_with_matcher() throws Exception {
with(JSON).assertThat("$.store.book[0]", hasEntry("category", "reference"))
.assertThat("$.store.book[0]", hasEntry("title", "Sayings of the Century"));
}
@Test
public void a_path_can_be_asserted_equal_to() throws Exception {
with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century")
.assertThat("$.store.book[0].title", equalTo("Sayings of the Century"));
}
/*
@Test
public void a_sub_document_can_asserted_on__by_path() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertThat("subDocument.subField", is(equalTo("sub-field")));
}
@Test
public void a_path_can_be_asserted_equal_to() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertEquals("stringField", "string-field");
}
@Test
public void a_path_can_be_asserted_is_null() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertNull("nullField");
}
@Test(expected = AssertionError.class)
public void failed_assert_throws() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertThat("stringField", equalTo("SOME CRAP"));
}
@Test
public void multiple_asserts_can_be_chained() throws Exception {
JsonAssert.with(TEST_DOCUMENT)
.assertThat("stringField", equalTo("string-field"))
.assertThat("numberField", is(notNullValue()))
.and()
.assertNull("nullField")
.and()
.assertEquals("stringField", "string-field");
}
*/
}

14
json-path/pom.xml

@ -30,20 +30,11 @@
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
<dependencies> <dependencies>
<dependency>
<groupId>commons-jxpath</groupId>
<artifactId>commons-jxpath</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.googlecode.json-simple</groupId> <groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId> <artifactId>json-simple</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
@ -59,5 +50,10 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

84
json-path/src/main/java/com/jayway/jsonassert/JsonAssert.java

@ -1,84 +0,0 @@
package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JsonAsserterImpl;
import com.jayway.jsonassert.impl.JsonPathImpl;
import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
/**
* User: kalle stenflo
* Date: 1/24/11
* Time: 9:31 PM
*/
public class JsonAssert {
/**
* Creates a JSONReader
*
* @param jsonDoc the json document to read
* @return a new reader
* @throws ParseException
*/
public static JsonPath parse(String jsonDoc) throws ParseException {
return JsonPathImpl.parse(jsonDoc);
}
/**
* Creates a JSONReader
*
* @param reader he json document to read
* @return a new reader
* @throws ParseException document could not pe parsed
* @throws IOException
*/
public static JsonPath parse(Reader reader) throws ParseException, IOException {
return parse(reader, false);
}
/**
* Creates a JSONReader
*
* @param reader he json document to read
* @return a new reader
* @throws ParseException document could not pe parsed
* @throws IOException
*/
public static JsonPath parse(Reader reader, boolean closeReader) throws ParseException, IOException {
JsonPath jsonReader = null;
try {
jsonReader = JsonPathImpl.parse(reader);
} finally {
if(closeReader){
try {
reader.close();
} catch (IOException ignore) {}
}
}
return jsonReader;
}
/**
* Creates a JSONAsserter
*
* @param json the JSON document to create a JSONAsserter for
* @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed
*/
public static JsonAsserter with(String json) throws ParseException {
return new JsonAsserterImpl(JsonPathImpl.parse(json));
}
/**
* Creates a JSONAsserter
*
* @param reader the reader of the json document
* @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed
*/
public static JsonAsserter with(Reader reader) throws ParseException, IOException {
return new JsonAsserterImpl(JsonPathImpl.parse(reader));
}
}

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

@ -50,7 +50,7 @@ public class JsonPath {
return path.read(json); return path.read(json);
} }
public static <T> List<T> read(Object json, String jsonPath) throws java.text.ParseException { public static <T> List<T> read(Object json, String jsonPath) {
JsonPath path = compile(jsonPath); JsonPath path = compile(jsonPath);
return path.read(json); return path.read(json);
@ -68,7 +68,7 @@ public class JsonPath {
return (T) result.get(0); return (T) result.get(0);
} }
public static <T> T readOne(Object json, String jsonPath) throws java.text.ParseException { public static <T> T readOne(Object json, String jsonPath) {
JsonPath path = compile(jsonPath); JsonPath path = compile(jsonPath);
List<Object> result = read(json, jsonPath); List<Object> result = read(json, jsonPath);

29
json-path/src/main/java/com/jayway/jsonpath/PathItem.java

@ -1,29 +0,0 @@
package com.jayway.jsonpath;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/2/11
* Time: 2:33 PM
*/
public class PathItem {
private final String path;
private final Object target;
public PathItem(String path, Object target) {
this.path = path;
this.target = target;
}
public String getPath() {
return path;
}
public Object getTarget() {
return target;
}
}

6
json-path/src/main/java/com/jayway/jsonpath/PathUtil.java

@ -14,6 +14,10 @@ import java.util.List;
*/ */
public class PathUtil { public class PathUtil {
public static boolean isPathDefinite(String jsonPath) {
return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:|>|\\(|<|=|\\+).*");
}
public static boolean isContainer(Object obj) { public static boolean isContainer(Object obj) {
return (isArray(obj) || isDocument(obj)); return (isArray(obj) || isDocument(obj));
} }
@ -50,7 +54,7 @@ public class PathUtil {
String[] split = jsonPath.split("\\."); String[] split = jsonPath.split("\\.");
for (int i = 0; i < split.length; i++) { for (int i = 0; i < split.length; i++) {
if(split[i].trim().isEmpty()){ if (split[i].trim().isEmpty()) {
continue; continue;
} }
fragments.add(split[i].replace("@", "@.").replace("~", "..")); fragments.add(split[i].replace("@", "@.").replace("~", ".."));

73
json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java

@ -2,7 +2,11 @@ package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.PathUtil; import com.jayway.jsonpath.PathUtil;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -16,6 +20,8 @@ import java.util.regex.Pattern;
*/ */
public class ListFilter extends JsonPathFilterBase { public class ListFilter extends JsonPathFilterBase {
private static ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("js");
private static final Pattern LIST_INDEX_PATTERN = Pattern.compile("\\[(\\s?\\d+\\s?,?)+\\]"); private static final Pattern LIST_INDEX_PATTERN = Pattern.compile("\\[(\\s?\\d+\\s?,?)+\\]");
private static final Pattern LIST_PULL_PATTERN = Pattern.compile("\\[\\s?:(\\d+)\\s?\\]"); //[ :2 ] private static final Pattern LIST_PULL_PATTERN = Pattern.compile("\\[\\s?:(\\d+)\\s?\\]"); //[ :2 ]
private static final Pattern LIST_WILDCARD_PATTERN = Pattern.compile("\\[\\*\\]"); private static final Pattern LIST_WILDCARD_PATTERN = Pattern.compile("\\[\\*\\]");
@ -23,6 +29,7 @@ public class ListFilter extends JsonPathFilterBase {
private static final Pattern LIST_TAIL_PATTERN_LONG = Pattern.compile("\\[\\s*\\(\\s*@\\.length\\s*-\\s*(\\d+)\\s*\\)\\s*\\]"); private static final Pattern LIST_TAIL_PATTERN_LONG = Pattern.compile("\\[\\s*\\(\\s*@\\.length\\s*-\\s*(\\d+)\\s*\\)\\s*\\]");
private static final Pattern LIST_TAIL_PATTERN = Pattern.compile("(" + LIST_TAIL_PATTERN_SHORT.pattern() + "|" + LIST_TAIL_PATTERN_LONG.pattern() + ")"); private static final Pattern LIST_TAIL_PATTERN = Pattern.compile("(" + LIST_TAIL_PATTERN_SHORT.pattern() + "|" + LIST_TAIL_PATTERN_LONG.pattern() + ")");
private static final Pattern LIST_ITEM_HAS_PROPERTY_PATTERN = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@\\.(\\w+)\\s?\\)\\s?\\]"); private static final Pattern LIST_ITEM_HAS_PROPERTY_PATTERN = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@\\.(\\w+)\\s?\\)\\s?\\]");
private static final Pattern LIST_ITEM_MATCHES_EVAL = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@.(\\w+)\\s?([=<>]+)\\s?(.*)\\s?\\)\\s?\\]"); //[ ?( @.title< 'ko' ) ]
private final String pathFragment; private final String pathFragment;
@ -42,13 +49,30 @@ public class ListFilter extends JsonPathFilterBase {
return filterByListTailIndex(items); return filterByListTailIndex(items);
} else if (LIST_PULL_PATTERN.matcher(pathFragment).matches()) { } else if (LIST_PULL_PATTERN.matcher(pathFragment).matches()) {
return filterByPullIndex(items); return filterByPullIndex(items);
} else if (LIST_ITEM_HAS_PROPERTY_PATTERN.matcher(pathFragment).matches()){ } else if (LIST_ITEM_HAS_PROPERTY_PATTERN.matcher(pathFragment).matches()) {
return filterByItemProperty(items); return filterByItemProperty(items);
} }
else if (LIST_ITEM_MATCHES_EVAL.matcher(pathFragment).matches()) {
return filterByItemEvalMatch(items);
}
return result; return result;
} }
private List<Object> filterByItemEvalMatch(List<Object> items) {
List<Object> result = new JSONArray();
for (Object current : items) {
for (Object item : PathUtil.toArray(current)) {
if(isEvalMatch(item)){
result.add(item);
}
}
}
return result;
}
private List<Object> filterByItemProperty(List<Object> items) { private List<Object> filterByItemProperty(List<Object> items) {
List<Object> result = new JSONArray(); List<Object> result = new JSONArray();
@ -114,7 +138,52 @@ public class ListFilter extends JsonPathFilterBase {
return result; return result;
} }
private String getFilterProperty(){ private boolean isEvalMatch(Object check) {
Matcher matcher = LIST_ITEM_MATCHES_EVAL.matcher(pathFragment);
if (matcher.matches()) {
String property = matcher.group(1);
String operator = matcher.group(2);
String expected = matcher.group(3);
if(!PathUtil.isDocument(check)){
return false;
}
JSONObject obj = PathUtil.toDocument(check);
if(!obj.containsKey(property)){
return false;
}
Object propertyValue = obj.get(property);
if(PathUtil.isContainer(propertyValue)){
return false;
}
if(propertyValue instanceof String){
propertyValue = "'" + propertyValue + "'";
}
if(operator.trim().equals("=")){
operator = "===";
}
String expression = propertyValue + " " + operator + " " + expected;
System.out.println("EVAL" + expression);
try {
return (Boolean) SCRIPT_ENGINE.eval(expression);
} catch (ScriptException e) {
return false;
}
}
return false;
}
private String getFilterProperty() {
Matcher matcher = LIST_ITEM_HAS_PROPERTY_PATTERN.matcher(pathFragment); Matcher matcher = LIST_ITEM_HAS_PROPERTY_PATTERN.matcher(pathFragment);
if (matcher.matches()) { if (matcher.matches()) {
return matcher.group(1); return matcher.group(1);

25
json-path/src/test/java/com/jayway/ScriptTest.java

@ -0,0 +1,25 @@
package com.jayway;
import org.junit.Test;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/3/11
* Time: 8:52 PM
*/
public class ScriptTest {
@Test
public void script() throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
System.out.println(engine.eval("1 === 2"));
System.out.println(engine.eval("'APA' === 'APA'").getClass());
}
}

72
json-path/src/test/java/com/jayway/jsonassert/JsonAssertTest.java

@ -1,72 +0,0 @@
package com.jayway.jsonassert;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
/**
* User: kalle stenflo
* Date: 1/21/11
* Time: 4:04 PM
*/
public class JsonAssertTest {
private static String TEST_DOCUMENT = "{ \"nullField\" : null \"stringField\" : \"string-field\" , \"numberField\" : 1234 , \"booleanField\" : true , \"subDocument\" : {\"subField\" : \"sub-field\"} , \"stringList\" : [\"ONE\", \"TWO\"], \"objectList\" : [{\"subField\" : \"sub-field-0\"}, {\"subField\" : \"sub-field-1\"}], \"listList\" : [[\"0.0\", \"0.1\"], [\"1.0\", \"1.1\"]], }";
@Test
public void a_path_can_be_asserted_with_matcher() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertThat("stringField", equalTo("string-field"));
}
@Test
public void list_content_can_be_asserted_with_matcher() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertThat("stringList", hasItems("ONE", "TWO"));
}
@Test
public void map_content_can_be_asserted_with_matcher() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertThat("subDocument", hasEntry("subField", "sub-field"));
}
@Test
public void a_sub_document_can_asserted_on__by_path() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertThat("subDocument.subField", is(equalTo("sub-field")));
}
@Test
public void a_path_can_be_asserted_equal_to() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertEquals("stringField", "string-field");
}
@Test
public void a_path_can_be_asserted_is_null() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertNull("nullField");
}
@Test(expected = AssertionError.class)
public void failed_assert_throws() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertThat("stringField", equalTo("SOME CRAP"));
}
@Test
public void multiple_asserts_can_be_chained() throws Exception {
JsonAssert.with(TEST_DOCUMENT)
.assertThat("stringField", equalTo("string-field"))
.assertThat("numberField", is(notNullValue()))
.and()
.assertNull("nullField")
.and()
.assertEquals("stringField", "string-field");
}
}

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

@ -123,7 +123,6 @@ public class JsonPathTest {
assertThat(JsonPath.<String>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh")); assertThat(JsonPath.<String>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh"));
assertTrue(JsonPath.<String>read(DOCUMENT, "$.store.book[0,1].author").size() == 2); assertTrue(JsonPath.<String>read(DOCUMENT, "$.store.book[0,1].author").size() == 2);
} }
@Test @Test
public void read_store_book_pull_first_2() throws Exception { public void read_store_book_pull_first_2() throws Exception {
@ -131,6 +130,7 @@ public class JsonPathTest {
assertTrue(JsonPath.<String>read(DOCUMENT, "$.store.book[:2].author").size() == 2); assertTrue(JsonPath.<String>read(DOCUMENT, "$.store.book[:2].author").size() == 2);
} }
@Test @Test
public void read_store_book_filter_by_isbn() throws Exception { public void read_store_book_filter_by_isbn() throws Exception {
@ -138,6 +138,16 @@ public class JsonPathTest {
assertTrue(JsonPath.<String>read(DOCUMENT, "$.store.book[?(@.isbn)].isbn").size() == 2); assertTrue(JsonPath.<String>read(DOCUMENT, "$.store.book[?(@.isbn)].isbn").size() == 2);
} }
@Test
public void all_books_cheapier_than_10() throws Exception {
assertThat(JsonPath.<String>read(DOCUMENT, "$.store.book[?(@.price < 10)].title"), hasItems("Sayings of the Century", "Moby Dick"));
assertThat(JsonPath.<String>read(DOCUMENT, "$.store.book[?(@.category = 'reference')].title"), hasItems("Sayings of the Century"));
}
@Test @Test
public void all_members_of_all_documents() throws Exception { public void all_members_of_all_documents() throws Exception {

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

@ -0,0 +1,22 @@
package com.jayway.jsonpath;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/3/11
* Time: 9:58 PM
*/
public class PathUtilTest {
@Test
public void wildcard_is_not_definite() throws Exception {
assertFalse(PathUtil.isPathDefinite("$..book[0]"));
}
}

28
pom.xml

@ -13,7 +13,9 @@
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and ~ See the License for the specific language governing permissions and
~ limitations under the License. ~ limitations under the License.
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.sonatype.oss</groupId> <groupId>org.sonatype.oss</groupId>
@ -24,9 +26,9 @@
<artifactId>json-path-parent</artifactId> <artifactId>json-path-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<url>http://code.google.com/p/rest-assured</url> <url>http://code.google.com/p/json-path</url>
<name>json-path-parent-pom</name> <name>json-path-parent-pom</name>
<description>Java DSL for easy testing and reading of JSON data</description> <description>Java JsonPath implementation</description>
<inceptionYear>2011</inceptionYear> <inceptionYear>2011</inceptionYear>
<issueManagement> <issueManagement>
<system>GitHub Issue Tracking</system> <system>GitHub Issue Tracking</system>
@ -56,23 +58,23 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<scm> <scm>
<url>http://github.com/jayway/json-assert/tree/${scm.branch}</url> <url>http://github.com/jayway/JsonPath/tree/${scm.branch}</url>
<connection>scm:git:git://github.com/jayway/json-assert.git</connection> <connection>scm:git:git://github.com/jayway/JsonPath.git</connection>
<developerConnection>scm:git:ssh://git@github.com/jayway/json-assert.git</developerConnection> <developerConnection>scm:git:ssh://git@github.com/jayway/JsonPath.git</developerConnection>
</scm> </scm>
<mailingLists> <mailingLists>
<mailingList> <mailingList>
<name>json-assert mailing-list</name> <name>JsonPath mailing-list</name>
<archive>http://groups.google.com/group/rest-assured/topics</archive> <archive>http://groups.google.com/group/json-path/topics</archive>
</mailingList> </mailingList>
</mailingLists> </mailingLists>
<modules> <modules>
<module>json-path</module> <module>json-path</module>
<module>examples</module> <module>examples</module>
<module>json-path-assert</module> <module>json-path-assert</module>
</modules> </modules>
<build> <build>
<pluginManagement> <pluginManagement>
@ -129,12 +131,6 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>commons-jxpath</groupId>
<artifactId>commons-jxpath</artifactId>
<version>1.3</version>
</dependency>
<dependency> <dependency>
<groupId>com.googlecode.json-simple</groupId> <groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId> <artifactId>json-simple</artifactId>

Loading…
Cancel
Save