/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.shindig.common;

import static org.junit.Assert.assertEquals;

import org.json.JSONArray;
import org.json.JSONObject;

public final class JsonAssert {
  private JsonAssert() {}

  public static void assertJsonArrayEquals(JSONArray left, JSONArray right) throws Exception {
    if (left.length() != right.length()) {
      assertEquals("Arrays are not of equal length", left.toString(), right.toString());
    }

    for (int i = 0; i < left.length(); ++i) {
      Object leftValue = left.opt(i);
      Object rightValue = right.opt(i);

      assertEquals(left.toString() + " != " + right.toString(),
                   leftValue.getClass(), rightValue.getClass());

      if (leftValue instanceof JSONObject) {
        assertJsonObjectEquals((JSONObject) leftValue, (JSONObject) rightValue);
      } else if (leftValue instanceof JSONArray) {
        assertJsonArrayEquals((JSONArray) leftValue, (JSONArray) rightValue);
      } else {
        assertEquals(leftValue, rightValue);
      }
    }
  }

  public static void assertJsonObjectEquals(JSONObject left, JSONObject right) throws Exception {
    if (left.length() != right.length()) {
      assertEquals("Objects are not of equal size", left.toString(2), right.toString(2));
    }

    for (String name : JSONObject.getNames(left)) {
      Object leftValue = left.opt(name);
      Object rightValue = right.opt(name);

      assertEquals(left.toString() + " != " + right.toString(),
                   leftValue.getClass(), rightValue.getClass());

      if (leftValue instanceof JSONObject) {
        assertJsonObjectEquals((JSONObject) leftValue, (JSONObject) rightValue);
      } else if (leftValue instanceof JSONArray) {
        assertJsonArrayEquals((JSONArray) leftValue, (JSONArray) rightValue);
      } else {
        assertEquals(leftValue, rightValue);
      }
    }
  }

  public static void assertJsonEquals(String left, String right) throws Exception {
    switch (left.charAt(0)) {
      case '{':
        assertJsonObjectEquals(new JSONObject(left), new JSONObject(right));
        break;
      case '[':
        assertJsonArrayEquals(new JSONArray(left), new JSONArray(right));
        break;
      default:
        assertEquals(left, right);
        break;
    }
  }
}

@@ -837,16 +837,71 @@
 ommon;%0A%0A
+import org.json.JSONArray;%0Aimport org.json.JSONObject;%0A
 import s
@@ -941,62 +941,52 @@
 ls;%0A
-%0A
 import 
-org.json.JSONArray;%0Aimport org.json.JSONObject
+static org.junit.Assert.assertNotNull
 ;%0A%0Ap
@@ -2096,32 +2096,165 @@
 ing(2));%0A    %7D%0A%0A
+    // Both are emtpy so skip%0A    if (JSONObject.getNames(left) == null && JSONObject.getNames(right) == null) %7B%0A      return;%0A    %7D%0A
     for (String 
@@ -2370,24 +2370,143 @@
 opt(name);%0A%0A
+      if (leftValue != null) %7B%0A        assertNotNull(left.toString() + %22 != %22 + right.toString(), rightValue);%0A      %7D%0A
       assert

Fix for comparison failures when rvalue is null or both values are undefined