1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json.test;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import junit.framework.AssertionFailedError;
23 import junit.framework.TestCase;
24 import net.sf.json.JSON;
25 import net.sf.json.JSONArray;
26 import net.sf.json.JSONFunction;
27 import net.sf.json.JSONNull;
28 import net.sf.json.JSONObject;
29
30
31
32
33 public class TestJSONAssert extends TestCase {
34 public static void main( String[] args ) {
35 junit.textui.TestRunner.run( TestJSONAssert.class );
36 }
37
38 public TestJSONAssert( String testName ) {
39 super( testName );
40 }
41
42 public void testArrayWithNullsShouldFail() {
43 JSONArray one = new JSONArray();
44 one.element( "hello" );
45 one.element( (Object) null );
46 one.element( "world" );
47
48 JSONArray two = new JSONArray();
49 two.element( "hello" );
50 two.element( (Object) null );
51 two.element( "world!" );
52
53 try{
54 JSONAssert.assertEquals( one, two );
55 fail( "Did not throw an AssertionFailedError" );
56 }catch( AssertionFailedError e ){
57 assertTrue( e.getMessage()
58 .startsWith( "arrays first differed at element [2]" ) );
59 }
60 }
61
62 public void testArrayWithNullsShouldPass() {
63 JSONArray one = new JSONArray();
64 one.element( "hello" );
65 one.element( (Object) null );
66 one.element( "world" );
67
68 JSONArray two = new JSONArray();
69 two.element( "hello" );
70 two.element( (Object) null );
71 two.element( "world" );
72
73 JSONAssert.assertEquals( one, two );
74 }
75
76 public void testAssertEquals_JSON_JSON__actual_null() {
77 try{
78 JSON expected = JSONArray.fromObject( "[1,2,3]" );
79 JSONAssert.assertEquals( expected, null );
80 }catch( AssertionFailedError e ){
81 assertEquals( e.getMessage(), "actual was null" );
82 }
83 }
84
85 public void testAssertEquals_JSON_JSON__expected_null() {
86 try{
87 JSON actual = JSONObject.fromObject( "{\"str\":\"json\"}" );
88 JSONAssert.assertEquals( null, actual );
89 }catch( AssertionFailedError e ){
90 assertEquals( e.getMessage(), "expected was null" );
91 }
92 }
93
94 public void testAssertEquals_JSON_JSON__JSONArray_JSONArray() {
95 try{
96 JSON expected = JSONArray.fromObject( "[1,2,3]" );
97 JSON actual = JSONArray.fromObject( "[1,2,3]" );
98 JSONAssert.assertEquals( expected, actual );
99 }catch( AssertionFailedError e ){
100 fail( "Arrays should be equal" );
101 }
102 }
103
104 public void testAssertEquals_JSON_JSON__JSONArray_JSONObject() {
105 try{
106 JSON expected = JSONArray.fromObject( "[1,2,3]" );
107 JSON actual = JSONObject.fromObject( "{\"str\":\"json\"}" );
108 JSONAssert.assertEquals( expected, actual );
109 }catch( AssertionFailedError e ){
110 assertEquals( e.getMessage(), "actual is not a JSONArray" );
111 }
112 }
113
114 public void testAssertEquals_JSON_JSON__JSONNull_JSONArray() {
115 try{
116 JSON expected = JSONNull.getInstance();
117 JSON actual = JSONArray.fromObject( "[1,2,3]" );
118
119 JSONAssert.assertEquals( expected, actual );
120 }catch( AssertionFailedError e ){
121 assertEquals( e.getMessage(), "actual is not a JSONNull" );
122 }
123 }
124
125 public void testAssertEquals_JSON_JSON__JSONNull_JSONNull() {
126 try{
127 JSON expected = JSONNull.getInstance();
128 JSON actual = JSONNull.getInstance();
129 JSONAssert.assertEquals( expected, actual );
130 }catch( AssertionFailedError e ){
131 fail( "JSONNull should be equal to itself" );
132 }
133 }
134
135 public void testAssertEquals_JSON_JSON__JSONObject_JSONArray() {
136 try{
137 JSON expected = JSONObject.fromObject( "{\"str\":\"json\"}" );
138 JSON actual = JSONArray.fromObject( "[1,2,3]" );
139
140 JSONAssert.assertEquals( expected, actual );
141 }catch( AssertionFailedError e ){
142 assertEquals( e.getMessage(), "actual is not a JSONObject" );
143 }
144 }
145
146 public void testAssertEquals_JSON_JSON__JSONObject_JSONObject() {
147 try{
148 JSON expected = JSONObject.fromObject( "{\"str\":\"json\"}" );
149 JSON actual = JSONObject.fromObject( "{\"str\":\"json\"}" );
150 JSONAssert.assertEquals( expected, actual );
151 }catch( AssertionFailedError e ){
152 fail( "Objects should be equal" );
153 }
154 }
155
156 public void testAssertEquals_JSONArray_JSONArray() {
157 Object[] values = new Object[] { Boolean.valueOf( true ), new Integer( Integer.MAX_VALUE ),
158 new Long( Long.MAX_VALUE ), new Float( Float.MAX_VALUE ),
159 new Double( Double.MAX_VALUE ), "json", new JSONArray(), new JSONObject( true ),
160 new JSONObject(), new JSONObject().element( "str", "json" ), "function(){ return this; }",
161 new JSONFunction( "return that;" ), new int[] { 1, 2 } };
162 JSONArray expected = JSONArray.fromObject( values );
163 JSONArray actual = JSONArray.fromObject( values );
164
165 try{
166 JSONAssert.assertEquals( expected, actual );
167 }catch( AssertionFailedError e ){
168 fail( "Arrays should be equal" );
169 }
170 }
171
172 public void testAssertEquals_JSONArray_JSONArray__actual_null() {
173 try{
174 JSONArray expected = JSONArray.fromObject( "[1,2,3]" );
175 JSONAssert.assertEquals( expected, (JSONArray) null );
176 }catch( AssertionFailedError e ){
177 assertEquals( e.getMessage(), "actual array was null" );
178 }
179 }
180
181 public void testAssertEquals_JSONArray_JSONArray__different_length() {
182 try{
183 JSONArray expected = JSONArray.fromObject( "[1]" );
184 JSONArray actual = new JSONArray();
185 JSONAssert.assertEquals( expected, actual );
186 }catch( AssertionFailedError e ){
187 assertEquals( e.getMessage(),
188 "arrays sizes differed, expected.length()=1 actual.length()=0" );
189 }
190 }
191
192 public void testAssertEquals_JSONArray_JSONArray__expected_null() {
193 try{
194 JSONArray actual = JSONArray.fromObject( "[1,2,3]" );
195 JSONAssert.assertEquals( (JSONArray) null, actual );
196 }catch( AssertionFailedError e ){
197 assertEquals( e.getMessage(), "expected array was null" );
198 }
199 }
200
201 public void testAssertEquals_JSONArray_JSONArray__nulls() {
202 try{
203 JSONArray expected = JSONArray.fromObject( "[1]" );
204 JSONArray actual = new JSONArray().element( JSONNull.getInstance() );
205 JSONAssert.assertEquals( expected, actual );
206 }catch( AssertionFailedError e ){
207 assertEquals( e.getMessage(), "arrays first differed at element [0];" );
208 }
209
210 try{
211 JSONArray expected = new JSONArray().element( JSONNull.getInstance() );
212 JSONArray actual = JSONArray.fromObject( "[1]" );
213 JSONAssert.assertEquals( expected, actual );
214 }catch( AssertionFailedError e ){
215 assertEquals( e.getMessage(), "arrays first differed at element [0];" );
216 }
217 }
218
219 public void testAssertEquals_JSONArray_String() {
220 try{
221 JSONArray expected = JSONArray.fromObject( "[1,2,3]" );
222 String actual = "[1,2,3]";
223 JSONAssert.assertEquals( expected, actual );
224 }catch( AssertionFailedError e ){
225 fail( "Arrays should be equal" );
226 }
227 }
228
229 public void testAssertEquals_JSONArray_String_fail() {
230 try{
231 JSONArray expected = JSONArray.fromObject( "[1,2,3]" );
232 String actual = "{1,2,3}";
233 JSONAssert.assertEquals( expected, actual );
234 }catch( AssertionFailedError e ){
235 assertEquals( e.getMessage(), "actual is not a JSONArray" );
236 }
237 }
238
239 public void testAssertEquals_JSONFunction_String() {
240 try{
241 JSONFunction expected = new JSONFunction( "return this;" );
242 String actual = "function(){ return this; }";
243 JSONAssert.assertEquals( expected, actual );
244 }catch( AssertionFailedError e ){
245 fail( "Functions should be equal" );
246 }
247 }
248
249 public void testAssertEquals_JSONFunction_String__actual_null() {
250 try{
251 JSONFunction expected = new JSONFunction( ";" );
252 JSONAssert.assertEquals( expected, (String) null );
253 }catch( AssertionFailedError e ){
254 assertEquals( e.getMessage(), "actual string was null" );
255 }
256 }
257
258 public void testAssertEquals_JSONFunction_String__expected_null() {
259 try{
260 JSONAssert.assertEquals( (JSONFunction) null, ";" );
261 }catch( AssertionFailedError e ){
262 assertEquals( e.getMessage(), "expected function was null" );
263 }
264 }
265
266 public void testAssertEquals_JSONFunction_String_fail() {
267 try{
268 JSONFunction expected = new JSONFunction( "return this;" );
269 String actual = "function(){ return this;";
270 JSONAssert.assertEquals( expected, actual );
271 }catch( AssertionFailedError e ){
272 assertEquals( e.getMessage(), "'function(){ return this;' is not a function" );
273 }
274 }
275
276 public void testAssertEquals_JSONNull_String() {
277 try{
278 JSONNull expected = JSONNull.getInstance();
279 String actual = "null";
280 JSONAssert.assertEquals( expected, actual );
281 }catch( AssertionFailedError e ){
282 fail( "Should be equal" );
283 }
284 }
285
286 public void testAssertEquals_JSONNull_String__actual_null() {
287 try{
288 JSONNull expected = JSONNull.getInstance();
289 JSONAssert.assertEquals( expected, (String) null );
290 }catch( AssertionFailedError e ){
291 assertEquals( e.getMessage(), "actual string was null" );
292 }
293 }
294
295 public void testAssertEquals_JSONNull_String__expected_null() {
296 try{
297 JSONAssert.assertEquals( (JSONNull) null, "null" );
298 }catch( AssertionFailedError e ){
299 fail( "Should be equal" );
300 }
301 }
302
303 public void testAssertEquals_JSONObject_JSONObject_() {
304 try{
305 String[] names = new String[] { "b", "i", "l", "f", "d", "s", "a1", "o1", "o2", "o3",
306 "u1", "u2" };
307 Object[] values = new Object[] { Boolean.valueOf( true ),
308 new Integer( Integer.MAX_VALUE ), new Long( Long.MAX_VALUE ),
309 new Float( Float.MAX_VALUE ), new Double( Double.MAX_VALUE ), "json",
310 new JSONArray(), new JSONObject( true ), new JSONObject(),
311 new JSONObject().element( "str", "json" ), "function(){ return this; }",
312 new JSONFunction( "return that;" ) };
313 Map map = new HashMap();
314 for( int i = 0; i < names.length; i++ ){
315 map.put( names[i], values[i] );
316 }
317 JSONObject expected = JSONObject.fromObject( map );
318 JSONObject actual = JSONObject.fromObject( map );
319 JSONAssert.assertEquals( expected, actual );
320 }catch( AssertionFailedError e ){
321 fail( "Objects should be equal" );
322 }
323 }
324
325 public void testAssertEquals_JSONObject_JSONObject__actual_null() {
326 try{
327 JSONObject expected = JSONObject.fromObject( "{}" );
328 JSONAssert.assertEquals( expected, (JSONObject) null );
329 }catch( AssertionFailedError e ){
330 assertEquals( e.getMessage(), "actual object was null" );
331 }
332 }
333
334 public void testAssertEquals_JSONObject_JSONObject__expected_null() {
335 try{
336 JSONObject actual = JSONObject.fromObject( "{}" );
337 JSONAssert.assertEquals( (JSONObject) null, actual );
338 }catch( AssertionFailedError e ){
339 assertEquals( e.getMessage(), "expected object was null" );
340 }
341 }
342
343 public void testAssertEquals_JSONObject_JSONObject_names() {
344 try{
345 JSONObject expected = new JSONObject().element( "str", "json" );
346 JSONObject actual = new JSONObject();
347 JSONAssert.assertEquals( expected, actual );
348 }catch( AssertionFailedError e ){
349 assertEquals(
350 e.getMessage(),
351 "names sizes differed, expected.names().length()=1 actual.names().length()=0 expected:<1> but was:<0>" );
352 }
353 }
354
355 public void testAssertEquals_JSONObject_JSONObject_nullObjects() {
356 try{
357 JSONObject expected = new JSONObject( true );
358 JSONObject actual = new JSONObject( true );
359 JSONAssert.assertEquals( expected, actual );
360 }catch( AssertionFailedError e ){
361 fail( "Objects should be equal" );
362 }
363 }
364
365 public void testAssertEquals_JSONObject_JSONObject_nullObjects_fail1() {
366 try{
367 JSONObject expected = new JSONObject();
368 JSONObject actual = new JSONObject( true );
369 JSONAssert.assertEquals( expected, actual );
370 }catch( AssertionFailedError e ){
371 assertEquals( e.getMessage(), "actual is a null JSONObject" );
372 }
373 }
374
375 public void testAssertEquals_JSONObject_JSONObject_nullObjects_fail2() {
376 try{
377 JSONObject expected = new JSONObject( true );
378 JSONObject actual = new JSONObject();
379 JSONAssert.assertEquals( expected, actual );
380 }catch( AssertionFailedError e ){
381 assertEquals( e.getMessage(), "actual is not a null JSONObject" );
382 }
383 }
384
385 public void testAssertEquals_JSONObject_String() {
386 try{
387 JSONObject expected = JSONObject.fromObject( "{\"str\":\"json\"}" );
388 String actual = "{\"str\":\"json\"}";
389 JSONAssert.assertEquals( expected, actual );
390 }catch( AssertionFailedError e ){
391 fail( "Objects should be equal" );
392 }
393 }
394
395 public void testAssertEquals_JSONObject_String_fail() {
396 try{
397 JSONObject expected = JSONObject.fromObject( "{\"str\":\"json\"}" );
398 String actual = "[1,2,3]";
399 JSONAssert.assertEquals( expected, actual );
400 }catch( AssertionFailedError e ){
401 assertEquals( e.getMessage(), "actual is not a JSONObject" );
402 }
403 }
404
405 public void testAssertEquals_String_JSONArray() {
406 try{
407 String expected = "[1,2,3]";
408 JSONArray actual = JSONArray.fromObject( "[1,2,3]" );
409 JSONAssert.assertEquals( expected, actual );
410 }catch( AssertionFailedError e ){
411 fail( "Arrays should be equal" );
412 }
413 }
414
415 public void testAssertEquals_String_JSONArray_fail() {
416 try{
417 String expected = "{1,2,3}";
418 JSONArray actual = JSONArray.fromObject( "[1,2,3]" );
419 JSONAssert.assertEquals( expected, actual );
420 }catch( AssertionFailedError e ){
421 assertEquals( e.getMessage(), "expected is not a JSONArray" );
422 }
423 }
424
425 public void testAssertEquals_String_JSONFunction() {
426 try{
427 String expected = "function(){ return this; }";
428 JSONFunction actual = new JSONFunction( "return this;" );
429 JSONAssert.assertEquals( expected, actual );
430 }catch( AssertionFailedError e ){
431 fail( "Functions should be equal" );
432 }
433 }
434
435 public void testAssertEquals_String_JSONFunction_fail() {
436 try{
437 JSONFunction actual = new JSONFunction( "return this;" );
438 String expected = "function(){ return this;";
439 JSONAssert.assertEquals( expected, actual );
440 }catch( AssertionFailedError e ){
441 assertEquals( e.getMessage(), "'function(){ return this;' is not a function" );
442 }
443 }
444
445 public void testAssertEquals_String_JSONNull() {
446 try{
447 String expected = "null";
448 JSONNull actual = JSONNull.getInstance();
449 JSONAssert.assertEquals( expected, actual );
450 }catch( AssertionFailedError e ){
451 fail( "Should be equal" );
452 }
453 }
454
455 public void testAssertEquals_String_JSONObject() {
456 try{
457 String expected = "{\"str\":\"json\"}";
458 JSONObject actual = JSONObject.fromObject( "{\"str\":\"json\"}" );
459 JSONAssert.assertEquals( expected, actual );
460 }catch( AssertionFailedError e ){
461 fail( "Objects should be equal" );
462 }
463 }
464
465 public void testAssertEquals_String_JSONObject_fail() {
466 try{
467 String expected = "[1,2,3]";
468 JSONObject actual = JSONObject.fromObject( "{\"str\":\"json\"}" );
469 JSONAssert.assertEquals( expected, actual );
470 }catch( AssertionFailedError e ){
471 assertEquals( e.getMessage(), "expected is not a JSONObject" );
472 }
473 }
474
475 public void testAssertJsonEquals_garbage_json() {
476 try{
477 String expected = "garbage";
478 String actual = "null";
479 JSONAssert.assertJsonEquals( expected, actual );
480 }catch( AssertionFailedError e ){
481 assertEquals( e.getMessage(), "expected is not a valid JSON string" );
482 }
483 }
484
485 public void testAssertJsonEquals_json_garbage() {
486 try{
487 String expected = "null";
488 String actual = "garbage";
489 JSONAssert.assertJsonEquals( expected, actual );
490 }catch( AssertionFailedError e ){
491 assertEquals( e.getMessage(), "actual is not a valid JSON string" );
492 }
493 }
494
495 public void testAssertJsonEquals_jsonArray_jsonArray() {
496 try{
497 String expected = "[1,2,3]";
498 String actual = "[1,2,3]";
499 JSONAssert.assertJsonEquals( expected, actual );
500 }catch( AssertionFailedError e ){
501 fail( "Strings should be valid JSON and equal" );
502 }
503 }
504
505 public void testAssertJsonEquals_jsonNull_jsonNull() {
506 try{
507 String expected = "null";
508 String actual = "null";
509 JSONAssert.assertJsonEquals( expected, actual );
510 }catch( AssertionFailedError e ){
511 fail( "Strings should be valid JSON and equal" );
512 }
513 }
514
515 public void testAssertJsonEquals_jsonObject_jsonObject() {
516 try{
517 String expected = "{\"str\":\"json\"}";
518 String actual = "{\"str\":\"json\"}";
519 JSONAssert.assertJsonEquals( expected, actual );
520 }catch( AssertionFailedError e ){
521 fail( "Strings should be valid JSON and equal" );
522 }
523 }
524
525 public void testAssertNotNull_JSONNull() {
526 try{
527 JSONAssert.assertNotNull( JSONNull.getInstance() );
528 fail( "Parameter is null and assertion did not fail" );
529 }catch( AssertionFailedError e ){
530 if( !e.getMessage()
531 .equals( "Object is null" ) ){
532 fail( "Parameter is null and assertion did not fail" );
533 }
534 }
535 }
536
537 public void testAssertNotNull_jsonObject_null() {
538 try{
539 JSONAssert.assertNotNull( new JSONObject( true ) );
540 fail( "Parameter is null and assertion did not fail" );
541 }catch( AssertionFailedError e ){
542 if( !e.getMessage()
543 .equals( "Object is null" ) ){
544 fail( "Parameter is null and assertion did not fail" );
545 }
546 }
547 }
548
549 public void testAssertNotNull_null() {
550 try{
551 JSONAssert.assertNotNull( null );
552 fail( "Parameter is null and assertion did not fail" );
553 }catch( AssertionFailedError e ){
554 if( !e.getMessage()
555 .equals( "Object is null" ) ){
556 fail( "Parameter is null and assertion did not fail" );
557 }
558 }
559 }
560
561 public void testAssertNull_JSONNull() {
562 try{
563 JSONAssert.assertNull( JSONNull.getInstance() );
564 }catch( AssertionFailedError e ){
565 fail( "Parameter is null and assertion failed" );
566 }
567 }
568
569 public void testAssertNull_jsonObject_null() {
570 try{
571 JSONAssert.assertNull( new JSONObject( true ) );
572 }catch( AssertionFailedError e ){
573 fail( "Parameter is null and assertion failed" );
574 }
575 }
576
577 public void testAssertNull_null() {
578 try{
579 JSONAssert.assertNull( null );
580 }catch( AssertionFailedError e ){
581 fail( "Parameter is null and assertion failed" );
582 }
583 }
584 }