Skip to content

Commit 6406c7a

Browse files
committed
Merge pull request #28 from johnjaylward/FixCtorGenerics
Test cases for PR #153 in JSON-Java
2 parents 0dbd9be + 4a2f9b8 commit 6406c7a

File tree

2 files changed

+285
-41
lines changed

2 files changed

+285
-41
lines changed

JSONArrayTest.java

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package org.json.junit;
22

3-
import static org.junit.Assert.*;
4-
5-
import java.util.*;
6-
7-
import org.json.*;
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.Iterator;
10+
import java.util.Map;
11+
12+
import org.json.JSONArray;
13+
import org.json.JSONException;
14+
import org.json.JSONObject;
815
import org.junit.Test;
916

1017

@@ -80,6 +87,112 @@ public void badObject() {
8087
equals(e.getMessage()));
8188
}
8289
}
90+
91+
/**
92+
* Verifies that the constructor has backwards compatability with RAW types pre-java5.
93+
*/
94+
@Test
95+
public void verifyConstructor() {
96+
97+
final JSONArray expected = new JSONArray("[10]");
98+
99+
@SuppressWarnings("rawtypes")
100+
Collection myRawC = Collections.singleton(Integer.valueOf(10));
101+
JSONArray jaRaw = new JSONArray(myRawC);
102+
103+
Collection<Integer> myCInt = Collections.singleton(Integer.valueOf(10));
104+
JSONArray jaInt = new JSONArray(myCInt);
105+
106+
Collection<Object> myCObj = Collections.singleton((Object) Integer
107+
.valueOf(10));
108+
JSONArray jaObj = new JSONArray(myCObj);
109+
110+
assertTrue(
111+
"The RAW Collection should give me the same as the Typed Collection",
112+
expected.similar(jaRaw));
113+
assertTrue(
114+
"The RAW Collection should give me the same as the Typed Collection",
115+
expected.similar(jaInt));
116+
assertTrue(
117+
"The RAW Collection should give me the same as the Typed Collection",
118+
expected.similar(jaObj));
119+
}
120+
121+
/**
122+
* Verifies that the put Collection has backwards compatability with RAW types pre-java5.
123+
*/
124+
@Test
125+
public void verifyPutCollection() {
126+
127+
final JSONArray expected = new JSONArray("[[10]]");
128+
129+
@SuppressWarnings("rawtypes")
130+
Collection myRawC = Collections.singleton(Integer.valueOf(10));
131+
JSONArray jaRaw = new JSONArray();
132+
jaRaw.put(myRawC);
133+
134+
Collection<Object> myCObj = Collections.singleton((Object) Integer
135+
.valueOf(10));
136+
JSONArray jaObj = new JSONArray();
137+
jaObj.put(myCObj);
138+
139+
Collection<Integer> myCInt = Collections.singleton(Integer.valueOf(10));
140+
JSONArray jaInt = new JSONArray();
141+
jaInt.put(myCInt);
142+
143+
assertTrue(
144+
"The RAW Collection should give me the same as the Typed Collection",
145+
expected.similar(jaRaw));
146+
assertTrue(
147+
"The RAW Collection should give me the same as the Typed Collection",
148+
expected.similar(jaObj));
149+
assertTrue(
150+
"The RAW Collection should give me the same as the Typed Collection",
151+
expected.similar(jaInt));
152+
}
153+
154+
155+
/**
156+
* Verifies that the put Map has backwards compatability with RAW types pre-java5.
157+
*/
158+
@Test
159+
public void verifyPutMap() {
160+
161+
final JSONArray expected = new JSONArray("[{\"myKey\":10}]");
162+
163+
@SuppressWarnings("rawtypes")
164+
Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10));
165+
JSONArray jaRaw = new JSONArray();
166+
jaRaw.put(myRawC);
167+
168+
Map<String, Object> myCStrObj = Collections.singletonMap("myKey",
169+
(Object) Integer.valueOf(10));
170+
JSONArray jaStrObj = new JSONArray();
171+
jaStrObj.put(myCStrObj);
172+
173+
Map<String, Integer> myCStrInt = Collections.singletonMap("myKey",
174+
Integer.valueOf(10));
175+
JSONArray jaStrInt = new JSONArray();
176+
jaStrInt.put(myCStrInt);
177+
178+
Map<?, ?> myCObjObj = Collections.singletonMap((Object) "myKey",
179+
(Object) Integer.valueOf(10));
180+
JSONArray jaObjObj = new JSONArray();
181+
jaObjObj.put(myCObjObj);
182+
183+
assertTrue(
184+
"The RAW Collection should give me the same as the Typed Collection",
185+
expected.similar(jaRaw));
186+
assertTrue(
187+
"The RAW Collection should give me the same as the Typed Collection",
188+
expected.similar(jaStrObj));
189+
assertTrue(
190+
"The RAW Collection should give me the same as the Typed Collection",
191+
expected.similar(jaStrInt));
192+
assertTrue(
193+
"The RAW Collection should give me the same as the Typed Collection",
194+
expected.similar(jaObjObj));
195+
}
83196

84197
/**
85198
* Create a JSONArray doc with a variety of different elements.

JSONObjectTest.java

Lines changed: 167 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
package 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;
711
import java.math.BigDecimal;
812
import 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

Comments
 (0)