From 969e2d4fd52a503d8f4a7fe81b8758c13a9180e4 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 4 Jun 2015 22:29:55 -0500 Subject: [PATCH] test iterable --- JSONArrayTest.java | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/JSONArrayTest.java b/JSONArrayTest.java index 78b194e..b033a60 100644 --- a/JSONArrayTest.java +++ b/JSONArrayTest.java @@ -2,10 +2,7 @@ package org.json.junit; import static org.junit.Assert.*; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import org.json.*; import org.junit.Test; @@ -464,4 +461,42 @@ public class JSONArrayTest { JSONArray expectedJsonArray = new JSONArray(expectedStr); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } + + @Test + public void iterator() { + JSONArray jsonArray = new JSONArray(arrayStr); + Iterator it = jsonArray.iterator(); + assertTrue("Array true", + Boolean.TRUE.equals(it.next())); + assertTrue("Array false", + Boolean.FALSE.equals(it.next())); + assertTrue("Array string true", + "true".equals(it.next())); + assertTrue("Array string false", + "false".equals(it.next())); + assertTrue("Array string", + "hello".equals(it.next())); + + assertTrue("Array double", + new Double(23.45e-4).equals(it.next())); + assertTrue("Array string double", + new Double(23.45).equals(Double.parseDouble((String)it.next()))); + + assertTrue("Array value int", + new Integer(42).equals(it.next())); + assertTrue("Array value string int", + new Integer(43).equals(Integer.parseInt((String)it.next()))); + + JSONArray nestedJsonArray = (JSONArray)it.next(); + assertTrue("Array value JSONArray", nestedJsonArray != null); + + JSONObject nestedJsonObject = (JSONObject)it.next(); + assertTrue("Array value JSONObject", nestedJsonObject != null); + + assertTrue("Array value long", + new Long(0).equals(((Number) it.next()).longValue())); + assertTrue("Array value string long", + new Long(-1).equals(Long.parseLong((String) it.next()))); + assertTrue("should be at end of array", !it.hasNext()); + } }