Coverage Report - net.sf.json.test.JSONAssert
 
Classes in this File Line Coverage Branch Coverage Complexity
JSONAssert
0%
0/229
0%
0/182
3.607
 
 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  0
 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  0
       assertEquals( null, expected, actual );
 44  0
    }
 45  
 
 46  
    /**
 47  
     * Asserts that two JSONArrays are equal.
 48  
     */
 49  
    public static void assertEquals( JSONArray expected, JSONArray actual ) {
 50  0
       assertEquals( null, expected, actual );
 51  0
    }
 52  
 
 53  
    /**
 54  
     * Asserts that two JSONArrays are equal.
 55  
     */
 56  
    public static void assertEquals( JSONArray expected, String actual ) {
 57  0
       assertEquals( null, expected, actual );
 58  0
    }
 59  
 
 60  
    /**
 61  
     * Asserts that two JSONFunctions are equal.
 62  
     */
 63  
    public static void assertEquals( JSONFunction expected, String actual ) {
 64  0
       assertEquals( null, expected, actual );
 65  0
    }
 66  
 
 67  
    /**
 68  
     * Asserts that two JSONNulls are equal.
 69  
     */
 70  
    public static void assertEquals( JSONNull expected, String actual ) {
 71  0
       assertEquals( null, expected, actual );
 72  0
    }
 73  
 
 74  
    /**
 75  
     * Asserts that two JSONObjects are equal.
 76  
     */
 77  
    public static void assertEquals( JSONObject expected, JSONObject actual ) {
 78  0
       assertEquals( null, expected, actual );
 79  0
    }
 80  
 
 81  
    /**
 82  
     * Asserts that two JSONObjects are equal.
 83  
     */
 84  
    public static void assertEquals( JSONObject expected, String actual ) {
 85  0
       assertEquals( null, expected, actual );
 86  0
    }
 87  
 
 88  
    /**
 89  
     * Asserts that two JSON values are equal.
 90  
     */
 91  
    public static void assertEquals( String message, JSON expected, JSON actual ) {
 92  0
       String header = message == null ? "" : message + ": ";
 93  0
       if( expected == null ){
 94  0
          fail( header + "expected was null" );
 95  
       }
 96  0
       if( actual == null ){
 97  0
          fail( header + "actual was null" );
 98  
       }
 99  0
       if( expected == actual || expected.equals( actual ) ){
 100  0
          return;
 101  
       }
 102  
 
 103  0
       if( expected instanceof JSONArray ){
 104  0
          if( actual instanceof JSONArray ){
 105  0
             assertEquals( header, (JSONArray) expected, (JSONArray) actual );
 106  
          }else{
 107  0
             fail( header + "actual is not a JSONArray" );
 108  
          }
 109  0
       }else if( expected instanceof JSONObject ){
 110  0
          if( actual instanceof JSONObject ){
 111  0
             assertEquals( header, (JSONObject) expected, (JSONObject) actual );
 112  
          }else{
 113  0
             fail( header + "actual is not a JSONObject" );
 114  
          }
 115  0
       }else if( expected instanceof JSONNull ){
 116  0
          if( actual instanceof JSONNull ){
 117  0
             return;
 118  
          }else{
 119  0
             fail( header + "actual is not a JSONNull" );
 120  
          }
 121  
       }
 122  0
    }
 123  
 
 124  
    /**
 125  
     * Asserts that two JSONArrays are equal.
 126  
     */
 127  
    public static void assertEquals( String expected, JSONArray actual ) {
 128  0
       assertEquals( null, expected, actual );
 129  0
    }
 130  
 
 131  
    /**
 132  
     * Asserts that two JSONArrays are equal.
 133  
     */
 134  
    public static void assertEquals( String message, JSONArray expected, JSONArray actual ) {
 135  0
       String header = message == null ? "" : message + ": ";
 136  0
       if( expected == null ){
 137  0
          fail( header + "expected array was null" );
 138  
       }
 139  0
       if( actual == null ){
 140  0
          fail( header + "actual array was null" );
 141  
       }
 142  0
       if( expected == actual || expected.equals( actual ) ){
 143  0
          return;
 144  
       }
 145  0
       if( actual.size() != expected.size() ){
 146  0
          fail( header + "arrays sizes differed, expected.length()=" + expected.size()
 147  
                + " actual.length()=" + actual.size() );
 148  
       }
 149  
 
 150  0
       int max = expected.size();
 151  0
       for( int i = 0; i < max; i++ ){
 152  0
          Object o1 = expected.get( i );
 153  0
          Object o2 = actual.get( i );
 154  
 
 155  
          // handle nulls
 156  0
          if( JSONNull.getInstance()
 157  
                .equals( o1 ) ){
 158  0
             if( JSONNull.getInstance()
 159  
                   .equals( o2 ) ){
 160  0
                continue;
 161  
             }else{
 162  0
                fail( header + "arrays first differed at element [" + i + "];" );
 163  
             }
 164  
          }else{
 165  0
             if( JSONNull.getInstance()
 166  
                   .equals( o2 ) ){
 167  0
                fail( header + "arrays first differed at element [" + i + "];" );
 168  
             }
 169  
          }
 170  
 
 171  0
          if( o1 instanceof JSONArray && o2 instanceof JSONArray ){
 172  0
             JSONArray e = (JSONArray) o1;
 173  0
             JSONArray a = (JSONArray) o2;
 174  0
             assertEquals( header + "arrays first differed at element " + i + ";", e, a );
 175  0
          }else{
 176  0
             if( o1 instanceof String && o2 instanceof JSONFunction ){
 177  0
                assertEquals( header + "arrays first differed at element [" + i + "];", (String) o1,
 178  
                      (JSONFunction) o2 );
 179  0
             }else if( o1 instanceof JSONFunction && o2 instanceof String ){
 180  0
                assertEquals( header + "arrays first differed at element [" + i + "];",
 181  
                      (JSONFunction) o1, (String) o2 );
 182  0
             }else if( o1 instanceof JSONObject && o2 instanceof JSONObject ){
 183  0
                assertEquals( header + "arrays first differed at element [" + i + "];",
 184  
                      (JSONObject) o1, (JSONObject) o2 );
 185  0
             }else if( o1 instanceof JSONArray && o2 instanceof JSONArray ){
 186  0
                assertEquals( header + "arrays first differed at element [" + i + "];",
 187  
                      (JSONArray) o1, (JSONArray) o2 );
 188  0
             }else if( o1 instanceof JSONFunction && o2 instanceof JSONFunction ){
 189  0
                assertEquals( header + "arrays first differed at element [" + i + "];",
 190  
                      (JSONFunction) o1, (JSONFunction) o2 );
 191  
             }else{
 192  0
                if( o1 instanceof String ){
 193  0
                   assertEquals( header + "arrays first differed at element [" + i + "];",
 194  
                         (String) o1, String.valueOf( o2 ) );
 195  0
                }else if( o2 instanceof String ){
 196  0
                   assertEquals( header + "arrays first differed at element [" + i + "];",
 197  
                         String.valueOf( o1 ), (String) o2 );
 198  
                }else{
 199  0
                   Morpher m1 = JSONUtils.getMorpherRegistry()
 200  
                         .getMorpherFor( o1.getClass() );
 201  0
                   Morpher m2 = JSONUtils.getMorpherRegistry()
 202  
                         .getMorpherFor( o2.getClass() );
 203  0
                   if( m1 != null && m1 != IdentityObjectMorpher.getInstance() ){
 204  0
                      assertEquals( header + "arrays first differed at element [" + i + "];", o1,
 205  
                            JSONUtils.getMorpherRegistry()
 206  
                                  .morph( o1.getClass(), o2 ) );
 207  0
                   }else if( m2 != null && m2 != IdentityObjectMorpher.getInstance() ){
 208  0
                      assertEquals( header + "arrays first differed at element [" + i + "];",
 209  
                            JSONUtils.getMorpherRegistry()
 210  
                                  .morph( o1.getClass(), o1 ), o2 );
 211  
                   }else{
 212  0
                      assertEquals( header + "arrays first differed at element [" + i + "];", o1, o2 );
 213  
                   }
 214  
                }
 215  
             }
 216  
          }
 217  
       }
 218  0
    }
 219  
 
 220  
    /**
 221  
     * Asserts that two JSONArrays are equal.
 222  
     */
 223  
    public static void assertEquals( String message, JSONArray expected, String actual ) {
 224  
       try{
 225  0
          assertEquals( message, expected, JSONArray.fromObject( actual ) );
 226  0
       }catch( JSONException e ){
 227  0
          String header = message == null ? "" : message + ": ";
 228  0
          fail( header + "actual is not a JSONArray" );
 229  0
       }
 230  0
    }
 231  
 
 232  
    /**
 233  
     * Asserts that two JSONFunctions are equal.
 234  
     */
 235  
    public static void assertEquals( String expected, JSONFunction actual ) {
 236  0
       assertEquals( null, expected, actual );
 237  0
    }
 238  
 
 239  
    /**
 240  
     * Asserts that two JSONFunctions are equal.
 241  
     */
 242  
    public static void assertEquals( String message, JSONFunction expected, String actual ) {
 243  0
       String header = message == null ? "" : message + ": ";
 244  0
       if( expected == null ){
 245  0
          fail( header + "expected function was null" );
 246  
       }
 247  0
       if( actual == null ){
 248  0
          fail( header + "actual string was null" );
 249  
       }
 250  
 
 251  
       try{
 252  0
          assertEquals( header, expected, JSONFunction.parse( actual ) );
 253  0
       }catch( JSONException jsone ){
 254  0
          fail( header + "'" + actual + "' is not a function" );
 255  0
       }
 256  0
    }
 257  
 
 258  
    /**
 259  
     * Asserts that two JSONNulls are equal.
 260  
     */
 261  
    public static void assertEquals( String expected, JSONNull actual ) {
 262  0
       assertEquals( null, expected, actual );
 263  0
    }
 264  
 
 265  
    /**
 266  
     * Asserts that two JSONNulls are equal.
 267  
     */
 268  
    public static void assertEquals( String message, JSONNull expected, String actual ) {
 269  0
       String header = message == null ? "" : message + ": ";
 270  0
       if( actual == null ){
 271  0
          fail( header + "actual string was null" );
 272  0
       }else if( expected == null ){
 273  0
          assertEquals( header, "null", actual );
 274  
       }else{
 275  0
          assertEquals( header, expected.toString(), actual );
 276  
       }
 277  0
    }
 278  
 
 279  
    /**
 280  
     * Asserts that two JSONObjects are equal.
 281  
     */
 282  
    public static void assertEquals( String expected, JSONObject actual ) {
 283  0
       assertEquals( null, expected, actual );
 284  0
    }
 285  
 
 286  
    /**
 287  
     * Asserts that two JSONObjects are equal.
 288  
     */
 289  
    public static void assertEquals( String message, JSONObject expected, JSONObject actual ) {
 290  0
       String header = message == null ? "" : message + ": ";
 291  0
       if( expected == null ){
 292  0
          fail( header + "expected object was null" );
 293  
       }
 294  0
       if( actual == null ){
 295  0
          fail( header + "actual object was null" );
 296  
       }
 297  0
       if( expected == actual /* || expected.equals( actual ) */){
 298  0
          return;
 299  
       }
 300  
 
 301  0
       if( expected.isNullObject() ){
 302  0
          if( actual.isNullObject() ){
 303  0
             return;
 304  
          }else{
 305  0
             fail( header + "actual is not a null JSONObject" );
 306  
          }
 307  
       }else{
 308  0
          if( actual.isNullObject() ){
 309  0
             fail( header + "actual is a null JSONObject" );
 310  
          }
 311  
       }
 312  
 
 313  0
       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  0
       for( Iterator keys = expected.keys(); keys.hasNext(); ){
 319  0
          String key = (String) keys.next();
 320  0
          Object o1 = expected.opt( key );
 321  0
          Object o2 = actual.opt( key );
 322  
 
 323  0
          if( JSONNull.getInstance()
 324  
                .equals( o1 ) ){
 325  0
             if( JSONNull.getInstance()
 326  
                   .equals( o2 ) ){
 327  0
                continue;
 328  
             }else{
 329  0
                fail( header + "objects differed at key [" + key + "];" );
 330  
             }
 331  
          }else{
 332  0
             if( JSONNull.getInstance()
 333  
                   .equals( o2 ) ){
 334  0
                fail( header + "objects differed at key [" + key + "];" );
 335  
             }
 336  
          }
 337  
 
 338  0
          if( o1 instanceof String && o2 instanceof JSONFunction ){
 339  0
             assertEquals( header + "objects differed at key [" + key + "];", (String) o1,
 340  
                   (JSONFunction) o2 );
 341  0
          }else if( o1 instanceof JSONFunction && o2 instanceof String ){
 342  0
             assertEquals( header + "objects differed at key [" + key + "];", (JSONFunction) o1,
 343  
                   (String) o2 );
 344  0
          }else if( o1 instanceof JSONObject && o2 instanceof JSONObject ){
 345  0
             assertEquals( header + "objects differed at key [" + key + "];", (JSONObject) o1,
 346  
                   (JSONObject) o2 );
 347  0
          }else if( o1 instanceof JSONArray && o2 instanceof JSONArray ){
 348  0
             assertEquals( header + "objects differed at key [" + key + "];", (JSONArray) o1,
 349  
                   (JSONArray) o2 );
 350  0
          }else if( o1 instanceof JSONFunction && o2 instanceof JSONFunction ){
 351  0
             assertEquals( header + "objects differed at key [" + key + "];", (JSONFunction) o1,
 352  
                   (JSONFunction) o2 );
 353  
          }else{
 354  0
             if( o1 instanceof String ){
 355  0
                assertEquals( header + "objects differed at key [" + key + "];", (String) o1,
 356  
                      String.valueOf( o2 ) );
 357  0
             }else if( o2 instanceof String ){
 358  0
                assertEquals( header + "objects differed at key [" + key + "];",
 359  
                      String.valueOf( o1 ), (String) o2 );
 360  
             }else{
 361  0
                Morpher m1 = JSONUtils.getMorpherRegistry()
 362  
                      .getMorpherFor( o1.getClass() );
 363  0
                Morpher m2 = JSONUtils.getMorpherRegistry()
 364  
                      .getMorpherFor( o2.getClass() );
 365  0
                if( m1 != null && m1 != IdentityObjectMorpher.getInstance() ){
 366  0
                   assertEquals( header + "objects differed at key [" + key + "];", o1,
 367  
                         JSONUtils.getMorpherRegistry()
 368  
                               .morph( o1.getClass(), o2 ) );
 369  0
                }else if( m2 != null && m2 != IdentityObjectMorpher.getInstance() ){
 370  0
                   assertEquals( header + "objects differed at key [" + key + "];",
 371  
                         JSONUtils.getMorpherRegistry()
 372  
                               .morph( o1.getClass(), o1 ), o2 );
 373  
                }else{
 374  0
                   assertEquals( header + "objects differed at key [" + key + "];", o1, o2 );
 375  
                }
 376  
             }
 377  
          }
 378  0
       }
 379  0
    }
 380  
 
 381  
    /**
 382  
     * Asserts that two JSONObjects are equal.
 383  
     */
 384  
    public static void assertEquals( String message, JSONObject expected, String actual ) {
 385  
       try{
 386  0
          assertEquals( message, expected, JSONObject.fromObject( actual ) );
 387  0
       }catch( JSONException e ){
 388  0
          String header = message == null ? "" : message + ": ";
 389  0
          fail( header + "actual is not a JSONObject" );
 390  0
       }
 391  0
    }
 392  
 
 393  
    /**
 394  
     * Asserts that two JSONArrays are equal.
 395  
     */
 396  
    public static void assertEquals( String message, String expected, JSONArray actual ) {
 397  
       try{
 398  0
          assertEquals( message, JSONArray.fromObject( expected ), actual );
 399  0
       }catch( JSONException e ){
 400  0
          String header = message == null ? "" : message + ": ";
 401  0
          fail( header + "expected is not a JSONArray" );
 402  0
       }
 403  0
    }
 404  
 
 405  
    /**
 406  
     * Asserts that two JSONFunctions are equal.
 407  
     */
 408  
    public static void assertEquals( String message, String expected, JSONFunction actual ) {
 409  0
       String header = message == null ? "" : message + ": ";
 410  0
       if( expected == null ){
 411  0
          fail( header + "expected string was null" );
 412  
       }
 413  0
       if( actual == null ){
 414  0
          fail( header + "actual function was null" );
 415  
       }
 416  
 
 417  
       try{
 418  0
          assertEquals( header, JSONFunction.parse( expected ), actual );
 419  0
       }catch( JSONException jsone ){
 420  0
          fail( header + "'" + expected + "' is not a function" );
 421  0
       }
 422  0
    }
 423  
 
 424  
    /**
 425  
     * Asserts that two JSONNulls are equal.
 426  
     */
 427  
    public static void assertEquals( String message, String expected, JSONNull actual ) {
 428  0
       String header = message == null ? "" : message + ": ";
 429  0
       if( expected == null ){
 430  0
          fail( header + "expected was null" );
 431  0
       }else if( actual == null ){
 432  0
          assertEquals( header, expected, "null" );
 433  
       }else{
 434  0
          assertEquals( header, expected, actual.toString() );
 435  
       }
 436  0
    }
 437  
 
 438  
    /**
 439  
     * Asserts that two JSONObjects are equal.
 440  
     */
 441  
    public static void assertEquals( String message, String expected, JSONObject actual ) {
 442  
       try{
 443  0
          assertEquals( message, JSONObject.fromObject( expected ), actual );
 444  0
       }catch( JSONException e ){
 445  0
          String header = message == null ? "" : message + ": ";
 446  0
          fail( header + "expected is not a JSONObject" );
 447  0
       }
 448  0
    }
 449  
 
 450  
    /**
 451  
     * Asserts that two JSON strings are equal.
 452  
     */
 453  
    public static void assertJsonEquals( String expected, String actual ) {
 454  0
       assertJsonEquals( null, expected, actual );
 455  0
    }
 456  
 
 457  
    /**
 458  
     * Asserts that two JSON strings are equal.
 459  
     */
 460  
    public static void assertJsonEquals( String message, String expected, String actual ) {
 461  0
       String header = message == null ? "" : message + ": ";
 462  0
       if( expected == null ){
 463  0
          fail( header + "expected was null" );
 464  
       }
 465  0
       if( actual == null ){
 466  0
          fail( header + "actual was null" );
 467  
       }
 468  
 
 469  0
       JSON json1 = null;
 470  0
       JSON json2 = null;
 471  
       try{
 472  0
          json1 = JSONSerializer.toJSON( expected );
 473  0
       }catch( JSONException jsone ){
 474  0
          fail( header + "expected is not a valid JSON string" );
 475  0
       }
 476  
       try{
 477  0
          json2 = JSONSerializer.toJSON( actual );
 478  0
       }catch( JSONException jsone ){
 479  0
          fail( header + "actual is not a valid JSON string" );
 480  0
       }
 481  0
       assertEquals( header, json1, json2 );
 482  0
    }
 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  0
       assertNotNull( null, json );
 494  0
    }
 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  0
       String header = message == null ? "" : message + ": ";
 506  0
       if( json instanceof JSONObject ){
 507  0
          assertFalse( header + "Object is null", ((JSONObject) json).isNullObject() );
 508  0
       }else if( JSONNull.getInstance()
 509  
             .equals( json ) ){
 510  0
          fail( header + "Object is null" );
 511  
       }
 512  0
    }
 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  0
       assertNull( null, json );
 524  0
    }
 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  0
       String header = message == null ? "" : message + ": ";
 536  0
       if( json instanceof JSONObject ){
 537  0
          assertTrue( header + "Object is not null", ((JSONObject) json).isNullObject() );
 538  0
       }else if( !JSONNull.getInstance()
 539  
             .equals( json ) ){
 540  0
          fail( header + "Object is not null" );
 541  
       }
 542  0
    }
 543  
 }