11package org .json .junit ;
22
3- import static org .junit .Assert .*;
4- import static org .mockito .Mockito .*;
5-
6- import java .io .*;
3+ import static org .junit .Assert .assertFalse ;
4+ import static org .junit .Assert .assertTrue ;
5+ import static org .mockito .Mockito .mock ;
6+ import static org .mockito .Mockito .when ;
7+
8+ import java .io .StringReader ;
9+ import java .io .StringWriter ;
10+ import java .io .Writer ;
711import java .math .BigDecimal ;
812import java .math .BigInteger ;
9- import java .util .*;
10-
11- import org .json .*;
12- import org .junit .*;
13+ import java .util .ArrayList ;
14+ import java .util .Arrays ;
15+ import java .util .Collection ;
16+ import java .util .Collections ;
17+ import java .util .HashMap ;
18+ import java .util .Iterator ;
19+ import java .util .List ;
20+ import java .util .Locale ;
21+ import java .util .Map ;
22+
23+ import org .json .CDL ;
24+ import org .json .JSONArray ;
25+ import org .json .JSONException ;
26+ import org .json .JSONObject ;
27+ import org .json .JSONString ;
28+ import org .json .XML ;
29+ import org .junit .Test ;
1330
1431/**
1532 * Used in testing when a JSONString is needed
@@ -149,6 +166,122 @@ public void jsonObjectByMap() {
149166 JSONObject expectedJsonObject = new JSONObject (expectedStr );
150167 Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
151168 }
169+
170+ /**
171+ * Verifies that the constructor has backwards compatability with RAW types pre-java5.
172+ */
173+ @ Test
174+ public void verifyConstructor () {
175+
176+ final JSONObject expected = new JSONObject ("{\" myKey\" :10}" );
177+
178+ @ SuppressWarnings ("rawtypes" )
179+ Map myRawC = Collections .singletonMap ("myKey" , Integer .valueOf (10 ));
180+ JSONObject jaRaw = new JSONObject (myRawC );
181+
182+ Map <String , Object > myCStrObj = Collections .singletonMap ("myKey" ,
183+ (Object ) Integer .valueOf (10 ));
184+ JSONObject jaStrObj = new JSONObject (myCStrObj );
185+
186+ Map <String , Integer > myCStrInt = Collections .singletonMap ("myKey" ,
187+ Integer .valueOf (10 ));
188+ JSONObject jaStrInt = new JSONObject (myCStrInt );
189+
190+ Map <?, ?> myCObjObj = Collections .singletonMap ((Object ) "myKey" ,
191+ (Object ) Integer .valueOf (10 ));
192+ JSONObject jaObjObj = new JSONObject (myCObjObj );
193+
194+ assertTrue (
195+ "The RAW Collection should give me the same as the Typed Collection" ,
196+ expected .similar (jaRaw ));
197+ assertTrue (
198+ "The RAW Collection should give me the same as the Typed Collection" ,
199+ expected .similar (jaStrObj ));
200+ assertTrue (
201+ "The RAW Collection should give me the same as the Typed Collection" ,
202+ expected .similar (jaStrInt ));
203+ assertTrue (
204+ "The RAW Collection should give me the same as the Typed Collection" ,
205+ expected .similar (jaObjObj ));
206+ }
207+
208+ /**
209+ * Verifies that the put Collection has backwards compatability with RAW types pre-java5.
210+ */
211+ @ Test
212+ public void verifyPutCollection () {
213+
214+ final JSONObject expected = new JSONObject ("{\" myCollection\" :[10]}" );
215+
216+ @ SuppressWarnings ("rawtypes" )
217+ Collection myRawC = Collections .singleton (Integer .valueOf (10 ));
218+ JSONObject jaRaw = new JSONObject ();
219+ jaRaw .put ("myCollection" , myRawC );
220+
221+ Collection <Object > myCObj = Collections .singleton ((Object ) Integer
222+ .valueOf (10 ));
223+ JSONObject jaObj = new JSONObject ();
224+ jaObj .put ("myCollection" , myCObj );
225+
226+ Collection <Integer > myCInt = Collections .singleton (Integer
227+ .valueOf (10 ));
228+ JSONObject jaInt = new JSONObject ();
229+ jaInt .put ("myCollection" , myCInt );
230+
231+ assertTrue (
232+ "The RAW Collection should give me the same as the Typed Collection" ,
233+ expected .similar (jaRaw ));
234+ assertTrue (
235+ "The RAW Collection should give me the same as the Typed Collection" ,
236+ expected .similar (jaObj ));
237+ assertTrue (
238+ "The RAW Collection should give me the same as the Typed Collection" ,
239+ expected .similar (jaInt ));
240+ }
241+
242+
243+ /**
244+ * Verifies that the put Map has backwards compatability with RAW types pre-java5.
245+ */
246+ @ Test
247+ public void verifyPutMap () {
248+
249+ final JSONObject expected = new JSONObject ("{\" myMap\" :{\" myKey\" :10}}" );
250+
251+ @ SuppressWarnings ("rawtypes" )
252+ Map myRawC = Collections .singletonMap ("myKey" , Integer .valueOf (10 ));
253+ JSONObject jaRaw = new JSONObject ();
254+ jaRaw .put ("myMap" , myRawC );
255+
256+ Map <String , Object > myCStrObj = Collections .singletonMap ("myKey" ,
257+ (Object ) Integer .valueOf (10 ));
258+ JSONObject jaStrObj = new JSONObject ();
259+ jaStrObj .put ("myMap" , myCStrObj );
260+
261+ Map <String , Integer > myCStrInt = Collections .singletonMap ("myKey" ,
262+ Integer .valueOf (10 ));
263+ JSONObject jaStrInt = new JSONObject ();
264+ jaStrInt .put ("myMap" , myCStrInt );
265+
266+ Map <?, ?> myCObjObj = Collections .singletonMap ((Object ) "myKey" ,
267+ (Object ) Integer .valueOf (10 ));
268+ JSONObject jaObjObj = new JSONObject ();
269+ jaObjObj .put ("myMap" , myCObjObj );
270+
271+ assertTrue (
272+ "The RAW Collection should give me the same as the Typed Collection" ,
273+ expected .similar (jaRaw ));
274+ assertTrue (
275+ "The RAW Collection should give me the same as the Typed Collection" ,
276+ expected .similar (jaStrObj ));
277+ assertTrue (
278+ "The RAW Collection should give me the same as the Typed Collection" ,
279+ expected .similar (jaStrInt ));
280+ assertTrue (
281+ "The RAW Collection should give me the same as the Typed Collection" ,
282+ expected .similar (jaObjObj ));
283+ }
284+
152285
153286 /**
154287 * JSONObjects can be built from a Map<String, Object>.
@@ -1229,10 +1362,9 @@ public void jsonObjectToString() {
12291362 * Confirm that map and nested JSONObject have the same contents.
12301363 */
12311364 @ Test
1232- @ SuppressWarnings ("unchecked" )
12331365 public void jsonObjectToStringSuppressWarningOnCastToMap () {
12341366 JSONObject jsonObject = new JSONObject ();
1235- Map <String , String > map = new HashMap <String , String >();
1367+ Map <String , String > map = new HashMap <>();
12361368 map .put ("abc" , "def" );
12371369 jsonObject .put ("key" , map );
12381370 String toStr = jsonObject .toString ();
@@ -1245,7 +1377,7 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() {
12451377 * in the debugger, one is a map and the other is a JSONObject.
12461378 * TODO: write a util method for such comparisons
12471379 */
1248- map = ( Map < String , String >) jsonObject .get ("key" );
1380+ assertTrue ( "Maps should be entered as JSONObject" , jsonObject .get ("key" ) instanceof JSONObject );
12491381 JSONObject mapJsonObject = expectedJsonObject .getJSONObject ("key" );
12501382 assertTrue ("value size should be equal" ,
12511383 map .size () == mapJsonObject .length () && map .size () == 1 );
@@ -1264,32 +1396,31 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() {
12641396 * Confirm that collection and nested JSONArray have the same contents.
12651397 */
12661398 @ Test
1267- @ SuppressWarnings ("unchecked" )
12681399 public void jsonObjectToStringSuppressWarningOnCastToCollection () {
1269- JSONObject jsonObject = new JSONObject ();
1270- Collection <String > collection = new ArrayList <String >();
1271- collection .add ("abc" );
1272- // ArrayList will be added as an object
1273- jsonObject .put ("key" , collection );
1274- String toStr = jsonObject .toString ();
1275- // [abc] will be added as a JSONArray
1276- JSONObject expectedJsonObject = new JSONObject (toStr );
1277- /**
1278- * Can't do a Util compare because although they look the same
1279- * in the debugger, one is a collection and the other is a JSONArray.
1280- */
1281- assertTrue ("keys should be equal" ,
1282- jsonObject . keySet ().iterator ().next (). equals (
1283- expectedJsonObject . keySet (). iterator (). next ()));
1284- collection = ( Collection < String >) jsonObject .get ("key" );
1285- JSONArray jsonArray = expectedJsonObject .getJSONArray ("key" );
1286- assertTrue ("value size should be equal" ,
1287- collection .size () == jsonArray .length ());
1288- Iterator <String > it = collection .iterator ();
1289- for (int i = 0 ; i < collection .size (); ++i ) {
1290- assertTrue ("items should be equal for index: " + i ,
1291- jsonArray .get (i ).toString ().equals (it .next ().toString ()));
1292- }
1400+ JSONObject jsonObject = new JSONObject ();
1401+ Collection <String > collection = new ArrayList <String >();
1402+ collection .add ("abc" );
1403+ // ArrayList will be added as an object
1404+ jsonObject .put ("key" , collection );
1405+ String toStr = jsonObject .toString ();
1406+ // [abc] will be added as a JSONArray
1407+ JSONObject expectedJsonObject = new JSONObject (toStr );
1408+ /**
1409+ * Can't do a Util compare because although they look the same in the
1410+ * debugger, one is a collection and the other is a JSONArray.
1411+ */
1412+ assertTrue ("keys should be equal" , jsonObject . keySet (). iterator ()
1413+ . next (). equals ( expectedJsonObject . keySet ().iterator ().next ()));
1414+ assertTrue ( "Collections should be converted to JSONArray" ,
1415+ jsonObject .get ("key" ) instanceof JSONArray );
1416+ JSONArray jsonArray = expectedJsonObject .getJSONArray ("key" );
1417+ assertTrue ("value size should be equal" ,
1418+ collection .size () == jsonArray .length ());
1419+ Iterator <String > it = collection .iterator ();
1420+ for (int i = 0 ; i < collection .size (); ++i ) {
1421+ assertTrue ("items should be equal for index: " + i , jsonArray
1422+ .get (i ).toString ().equals (it .next ().toString ()));
1423+ }
12931424 }
12941425
12951426 /**
0 commit comments