diff --git a/json-path-assert/pom.xml b/json-path-assert/pom.xml
index 4febfadc..bae633d3 100644
--- a/json-path-assert/pom.xml
+++ b/json-path-assert/pom.xml
@@ -19,5 +19,21 @@
json-path
${project.version}
+
+
+ junit
+ junit
+ test
+
+
+
+ com.googlecode.json-simple
+ json-simple
+
+
+
+ org.hamcrest
+ hamcrest-all
+
diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
new file mode 100644
index 00000000..d55eb299
--- /dev/null
+++ b/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);
+ }
+
+}
diff --git a/json-path/src/main/java/com/jayway/jsonassert/JsonAsserter.java b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java
similarity index 93%
rename from json-path/src/main/java/com/jayway/jsonassert/JsonAsserter.java
rename to json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java
index 27b28c3f..0f0024ab 100644
--- a/json-path/src/main/java/com/jayway/jsonassert/JsonAsserter.java
+++ b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java
@@ -9,13 +9,6 @@ import org.hamcrest.Matcher;
*/
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.
* If not, an AssertionError is thrown with information about the matcher
diff --git a/json-path/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
similarity index 66%
rename from json-path/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
rename to json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
index 80c9a5c3..111bd247 100644
--- a/json-path/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
+++ b/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.JsonPath;
+import com.jayway.jsonpath.JsonPath;
+import com.jayway.jsonpath.PathUtil;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
+import java.text.ParseException;
+import java.util.List;
+
import static org.hamcrest.Matchers.*;
/**
@@ -15,31 +19,34 @@ import static org.hamcrest.Matchers.*;
*/
public class JsonAsserterImpl implements JsonAsserter {
- private final JsonPath reader;
+
+
+ private final Object jsonObject;
/**
* 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) {
- this.reader = reader;
+ public JsonAsserterImpl(Object jsonObject) {
+ this.jsonObject = jsonObject;
}
- /**
- * {@inheritDoc}
- */
- public JsonPath reader() {
- return reader;
- }
+
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public JsonAsserter assertThat(String path, Matcher matcher) {
- MatcherAssert.assertThat((T) reader.get(path), matcher);
+
+ if(PathUtil.isPathDefinite(path)){
+ MatcherAssert.assertThat(JsonPath.readOne(jsonObject, path), matcher);
+ }
+ else {
+ MatcherAssert.assertThat((T) JsonPath.read(jsonObject, path), matcher);
+ }
return this;
}
diff --git a/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java b/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
new file mode 100644
index 00000000..dc6802d4
--- /dev/null
+++ b/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");
+
+ }
+ */
+
+
+}
diff --git a/json-path/pom.xml b/json-path/pom.xml
index 96952770..06ac6556 100644
--- a/json-path/pom.xml
+++ b/json-path/pom.xml
@@ -30,20 +30,11 @@
http://maven.apache.org
-
- commons-jxpath
- commons-jxpath
-
-
com.googlecode.json-simple
json-simple
-
- org.hamcrest
- hamcrest-all
-
commons-lang
@@ -59,5 +50,10 @@
test
+
+ org.hamcrest
+ hamcrest-all
+
+
diff --git a/json-path/src/main/java/com/jayway/jsonassert/JsonAssert.java b/json-path/src/main/java/com/jayway/jsonassert/JsonAssert.java
deleted file mode 100644
index a171378d..00000000
--- a/json-path/src/main/java/com/jayway/jsonassert/JsonAssert.java
+++ /dev/null
@@ -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));
- }
-
-}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
index 26188ea3..3586ea10 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
@@ -50,7 +50,7 @@ public class JsonPath {
return path.read(json);
}
- public static List read(Object json, String jsonPath) throws java.text.ParseException {
+ public static List read(Object json, String jsonPath) {
JsonPath path = compile(jsonPath);
return path.read(json);
@@ -68,7 +68,7 @@ public class JsonPath {
return (T) result.get(0);
}
- public static T readOne(Object json, String jsonPath) throws java.text.ParseException {
+ public static T readOne(Object json, String jsonPath) {
JsonPath path = compile(jsonPath);
List