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;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import junit.framework.TestCase;
23  
24  /**
25   * @author Andres Almiray <aalmiray@users.sourceforge.net>
26   */
27  public class TestJSONObjectEqualsHashCodeCompareTo extends TestCase {
28     private static JSONObject strings;
29     private static Map values = new HashMap();
30     private static JSONObject values1;
31     private static JSONObject values2;
32     private static JSONObject values3;
33  
34     static{
35        values.put( "JSONObject.null.1", new JSONObject( true ) );
36        values.put( "JSONObject.null.2", new JSONObject( true ) );
37        values.put( "int.1", Integer.valueOf( "1" ) );
38        values.put( "int.2", Integer.valueOf( "2" ) );
39        values.put( "long.1", Long.valueOf( "1" ) );
40        values.put( "long.2", Long.valueOf( "2" ) );
41        values.put( "string.1", "1" );
42        values.put( "string.2", "2" );
43        values.put( "boolean.1", Boolean.TRUE );
44        values.put( "boolean.2", Boolean.FALSE );
45  
46        strings = new JSONObject().element( "int", "1" )
47              .element( "long", "1" )
48              .element( "boolean", "true" )
49              .element( "string", "string" )
50              .element( "func", "function(){ return this; }" )
51              .element( "array", "[1,2,3]" );
52        values.put( "JSONObject.strings", strings );
53        values1 = new JSONObject().element( "int", Integer.valueOf( "1" ) )
54              .element( "long", Long.valueOf( "1" ) )
55              .element( "boolean", Boolean.TRUE )
56              .element( "string", "string" )
57              .element( "func", new JSONFunction( "return this;" ) )
58              .element( "array", JSONArray.fromObject( new int[] { 1, 2, 3 } ) );
59        values.put( "JSONObject.values.1", values1 );
60        values2 = new JSONObject().element( "int", Integer.valueOf( "1" ) )
61              .element( "long", Long.valueOf( "1" ) )
62              .element( "boolean", Boolean.TRUE )
63              .element( "string", "string" );
64        values.put( "JSONObject.values.2", values2 );
65        values3 = new JSONObject().element( "int", Integer.valueOf( "2" ) )
66              .element( "long", Long.valueOf( "2" ) )
67              .element( "boolean", Boolean.FALSE )
68              .element( "string", "string2" );
69        values.put( "JSONObject.values.3", values3 );
70     }
71  
72     public static void main( String[] args ) {
73        junit.textui.TestRunner.run( TestJSONObjectEqualsHashCodeCompareTo.class );
74     }
75  
76     public TestJSONObjectEqualsHashCodeCompareTo( String name ) {
77        super( name );
78     }
79  
80     public void testCompareTo_different_size() {
81        assertEquals( -1, values2.compareTo( strings ) );
82        assertEquals( 1, strings.compareTo( values2 ) );
83     }
84  
85     public void testCompareTo_null() {
86        assertEquals( -1, strings.compareTo( null ) );
87     }
88  
89     public void testCompareTo_object() {
90        assertEquals( -1, strings.compareTo( new Object() ) );
91     }
92  
93     public void testCompareTo_same_array() {
94        assertEquals( 0, strings.compareTo( strings ) );
95     }
96  
97     public void testCompareTo_same_size_different_values() {
98        assertEquals( -1, values2.compareTo( values3 ) );
99     }
100 
101    public void testCompareTo_same_size_similar_values() {
102       assertEquals( 0, strings.compareTo( values1 ) );
103    }
104 
105    public void testEquals_different_key_same_size() {
106       JSONObject a = new JSONObject().element( "key1", "string" );
107       JSONObject b = new JSONObject().element( "key2", "json" );
108       assertFalse( a.equals( b ) );
109       assertFalse( b.equals( a ) );
110    }
111 
112    public void testEquals_different_sizes() {
113       assertFalse( values.get( "JSONObject.values.1" )
114             .equals( values.get( "JSONObject.values.2" ) ) );
115    }
116 
117    public void testEquals_nullObject_other() {
118       assertFalse( values.get( "JSONObject.null.1" )
119             .equals( values.get( "JSONObject.strings" ) ) );
120    }
121 
122    public void testEquals_nullObjects_different() {
123       assertTrue( values.get( "JSONObject.null.1" )
124             .equals( values.get( "JSONObject.null.2" ) ) );
125    }
126 
127    public void testEquals_other_nullObject() {
128       assertFalse( values.get( "JSONObject.strings" )
129             .equals( values.get( "JSONObject.null.1" ) ) );
130    }
131 
132    public void testEquals_same() {
133       assertTrue( values.get( "JSONObject.null.1" )
134             .equals( values.get( "JSONObject.null.1" ) ) );
135    }
136 
137    public void testEquals_same_key_different_value() {
138       JSONObject a = new JSONObject().element( "key", "string" );
139       JSONObject b = new JSONObject().element( "key", "json" );
140       assertFalse( a.equals( b ) );
141       assertFalse( b.equals( a ) );
142    }
143 
144    public void testEquals_strings_values() {
145       assertTrue( values.get( "JSONObject.strings" )
146             .equals( values.get( "JSONObject.values.1" ) ) );
147    }
148 
149    public void testEquals_to_null() {
150       assertFalse( values.get( "JSONObject.null.1" )
151             .equals( null ) );
152    }
153 
154    public void testEquals_to_other() {
155       assertFalse( values.get( "JSONObject.null.1" )
156             .equals( new Object() ) );
157    }
158 
159    public void testEquals_values_strings() {
160       assertTrue( values.get( "JSONObject.values.1" )
161             .equals( values.get( "JSONObject.strings" ) ) );
162    }
163 
164    public void testHashCode_different_size() {
165       assertFalse( values.get( "JSONObject.values.1" )
166             .hashCode() == values.get( "JSONObject.values.2" )
167             .hashCode() );
168    }
169 
170    public void testHashCode_nullObject_other() {
171       assertFalse( values.get( "JSONObject.null.1" )
172             .hashCode() == values.get( "JSONObject.strings" )
173             .hashCode() );
174    }
175 
176    public void testHashCode_nullObjects_different() {
177       assertTrue( values.get( "JSONObject.null.1" )
178             .hashCode() == values.get( "JSONObject.null.2" )
179             .hashCode() );
180    }
181 
182    public void testHashCode_other_nullObject() {
183       assertFalse( values.get( "JSONObject.strings" )
184             .hashCode() == values.get( "JSONObject.null.1" )
185             .hashCode() );
186    }
187 
188    public void testHashCode_same() {
189       assertTrue( values.get( "JSONObject.null.1" )
190             .hashCode() == values.get( "JSONObject.null.1" )
191             .hashCode() );
192    }
193 
194    public void testHashCode_same_key_different_value() {
195       JSONObject a = new JSONObject().element( "key", "string" );
196       JSONObject b = new JSONObject().element( "key", "json" );
197       assertFalse( a.hashCode() == b.hashCode() );
198    }
199 
200    public void testHashCode_strings_values() {
201       assertTrue( values.get( "JSONObject.strings" )
202             .hashCode() == values.get( "JSONObject.values.1" )
203             .hashCode() );
204    }
205 
206    public void testHashCode_to_other() {
207       assertFalse( values.get( "JSONObject.null.1" )
208             .hashCode() == new Object().hashCode() );
209    }
210 
211    public void testHashCode_values_strings() {
212       assertTrue( values.get( "JSONObject.values.1" )
213             .hashCode() == values.get( "JSONObject.strings" )
214             .hashCode() );
215    }
216 }