Skip to content

Commit acb2663

Browse files
committed
moved files from yoga
0 parents  commit acb2663

7 files changed

Lines changed: 775 additions & 0 deletions

File tree

pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.skyscreamer</groupId>
8+
<artifactId>jsonassert</artifactId>
9+
<version>0.9.0-SNAPSHOT</version>
10+
11+
<name>JSONassert</name>
12+
<description>A library to develop RESTful but flexible APIs</description>
13+
<url>http://github.com/skyscreamer/yoga</url>
14+
15+
<!-- For JSONAssert -->
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.json</groupId>
19+
<artifactId>json</artifactId>
20+
<version>20090211</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>commons-collections</groupId>
24+
<artifactId>commons-collections</artifactId>
25+
<version>3.0</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<version>4.0</version>
32+
<scope>test</scope>
33+
</dependency>
34+
</dependencies>
35+
36+
</project>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package org.skyscreamer.jsonassert;
2+
3+
import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT;
4+
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;
5+
6+
import org.json.JSONArray;
7+
import org.json.JSONException;
8+
import org.json.JSONObject;
9+
10+
/**
11+
* <p>A set of assertion methods useful for writing tests methods that return JSON.</p>
12+
*
13+
* <p>There are two modes, strict and non-strict. In most cases, you will probably want
14+
* to set strict to <i>false</i>, since that will make the tests less brittle.</p>
15+
*
16+
* <p>Strict tests require all of the elements requested to be returned, and only those elements
17+
* (ie, the tests are non-extensible). Arrays of elements must be returned in the same
18+
* order as expected. For example, say I'm expecting:</p>
19+
*
20+
* <code>{id:123,things['a','b','c']}</code>
21+
*
22+
* <p>The following would match when doing non-strict checking, but would fail on strict checking:</p>
23+
*
24+
* <code>{id:123,things['c','b','a'],anotherfield:'blah'}</code>
25+
*
26+
* <p><i>This library uses org.json. It has fewer dependencies than other JSON libraries (like net.sf.json),
27+
* making JSONassert more portable.</i></p>
28+
*
29+
* <p>There are two known issues when dealing with non-strict comparisons:</p>
30+
* <ul>
31+
* <li>Unless the order is strict, checking does not handle mixed types in the JSONArray
32+
* (e.g. <code>[1,2,{a:"b"}]</code> or <code>[{pet:"cat"},{car:"Ford"}]</code>)</li>
33+
* <li>Unless the order is strict, checking cannot handle arrays of arrays (e.g. <code>[[1,2],[3,4]]</code>)</li>
34+
* </ul>
35+
* <p>You do not have to worry about encountering a false positive or false negative in these two edge cases.
36+
* <i>JSONassert</i> will identify the conditions and throw a descriptive {@link IllegalArgumentException}. These
37+
* cases will be fixed in future versions.</p>
38+
*
39+
*/
40+
public class JSONAssert {
41+
/**
42+
* Asserts that the JSONObject provided matches the expected string. If it isn't it throws an
43+
* {@link AssertionError}.
44+
*
45+
* @param expectedStr Expected JSON string
46+
* @param actual JSONObject to compare
47+
* @param strict Enables strict checking
48+
* @throws JSONException
49+
*/
50+
public static void assertEquals(String expectedStr, JSONObject actual, boolean strict)
51+
throws JSONException
52+
{
53+
Object expected = JSONParser.parseJSON(expectedStr);
54+
if (expected instanceof JSONObject) {
55+
assertEquals((JSONObject)expected, actual, strict);
56+
}
57+
else {
58+
throw new AssertionError("Expecting a JSON array, but passing in a JSON object");
59+
}
60+
}
61+
62+
/**
63+
* Asserts that the JSONArray provided matches the expected string. If it isn't it throws an
64+
* {@link AssertionError}.
65+
*
66+
* @param expectedStr Expected JSON string
67+
* @param actual JSONArray to compare
68+
* @param strict Enables strict checking
69+
* @throws JSONException
70+
*/
71+
public static void assertEquals(String expectedStr, JSONArray actual, boolean strict)
72+
throws JSONException
73+
{
74+
Object expected = JSONParser.parseJSON(expectedStr);
75+
if (expected instanceof JSONArray) {
76+
assertEquals((JSONArray)expected, actual, strict);
77+
}
78+
else {
79+
throw new AssertionError("Expecting a JSON object, but passing in a JSON array");
80+
}
81+
}
82+
83+
/**
84+
* Asserts that the JSONArray provided matches the expected string. If it isn't it throws an
85+
* {@link AssertionError}.
86+
*
87+
* @param expectedStr Expected JSON string
88+
* @param actualStr String to compare
89+
* @param strict Enables strict checking
90+
* @throws JSONException
91+
*/
92+
public static void assertEquals(String expectedStr, String actualStr, boolean strict)
93+
throws JSONException
94+
{
95+
JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
96+
if (result.failed()) {
97+
throw new AssertionError(result.getMessage());
98+
}
99+
}
100+
101+
/**
102+
* Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an
103+
* {@link AssertionError}.
104+
*
105+
* @param expected Expected JSONObject
106+
* @param actual JSONObject to compare
107+
* @param strict Enables strict checking
108+
* @throws JSONException
109+
*/
110+
public static void assertEquals(JSONObject expected, JSONObject actual, boolean strict)
111+
throws JSONException
112+
{
113+
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
114+
if (result.failed()) {
115+
throw new AssertionError(result.getMessage());
116+
}
117+
}
118+
119+
/**
120+
* Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws an
121+
* {@link AssertionError}.
122+
*
123+
* @param expected Expected JSONArray
124+
* @param actual JSONArray to compare
125+
* @param strict Enables strict checking
126+
* @throws JSONException
127+
*/
128+
public static void assertEquals(JSONArray expected, JSONArray actual, boolean strict)
129+
throws JSONException
130+
{
131+
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
132+
if (result.failed()) {
133+
throw new AssertionError(result.getMessage());
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)