Skip to content

Commit d2dbe4e

Browse files
committed
Merge pull request skyscreamer#36 from ststo/master
AssertNotEquals
2 parents f04b814 + 56123c9 commit d2dbe4e

File tree

2 files changed

+270
-1
lines changed

2 files changed

+270
-1
lines changed

src/main/java/org/skyscreamer/jsonassert/JSONAssert.java

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ public static void assertEquals(String expectedStr, JSONObject actual, boolean s
5252
assertEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
5353
}
5454

55+
/**
56+
* Asserts that the JSONObject provided does not match the expected string. If it is it throws an
57+
* {@link AssertionError}.
58+
*
59+
* @see #assertEquals(String JSONObject, boolean)
60+
*
61+
* @param expectedStr Expected JSON string
62+
* @param actual JSONObject to compare
63+
* @param strict Enables strict checking
64+
* @throws JSONException
65+
*/
66+
public static void assertNotEquals(String expectedStr, JSONObject actual, boolean strict)
67+
throws JSONException {
68+
assertNotEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
69+
}
70+
5571
/**
5672
* Asserts that the JSONObject provided matches the expected string. If it isn't it throws an
5773
* {@link AssertionError}.
@@ -72,6 +88,28 @@ public static void assertEquals(String expectedStr, JSONObject actual, JSONCompa
7288
}
7389
}
7490

91+
/**
92+
* Asserts that the JSONObject provided does not match the expected string. If it is it throws an
93+
* {@link AssertionError}.
94+
*
95+
* @see #assertEquals(String, JSONObject, JSONCompareMode)
96+
*
97+
* @param expectedStr Expected JSON string
98+
* @param actual JSONObject to compare
99+
* @param compareMode Specifies which comparison mode to use
100+
* @throws JSONException
101+
*/
102+
public static void assertNotEquals(String expectedStr, JSONObject actual, JSONCompareMode compareMode)
103+
throws JSONException {
104+
Object expected = JSONParser.parseJSON(expectedStr);
105+
if (expected instanceof JSONObject) {
106+
assertNotEquals((JSONObject) expected, actual, compareMode);
107+
}
108+
else {
109+
throw new AssertionError("Expecting a JSON array, but passing in a JSON object");
110+
}
111+
}
112+
75113
/**
76114
* Asserts that the JSONArray provided matches the expected string. If it isn't it throws an
77115
* {@link AssertionError}.
@@ -86,6 +124,20 @@ public static void assertEquals(String expectedStr, JSONArray actual, boolean st
86124
assertEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
87125
}
88126

127+
/**
128+
* Asserts that the JSONArray provided does not match the expected string. If it is it throws an
129+
* {@link AssertionError}.
130+
*
131+
* @param expectedStr Expected JSON string
132+
* @param actual JSONArray to compare
133+
* @param strict Enables strict checking
134+
* @throws JSONException
135+
*/
136+
public static void assertNotEquals(String expectedStr, JSONArray actual, boolean strict)
137+
throws JSONException {
138+
assertNotEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
139+
}
140+
89141
/**
90142
* Asserts that the JSONArray provided matches the expected string. If it isn't it throws an
91143
* {@link AssertionError}.
@@ -99,7 +151,27 @@ public static void assertEquals(String expectedStr, JSONArray actual, JSONCompar
99151
throws JSONException {
100152
Object expected = JSONParser.parseJSON(expectedStr);
101153
if (expected instanceof JSONArray) {
102-
assertEquals((JSONArray)expected, actual, compareMode);
154+
assertEquals((JSONArray) expected, actual, compareMode);
155+
}
156+
else {
157+
throw new AssertionError("Expecting a JSON object, but passing in a JSON array");
158+
}
159+
}
160+
161+
/**
162+
* Asserts that the JSONArray provided does not match the expected string. If it is it throws an
163+
* {@link AssertionError}.
164+
*
165+
* @param expectedStr Expected JSON string
166+
* @param actual JSONArray to compare
167+
* @param compareMode Specifies which comparison mode to use
168+
* @throws JSONException
169+
*/
170+
public static void assertNotEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode)
171+
throws JSONException {
172+
Object expected = JSONParser.parseJSON(expectedStr);
173+
if (expected instanceof JSONArray) {
174+
assertNotEquals((JSONArray) expected, actual, compareMode);
103175
}
104176
else {
105177
throw new AssertionError("Expecting a JSON object, but passing in a JSON array");
@@ -120,6 +192,20 @@ public static void assertEquals(String expectedStr, String actualStr, boolean st
120192
assertEquals(expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
121193
}
122194

195+
/**
196+
* Asserts that the JSONArray provided does not match the expected string. If it is it throws an
197+
* {@link AssertionError}.
198+
*
199+
* @param expectedStr Expected JSON string
200+
* @param actualStr String to compare
201+
* @param strict Enables strict checking
202+
* @throws JSONException
203+
*/
204+
public static void assertNotEquals(String expectedStr, String actualStr, boolean strict)
205+
throws JSONException {
206+
assertNotEquals(expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
207+
}
208+
123209
/**
124210
* Asserts that the JSONArray provided matches the expected string. If it isn't it throws an
125211
* {@link AssertionError}.
@@ -137,6 +223,23 @@ public static void assertEquals(String expectedStr, String actualStr, JSONCompar
137223
}
138224
}
139225

226+
/**
227+
* Asserts that the JSONArray provided does not match the expected string. If it is it throws an
228+
* {@link AssertionError}.
229+
*
230+
* @param expectedStr Expected JSON string
231+
* @param actualStr String to compare
232+
* @param compareMode Specifies which comparison mode to use
233+
* @throws JSONException
234+
*/
235+
public static void assertNotEquals(String expectedStr, String actualStr, JSONCompareMode compareMode)
236+
throws JSONException {
237+
JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, compareMode);
238+
if (result.passed()) {
239+
throw new AssertionError(result.getMessage());
240+
}
241+
}
242+
140243
/**
141244
* Asserts that the json string provided matches the expected string. If it isn't it throws an
142245
* {@link AssertionError}.
@@ -154,6 +257,23 @@ public static void assertEquals(String expectedStr, String actualStr, JSONCompar
154257
}
155258
}
156259

260+
/**
261+
* Asserts that the json string provided does not match the expected string. If it is it throws an
262+
* {@link AssertionError}.
263+
*
264+
* @param expectedStr Expected JSON string
265+
* @param actualStr String to compare
266+
* @param comparator Comparator
267+
* @throws JSONException
268+
*/
269+
public static void assertNotEquals(String expectedStr, String actualStr, JSONComparator comparator)
270+
throws JSONException {
271+
JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, comparator);
272+
if (result.passed()) {
273+
throw new AssertionError(result.getMessage());
274+
}
275+
}
276+
157277
/**
158278
* Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an
159279
* {@link AssertionError}.
@@ -168,6 +288,20 @@ public static void assertEquals(JSONObject expected, JSONObject actual, boolean
168288
assertEquals(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
169289
}
170290

291+
/**
292+
* Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws an
293+
* {@link AssertionError}.
294+
*
295+
* @param expected Expected JSONObject
296+
* @param actual JSONObject to compare
297+
* @param strict Enables strict checking
298+
* @throws JSONException
299+
*/
300+
public static void assertNotEquals(JSONObject expected, JSONObject actual, boolean strict)
301+
throws JSONException {
302+
assertNotEquals(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
303+
}
304+
171305
/**
172306
* Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an
173307
* {@link AssertionError}.
@@ -186,6 +320,24 @@ public static void assertEquals(JSONObject expected, JSONObject actual, JSONComp
186320
}
187321
}
188322

323+
/**
324+
* Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws an
325+
* {@link AssertionError}.
326+
*
327+
* @param expected Expected JSONObject
328+
* @param actual JSONObject to compare
329+
* @param compareMode Specifies which comparison mode to use
330+
* @throws JSONException
331+
*/
332+
public static void assertNotEquals(JSONObject expected, JSONObject actual, JSONCompareMode compareMode)
333+
throws JSONException
334+
{
335+
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode);
336+
if (result.passed()) {
337+
throw new AssertionError(result.getMessage());
338+
}
339+
}
340+
189341
/**
190342
* Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws an
191343
* {@link AssertionError}.
@@ -200,6 +352,20 @@ public static void assertEquals(JSONArray expected, JSONArray actual, boolean st
200352
assertEquals(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
201353
}
202354

355+
/**
356+
* Asserts that the JSONArray provided does not match the expected JSONArray. If it is it throws an
357+
* {@link AssertionError}.
358+
*
359+
* @param expected Expected JSONArray
360+
* @param actual JSONArray to compare
361+
* @param strict Enables strict checking
362+
* @throws JSONException
363+
*/
364+
public static void assertNotEquals(JSONArray expected, JSONArray actual, boolean strict)
365+
throws JSONException {
366+
assertNotEquals(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
367+
}
368+
203369
/**
204370
* Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws an
205371
* {@link AssertionError}.
@@ -216,4 +382,21 @@ public static void assertEquals(JSONArray expected, JSONArray actual, JSONCompar
216382
throw new AssertionError(result.getMessage());
217383
}
218384
}
385+
386+
/**
387+
* Asserts that the JSONArray provided does not match the expected JSONArray. If it is it throws an
388+
* {@link AssertionError}.
389+
*
390+
* @param expected Expected JSONArray
391+
* @param actual JSONArray to compare
392+
* @param compareMode Specifies which comparison mode to use
393+
* @throws JSONException
394+
*/
395+
public static void assertNotEquals(JSONArray expected, JSONArray actual, JSONCompareMode compareMode)
396+
throws JSONException {
397+
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode);
398+
if (result.passed()) {
399+
throw new AssertionError(result.getMessage());
400+
}
401+
}
219402
}

src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.skyscreamer.jsonassert;
22

3+
import java.util.Arrays;
4+
5+
import org.json.JSONArray;
36
import org.json.JSONException;
47
import org.json.JSONObject;
58
import org.junit.Assert;
@@ -243,6 +246,89 @@ public void testEquivalentIntAndDouble() throws JSONException {
243246
JSONAssert.assertEquals(actual, expected, true);
244247
}
245248

249+
@Test(expected = AssertionError.class)
250+
public void testAssertNotEqualsWhenEqualStrict() throws JSONException {
251+
JSONObject expected = new JSONObject();
252+
JSONObject actual = new JSONObject();
253+
expected.put("id", new Integer(12345));
254+
actual.put("id", new Double(12345));
255+
JSONAssert.assertNotEquals(expected, actual, true);
256+
}
257+
258+
@Test(expected = AssertionError.class)
259+
public void testAssertNotEqualsWhenEqualLenient() throws JSONException {
260+
JSONObject expected = new JSONObject();
261+
JSONObject actual = new JSONObject();
262+
expected.put("id", new Integer(12345));
263+
actual.put("id", new Double(12345));
264+
JSONAssert.assertNotEquals(expected, actual, false);
265+
}
266+
267+
@Test()
268+
public void testAssertNotEqualsWhenEqualDiffObjectsStrict() throws JSONException {
269+
JSONObject expected = new JSONObject();
270+
JSONObject actual = new JSONObject();
271+
expected.put("id", new Integer(12345));
272+
expected.put("name", "Joe");
273+
actual.put("id", new Double(12345));
274+
JSONAssert.assertNotEquals(expected, actual, true);
275+
}
276+
277+
@Test(expected = AssertionError.class)
278+
public void testAssertNotEqualsWhenEqualDiffObjectsLenient() throws JSONException {
279+
JSONObject expected = new JSONObject();
280+
JSONObject actual = new JSONObject();
281+
expected.put("id", new Integer(12345));
282+
expected.put("name", "Joe");
283+
actual.put("name", "Joe");
284+
actual.put("id", new Double(12345));
285+
JSONAssert.assertNotEquals(expected, actual, false);
286+
}
287+
288+
@Test()
289+
public void testAssertNotEqualsWhenDifferentStrict() throws JSONException {
290+
JSONObject expected = new JSONObject();
291+
JSONObject actual = new JSONObject();
292+
expected.put("id", new Integer(12345));
293+
actual.put("id", new Double(12346));
294+
JSONAssert.assertNotEquals(expected, actual, true);
295+
}
296+
297+
@Test()
298+
public void testAssertNotEqualsWhenDifferentLenient() throws JSONException {
299+
JSONObject expected = new JSONObject();
300+
JSONObject actual = new JSONObject();
301+
expected.put("id", new Integer(12345));
302+
actual.put("id", new Double(12346));
303+
JSONAssert.assertNotEquals(expected, actual, false);
304+
}
305+
306+
@Test()
307+
public void testAssertNotEqualsString() throws JSONException {
308+
JSONAssert.assertNotEquals("[1,2,3]", "[1,3,2]", STRICT);
309+
JSONAssert.assertNotEquals("[1,2,3]", "[1,2,4]", LENIENT);
310+
JSONAssert.assertNotEquals("[1,2,3]", "[1,3,2]", true);
311+
JSONAssert.assertNotEquals("[1,2,3]", "[1,2,4]", false);
312+
}
313+
314+
@Test()
315+
public void testAssertNotEqualsStringAndJSONObject() throws JSONException {
316+
JSONObject actual = new JSONObject();
317+
actual.put("id", new Double(12345));
318+
JSONAssert.assertEquals("{id:12345}", actual, false);
319+
JSONAssert.assertNotEquals("{id:12346}", actual, false);
320+
}
321+
322+
@Test()
323+
public void testAssertNotEqualsJSONArray() throws JSONException {
324+
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
325+
JSONAssert.assertEquals("[1,2,3]", actual, false);
326+
JSONAssert.assertNotEquals("[1,2,4]", actual, false);
327+
JSONAssert.assertNotEquals("[1,3,2]", actual, true);
328+
JSONAssert.assertNotEquals(new JSONArray(Arrays.asList(1, 2, 4)), actual, false);
329+
JSONAssert.assertNotEquals(new JSONArray(Arrays.asList(1, 3, 2)), actual, true);
330+
}
331+
246332
private void testPass(String expected, String actual, JSONCompareMode compareMode)
247333
throws JSONException
248334
{

0 commit comments

Comments
 (0)