|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.*; |
4 | 4 |
|
5 | | -import java.util.ArrayList; |
6 | | -import java.util.Collection; |
7 | | -import java.util.HashMap; |
8 | | -import java.util.Map; |
| 5 | +import java.util.*; |
9 | 6 |
|
10 | 7 | import org.json.*; |
11 | 8 | import org.junit.Test; |
@@ -464,4 +461,42 @@ public void objectArrayVsIsArray() { |
464 | 461 | JSONArray expectedJsonArray = new JSONArray(expectedStr); |
465 | 462 | Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); |
466 | 463 | } |
| 464 | + |
| 465 | + @Test |
| 466 | + public void iterator() { |
| 467 | + JSONArray jsonArray = new JSONArray(arrayStr); |
| 468 | + Iterator<Object> it = jsonArray.iterator(); |
| 469 | + assertTrue("Array true", |
| 470 | + Boolean.TRUE.equals(it.next())); |
| 471 | + assertTrue("Array false", |
| 472 | + Boolean.FALSE.equals(it.next())); |
| 473 | + assertTrue("Array string true", |
| 474 | + "true".equals(it.next())); |
| 475 | + assertTrue("Array string false", |
| 476 | + "false".equals(it.next())); |
| 477 | + assertTrue("Array string", |
| 478 | + "hello".equals(it.next())); |
| 479 | + |
| 480 | + assertTrue("Array double", |
| 481 | + new Double(23.45e-4).equals(it.next())); |
| 482 | + assertTrue("Array string double", |
| 483 | + new Double(23.45).equals(Double.parseDouble((String)it.next()))); |
| 484 | + |
| 485 | + assertTrue("Array value int", |
| 486 | + new Integer(42).equals(it.next())); |
| 487 | + assertTrue("Array value string int", |
| 488 | + new Integer(43).equals(Integer.parseInt((String)it.next()))); |
| 489 | + |
| 490 | + JSONArray nestedJsonArray = (JSONArray)it.next(); |
| 491 | + assertTrue("Array value JSONArray", nestedJsonArray != null); |
| 492 | + |
| 493 | + JSONObject nestedJsonObject = (JSONObject)it.next(); |
| 494 | + assertTrue("Array value JSONObject", nestedJsonObject != null); |
| 495 | + |
| 496 | + assertTrue("Array value long", |
| 497 | + new Long(0).equals(((Number) it.next()).longValue())); |
| 498 | + assertTrue("Array value string long", |
| 499 | + new Long(-1).equals(Long.parseLong((String) it.next()))); |
| 500 | + assertTrue("should be at end of array", !it.hasNext()); |
| 501 | + } |
467 | 502 | } |
0 commit comments