Open Source Repository

Home /json/json-lib-2.4-jdk15 | Repository Home



net/sf/json/test/JSONAssert.java
/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sf.json.test;

import java.util.Iterator;

import junit.framework.Assert;
import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.object.IdentityObjectMorpher;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONFunction;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.util.JSONUtils;

/**
 * Provides assertions on equality for JSON strings and JSON types.
 *
 @author Andres Almiray <[email protected]>
 */
public class JSONAssert extends Assert {
   /**
    * Asserts that two JSON values are equal.
    */
   public static void assertEqualsJSON expected, JSON actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONArrays are equal.
    */
   public static void assertEqualsJSONArray expected, JSONArray actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONArrays are equal.
    */
   public static void assertEqualsJSONArray expected, String actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONFunctions are equal.
    */
   public static void assertEqualsJSONFunction expected, String actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONNulls are equal.
    */
   public static void assertEqualsJSONNull expected, String actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONObjects are equal.
    */
   public static void assertEqualsJSONObject expected, JSONObject actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONObjects are equal.
    */
   public static void assertEqualsJSONObject expected, String actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSON values are equal.
    */
   public static void assertEqualsString message, JSON expected, JSON actual ) {
      String header = message == null "" : message + ": ";
      ifexpected == null ){
         failheader + "expected was null" );
      }
      ifactual == null ){
         failheader + "actual was null" );
      }
      ifexpected == actual || expected.equalsactual ) ){
         return;
      }

      ifexpected instanceof JSONArray ){
         ifactual instanceof JSONArray ){
            assertEqualsheader, (JSONArrayexpected, (JSONArrayactual );
         }else{
            failheader + "actual is not a JSONArray" );
         }
      }else ifexpected instanceof JSONObject ){
         ifactual instanceof JSONObject ){
            assertEqualsheader, (JSONObjectexpected, (JSONObjectactual );
         }else{
            failheader + "actual is not a JSONObject" );
         }
      }else ifexpected instanceof JSONNull ){
         ifactual instanceof JSONNull ){
            return;
         }else{
            failheader + "actual is not a JSONNull" );
         }
      }
   }

   /**
    * Asserts that two JSONArrays are equal.
    */
   public static void assertEqualsString expected, JSONArray actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONArrays are equal.
    */
   public static void assertEqualsString message, JSONArray expected, JSONArray actual ) {
      String header = message == null "" : message + ": ";
      ifexpected == null ){
         failheader + "expected array was null" );
      }
      ifactual == null ){
         failheader + "actual array was null" );
      }
      ifexpected == actual || expected.equalsactual ) ){
         return;
      }
      ifactual.size() != expected.size() ){
         failheader + "arrays sizes differed, expected.length()=" + expected.size()
               " actual.length()=" + actual.size() );
      }

      int max = expected.size();
      forint i = 0; i < max; i++ ){
         Object o1 = expected.get);
         Object o2 = actual.get);

         // handle nulls
         ifJSONNull.getInstance()
               .equalso1 ) ){
            ifJSONNull.getInstance()
                  .equalso2 ) ){
               continue;
            }else{
               failheader + "arrays first differed at element [" + i + "];" );
            }
         }else{
            ifJSONNull.getInstance()
                  .equalso2 ) ){
               failheader + "arrays first differed at element [" + i + "];" );
            }
         }

         ifo1 instanceof JSONArray && o2 instanceof JSONArray ){
            JSONArray e = (JSONArrayo1;
            JSONArray a = (JSONArrayo2;
            assertEqualsheader + "arrays first differed at element " + i + ";", e, a );
         }else{
            ifo1 instanceof String && o2 instanceof JSONFunction ){
               assertEqualsheader + "arrays first differed at element [" + i + "];"(Stringo1,
                     (JSONFunctiono2 );
            }else ifo1 instanceof JSONFunction && o2 instanceof String ){
               assertEqualsheader + "arrays first differed at element [" + i + "];",
                     (JSONFunctiono1, (Stringo2 );
            }else ifo1 instanceof JSONObject && o2 instanceof JSONObject ){
               assertEqualsheader + "arrays first differed at element [" + i + "];",
                     (JSONObjecto1, (JSONObjecto2 );
            }else ifo1 instanceof JSONArray && o2 instanceof JSONArray ){
               assertEqualsheader + "arrays first differed at element [" + i + "];",
                     (JSONArrayo1, (JSONArrayo2 );
            }else ifo1 instanceof JSONFunction && o2 instanceof JSONFunction ){
               assertEqualsheader + "arrays first differed at element [" + i + "];",
                     (JSONFunctiono1, (JSONFunctiono2 );
            }else{
               ifo1 instanceof String ){
                  assertEqualsheader + "arrays first differed at element [" + i + "];",
                        (Stringo1, String.valueOfo2 ) );
               }else ifo2 instanceof String ){
                  assertEqualsheader + "arrays first differed at element [" + i + "];",
                        String.valueOfo1 )(Stringo2 );
               }else{
                  Morpher m1 = JSONUtils.getMorpherRegistry()
                        .getMorpherForo1.getClass() );
                  Morpher m2 = JSONUtils.getMorpherRegistry()
                        .getMorpherForo2.getClass() );
                  ifm1 != null && m1 != IdentityObjectMorpher.getInstance() ){
                     assertEqualsheader + "arrays first differed at element [" + i + "];", o1,
                           JSONUtils.getMorpherRegistry()
                                 .morpho1.getClass(), o2 ) );
                  }else ifm2 != null && m2 != IdentityObjectMorpher.getInstance() ){
                     assertEqualsheader + "arrays first differed at element [" + i + "];",
                           JSONUtils.getMorpherRegistry()
                                 .morpho1.getClass(), o1 ), o2 );
                  }else{
                     assertEqualsheader + "arrays first differed at element [" + i + "];", o1, o2 );
                  }
               }
            }
         }
      }
   }

   /**
    * Asserts that two JSONArrays are equal.
    */
   public static void assertEqualsString message, JSONArray expected, String actual ) {
      try{
         assertEqualsmessage, expected, JSONArray.fromObjectactual ) );
      }catchJSONException e ){
         String header = message == null "" : message + ": ";
         failheader + "actual is not a JSONArray" );
      }
   }

   /**
    * Asserts that two JSONFunctions are equal.
    */
   public static void assertEqualsString expected, JSONFunction actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONFunctions are equal.
    */
   public static void assertEqualsString message, JSONFunction expected, String actual ) {
      String header = message == null "" : message + ": ";
      ifexpected == null ){
         failheader + "expected function was null" );
      }
      ifactual == null ){
         failheader + "actual string was null" );
      }

      try{
         assertEqualsheader, expected, JSONFunction.parseactual ) );
      }catchJSONException jsone ){
         failheader + "'" + actual + "' is not a function" );
      }
   }

   /**
    * Asserts that two JSONNulls are equal.
    */
   public static void assertEqualsString expected, JSONNull actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONNulls are equal.
    */
   public static void assertEqualsString message, JSONNull expected, String actual ) {
      String header = message == null "" : message + ": ";
      ifactual == null ){
         failheader + "actual string was null" );
      }else ifexpected == null ){
         assertEqualsheader, "null", actual );
      }else{
         assertEqualsheader, expected.toString(), actual );
      }
   }

   /**
    * Asserts that two JSONObjects are equal.
    */
   public static void assertEqualsString expected, JSONObject actual ) {
      assertEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSONObjects are equal.
    */
   public static void assertEqualsString message, JSONObject expected, JSONObject actual ) {
      String header = message == null "" : message + ": ";
      ifexpected == null ){
         failheader + "expected object was null" );
      }
      ifactual == null ){
         failheader + "actual object was null" );
      }
      ifexpected == actual /* || expected.equals( actual ) */){
         return;
      }

      ifexpected.isNullObject() ){
         ifactual.isNullObject() ){
            return;
         }else{
            failheader + "actual is not a null JSONObject" );
         }
      }else{
         ifactual.isNullObject() ){
            failheader + "actual is a null JSONObject" );
         }
      }

      assertEqualsheader + "names sizes differed, expected.names().length()=" + expected.names()
            .size() " actual.names().length()=" + actual.names()
            .size(), expected.names()
            .size(), actual.names()
            .size() );
      forIterator keys = expected.keys(); keys.hasNext()){
         String key = (Stringkeys.next();
         Object o1 = expected.optkey );
         Object o2 = actual.optkey );

         ifJSONNull.getInstance()
               .equalso1 ) ){
            ifJSONNull.getInstance()
                  .equalso2 ) ){
               continue;
            }else{
               failheader + "objects differed at key [" + key + "];" );
            }
         }else{
            ifJSONNull.getInstance()
                  .equalso2 ) ){
               failheader + "objects differed at key [" + key + "];" );
            }
         }

         ifo1 instanceof String && o2 instanceof JSONFunction ){
            assertEqualsheader + "objects differed at key [" + key + "];"(Stringo1,
                  (JSONFunctiono2 );
         }else ifo1 instanceof JSONFunction && o2 instanceof String ){
            assertEqualsheader + "objects differed at key [" + key + "];"(JSONFunctiono1,
                  (Stringo2 );
         }else ifo1 instanceof JSONObject && o2 instanceof JSONObject ){
            assertEqualsheader + "objects differed at key [" + key + "];"(JSONObjecto1,
                  (JSONObjecto2 );
         }else ifo1 instanceof JSONArray && o2 instanceof JSONArray ){
            assertEqualsheader + "objects differed at key [" + key + "];"(JSONArrayo1,
                  (JSONArrayo2 );
         }else ifo1 instanceof JSONFunction && o2 instanceof JSONFunction ){
            assertEqualsheader + "objects differed at key [" + key + "];"(JSONFunctiono1,
                  (JSONFunctiono2 );
         }else{
            ifo1 instanceof String ){
               assertEqualsheader + "objects differed at key [" + key + "];"(Stringo1,
                     String.valueOfo2 ) );
            }else ifo2 instanceof String ){
               assertEqualsheader + "objects differed at key [" + key + "];",
                     String.valueOfo1 )(Stringo2 );
            }else{
               Morpher m1 = JSONUtils.getMorpherRegistry()
                     .getMorpherForo1.getClass() );
               Morpher m2 = JSONUtils.getMorpherRegistry()
                     .getMorpherForo2.getClass() );
               ifm1 != null && m1 != IdentityObjectMorpher.getInstance() ){
                  assertEqualsheader + "objects differed at key [" + key + "];", o1,
                        JSONUtils.getMorpherRegistry()
                              .morpho1.getClass(), o2 ) );
               }else ifm2 != null && m2 != IdentityObjectMorpher.getInstance() ){
                  assertEqualsheader + "objects differed at key [" + key + "];",
                        JSONUtils.getMorpherRegistry()
                              .morpho1.getClass(), o1 ), o2 );
               }else{
                  assertEqualsheader + "objects differed at key [" + key + "];", o1, o2 );
               }
            }
         }
      }
   }

   /**
    * Asserts that two JSONObjects are equal.
    */
   public static void assertEqualsString message, JSONObject expected, String actual ) {
      try{
         assertEqualsmessage, expected, JSONObject.fromObjectactual ) );
      }catchJSONException e ){
         String header = message == null "" : message + ": ";
         failheader + "actual is not a JSONObject" );
      }
   }

   /**
    * Asserts that two JSONArrays are equal.
    */
   public static void assertEqualsString message, String expected, JSONArray actual ) {
      try{
         assertEqualsmessage, JSONArray.fromObjectexpected ), actual );
      }catchJSONException e ){
         String header = message == null "" : message + ": ";
         failheader + "expected is not a JSONArray" );
      }
   }

   /**
    * Asserts that two JSONFunctions are equal.
    */
   public static void assertEqualsString message, String expected, JSONFunction actual ) {
      String header = message == null "" : message + ": ";
      ifexpected == null ){
         failheader + "expected string was null" );
      }
      ifactual == null ){
         failheader + "actual function was null" );
      }

      try{
         assertEqualsheader, JSONFunction.parseexpected ), actual );
      }catchJSONException jsone ){
         failheader + "'" + expected + "' is not a function" );
      }
   }

   /**
    * Asserts that two JSONNulls are equal.
    */
   public static void assertEqualsString message, String expected, JSONNull actual ) {
      String header = message == null "" : message + ": ";
      ifexpected == null ){
         failheader + "expected was null" );
      }else ifactual == null ){
         assertEqualsheader, expected, "null" );
      }else{
         assertEqualsheader, expected, actual.toString() );
      }
   }

   /**
    * Asserts that two JSONObjects are equal.
    */
   public static void assertEqualsString message, String expected, JSONObject actual ) {
      try{
         assertEqualsmessage, JSONObject.fromObjectexpected ), actual );
      }catchJSONException e ){
         String header = message == null "" : message + ": ";
         failheader + "expected is not a JSONObject" );
      }
   }

   /**
    * Asserts that two JSON strings are equal.
    */
   public static void assertJsonEqualsString expected, String actual ) {
      assertJsonEqualsnull, expected, actual );
   }

   /**
    * Asserts that two JSON strings are equal.
    */
   public static void assertJsonEqualsString message, String expected, String actual ) {
      String header = message == null "" : message + ": ";
      ifexpected == null ){
         failheader + "expected was null" );
      }
      ifactual == null ){
         failheader + "actual was null" );
      }

      JSON json1 = null;
      JSON json2 = null;
      try{
         json1 = JSONSerializer.toJSONexpected );
      }catchJSONException jsone ){
         failheader + "expected is not a valid JSON string" );
      }
      try{
         json2 = JSONSerializer.toJSONactual );
      }catchJSONException jsone ){
         failheader + "actual is not a valid JSON string" );
      }
      assertEqualsheader, json1, json2 );
   }

   /**
    * Asserts that a JSON value is not null.<br>
    * Fails if:
    <ul>
    <li>JSONNull.getInstance().equals( json )</li>
    <li>((JSONObject) json).isNullObject()</li>
    </ul>
    */
   public static void assertNotNullJSON json ) {
      assertNotNullnull, json );
   }

   /**
    * Asserts that a JSON value is not null.<br>
    * Fails if:
    <ul>
    <li>JSONNull.getInstance().equals( json )</li>
    <li>((JSONObject) json).isNullObject()</li>
    </ul>
    */
   public static void assertNotNullString message, JSON json ) {
      String header = message == null "" : message + ": ";
      ifjson instanceof JSONObject ){
         assertFalseheader + "Object is null"((JSONObjectjson).isNullObject() );
      }else ifJSONNull.getInstance()
            .equalsjson ) ){
         failheader + "Object is null" );
      }
   }

   /**
    * Asserts that a JSON value is null.<br>
    * Fails if:
    <ul>
    <li>!JSONNull.getInstance().equals( json )</li>
    <li>!((JSONObject) json).isNullObject()</li>
    </ul>
    */
   public static void assertNullJSON json ) {
      assertNullnull, json );
   }

   /**
    * Asserts that a JSON value is null.<br>
    * Fails if:
    <ul>
    <li>!JSONNull.getInstance().equals( json )</li>
    <li>!((JSONObject) json).isNullObject()</li>
    </ul>
    */
   public static void assertNullString message, JSON json ) {
      String header = message == null "" : message + ": ";
      ifjson instanceof JSONObject ){
         assertTrueheader + "Object is not null"((JSONObjectjson).isNullObject() );
      }else if!JSONNull.getInstance()
            .equalsjson ) ){
         failheader + "Object is not null" );
      }
   }
}