Browse Source

improved code structure

pull/1/merge
kalle 14 years ago
parent
commit
95e91ea482
  1. 45
      json-assert/src/main/java/com/jayway/jsonassert/JSONAssert.java
  2. 15
      json-assert/src/main/java/com/jayway/jsonassert/impl/JSONAsserterImpl.java
  3. 25
      json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReaderImpl.java
  4. 17
      json-assert/src/test/java/com/jayway/jsonassert/JSONAssertTest.java
  5. 44
      json-assert/src/test/java/com/jayway/jsonassert/JSONReaderTest.java

45
json-assert/src/main/java/com/jayway/jsonassert/JSONAssert.java

@ -1,27 +1,62 @@
package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JSONAsserterImpl;
import com.jayway.jsonassert.impl.JSONReaderImpl;
import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* User: kalle stenflo
* Date: 1/24/11
* Time: 9:31 PM
*/
public class JSONAssert {
/**
* Creates a JSONAsserter instance that can be used to make
* assertions on the provided JSON document or array.
* Creates a JSONReader
*
* @param jsonDoc the json document to read
* @return a new reader
* @throws ParseException
*/
public static JSONReader parse(String jsonDoc) throws ParseException {
return JSONReaderImpl.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 JSONReader parse(Reader reader) throws ParseException, IOException {
return JSONReaderImpl.parse(reader);
}
/**
* 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 JSONAsserterImpl.with(json);
return new JSONAsserterImpl(JSONReaderImpl.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(JSONReaderImpl.parse(reader));
}
}

15
json-assert/src/main/java/com/jayway/jsonassert/impl/JSONAsserterImpl.java

@ -6,8 +6,6 @@ import com.jayway.jsonassert.JSONReader;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import java.text.ParseException;
import static org.hamcrest.Matchers.*;
/**
@ -19,24 +17,13 @@ public class JSONAsserterImpl implements JSONAsserter {
private final JSONReader reader;
/**
* Creates a new instance of a JSONAsserter that can be used to make
* assertions on the provided JSON document or array.
*
* @param json the JSON document to create a JSONAsserter for
* @return a JSON asserter initialized with the provided document
* @throws ParseException
*/
public static JSONAsserter with(String json) throws ParseException {
return new JSONAsserterImpl(JSONReaderImpl.parse(json));
}
/**
* Instantiates a new JSONAsserter
*
* @param reader initialized with the JSON document to be asserted upon
*/
private JSONAsserterImpl(JSONReader reader) {
public JSONAsserterImpl(JSONReader reader) {
this.reader = reader;
}

25
json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReaderImpl.java

@ -7,6 +7,8 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.io.Reader;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -26,7 +28,17 @@ public class JSONReaderImpl implements JSONReader {
private Object root;
public static JSONReader parse(String jsonDoc) throws java.text.ParseException {
public static synchronized JSONReader parse(Reader reader) throws java.text.ParseException, IOException {
try {
return new JSONReaderImpl(JSON_PARSER.parse(reader));
} catch (IOException e) {
throw e;
} catch (ParseException e) {
throw new java.text.ParseException(e.getMessage(), e.getPosition());
}
}
public static synchronized JSONReader parse(String jsonDoc) throws java.text.ParseException {
try {
return new JSONReaderImpl(JSON_PARSER.parse(jsonDoc));
} catch (ParseException e) {
@ -34,6 +46,12 @@ public class JSONReaderImpl implements JSONReader {
}
}
private JSONReaderImpl(Object root) {
notNull(root, "root object can not be null");
this.root = root;
}
/**
* {@inheritDoc}
*/
@ -102,10 +120,7 @@ public class JSONReaderImpl implements JSONReader {
// private methods
//
//------------------------------------------------------------
private JSONReaderImpl(Object root) {
notNull(root, "root object can not be null");
this.root = root;
}
private <T> T getByPath(Class<T> clazz, String stringPath) {
Object current = this.root;

17
json-assert/src/test/java/com/jayway/jsonassert/JSONAsserterTest.java → json-assert/src/test/java/com/jayway/jsonassert/JSONAssertTest.java

@ -1,6 +1,5 @@
package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JSONAsserterImpl;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
@ -10,7 +9,7 @@ import static org.hamcrest.Matchers.*;
* Date: 1/21/11
* Time: 4:04 PM
*/
public class JSONAsserterTest {
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\"]], }";
@ -18,44 +17,44 @@ public class JSONAsserterTest {
@Test
public void a_path_can_be_asserted_with_matcher() throws Exception {
JSONAsserterImpl.with(TEST_DOCUMENT).assertThat("stringField", equalTo("string-field"));
JSONAssert.with(TEST_DOCUMENT).assertThat("stringField", equalTo("string-field"));
}
@Test
public void array_content_can_be_asserted_with_matcher() throws Exception {
JSONAsserterImpl.with(TEST_DOCUMENT).assertThat("stringList", hasItems("ONE", "TWO"));
JSONAssert.with(TEST_DOCUMENT).assertThat("stringList", hasItems("ONE", "TWO"));
}
@Test
public void map_content_can_be_asserted_with_matcher() throws Exception {
JSONAsserterImpl.with(TEST_DOCUMENT).assertThat("subDocument", hasEntry("subField", "sub-field"));
JSONAssert.with(TEST_DOCUMENT).assertThat("subDocument", hasEntry("subField", "sub-field"));
}
@Test
public void a_path_can_be_asserted_equal_to() throws Exception {
JSONAsserterImpl.with(TEST_DOCUMENT).assertEquals("stringField", "string-field");
JSONAssert.with(TEST_DOCUMENT).assertEquals("stringField", "string-field");
}
@Test
public void a_path_can_be_asserted_is_null() throws Exception {
JSONAsserterImpl.with(TEST_DOCUMENT).assertNull("nullField");
JSONAssert.with(TEST_DOCUMENT).assertNull("nullField");
}
@Test(expected = AssertionError.class)
public void failed_assert_throws() throws Exception {
JSONAsserterImpl.with(TEST_DOCUMENT).assertThat("stringField", equalTo("SOME CRAP"));
JSONAssert.with(TEST_DOCUMENT).assertThat("stringField", equalTo("SOME CRAP"));
}
@Test
public void multiple_asserts_can_be_chained() throws Exception {
JSONAsserterImpl.with(TEST_DOCUMENT)
JSONAssert.with(TEST_DOCUMENT)
.assertThat("stringField", equalTo("string-field"))
.and()
.assertNull("nullField")

44
json-assert/src/test/java/com/jayway/jsonassert/JSONReaderTest.java

@ -29,21 +29,21 @@ public class JSONReaderTest {
@Test
public void a_string_field_can_be_accessed() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals("string-field", reader.getString("stringField"));
}
@Test
public void is_null_returns_true_for_null_fields() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertTrue(reader.isNull("nullField"));
}
@Test
public void is_null_returns_false_for_not_null_fields() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertFalse(reader.isNull("stringField"));
}
@ -51,42 +51,42 @@ public class JSONReaderTest {
@Test
public void a_long_field_can_be_accessed() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertTrue(1234L == reader.getLong("numberField"));
}
@Test
public void a_boolean_field_can_be_accessed() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals(true, reader.getBoolean("booleanField"));
}
@Test
public void a_path_can_be_checked_for_existence() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DEEP_PATH_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DEEP_PATH_DOCUMENT);
assertTrue(reader.hasJsonPath("a.b.c.say"));
}
@Test
public void a_path_can_be_checked_for_non_existence() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DEEP_PATH_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DEEP_PATH_DOCUMENT);
assertFalse(reader.hasJsonPath("a.b.c.FOO"));
}
@Test
public void a_string_field_can_be_accessed_in_a_nested_document() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals("sub-field", reader.getString("subDocument.subField"));
}
@Test
public void a_list_can_be_accessed_in_a_document() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
List<String> l = reader.<String>getList("stringList");
@ -95,7 +95,7 @@ public class JSONReaderTest {
@Test
public void a_list_can_be_accessed_by_array_index() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals("ONE", reader.getString("stringList[0]"));
@ -104,7 +104,7 @@ public class JSONReaderTest {
@Test
public void a_list_can_be_accessed_by_groovy_index() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals("ONE", reader.getString("stringList.get(0)"));
@ -113,21 +113,21 @@ public class JSONReaderTest {
@Test
public void a_document_contained_in_a_list_can_be_accessed_by_array_index() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals("sub-field-0", reader.getString("objectList[0].subField"));
}
@Test
public void a_document_contained_in_a_list_can_be_accessed_by_groovy_index() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals("sub-field-0", reader.getString("objectList.get(0).subField"));
}
@Test
public void an_array_in_an_array_can_be_accessed_by_array_index() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT_ARRAY);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT_ARRAY);
assertEquals("0.0", reader.getString("listList[0][0]"));
assertEquals("0.1", reader.getString("listList[0][1]"));
@ -137,7 +137,7 @@ public class JSONReaderTest {
@Test
public void an_array_in_an_array_can_be_accessed_by_groovy_index() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT_ARRAY);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT_ARRAY);
assertEquals("0.0", reader.getString("listList.get(0).get(0)"));
assertEquals("0.1", reader.getString("listList.get(0).get(1)"));
@ -147,7 +147,7 @@ public class JSONReaderTest {
@Test
public void an_array_with_documents_can_be_accessed_by_index() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_ARRAY);
JSONReader reader = JSONAssert.parse(TEST_ARRAY);
assertEquals("name0", reader.getString("[0].name"));
assertEquals("name1", reader.getString("[1].name"));
@ -155,7 +155,7 @@ public class JSONReaderTest {
@Test
public void a_nested_document_can_be_accessed_as_a_map() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals("sub-field", reader.getMap("subDocument").get("subField"));
}
@ -163,7 +163,7 @@ public class JSONReaderTest {
@Test
public void every_thing_can_be_fetched_as_object() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals(true, reader.get("booleanField"));
assertEquals(1234L, reader.get("numberField"));
@ -172,14 +172,14 @@ public class JSONReaderTest {
@Test
public void a_deep_document_path_can_be_read() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DEEP_PATH_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DEEP_PATH_DOCUMENT);
assertEquals("hello", reader.getString("a.b.c.say"));
}
@Test(expected = InvalidPathException.class)
public void exception_is_thrown_when_field_is_not_found() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
String s = reader.getString("invalidProperty");
@ -190,7 +190,7 @@ public class JSONReaderTest {
@Test
public void array_wildcard_property_extract() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
List<String> l = reader.<String>getList("objectList[*].subField");
@ -211,7 +211,7 @@ public class JSONReaderTest {
@Test
public void list_to_string_returns_json() throws Exception {
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals("[\"ONE\",\"TWO\"]", reader.getList("stringList").toString());

Loading…
Cancel
Save