1   /**
2    * Copyright 2007 Art Technology Group, Inc (ATG)
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    *
7    * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and limitations under the License.
13   */
14  
15  package atg.taglib.json.util;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  
21  import org.junit.Test;
22  
23  /**
24   * Test JSONObject equality
25   *
26   * @author James Wiltshire
27   * @version $Id$
28   */
29  
30  public class JsonObjectEqualityTest
31  {
32    @Test
33    public void testObjectsAreEqual() throws JSONException
34    {
35      String s1="{foo:'bar',baz:'boz'}";
36      String s2="{baz:'boz',foo:'bar'}";
37      String s3="{foo:'bar'}";
38      JSONObject o1= new JSONObject(s1);
39      JSONObject o2= new JSONObject(s2);
40      JSONObject o3= new JSONObject(s3);
41      assertEquals(o1,o2);
42      
43      assertEquals(new JSONObject(),new JSONObject());
44      
45      assertEquals(new JSONObject("{}"),new JSONObject());
46      assertEquals(new JSONObject("{   }"),new JSONObject());
47      
48      assertFalse(o1.equals(new JSONObject()));
49      assertFalse((new JSONObject()).equals(o1));
50      
51      assertFalse(o1.equals(o3));
52      assertFalse(o2.equals(o3));
53      
54      assertFalse(o1.equals(null));
55    }
56    
57    @Test
58    public void testNestedObjects() throws JSONException
59    {
60      String s1="{foo:{bar:\"baz\"},foo2:{biz:true,boz:7}}";
61      JSONObject o1= new JSONObject(s1);
62      
63      assertTrue(o1.equals(new JSONObject(s1)));
64      assertTrue(!o1.equals(new JSONObject()));
65    }
66  
67  }
68