View Javadoc

1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.json.test;
18  
19  import java.util.Iterator;
20  
21  import junit.framework.Assert;
22  import net.sf.ezmorph.Morpher;
23  import net.sf.ezmorph.object.IdentityObjectMorpher;
24  import net.sf.json.JSON;
25  import net.sf.json.JSONArray;
26  import net.sf.json.JSONException;
27  import net.sf.json.JSONFunction;
28  import net.sf.json.JSONNull;
29  import net.sf.json.JSONObject;
30  import net.sf.json.JSONSerializer;
31  import net.sf.json.util.JSONUtils;
32  
33  /**
34   * Provides assertions on equality for JSON strings and JSON types.
35   *
36   * @author Andres Almiray <aalmiray@users.sourceforge.net>
37   */
38  public class JSONAssert extends Assert {
39     /**
40      * Asserts that two JSON values are equal.
41      */
42     public static void assertEquals( JSON expected, JSON actual ) {
43        assertEquals( null, expected, actual );
44     }
45  
46     /**
47      * Asserts that two JSONArrays are equal.
48      */
49     public static void assertEquals( JSONArray expected, JSONArray actual ) {
50        assertEquals( null, expected, actual );
51     }
52  
53     /**
54      * Asserts that two JSONArrays are equal.
55      */
56     public static void assertEquals( JSONArray expected, String actual ) {
57        assertEquals( null, expected, actual );
58     }
59  
60     /**
61      * Asserts that two JSONFunctions are equal.
62      */
63     public static void assertEquals( JSONFunction expected, String actual ) {
64        assertEquals( null, expected, actual );
65     }
66  
67     /**
68      * Asserts that two JSONNulls are equal.
69      */
70     public static void assertEquals( JSONNull expected, String actual ) {
71        assertEquals( null, expected, actual );
72     }
73  
74     /**
75      * Asserts that two JSONObjects are equal.
76      */
77     public static void assertEquals( JSONObject expected, JSONObject actual ) {
78        assertEquals( null, expected, actual );
79     }
80  
81     /**
82      * Asserts that two JSONObjects are equal.
83      */
84     public static void assertEquals( JSONObject expected, String actual ) {
85        assertEquals( null, expected, actual );
86     }
87  
88     /**
89      * Asserts that two JSON values are equal.
90      */
91     public static void assertEquals( String message, JSON expected, JSON actual ) {
92        String header = message == null ? "" : message + ": ";
93        if( expected == null ){
94           fail( header + "expected was null" );
95        }
96        if( actual == null ){
97           fail( header + "actual was null" );
98        }
99        if( expected == actual || expected.equals( actual ) ){
100          return;
101       }
102 
103       if( expected instanceof JSONArray ){
104          if( actual instanceof JSONArray ){
105             assertEquals( header, (JSONArray) expected, (JSONArray) actual );
106          }else{
107             fail( header + "actual is not a JSONArray" );
108          }
109       }else if( expected instanceof JSONObject ){
110          if( actual instanceof JSONObject ){
111             assertEquals( header, (JSONObject) expected, (JSONObject) actual );
112          }else{
113             fail( header + "actual is not a JSONObject" );
114          }
115       }else if( expected instanceof JSONNull ){
116          if( actual instanceof JSONNull ){
117             return;
118          }else{
119             fail( header + "actual is not a JSONNull" );
120          }
121       }
122    }
123 
124    /**
125     * Asserts that two JSONArrays are equal.
126     */
127    public static void assertEquals( String expected, JSONArray actual ) {
128       assertEquals( null, expected, actual );
129    }
130 
131    /**
132     * Asserts that two JSONArrays are equal.
133     */
134    public static void assertEquals( String message, JSONArray expected, JSONArray actual ) {
135       String header = message == null ? "" : message + ": ";
136       if( expected == null ){
137          fail( header + "expected array was null" );
138       }
139       if( actual == null ){
140          fail( header + "actual array was null" );
141       }
142       if( expected == actual || expected.equals( actual ) ){
143          return;
144       }
145       if( actual.size() != expected.size() ){
146          fail( header + "arrays sizes differed, expected.length()=" + expected.size()
147                + " actual.length()=" + actual.size() );
148       }
149 
150       int max = expected.size();
151       for( int i = 0; i < max; i++ ){
152          Object o1 = expected.get( i );
153          Object o2 = actual.get( i );
154 
155          // handle nulls
156          if( JSONNull.getInstance()
157                .equals( o1 ) ){
158             if( JSONNull.getInstance()
159                   .equals( o2 ) ){
160                continue;
161             }else{
162                fail( header + "arrays first differed at element [" + i + "];" );
163             }
164          }else{
165             if( JSONNull.getInstance()
166                   .equals( o2 ) ){
167                fail( header + "arrays first differed at element [" + i + "];" );
168             }
169          }
170 
171          if( o1 instanceof JSONArray && o2 instanceof JSONArray ){
172             JSONArray e = (JSONArray) o1;
173             JSONArray a = (JSONArray) o2;
174             assertEquals( header + "arrays first differed at element " + i + ";", e, a );
175          }else{
176             if( o1 instanceof String && o2 instanceof JSONFunction ){
177                assertEquals( header + "arrays first differed at element [" + i + "];", (String) o1,
178                      (JSONFunction) o2 );
179             }else if( o1 instanceof JSONFunction && o2 instanceof String ){
180                assertEquals( header + "arrays first differed at element [" + i + "];",
181                      (JSONFunction) o1, (String) o2 );
182             }else if( o1 instanceof JSONObject && o2 instanceof JSONObject ){
183                assertEquals( header + "arrays first differed at element [" + i + "];",
184                      (JSONObject) o1, (JSONObject) o2 );
185             }else if( o1 instanceof JSONArray && o2 instanceof JSONArray ){
186                assertEquals( header + "arrays first differed at element [" + i + "];",
187                      (JSONArray) o1, (JSONArray) o2 );
188             }else if( o1 instanceof JSONFunction && o2 instanceof JSONFunction ){
189                assertEquals( header + "arrays first differed at element [" + i + "];",
190                      (JSONFunction) o1, (JSONFunction) o2 );
191             }else{
192                if( o1 instanceof String ){
193                   assertEquals( header + "arrays first differed at element [" + i + "];",
194                         (String) o1, String.valueOf( o2 ) );
195                }else if( o2 instanceof String ){
196                   assertEquals( header + "arrays first differed at element [" + i + "];",
197                         String.valueOf( o1 ), (String) o2 );
198                }else{
199                   Morpher m1 = JSONUtils.getMorpherRegistry()
200                         .getMorpherFor( o1.getClass() );
201                   Morpher m2 = JSONUtils.getMorpherRegistry()
202                         .getMorpherFor( o2.getClass() );
203                   if( m1 != null && m1 != IdentityObjectMorpher.getInstance() ){
204                      assertEquals( header + "arrays first differed at element [" + i + "];", o1,
205                            JSONUtils.getMorpherRegistry()
206                                  .morph( o1.getClass(), o2 ) );
207                   }else if( m2 != null && m2 != IdentityObjectMorpher.getInstance() ){
208                      assertEquals( header + "arrays first differed at element [" + i + "];",
209                            JSONUtils.getMorpherRegistry()
210                                  .morph( o1.getClass(), o1 ), o2 );
211                   }else{
212                      assertEquals( header + "arrays first differed at element [" + i + "];", o1, o2 );
213                   }
214                }
215             }
216          }
217       }
218    }
219 
220    /**
221     * Asserts that two JSONArrays are equal.
222     */
223    public static void assertEquals( String message, JSONArray expected, String actual ) {
224       try{
225          assertEquals( message, expected, JSONArray.fromObject( actual ) );
226       }catch( JSONException e ){
227          String header = message == null ? "" : message + ": ";
228          fail( header + "actual is not a JSONArray" );
229       }
230    }
231 
232    /**
233     * Asserts that two JSONFunctions are equal.
234     */
235    public static void assertEquals( String expected, JSONFunction actual ) {
236       assertEquals( null, expected, actual );
237    }
238 
239    /**
240     * Asserts that two JSONFunctions are equal.
241     */
242    public static void assertEquals( String message, JSONFunction expected, String actual ) {
243       String header = message == null ? "" : message + ": ";
244       if( expected == null ){
245          fail( header + "expected function was null" );
246       }
247       if( actual == null ){
248          fail( header + "actual string was null" );
249       }
250 
251       try{
252          assertEquals( header, expected, JSONFunction.parse( actual ) );
253       }catch( JSONException jsone ){
254          fail( header + "'" + actual + "' is not a function" );
255       }
256    }
257 
258    /**
259     * Asserts that two JSONNulls are equal.
260     */
261    public static void assertEquals( String expected, JSONNull actual ) {
262       assertEquals( null, expected, actual );
263    }
264 
265    /**
266     * Asserts that two JSONNulls are equal.
267     */
268    public static void assertEquals( String message, JSONNull expected, String actual ) {
269       String header = message == null ? "" : message + ": ";
270       if( actual == null ){
271          fail( header + "actual string was null" );
272       }else if( expected == null ){
273          assertEquals( header, "null", actual );
274       }else{
275          assertEquals( header, expected.toString(), actual );
276       }
277    }
278 
279    /**
280     * Asserts that two JSONObjects are equal.
281     */
282    public static void assertEquals( String expected, JSONObject actual ) {
283       assertEquals( null, expected, actual );
284    }
285 
286    /**
287     * Asserts that two JSONObjects are equal.
288     */
289    public static void assertEquals( String message, JSONObject expected, JSONObject actual ) {
290       String header = message == null ? "" : message + ": ";
291       if( expected == null ){
292          fail( header + "expected object was null" );
293       }
294       if( actual == null ){
295          fail( header + "actual object was null" );
296       }
297       if( expected == actual /* || expected.equals( actual ) */){
298          return;
299       }
300 
301       if( expected.isNullObject() ){
302          if( actual.isNullObject() ){
303             return;
304          }else{
305             fail( header + "actual is not a null JSONObject" );
306          }
307       }else{
308          if( actual.isNullObject() ){
309             fail( header + "actual is a null JSONObject" );
310          }
311       }
312 
313       assertEquals( header + "names sizes differed, expected.names().length()=" + expected.names()
314             .size() + " actual.names().length()=" + actual.names()
315             .size(), expected.names()
316             .size(), actual.names()
317             .size() );
318       for( Iterator keys = expected.keys(); keys.hasNext(); ){
319          String key = (String) keys.next();
320          Object o1 = expected.opt( key );
321          Object o2 = actual.opt( key );
322 
323          if( JSONNull.getInstance()
324                .equals( o1 ) ){
325             if( JSONNull.getInstance()
326                   .equals( o2 ) ){
327                continue;
328             }else{
329                fail( header + "objects differed at key [" + key + "];" );
330             }
331          }else{
332             if( JSONNull.getInstance()
333                   .equals( o2 ) ){
334                fail( header + "objects differed at key [" + key + "];" );
335             }
336          }
337 
338          if( o1 instanceof String && o2 instanceof JSONFunction ){
339             assertEquals( header + "objects differed at key [" + key + "];", (String) o1,
340                   (JSONFunction) o2 );
341          }else if( o1 instanceof JSONFunction && o2 instanceof String ){
342             assertEquals( header + "objects differed at key [" + key + "];", (JSONFunction) o1,
343                   (String) o2 );
344          }else if( o1 instanceof JSONObject && o2 instanceof JSONObject ){
345             assertEquals( header + "objects differed at key [" + key + "];", (JSONObject) o1,
346                   (JSONObject) o2 );
347          }else if( o1 instanceof JSONArray && o2 instanceof JSONArray ){
348             assertEquals( header + "objects differed at key [" + key + "];", (JSONArray) o1,
349                   (JSONArray) o2 );
350          }else if( o1 instanceof JSONFunction && o2 instanceof JSONFunction ){
351             assertEquals( header + "objects differed at key [" + key + "];", (JSONFunction) o1,
352                   (JSONFunction) o2 );
353          }else{
354             if( o1 instanceof String ){
355                assertEquals( header + "objects differed at key [" + key + "];", (String) o1,
356                      String.valueOf( o2 ) );
357             }else if( o2 instanceof String ){
358                assertEquals( header + "objects differed at key [" + key + "];",
359                      String.valueOf( o1 ), (String) o2 );
360             }else{
361                Morpher m1 = JSONUtils.getMorpherRegistry()
362                      .getMorpherFor( o1.getClass() );
363                Morpher m2 = JSONUtils.getMorpherRegistry()
364                      .getMorpherFor( o2.getClass() );
365                if( m1 != null && m1 != IdentityObjectMorpher.getInstance() ){
366                   assertEquals( header + "objects differed at key [" + key + "];", o1,
367                         JSONUtils.getMorpherRegistry()
368                               .morph( o1.getClass(), o2 ) );
369                }else if( m2 != null && m2 != IdentityObjectMorpher.getInstance() ){
370                   assertEquals( header + "objects differed at key [" + key + "];",
371                         JSONUtils.getMorpherRegistry()
372                               .morph( o1.getClass(), o1 ), o2 );
373                }else{
374                   assertEquals( header + "objects differed at key [" + key + "];", o1, o2 );
375                }
376             }
377          }
378       }
379    }
380 
381    /**
382     * Asserts that two JSONObjects are equal.
383     */
384    public static void assertEquals( String message, JSONObject expected, String actual ) {
385       try{
386          assertEquals( message, expected, JSONObject.fromObject( actual ) );
387       }catch( JSONException e ){
388          String header = message == null ? "" : message + ": ";
389          fail( header + "actual is not a JSONObject" );
390       }
391    }
392 
393    /**
394     * Asserts that two JSONArrays are equal.
395     */
396    public static void assertEquals( String message, String expected, JSONArray actual ) {
397       try{
398          assertEquals( message, JSONArray.fromObject( expected ), actual );
399       }catch( JSONException e ){
400          String header = message == null ? "" : message + ": ";
401          fail( header + "expected is not a JSONArray" );
402       }
403    }
404 
405    /**
406     * Asserts that two JSONFunctions are equal.
407     */
408    public static void assertEquals( String message, String expected, JSONFunction actual ) {
409       String header = message == null ? "" : message + ": ";
410       if( expected == null ){
411          fail( header + "expected string was null" );
412       }
413       if( actual == null ){
414          fail( header + "actual function was null" );
415       }
416 
417       try{
418          assertEquals( header, JSONFunction.parse( expected ), actual );
419       }catch( JSONException jsone ){
420          fail( header + "'" + expected + "' is not a function" );
421       }
422    }
423 
424    /**
425     * Asserts that two JSONNulls are equal.
426     */
427    public static void assertEquals( String message, String expected, JSONNull actual ) {
428       String header = message == null ? "" : message + ": ";
429       if( expected == null ){
430          fail( header + "expected was null" );
431       }else if( actual == null ){
432          assertEquals( header, expected, "null" );
433       }else{
434          assertEquals( header, expected, actual.toString() );
435       }
436    }
437 
438    /**
439     * Asserts that two JSONObjects are equal.
440     */
441    public static void assertEquals( String message, String expected, JSONObject actual ) {
442       try{
443          assertEquals( message, JSONObject.fromObject( expected ), actual );
444       }catch( JSONException e ){
445          String header = message == null ? "" : message + ": ";
446          fail( header + "expected is not a JSONObject" );
447       }
448    }
449 
450    /**
451     * Asserts that two JSON strings are equal.
452     */
453    public static void assertJsonEquals( String expected, String actual ) {
454       assertJsonEquals( null, expected, actual );
455    }
456 
457    /**
458     * Asserts that two JSON strings are equal.
459     */
460    public static void assertJsonEquals( String message, String expected, String actual ) {
461       String header = message == null ? "" : message + ": ";
462       if( expected == null ){
463          fail( header + "expected was null" );
464       }
465       if( actual == null ){
466          fail( header + "actual was null" );
467       }
468 
469       JSON json1 = null;
470       JSON json2 = null;
471       try{
472          json1 = JSONSerializer.toJSON( expected );
473       }catch( JSONException jsone ){
474          fail( header + "expected is not a valid JSON string" );
475       }
476       try{
477          json2 = JSONSerializer.toJSON( actual );
478       }catch( JSONException jsone ){
479          fail( header + "actual is not a valid JSON string" );
480       }
481       assertEquals( header, json1, json2 );
482    }
483 
484    /**
485     * Asserts that a JSON value is not null.<br>
486     * Fails if:
487     * <ul>
488     * <li>JSONNull.getInstance().equals( json )</li>
489     * <li>((JSONObject) json).isNullObject()</li>
490     * </ul>
491     */
492    public static void assertNotNull( JSON json ) {
493       assertNotNull( null, json );
494    }
495 
496    /**
497     * Asserts that a JSON value is not null.<br>
498     * Fails if:
499     * <ul>
500     * <li>JSONNull.getInstance().equals( json )</li>
501     * <li>((JSONObject) json).isNullObject()</li>
502     * </ul>
503     */
504    public static void assertNotNull( String message, JSON json ) {
505       String header = message == null ? "" : message + ": ";
506       if( json instanceof JSONObject ){
507          assertFalse( header + "Object is null", ((JSONObject) json).isNullObject() );
508       }else if( JSONNull.getInstance()
509             .equals( json ) ){
510          fail( header + "Object is null" );
511       }
512    }
513 
514    /**
515     * Asserts that a JSON value is null.<br>
516     * Fails if:
517     * <ul>
518     * <li>!JSONNull.getInstance().equals( json )</li>
519     * <li>!((JSONObject) json).isNullObject()</li>
520     * </ul>
521     */
522    public static void assertNull( JSON json ) {
523       assertNull( null, json );
524    }
525 
526    /**
527     * Asserts that a JSON value is null.<br>
528     * Fails if:
529     * <ul>
530     * <li>!JSONNull.getInstance().equals( json )</li>
531     * <li>!((JSONObject) json).isNullObject()</li>
532     * </ul>
533     */
534    public static void assertNull( String message, JSON json ) {
535       String header = message == null ? "" : message + ": ";
536       if( json instanceof JSONObject ){
537          assertTrue( header + "Object is not null", ((JSONObject) json).isNullObject() );
538       }else if( !JSONNull.getInstance()
539             .equals( json ) ){
540          fail( header + "Object is not null" );
541       }
542    }
543 }