Codebase list libandroid-json-org-java / fbe00c3
New tests for JSONObject and for JSON's self-use. Alongside development of these tests, I'm working on a new cleanroom implementation. The self use test was written to prevent me from self-using in a way that the original implementation does not. Change-Id: Ie617aca1978bd39d85b05e5c2c7bd657ed159dd6 Jesse Wilson authored 14 years ago Thomas Koch committed 11 years ago
5 changed file(s) with 1098 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
2020
2121 public class AllTests {
2222 public static Test suite() {
23 TestSuite suite = tests.TestSuiteFactory.createTestSuite();
23 TestSuite suite = new TestSuite();
2424 suite.addTestSuite(JSONArrayTest.class);
25 suite.addTestSuite(JSONObjectTest.class);
2526 suite.addTestSuite(JSONStringerTest.class);
26 suite.addTestSuite(JSONStringerTest.class);
27 suite.addTestSuite(JSONTokenerTest.class);
28 suite.addTestSuite(SelfUseTest.class);
2729 return suite;
2830 }
2931 }
1818 import junit.framework.TestCase;
1919
2020 import java.util.Arrays;
21 import java.util.List;
2122
2223 /**
2324 * This black box test was written without inspecting the non-free org.json sourcecode.
130131 // bogus behaviour: there's 2 ways to represent null; each behaves differently!
131132 assertEquals(JSONObject.NULL, array.get(0));
132133 try {
133 assertEquals(null, array.get(1));
134 fail();
135 } catch (JSONException e) {
136 }
137 try {
138 assertEquals(null, array.get(2));
139 fail();
140 } catch (JSONException e) {
141 }
142 try {
143 assertEquals(null, array.get(3));
134 array.get(1);
135 fail();
136 } catch (JSONException e) {
137 }
138 try {
139 array.get(2);
140 fail();
141 } catch (JSONException e) {
142 }
143 try {
144 array.get(3);
144145 fail();
145146 } catch (JSONException e) {
146147 }
237238 assertEquals(-2, array.optInt(0, -2));
238239
239240 assertEquals(5.5d, array.getDouble(1));
240 assertEquals(5, array.getLong(1));
241 assertEquals(5L, array.getLong(1));
241242 assertEquals(5, array.getInt(1));
242243 assertEquals(5, array.optInt(1, 3));
243244
284285 values.put(5.5d);
285286 values.put(null);
286287
287 // bogus behaviour: null values are stripped
288 // bogus behaviour: null values are stripped
288289 JSONObject object = values.toJSONObject(keys);
289290 assertEquals(1, object.length());
290291 assertFalse(object.has("b"));
291292 assertEquals("{\"a\":5.5}", object.toString());
292293 }
293294
295 public void testToJSONObjectMoreNamesThanValues() throws JSONException {
296 JSONArray keys = new JSONArray();
297 keys.put("a");
298 keys.put("b");
299 JSONArray values = new JSONArray();
300 values.put(5.5d);
301 JSONObject object = values.toJSONObject(keys);
302 assertEquals(1, object.length());
303 assertEquals(5.5d, object.get("a"));
304 }
305
306 public void testToJSONObjectMoreValuesThanNames() throws JSONException {
307 JSONArray keys = new JSONArray();
308 keys.put("a");
309 JSONArray values = new JSONArray();
310 values.put(5.5d);
311 values.put(11.0d);
312 JSONObject object = values.toJSONObject(keys);
313 assertEquals(1, object.length());
314 assertEquals(5.5d, object.get("a"));
315 }
316
317 public void testToJSONObjectNullKey() throws JSONException {
318 JSONArray keys = new JSONArray();
319 keys.put(JSONObject.NULL);
320 JSONArray values = new JSONArray();
321 values.put(5.5d);
322 JSONObject object = values.toJSONObject(keys);
323 assertEquals(1, object.length());
324 assertEquals(5.5d, object.get("null"));
325 }
326
294327 public void testPutUnsupportedNumbers() throws JSONException {
295328 JSONArray array = new JSONArray();
296329
311344 }
312345 }
313346
347 /**
348 * Although JSONArray is usually defensive about which numbers it accepts,
349 * it doesn't check inputs in its constructor.
350 */
314351 public void testCreateWithUnsupportedNumbers() throws JSONException {
315352 JSONArray array = new JSONArray(Arrays.asList(5.5, Double.NaN));
316353 assertEquals(2, array.length());
322359 // bogus behaviour: when the array contains an unsupported number, toString returns null
323360 JSONArray array = new JSONArray(Arrays.asList(5.5, Double.NaN));
324361 assertNull(array.toString());
362 }
363
364 public void testListConstructorCopiesContents() throws JSONException {
365 List<Object> contents = Arrays.<Object>asList(5);
366 JSONArray array = new JSONArray(contents);
367 contents.set(0, 10);
368 assertEquals(5, array.get(0));
325369 }
326370
327371 public void testCreate() throws JSONException {
332376 assertEquals("[5.5,true]", array.toString());
333377 }
334378
379 public void testAccessOutOfBounds() throws JSONException {
380 JSONArray array = new JSONArray();
381 array.put("foo");
382 assertEquals(null, array.opt(3));
383 assertEquals(null, array.opt(-3));
384 assertEquals("", array.optString(3));
385 assertEquals("", array.optString(-3));
386 try {
387 array.get(3);
388 fail();
389 } catch (JSONException e) {
390 }
391 try {
392 array.get(-3);
393 fail();
394 } catch (JSONException e) {
395 }
396 try {
397 array.getString(3);
398 fail();
399 } catch (JSONException e) {
400 }
401 try {
402 array.getString(-3);
403 fail();
404 } catch (JSONException e) {
405 }
406 }
407
335408 public void testParsingConstructor() {
336409 fail("TODO");
337410 }
0 /**
1 * Copyright (C) 2010 Google Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * 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
13 * limitations under the License.
14 */
15
16
17 package org.json;
18
19 import junit.framework.TestCase;
20
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 import java.util.*;
24
25 /**
26 * This black box test was written without inspecting the non-free org.json sourcecode.
27 */
28 public class JSONObjectTest extends TestCase {
29
30 public void testEmptyObject() throws JSONException {
31 JSONObject object = new JSONObject();
32 assertEquals(0, object.length());
33
34 // bogus (but documented) behaviour: returns null rather than the empty object
35 assertNull(object.names());
36
37 // bogus behaviour: returns null rather than an empty array
38 assertNull(object.toJSONArray(new JSONArray()));
39 assertEquals("{}", object.toString());
40 assertEquals("{}", object.toString(5));
41 try {
42 object.get("foo");
43 fail();
44 } catch (JSONException e) {
45 }
46 try {
47 object.getBoolean("foo");
48 fail();
49 } catch (JSONException e) {
50 }
51 try {
52 object.getDouble("foo");
53 fail();
54 } catch (JSONException e) {
55 }
56 try {
57 object.getInt("foo");
58 fail();
59 } catch (JSONException e) {
60 }
61 try {
62 object.getJSONArray("foo");
63 fail();
64 } catch (JSONException e) {
65 }
66 try {
67 object.getJSONObject("foo");
68 fail();
69 } catch (JSONException e) {
70 }
71 try {
72 object.getLong("foo");
73 fail();
74 } catch (JSONException e) {
75 }
76 try {
77 object.getString("foo");
78 fail();
79 } catch (JSONException e) {
80 }
81 assertFalse(object.has("foo"));
82 assertTrue(object.isNull("foo")); // isNull also means "is not present"
83 assertNull(object.opt("foo"));
84 assertEquals(false, object.optBoolean("foo"));
85 assertEquals(true, object.optBoolean("foo", true));
86 assertEquals(Double.NaN, object.optDouble("foo"));
87 assertEquals(5.0, object.optDouble("foo", 5.0));
88 assertEquals(0, object.optInt("foo"));
89 assertEquals(5, object.optInt("foo", 5));
90 assertEquals(null, object.optJSONArray("foo"));
91 assertEquals(null, object.optJSONObject("foo"));
92 assertEquals(0, object.optLong("foo"));
93 assertEquals(Long.MAX_VALUE-1, object.optLong("foo", Long.MAX_VALUE-1));
94 assertEquals("", object.optString("foo")); // empty string is default!
95 assertEquals("bar", object.optString("foo", "bar"));
96 assertNull(object.remove("foo"));
97 }
98
99 public void testEqualsAndHashCode() throws JSONException {
100 JSONObject a = new JSONObject();
101 JSONObject b = new JSONObject();
102
103 // JSON object doesn't override either equals or hashCode (!)
104 assertFalse(a.equals(b));
105 assertEquals(a.hashCode(), System.identityHashCode(a));
106 }
107
108 public void testGet() throws JSONException {
109 JSONObject object = new JSONObject();
110 Object value = new Object();
111 object.put("foo", value);
112 object.put("bar", new Object());
113 object.put("baz", new Object());
114 assertSame(value, object.get("foo"));
115 try {
116 object.get("FOO");
117 fail();
118 } catch (JSONException e) {
119 }
120 try {
121 object.put(null, value);
122 fail();
123 } catch (JSONException e) {
124 }
125 try {
126 object.get(null);
127 fail();
128 } catch (JSONException e) {
129 }
130 }
131
132 public void testPut() throws JSONException {
133 JSONObject object = new JSONObject();
134 assertSame(object, object.put("foo", true));
135 object.put("foo", false);
136 assertEquals(false, object.get("foo"));
137
138 object.put("foo", 5.0d);
139 assertEquals(5.0d, object.get("foo"));
140 object.put("foo", 0);
141 assertEquals(0, object.get("foo"));
142 object.put("bar", Long.MAX_VALUE - 1);
143 assertEquals(Long.MAX_VALUE - 1, object.get("bar"));
144 object.put("baz", "x");
145 assertEquals("x", object.get("baz"));
146 object.put("bar", JSONObject.NULL);
147 assertSame(JSONObject.NULL, object.get("bar"));
148 }
149
150 public void testPutNullRemoves() throws JSONException {
151 JSONObject object = new JSONObject();
152 object.put("foo", "bar");
153 object.put("foo", null);
154 assertEquals(0, object.length());
155 assertFalse(object.has("foo"));
156 try {
157 object.get("foo");
158 fail();
159 } catch (JSONException e) {
160 }
161 }
162
163 public void testPutOpt() throws JSONException {
164 JSONObject object = new JSONObject();
165 object.put("foo", "bar");
166 object.putOpt("foo", null);
167 assertEquals("bar", object.get("foo"));
168 object.putOpt(null, null);
169 assertEquals(1, object.length());
170 object.putOpt(null, "bar");
171 assertEquals(1, object.length());
172 }
173
174 public void testPutOptUnsupportedNumbers() throws JSONException {
175 JSONObject object = new JSONObject();
176 try {
177 object.putOpt("foo", Double.NaN);
178 fail();
179 } catch (JSONException e) {
180 }
181 try {
182 object.putOpt("foo", Double.NEGATIVE_INFINITY);
183 fail();
184 } catch (JSONException e) {
185 }
186 try {
187 object.putOpt("foo", Double.POSITIVE_INFINITY);
188 fail();
189 } catch (JSONException e) {
190 }
191 }
192
193 public void testRemove() throws JSONException {
194 JSONObject object = new JSONObject();
195 object.put("foo", "bar");
196 assertEquals(null, object.remove(null));
197 assertEquals(null, object.remove(""));
198 assertEquals(null, object.remove("bar"));
199 assertEquals("bar", object.remove("foo"));
200 assertEquals(null, object.remove("foo"));
201 }
202
203 public void testBooleans() throws JSONException {
204 JSONObject object = new JSONObject();
205 object.put("foo", true);
206 object.put("bar", false);
207 object.put("baz", "true");
208 object.put("quux", "false");
209 assertEquals(4, object.length());
210 assertEquals(true, object.getBoolean("foo"));
211 assertEquals(false, object.getBoolean("bar"));
212 assertEquals(true, object.getBoolean("baz"));
213 assertEquals(false, object.getBoolean("quux"));
214 assertFalse(object.isNull("foo"));
215 assertFalse(object.isNull("quux"));
216 assertTrue(object.has("foo"));
217 assertTrue(object.has("quux"));
218 assertFalse(object.has("missing"));
219 assertEquals(true, object.optBoolean("foo"));
220 assertEquals(false, object.optBoolean("bar"));
221 assertEquals(true, object.optBoolean("baz"));
222 assertEquals(false, object.optBoolean("quux"));
223 assertEquals(false, object.optBoolean("missing"));
224 assertEquals(true, object.optBoolean("foo", true));
225 assertEquals(false, object.optBoolean("bar", true));
226 assertEquals(true, object.optBoolean("baz", true));
227 assertEquals(false, object.optBoolean("quux", true));
228 assertEquals(true, object.optBoolean("missing", true));
229
230 object.put("foo", "truE");
231 object.put("bar", "FALSE");
232 assertEquals(true, object.getBoolean("foo"));
233 assertEquals(false, object.getBoolean("bar"));
234 assertEquals(true, object.optBoolean("foo"));
235 assertEquals(false, object.optBoolean("bar"));
236 assertEquals(true, object.optBoolean("foo", false));
237 assertEquals(false, object.optBoolean("bar", false));
238 }
239
240 public void testNumbers() throws JSONException {
241 JSONObject object = new JSONObject();
242 object.put("foo", Double.MIN_VALUE);
243 object.put("bar", 9223372036854775806L);
244 object.put("baz", Double.MAX_VALUE);
245 object.put("quux", -0d);
246 assertEquals(4, object.length());
247
248 assertTrue(object.toString().contains("\"foo\":4.9E-324"));
249 assertTrue(object.toString().contains("\"bar\":9223372036854775806"));
250 assertTrue(object.toString().contains("\"baz\":1.7976931348623157E308"));
251
252 // bogus behaviour: toString() and getString() return different values for -0d
253 assertTrue(object.toString().contains("\"quux\":-0}") // no trailing decimal point
254 || object.toString().contains("\"quux\":-0,"));
255
256 assertEquals(Double.MIN_VALUE, object.get("foo"));
257 assertEquals(9223372036854775806L, object.get("bar"));
258 assertEquals(Double.MAX_VALUE, object.get("baz"));
259 assertEquals(-0d, object.get("quux"));
260 assertEquals(Double.MIN_VALUE, object.getDouble("foo"));
261 assertEquals(9.223372036854776E18, object.getDouble("bar"));
262 assertEquals(Double.MAX_VALUE, object.getDouble("baz"));
263 assertEquals(-0d, object.getDouble("quux"));
264 assertEquals(0, object.getLong("foo"));
265 assertEquals(9223372036854775806L, object.getLong("bar"));
266 assertEquals(Long.MAX_VALUE, object.getLong("baz"));
267 assertEquals(0, object.getLong("quux"));
268 assertEquals(0, object.getInt("foo"));
269 assertEquals(-2, object.getInt("bar"));
270 assertEquals(Integer.MAX_VALUE, object.getInt("baz"));
271 assertEquals(0, object.getInt("quux"));
272 assertEquals(Double.MIN_VALUE, object.opt("foo"));
273 assertEquals(9223372036854775806L, object.optLong("bar"));
274 assertEquals(Double.MAX_VALUE, object.optDouble("baz"));
275 assertEquals(0, object.optInt("quux"));
276 assertEquals(Double.MIN_VALUE, object.opt("foo"));
277 assertEquals(9223372036854775806L, object.optLong("bar"));
278 assertEquals(Double.MAX_VALUE, object.optDouble("baz"));
279 assertEquals(0, object.optInt("quux"));
280 assertEquals(Double.MIN_VALUE, object.optDouble("foo", 5.0d));
281 assertEquals(9223372036854775806L, object.optLong("bar", 1L));
282 assertEquals(Long.MAX_VALUE, object.optLong("baz", 1L));
283 assertEquals(0, object.optInt("quux", -1));
284 assertEquals("4.9E-324", object.getString("foo"));
285 assertEquals("9223372036854775806", object.getString("bar"));
286 assertEquals("1.7976931348623157E308", object.getString("baz"));
287 assertEquals("-0.0", object.getString("quux"));
288 }
289
290 public void testFloats() throws JSONException {
291 JSONObject object = new JSONObject();
292 try {
293 object.put("foo", (Float) Float.NaN);
294 fail();
295 } catch (JSONException e) {
296 }
297 try {
298 object.put("foo", (Float) Float.NEGATIVE_INFINITY);
299 fail();
300 } catch (JSONException e) {
301 }
302 try {
303 object.put("foo", (Float) Float.POSITIVE_INFINITY);
304 fail();
305 } catch (JSONException e) {
306 }
307 }
308
309 public void testOtherNumbers() throws JSONException {
310 Number nan = new Number() {
311 public int intValue() {
312 return 0;
313 }
314 public long longValue() {
315 return 1L;
316 }
317 public float floatValue() {
318 return 2;
319 }
320 public double doubleValue() {
321 return Double.NaN;
322 }
323 @Override public String toString() {
324 return "x";
325 }
326 };
327
328 // bogus behaviour: foreign object types should be rejected!
329 JSONObject object = new JSONObject();
330 object.put("foo", nan);
331 assertEquals("{\"foo\":x}", object.toString());
332 }
333
334 public void testForeignObjects() throws JSONException {
335 Object foreign = new Object() {
336 @Override public String toString() {
337 return "x";
338 }
339 };
340
341 // bogus behaviour: foreign object types should be rejected and not treated as Strings
342 JSONObject object = new JSONObject();
343 object.put("foo", foreign);
344 assertEquals("{\"foo\":\"x\"}", object.toString());
345 }
346
347 public void testNullKeys() {
348 try {
349 new JSONObject().put(null, false);
350 fail();
351 } catch (JSONException e) {
352 }
353 try {
354 new JSONObject().put(null, 0.0d);
355 fail();
356 } catch (JSONException e) {
357 }
358 try {
359 new JSONObject().put(null, 5);
360 fail();
361 } catch (JSONException e) {
362 }
363 try {
364 new JSONObject().put(null, 5L);
365 fail();
366 } catch (JSONException e) {
367 }
368 try {
369 new JSONObject().put(null, "foo");
370 fail();
371 } catch (JSONException e) {
372 }
373 }
374
375 public void testStrings() throws JSONException {
376 JSONObject object = new JSONObject();
377 object.put("foo", "true");
378 object.put("bar", "5.5");
379 object.put("baz", "9223372036854775806");
380 object.put("quux", "null");
381 object.put("height", "5\"8' tall");
382
383 assertTrue(object.toString().contains("\"foo\":\"true\""));
384 assertTrue(object.toString().contains("\"bar\":\"5.5\""));
385 assertTrue(object.toString().contains("\"baz\":\"9223372036854775806\""));
386 assertTrue(object.toString().contains("\"quux\":\"null\""));
387 assertTrue(object.toString().contains("\"height\":\"5\\\"8' tall\""));
388
389 assertEquals("true", object.get("foo"));
390 assertEquals("null", object.getString("quux"));
391 assertEquals("5\"8' tall", object.getString("height"));
392 assertEquals("true", object.opt("foo"));
393 assertEquals("5.5", object.optString("bar"));
394 assertEquals("true", object.optString("foo", "x"));
395 assertFalse(object.isNull("foo"));
396
397 assertEquals(true, object.getBoolean("foo"));
398 assertEquals(true, object.optBoolean("foo"));
399 assertEquals(true, object.optBoolean("foo", false));
400 assertEquals(0, object.optInt("foo"));
401 assertEquals(-2, object.optInt("foo", -2));
402
403 assertEquals(5.5d, object.getDouble("bar"));
404 assertEquals(5L, object.getLong("bar"));
405 assertEquals(5, object.getInt("bar"));
406 assertEquals(5, object.optInt("bar", 3));
407
408 // The last digit of the string is a 6 but getLong returns a 7. It's probably parsing as a
409 // double and then converting that to a long. This is consistent with JavaScript.
410 assertEquals(9223372036854775807L, object.getLong("baz"));
411 assertEquals(9.223372036854776E18, object.getDouble("baz"));
412 assertEquals(Integer.MAX_VALUE, object.getInt("baz"));
413
414 assertFalse(object.isNull("quux"));
415 try {
416 object.getDouble("quux");
417 fail();
418 } catch (JSONException e) {
419 }
420 assertEquals(Double.NaN, object.optDouble("quux"));
421 assertEquals(-1.0d, object.optDouble("quux", -1.0d));
422
423 object.put("foo", "TRUE");
424 assertEquals(true, object.getBoolean("foo"));
425 }
426
427 public void testJSONObjects() throws JSONException {
428 JSONObject object = new JSONObject();
429
430 JSONArray a = new JSONArray();
431 JSONObject b = new JSONObject();
432 object.put("foo", a);
433 object.put("bar", b);
434
435 assertSame(a, object.getJSONArray("foo"));
436 assertSame(b, object.getJSONObject("bar"));
437 try {
438 object.getJSONObject("foo");
439 fail();
440 } catch (JSONException e) {
441 }
442 try {
443 object.getJSONArray("bar");
444 fail();
445 } catch (JSONException e) {
446 }
447 assertEquals(a, object.optJSONArray("foo"));
448 assertEquals(b, object.optJSONObject("bar"));
449 assertEquals(null, object.optJSONArray("bar"));
450 assertEquals(null, object.optJSONObject("foo"));
451 }
452
453 public void testToJSONArray() throws JSONException {
454 JSONObject object = new JSONObject();
455 Object value = new Object();
456 object.put("foo", true);
457 object.put("bar", 5.0d);
458 object.put("baz", -0.0d);
459 object.put("quux", value);
460
461 JSONArray names = new JSONArray();
462 names.put("baz");
463 names.put("quux");
464 names.put("foo");
465
466 JSONArray array = object.toJSONArray(names);
467 assertEquals(-0.0d, array.get(0));
468 assertEquals(value, array.get(1));
469 assertEquals(true, array.get(2));
470
471 object.put("foo", false);
472 assertEquals(true, array.get(2));
473 }
474
475 public void testToJSONArrayMissingNames() throws JSONException {
476 JSONObject object = new JSONObject();
477 object.put("foo", true);
478 object.put("bar", 5.0d);
479 object.put("baz", JSONObject.NULL);
480
481 JSONArray names = new JSONArray();
482 names.put("bar");
483 names.put("foo");
484 names.put("quux");
485 names.put("baz");
486
487 JSONArray array = object.toJSONArray(names);
488 assertEquals(4, array.length());
489
490 assertEquals(5.0d, array.get(0));
491 assertEquals(true, array.get(1));
492 try {
493 array.get(2);
494 fail();
495 } catch (JSONException e) {
496 }
497 assertEquals(JSONObject.NULL, array.get(3));
498 }
499
500 public void testToJSONArrayNull() throws JSONException {
501 JSONObject object = new JSONObject();
502 assertEquals(null, object.toJSONArray(null));
503 object.put("foo", 5);
504 try {
505 object.toJSONArray(null);
506 } catch (JSONException e) {
507 }
508 }
509
510 public void testToJSONArrayEndsUpEmpty() throws JSONException {
511 JSONObject object = new JSONObject();
512 object.put("foo", 5);
513 JSONArray array = new JSONArray();
514 array.put("bar");
515 assertEquals(1, object.toJSONArray(array).length());
516 }
517
518 public void testToJSONArrayNonString() throws JSONException {
519 JSONObject object = new JSONObject();
520 object.put("foo", 5);
521 object.put("null", 10);
522 object.put("false", 15);
523
524 JSONArray names = new JSONArray();
525 names.put(JSONObject.NULL);
526 names.put(false);
527 names.put("foo");
528
529 // bogus behaviour: array elements are converted to Strings
530 JSONArray array = object.toJSONArray(names);
531 assertEquals(3, array.length());
532 assertEquals(10, array.get(0));
533 assertEquals(15, array.get(1));
534 assertEquals(5, array.get(2));
535 }
536
537 public void testPutUnsupportedNumbers() throws JSONException {
538 JSONObject object = new JSONObject();
539 try {
540 object.put("foo", Double.NaN);
541 fail();
542 } catch (JSONException e) {
543 }
544 try {
545 object.put("foo", Double.NEGATIVE_INFINITY);
546 fail();
547 } catch (JSONException e) {
548 }
549 try {
550 object.put("foo", Double.POSITIVE_INFINITY);
551 fail();
552 } catch (JSONException e) {
553 }
554 }
555
556 public void testPutUnsupportedNumbersAsObjects() throws JSONException {
557 JSONObject object = new JSONObject();
558 try {
559 object.put("foo", (Double) Double.NaN);
560 fail();
561 } catch (JSONException e) {
562 }
563 try {
564 object.put("foo", (Double) Double.NEGATIVE_INFINITY);
565 fail();
566 } catch (JSONException e) {
567 }
568 try {
569 object.put("foo", (Double) Double.POSITIVE_INFINITY);
570 fail();
571 } catch (JSONException e) {
572 }
573 }
574
575 /**
576 * Although JSONObject is usually defensive about which numbers it accepts,
577 * it doesn't check inputs in its constructor.
578 */
579 public void testCreateWithUnsupportedNumbers() throws JSONException {
580 Map<String, Object> contents = new HashMap<String, Object>();
581 contents.put("foo", Double.NaN);
582 contents.put("bar", Double.NEGATIVE_INFINITY);
583 contents.put("baz", Double.POSITIVE_INFINITY);
584
585 JSONObject object = new JSONObject(contents);
586 assertEquals(Double.NaN, object.get("foo"));
587 assertEquals(Double.NEGATIVE_INFINITY, object.get("bar"));
588 assertEquals(Double.POSITIVE_INFINITY, object.get("baz"));
589 }
590
591 public void testToStringWithUnsupportedNumbers() {
592 // bogus behaviour: when the object contains an unsupported number, toString returns null
593 JSONObject object = new JSONObject(Collections.singletonMap("foo", Double.NaN));
594 assertEquals(null, object.toString());
595 }
596
597 public void testMapConstructorCopiesContents() throws JSONException {
598 Map<String, Object> contents = new HashMap<String, Object>();
599 contents.put("foo", 5);
600 JSONObject object = new JSONObject(contents);
601 contents.put("foo", 10);
602 assertEquals(5, object.get("foo"));
603 }
604
605 public void testMapConstructorWithBogusEntries() {
606 Map<Object, Object> contents = new HashMap<Object, Object>();
607 contents.put(5, 5);
608
609 // bogus behaviour: the constructor doesn't validate its input
610 new JSONObject(contents);
611 }
612
613 public void testAccumulateMutatesInPlace() throws JSONException {
614 JSONObject object = new JSONObject();
615 object.put("foo", 5);
616 object.accumulate("foo", 6);
617 JSONArray array = object.getJSONArray("foo");
618 assertEquals("[5,6]", array.toString());
619 object.accumulate("foo", 7);
620 assertEquals("[5,6,7]", array.toString());
621 }
622
623 public void testAccumulateExistingArray() throws JSONException {
624 JSONArray array = new JSONArray();
625 JSONObject object = new JSONObject();
626 object.put("foo", array);
627 object.accumulate("foo", 5);
628 assertEquals("[5]", array.toString());
629 }
630
631 public void testAccumulatePutArray() throws JSONException {
632 JSONObject object = new JSONObject();
633 object.accumulate("foo", 5);
634 assertEquals("{\"foo\":5}", object.toString());
635 object.accumulate("foo", new JSONArray());
636 assertEquals("{\"foo\":[5,[]]}", object.toString());
637 }
638
639 public void testAccumulateNull() {
640 JSONObject object = new JSONObject();
641 try {
642 object.accumulate(null, 5);
643 fail();
644 } catch (JSONException e) {
645 }
646 }
647
648 public void testEmptyStringKey() throws JSONException {
649 JSONObject object = new JSONObject();
650 object.put("", 5);
651 assertEquals(5, object.get(""));
652 assertEquals("{\"\":5}", object.toString());
653 }
654
655 public void testNullValue() throws JSONException {
656 JSONObject object = new JSONObject();
657 object.put("foo", JSONObject.NULL);
658 object.put("bar", null);
659
660 // bogus behaviour: there's 2 ways to represent null; each behaves differently!
661 assertTrue(object.has("foo"));
662 assertFalse(object.has("bar"));
663 assertTrue(object.isNull("foo"));
664 assertTrue(object.isNull("bar"));
665 }
666
667 public void testHas() throws JSONException {
668 JSONObject object = new JSONObject();
669 object.put("foo", 5);
670 assertTrue(object.has("foo"));
671 assertFalse(object.has("bar"));
672 assertFalse(object.has(null));
673 }
674
675 public void testOptNull() throws JSONException {
676 JSONObject object = new JSONObject();
677 object.put("foo", "bar");
678 assertEquals(null, object.opt(null));
679 assertEquals(false, object.optBoolean(null));
680 assertEquals(Double.NaN, object.optDouble(null));
681 assertEquals(0, object.optInt(null));
682 assertEquals(0L, object.optLong(null));
683 assertEquals(null, object.optJSONArray(null));
684 assertEquals(null, object.optJSONObject(null));
685 assertEquals("", object.optString(null));
686 assertEquals(true, object.optBoolean(null, true));
687 assertEquals(0.0d, object.optDouble(null, 0.0d));
688 assertEquals(1, object.optInt(null, 1));
689 assertEquals(1L, object.optLong(null, 1L));
690 assertEquals("baz", object.optString(null, "baz"));
691 }
692
693 public void testToStringWithIndentFactor() throws JSONException {
694 JSONObject object = new JSONObject();
695 object.put("foo", new JSONArray(Arrays.asList(5, 6)));
696 object.put("bar", new JSONObject());
697 String foobar = "{\n" +
698 " \"foo\": [\n" +
699 " 5,\n" +
700 " 6\n" +
701 " ],\n" +
702 " \"bar\": {}\n" +
703 "}";
704 String barfoo = "{\n" +
705 " \"bar\": {},\n" +
706 " \"foo\": [\n" +
707 " 5,\n" +
708 " 6\n" +
709 " ]\n" +
710 "}";
711 String string = object.toString(5);
712 assertTrue(string, foobar.equals(string) || barfoo.equals(string));
713 }
714
715 public void testNames() throws JSONException {
716 JSONObject object = new JSONObject();
717 object.put("foo", 5);
718 object.put("bar", 6);
719 object.put("baz", 7);
720 JSONArray array = object.names();
721 assertTrue(array.toString().contains("foo"));
722 assertTrue(array.toString().contains("bar"));
723 assertTrue(array.toString().contains("baz"));
724 }
725
726 public void testKeysEmptyObject() {
727 JSONObject object = new JSONObject();
728 assertFalse(object.keys().hasNext());
729 try {
730 object.keys().next();
731 fail();
732 } catch (NoSuchElementException e) {
733 }
734 }
735
736 public void testKeys() throws JSONException {
737 JSONObject object = new JSONObject();
738 object.put("foo", 5);
739 object.put("bar", 6);
740 object.put("foo", 7);
741
742 @SuppressWarnings("unchecked")
743 Iterator<String> keys = (Iterator<String>) object.keys();
744 Set<String> result = new HashSet<String>();
745 assertTrue(keys.hasNext());
746 result.add(keys.next());
747 assertTrue(keys.hasNext());
748 result.add(keys.next());
749 assertFalse(keys.hasNext());
750 assertEquals(new HashSet<String>(Arrays.asList("foo", "bar")), result);
751
752 try {
753 keys.next();
754 fail();
755 } catch (NoSuchElementException e) {
756 }
757 }
758
759 public void testMutatingKeysMutatesObject() throws JSONException {
760 JSONObject object = new JSONObject();
761 object.put("foo", 5);
762 Iterator keys = object.keys();
763 keys.next();
764 keys.remove();
765 assertEquals(0, object.length());
766 }
767
768 public void testQuote() {
769 // covered by JSONStringerTest.testEscaping()
770 }
771
772 public void testNumberToString() throws JSONException {
773 assertEquals("5", JSONObject.numberToString(5));
774 assertEquals("-0", JSONObject.numberToString(-0.0d));
775 assertEquals("9223372036854775806", JSONObject.numberToString(9223372036854775806L));
776 assertEquals("4.9E-324", JSONObject.numberToString(Double.MIN_VALUE));
777 assertEquals("1.7976931348623157E308", JSONObject.numberToString(Double.MAX_VALUE));
778 try {
779 JSONObject.numberToString(Double.NaN);
780 fail();
781 } catch (JSONException e) {
782 }
783 try {
784 JSONObject.numberToString(Double.NEGATIVE_INFINITY);
785 fail();
786 } catch (JSONException e) {
787 }
788 try {
789 JSONObject.numberToString(Double.POSITIVE_INFINITY);
790 fail();
791 } catch (JSONException e) {
792 }
793 assertEquals("0.001", JSONObject.numberToString(new BigDecimal("0.001")));
794 assertEquals("9223372036854775806",
795 JSONObject.numberToString(new BigInteger("9223372036854775806")));
796 try {
797 JSONObject.numberToString(null);
798 fail();
799 } catch (JSONException e) {
800 }
801 }
802 }
212212 new JSONStringer().object().key("a").value(original).endObject().toString());
213213 assertEquals("[\"" + escaped + "\"]",
214214 new JSONStringer().array().value(original).endArray().toString());
215 assertEquals("\"" + escaped + "\"", JSONObject.quote(original));
215216 }
216217
217218 public void testJSONArrayAsValue() throws JSONException {
0 /**
1 * Copyright (C) 2010 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * 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
13 * limitations under the License.
14 */
15
16 package org.json;
17
18 import junit.framework.TestCase;
19
20 /**
21 * These tests checks self use; ie. when methods delegate to non-final methods.
22 * For the most part we doesn't attempt to cover self-use, except in those cases
23 * where our clean room implementation does it.
24 *
25 * <p>This black box test was written without inspecting the non-free org.json
26 * sourcecode.
27 */
28 public class SelfUseTest extends TestCase {
29
30 private int objectPutCalls = 0;
31 private int objectGetCalls = 0;
32 private int objectOptCalls = 0;
33 private int objectOptTypeCalls = 0;
34 private int arrayPutCalls = 0;
35 private int arrayGetCalls = 0;
36 private int arrayOptCalls = 0;
37 private int arrayOptTypeCalls = 0;
38
39 private final JSONObject object = new JSONObject() {
40 @Override public JSONObject put(String name, Object value) throws JSONException {
41 objectPutCalls++;
42 return super.put(name, value);
43 }
44 @Override public Object get(String name) throws JSONException {
45 objectGetCalls++;
46 return super.get(name);
47 }
48 @Override public Object opt(String name) {
49 objectOptCalls++;
50 return super.opt(name);
51 }
52 @Override public boolean optBoolean(String key, boolean defaultValue) {
53 objectOptTypeCalls++;
54 return super.optBoolean(key, defaultValue);
55 }
56 @Override public double optDouble(String key, double defaultValue) {
57 objectOptTypeCalls++;
58 return super.optDouble(key, defaultValue);
59 }
60 @Override public int optInt(String key, int defaultValue) {
61 objectOptTypeCalls++;
62 return super.optInt(key, defaultValue);
63 }
64 @Override public long optLong(String key, long defaultValue) {
65 objectOptTypeCalls++;
66 return super.optLong(key, defaultValue);
67 }
68 @Override public String optString(String key, String defaultValue) {
69 objectOptTypeCalls++;
70 return super.optString(key, defaultValue);
71 }
72 };
73
74 private final JSONArray array = new JSONArray() {
75 @Override public JSONArray put(int index, Object value) throws JSONException {
76 arrayPutCalls++;
77 return super.put(index, value);
78 }
79 @Override public Object get(int index) throws JSONException {
80 arrayGetCalls++;
81 return super.get(index);
82 }
83 @Override public Object opt(int index) {
84 arrayOptCalls++;
85 return super.opt(index);
86 }
87 @Override public boolean optBoolean(int index, boolean fallback) {
88 arrayOptTypeCalls++;
89 return super.optBoolean(index, fallback);
90 }
91 @Override public double optDouble(int index, double fallback) {
92 arrayOptTypeCalls++;
93 return super.optDouble(index, fallback);
94 }
95 @Override public long optLong(int index, long fallback) {
96 arrayOptTypeCalls++;
97 return super.optLong(index, fallback);
98 }
99 @Override public String optString(int index, String fallback) {
100 arrayOptTypeCalls++;
101 return super.optString(index, fallback);
102 }
103 @Override public int optInt(int index, int fallback) {
104 arrayOptTypeCalls++;
105 return super.optInt(index, fallback);
106 }
107 };
108
109 public void testObjectPut() throws JSONException {
110 object.putOpt("foo", "bar");
111 assertEquals(1, objectPutCalls);
112 }
113
114 public void testObjectAccumulate() throws JSONException {
115 object.accumulate("foo", "bar");
116 assertEquals(1, objectPutCalls);
117 }
118
119 public void testObjectGetBoolean() throws JSONException {
120 object.put("foo", "true");
121 object.getBoolean("foo");
122 assertEquals(1, objectGetCalls);
123 }
124
125 public void testObjectOptType() throws JSONException {
126 object.optBoolean("foo");
127 assertEquals(1, objectOptCalls);
128 assertEquals(1, objectOptTypeCalls);
129 object.optDouble("foo");
130 assertEquals(2, objectOptCalls);
131 assertEquals(2, objectOptTypeCalls);
132 object.optInt("foo");
133 assertEquals(3, objectOptCalls);
134 assertEquals(3, objectOptTypeCalls);
135 object.optLong("foo");
136 assertEquals(4, objectOptCalls);
137 assertEquals(4, objectOptTypeCalls);
138 object.optString("foo");
139 assertEquals(5, objectOptCalls);
140 assertEquals(5, objectOptTypeCalls);
141 }
142
143 public void testToJSONArray() throws JSONException {
144 object.put("foo", 5);
145 object.put("bar", 10);
146 array.put("foo");
147 array.put("baz");
148 array.put("bar");
149 object.toJSONArray(array);
150 assertEquals(3, arrayOptCalls);
151 assertEquals(0, arrayOptTypeCalls);
152 assertEquals(3, objectOptCalls);
153 assertEquals(0, objectOptTypeCalls);
154 }
155
156 public void testPutAtIndex() throws JSONException {
157 array.put(10, false);
158 assertEquals(1, arrayPutCalls);
159 }
160
161 public void testIsNull() {
162 array.isNull(5);
163 assertEquals(1, arrayOptCalls);
164 }
165
166 public void testArrayGetType() throws JSONException {
167 array.put(true);
168 array.getBoolean(0);
169 assertEquals(1, arrayGetCalls);
170 }
171
172 public void testArrayOptType() throws JSONException {
173 array.optBoolean(3);
174 assertEquals(1, arrayOptCalls);
175 assertEquals(1, arrayOptTypeCalls);
176 array.optDouble(3);
177 assertEquals(2, arrayOptCalls);
178 assertEquals(2, arrayOptTypeCalls);
179 array.optInt(3);
180 assertEquals(3, arrayOptCalls);
181 assertEquals(3, arrayOptTypeCalls);
182 array.optLong(3);
183 assertEquals(4, arrayOptCalls);
184 assertEquals(4, arrayOptTypeCalls);
185 array.optString(3);
186 assertEquals(5, arrayOptCalls);
187 assertEquals(5, arrayOptTypeCalls);
188 }
189
190 public void testToJSONObject() throws JSONException {
191 array.put("foo");
192 array.put("baz");
193 array.put("bar");
194 JSONArray values = new JSONArray();
195 values.put(5.5d);
196 values.put(11d);
197 values.put(30);
198 values.toJSONObject(array);
199 assertEquals(3, arrayOptCalls);
200 assertEquals(0, arrayOptTypeCalls);
201 }
202
203 }