Browse Source

improved code

pull/1/merge
kalle 14 years ago
parent
commit
7b69b7f577
  1. 2
      README
  2. 13
      json-assert/pom.xml
  3. 24
      json-assert/src/main/java/com/jayway/jsonassert/JSONAssert.java
  4. 14
      json-assert/src/main/java/com/jayway/jsonassert/JSONReader.java
  5. 7
      json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReaderImpl.java
  6. 10
      json-assert/src/test/java/com/jayway/jsonassert/JSONAssertTest.java
  7. 33
      json-assert/src/test/java/com/jayway/jsonassert/JSONReaderTest.java
  8. 18
      json-assert/src/test/resources/json-test-doc.json
  9. 28
      pom.xml

2
README

@ -1 +1 @@
Java DSL for easy testing and reading of JSON data
Java DSL for testing and reading JSON documents

13
json-assert/pom.xml

@ -34,31 +34,24 @@
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>

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

@ -34,7 +34,29 @@ public class JSONAssert {
* @throws IOException
*/
public static JSONReader parse(Reader reader) throws ParseException, IOException {
return JSONReaderImpl.parse(reader);
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 JSONReader parse(Reader reader, boolean closeReader) throws ParseException, IOException {
JSONReader jsonReader = null;
try {
jsonReader = JSONReaderImpl.parse(reader);
} finally {
if(closeReader){
try {
reader.close();
} catch (IOException ignore) {}
}
}
return jsonReader;
}
/**

14
json-assert/src/main/java/com/jayway/jsonassert/JSONReader.java

@ -12,49 +12,48 @@ import java.util.Map;
public interface JSONReader {
/**
*
* @param path
* @return
*/
boolean hasJsonPath(String path);
/**
*
* @param path
* @return
*/
boolean isNull(String path);
/**
*
* @param path
* @return
*/
Object get(String path);
/**
*
* @param path
* @return
*/
String getString(String path);
/**
*
* @param path
* @return
*/
Long getLong(String path);
/**
*
* @param path
* @return
*/
Double getDouble(String path);
/**
* @param path
* @return
*/
Boolean getBoolean(String path);
/**
*
* @param path
* @param <T>
* @return
@ -62,7 +61,6 @@ public interface JSONReader {
<T> List<T> getList(String path);
/**
*
* @param path
* @return
*/

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

@ -93,6 +93,13 @@ public class JSONReaderImpl implements JSONReader {
return getByPath(Long.class, path);
}
/**
* {@inheritDoc}
*/
public Double getDouble(String path) {
return getByPath(Double.class, path);
}
/**
* {@inheritDoc}
*/

10
json-assert/src/test/java/com/jayway/jsonassert/JSONAssertTest.java

@ -21,7 +21,7 @@ public class JSONAssertTest {
}
@Test
public void array_content_can_be_asserted_with_matcher() throws Exception {
public void list_content_can_be_asserted_with_matcher() throws Exception {
JSONAssert.with(TEST_DOCUMENT).assertThat("stringList", hasItems("ONE", "TWO"));
}
@ -32,6 +32,11 @@ public class JSONAssertTest {
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 {
@ -50,16 +55,15 @@ public class JSONAssertTest {
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()
.assertThat("numberField", is(notNullValue()))
.assertEquals("stringField", "string-field");
}

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

@ -1,8 +1,11 @@
package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JSONReaderImpl;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.Test;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.List;
@ -16,7 +19,7 @@ import static org.junit.Assert.*;
public class JSONReaderTest {
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\"]], }";
private static String TEST_DOCUMENT = "{ \"nullField\" : null \"stringField\" : \"string-field\" , \"numberField\" : 1234 , \"doubleField\" : 12.34 , \"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\"]], }";
private static String TEST_DOCUMENT_ARRAY = "{ \"listList\" : [[\"0.0\", \"0.1\"], [\"1.0\", \"1.1\"]], }";
private static String TEST_DEEP_PATH_DOCUMENT = "{ \"a\" : { \"b\" : { \"c\" : { \"say\" : \"hello\" } } }}";
private static String TEST_ARRAY = "[{\"name\" : \"name0\"}, {\"name\" : \"name1\"}]";
@ -27,6 +30,17 @@ public class JSONReaderTest {
JSONReaderImpl.parse("not json");
}
@Test
public void reader_can_be_created_with_input_stream() throws Exception {
JSONReader reader = JSONAssert.parse(getInputStreamReader("json-test-doc.json"), true);
assertEquals("donut", reader.getString("type"));
assertThat(reader.<String>getList("toppings"), Matchers.hasItems("Glazed", "Sugar"));
}
@Test
public void a_string_field_can_be_accessed() throws Exception {
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
@ -56,6 +70,13 @@ public class JSONReaderTest {
assertTrue(1234L == reader.getLong("numberField"));
}
@Test
public void a_double_field_can_be_accessed() throws Exception {
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
assertEquals(12.34D, reader.getDouble("doubleField"), 0.001);
}
@Test
public void a_boolean_field_can_be_accessed() throws Exception {
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT);
@ -216,4 +237,14 @@ public class JSONReaderTest {
assertEquals("[\"ONE\",\"TWO\"]", reader.getList("stringList").toString());
}
//----------------------------------------------------------
//
// helpers
//
//----------------------------------------------------------
private InputStreamReader getInputStreamReader(String resource) {
return new InputStreamReader(ClassLoader.getSystemResourceAsStream(resource));
}
}

18
json-assert/src/test/resources/json-test-doc.json

@ -0,0 +1,18 @@
{
"id": 100,
"type": "donut",
"name": "Cake",
"available": true,
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"toppings": ["Glazed", "Sugar", "Chocolate", "Maple"]
}

28
pom.xml

@ -13,7 +13,9 @@
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ 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>
<parent>
<groupId>org.sonatype.oss</groupId>
@ -70,8 +72,8 @@
<modules>
<module>json-assert</module>
<module>examples</module>
</modules>
<module>examples</module>
</modules>
<build>
<pluginManagement>
@ -127,12 +129,32 @@
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Loading…
Cancel
Save