JSONArrayTest.java revision f33eae7e84eb6d3b0f4e86b59605bb3de73009f3
151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson/* 267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * Copyright (C) 2010 The Android Open Source Project 367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * 467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * you may not use this file except in compliance with the License. 667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * You may obtain a copy of the License at 767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * 851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * 1067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * Unless required by applicable law or agreed to in writing, software 1167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 1267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * See the License for the specific language governing permissions and 1467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * limitations under the License. 1567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson */ 1667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 1773c8bbb045404420ffe4440bb1c7c5578ca160a7Jesse Wilsonpackage org.json; 1867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 1967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilsonimport junit.framework.TestCase; 2067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 2167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilsonimport java.util.Arrays; 2253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilsonimport java.util.List; 2351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilsonimport java.util.Collections; 2467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 2567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson/** 2667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * This black box test was written without inspecting the non-free org.json sourcecode. 2767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson */ 2867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilsonpublic class JSONArrayTest extends TestCase { 2967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 3067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testEmptyArray() throws JSONException { 3167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 3267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.length()); 3367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("", array.join(" AND ")); 3467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 3567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.get(0); 3667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 3767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 3867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 3967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 4067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.getBoolean(0); 4167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 4267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 4367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 4467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 4567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[]", array.toString()); 4667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[]", array.toString(4)); 4767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 4867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson // out of bounds is co-opted with defaulting 4967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.isNull(0)); 5067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertNull(array.opt(0)); 5167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.optBoolean(0)); 5267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.optBoolean(0, true)); 5367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 5451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson // bogus (but documented) behaviour: returns null rather than an empty object! 5567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertNull(array.toJSONObject(new JSONArray())); 5667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 5767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 5867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testEqualsAndHashCode() throws JSONException { 5967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray a = new JSONArray(); 6067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray b = new JSONArray(); 6167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(a.equals(b)); 6251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("equals() not consistent with hashCode()", a.hashCode(), b.hashCode()); 6367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 6467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson a.put(true); 6567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson a.put(false); 6667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson b.put(true); 6767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson b.put(false); 6867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(a.equals(b)); 6967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(a.hashCode(), b.hashCode()); 7067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 7167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson b.put(true); 7267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(a.equals(b)); 7367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(a.hashCode() != b.hashCode()); 7467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 7567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 7667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testBooleans() throws JSONException { 7767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 7867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(true); 7967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(false); 8067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(2, false); 8167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(3, false); 8267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(2, true); 8367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[true,false,true,false]", array.toString()); 8467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(4, array.length()); 8567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Boolean.TRUE, array.get(0)); 8667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Boolean.FALSE, array.get(1)); 8767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Boolean.TRUE, array.get(2)); 8867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Boolean.FALSE, array.get(3)); 8967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(0)); 9067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(1)); 9167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(2)); 9267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(3)); 9367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.optBoolean(0)); 9467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(false, array.optBoolean(1, true)); 9567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.optBoolean(2, false)); 9667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(false, array.optBoolean(3)); 9767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("true", array.getString(0)); 9867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("false", array.getString(1)); 9967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("true", array.optString(2)); 10067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("false", array.optString(3, "x")); 10167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[\n true,\n false,\n true,\n false\n]", array.toString(5)); 10267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 10367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray other = new JSONArray(); 10467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(true); 10567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(false); 10667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(true); 10767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(false); 10867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.equals(other)); 10967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(true); 11067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.equals(other)); 11167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 11267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other = new JSONArray(); 11367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put("true"); 11467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put("false"); 11567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put("truE"); 11667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put("FALSE"); 11767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.equals(other)); 11867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(other.equals(array)); 11967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, other.getBoolean(0)); 12067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(false, other.optBoolean(1, true)); 12167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, other.optBoolean(2)); 12267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(false, other.getBoolean(3)); 12367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 12467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 12567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testNulls() throws JSONException { 12667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 12767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(3, null); 12867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(0, JSONObject.NULL); 12967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(4, array.length()); 13067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[null,null,null,null]", array.toString()); 13167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 13251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson // there's 2 ways to represent null; each behaves differently! 13367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(JSONObject.NULL, array.get(0)); 13467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 13553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(1); 13667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 13767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 13867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 13967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 14053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(2); 14167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 14267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 14367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 14467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 14553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(3); 14667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 14767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 14867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 14967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(JSONObject.NULL, array.opt(0)); 15067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(null, array.opt(1)); 15167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(null, array.opt(2)); 15267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(null, array.opt(3)); 15367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.isNull(0)); 15467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.isNull(1)); 15567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.isNull(2)); 15667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.isNull(3)); 15767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("null", array.optString(0)); 15867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("", array.optString(1)); 15967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("", array.optString(2)); 16067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("", array.optString(3)); 16167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 16267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 163adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson /** 164adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson * Our behaviour is questioned by this bug: 165adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson * http://code.google.com/p/android/issues/detail?id=7257 166adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson */ 167adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson public void testParseNullYieldsJSONObjectNull() throws JSONException { 168adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson JSONArray array = new JSONArray("[\"null\",null]"); 169adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson array.put(null); 170adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson assertEquals("null", array.get(0)); 171adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson assertEquals(JSONObject.NULL, array.get(1)); 172adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson try { 173adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson array.get(2); 174adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson fail(); 175adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } catch (JSONException e) { 176adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } 177adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson assertEquals("null", array.getString(0)); 178adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson assertEquals("null", array.getString(1)); 179adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson try { 180adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson array.getString(2); 181adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson fail(); 182adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } catch (JSONException e) { 183adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } 184adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } 185adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson 18667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testNumbers() throws JSONException { 18767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 18867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(Double.MIN_VALUE); 18967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(9223372036854775806L); 19067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(Double.MAX_VALUE); 19167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(-0d); 19267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(4, array.length()); 19367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 19451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson // toString() and getString(int) return different values for -0d 19567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[4.9E-324,9223372036854775806,1.7976931348623157E308,-0]", array.toString()); 19667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 19767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MIN_VALUE, array.get(0)); 19867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9223372036854775806L, array.get(1)); 19967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MAX_VALUE, array.get(2)); 20067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-0d, array.get(3)); 20167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MIN_VALUE, array.getDouble(0)); 20267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9.223372036854776E18, array.getDouble(1)); 20367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MAX_VALUE, array.getDouble(2)); 20467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-0d, array.getDouble(3)); 20567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.getLong(0)); 20667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9223372036854775806L, array.getLong(1)); 20767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Long.MAX_VALUE, array.getLong(2)); 20867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.getLong(3)); 20967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.getInt(0)); 21067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-2, array.getInt(1)); 21167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Integer.MAX_VALUE, array.getInt(2)); 21267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.getInt(3)); 21367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MIN_VALUE, array.opt(0)); 21467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MIN_VALUE, array.optDouble(0)); 21567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.optLong(0, 1L)); 21667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.optInt(0, 1)); 21767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("4.9E-324", array.getString(0)); 21867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("9223372036854775806", array.getString(1)); 21967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("1.7976931348623157E308", array.getString(2)); 22067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("-0.0", array.getString(3)); 22167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 22267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray other = new JSONArray(); 22367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(Double.MIN_VALUE); 22467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(9223372036854775806L); 22567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(Double.MAX_VALUE); 22667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(-0d); 22767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.equals(other)); 22867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(0, 0L); 22967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.equals(other)); 23067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 23167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 23267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testStrings() throws JSONException { 23367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 23467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("true"); 23567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("5.5"); 23667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("9223372036854775806"); 23767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("null"); 23867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("5\"8' tall"); 23967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5, array.length()); 24067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[\"true\",\"5.5\",\"9223372036854775806\",\"null\",\"5\\\"8' tall\"]", 24167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.toString()); 24267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 24367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson // although the documentation doesn't mention it, join() escapes text and wraps 24467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson // strings in quotes 24567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("\"true\" \"5.5\" \"9223372036854775806\" \"null\" \"5\\\"8' tall\"", 24667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.join(" ")); 24767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 24867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("true", array.get(0)); 24967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("null", array.getString(3)); 25067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("5\"8' tall", array.getString(4)); 25167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("true", array.opt(0)); 25267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("5.5", array.optString(1)); 25367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("9223372036854775806", array.optString(2, null)); 25467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("null", array.optString(3, "-1")); 25567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(0)); 25667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(3)); 25767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 25867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.getBoolean(0)); 25967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.optBoolean(0)); 26067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.optBoolean(0, false)); 26167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.optInt(0)); 26267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-2, array.optInt(0, -2)); 26367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 26467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5d, array.getDouble(1)); 26553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5L, array.getLong(1)); 26667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5, array.getInt(1)); 26767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5, array.optInt(1, 3)); 26867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 26967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson // The last digit of the string is a 6 but getLong returns a 7. It's probably parsing as a 27067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson // double and then converting that to a long. This is consistent with JavaScript. 27167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9223372036854775807L, array.getLong(2)); 27267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9.223372036854776E18, array.getDouble(2)); 27367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Integer.MAX_VALUE, array.getInt(2)); 27467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 27567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(3)); 27667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 27767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.getDouble(3); 27867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 27967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 28067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 28167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.NaN, array.optDouble(3)); 28267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-1.0d, array.optDouble(3, -1.0d)); 28367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 28467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 28551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testJoin() throws JSONException { 28651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray array = new JSONArray(); 28751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(null); 28851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null", array.join(" & ")); 28951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put("\""); 29051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\"", array.join(" & ")); 29151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(5); 29251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\" & 5", array.join(" & ")); 29351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(true); 29451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\" & 5 & true", array.join(" & ")); 29551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(new JSONArray(Arrays.asList(true, false))); 29651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\" & 5 & true & [true,false]", array.join(" & ")); 29751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(new JSONObject(Collections.singletonMap("x", 6))); 29851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\" & 5 & true & [true,false] & {\"x\":6}", array.join(" & ")); 29951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 30051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 30151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testJoinWithNull() throws JSONException { 30251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5, 6)); 30351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("5null6", array.join(null)); 30451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 30551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 30651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testJoinWithSpecialCharacters() throws JSONException { 30751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5, 6)); 30851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("5\"6", array.join("\"")); 30951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 31051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 31167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testToJSONObject() throws JSONException { 31267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray keys = new JSONArray(); 31367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put("a"); 31467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put("b"); 31567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 31667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray values = new JSONArray(); 31767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson values.put(5.5d); 31867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson values.put(false); 31967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 32067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONObject object = values.toJSONObject(keys); 32167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5d, object.get("a")); 32267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(false, object.get("b")); 32367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 32467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put(0, "a"); 32567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson values.put(0, 11.0d); 32667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5d, object.get("a")); 32767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 32867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 32967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testToJSONObjectWithNulls() throws JSONException { 33067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray keys = new JSONArray(); 33167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put("a"); 33267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put("b"); 33367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 33467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray values = new JSONArray(); 33567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson values.put(5.5d); 33667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson values.put(null); 33767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 33851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson // null values are stripped! 33967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONObject object = values.toJSONObject(keys); 34067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(1, object.length()); 34167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(object.has("b")); 34267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("{\"a\":5.5}", object.toString()); 34367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 34467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 34553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testToJSONObjectMoreNamesThanValues() throws JSONException { 34653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray keys = new JSONArray(); 34753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson keys.put("a"); 34853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson keys.put("b"); 34953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray values = new JSONArray(); 35053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson values.put(5.5d); 35153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONObject object = values.toJSONObject(keys); 35253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(1, object.length()); 35353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5.5d, object.get("a")); 35453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 35553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson 35653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testToJSONObjectMoreValuesThanNames() throws JSONException { 35753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray keys = new JSONArray(); 35853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson keys.put("a"); 35953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray values = new JSONArray(); 36053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson values.put(5.5d); 36153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson values.put(11.0d); 36253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONObject object = values.toJSONObject(keys); 36353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(1, object.length()); 36453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5.5d, object.get("a")); 36553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 36653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson 36753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testToJSONObjectNullKey() throws JSONException { 36853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray keys = new JSONArray(); 36953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson keys.put(JSONObject.NULL); 37053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray values = new JSONArray(); 37153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson values.put(5.5d); 37253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONObject object = values.toJSONObject(keys); 37353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(1, object.length()); 37453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5.5d, object.get("null")); 37553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 37653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson 37767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testPutUnsupportedNumbers() throws JSONException { 37867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 37967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 38067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 38167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(Double.NaN); 38267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 38367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 38467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 38567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 38667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(0, Double.NEGATIVE_INFINITY); 38767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 38867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 38967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 39067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 39167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(0, Double.POSITIVE_INFINITY); 39267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 39367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 39467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 39567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 39667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 39751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testPutUnsupportedNumbersAsObject() throws JSONException { 39851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray array = new JSONArray(); 39951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(Double.valueOf(Double.NaN)); 40051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(Double.valueOf(Double.NEGATIVE_INFINITY)); 40151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(Double.valueOf(Double.POSITIVE_INFINITY)); 40251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(null, array.toString()); 40351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 40451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 40553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson /** 40653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson * Although JSONArray is usually defensive about which numbers it accepts, 40753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson * it doesn't check inputs in its constructor. 40853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson */ 40967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testCreateWithUnsupportedNumbers() throws JSONException { 41067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5.5, Double.NaN)); 41167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(2, array.length()); 41267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5, array.getDouble(0)); 41367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.NaN, array.getDouble(1)); 41467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 41567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 41667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testToStringWithUnsupportedNumbers() throws JSONException { 41751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson // when the array contains an unsupported number, toString returns null! 41867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5.5, Double.NaN)); 41967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertNull(array.toString()); 42067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 421f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 42253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testListConstructorCopiesContents() throws JSONException { 42353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson List<Object> contents = Arrays.<Object>asList(5); 42453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray array = new JSONArray(contents); 42553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson contents.set(0, 10); 42653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5, array.get(0)); 42753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 42867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 42951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testTokenerConstructor() throws JSONException { 43051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray object = new JSONArray(new JSONTokener("[false]")); 43151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(1, object.length()); 43251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(false, object.get(0)); 43351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 43451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 43551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testTokenerConstructorWrongType() throws JSONException { 43651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 43751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray(new JSONTokener("{\"foo\": false}")); 43851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 43951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (JSONException e) { 44051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 44151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 44251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 44351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testTokenerConstructorNull() throws JSONException { 44451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 44551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray((JSONTokener) null); 44651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 44751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (NullPointerException e) { 44851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 44951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 45051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 45151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testTokenerConstructorParseFail() { 45251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 45351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray(new JSONTokener("[")); 45451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 45551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (JSONException e) { 45651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (StackOverflowError e) { 45751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail("Stack overflowed on input: \"[\""); 45851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 45951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 46051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 46151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testStringConstructor() throws JSONException { 46251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray object = new JSONArray("[false]"); 46351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(1, object.length()); 46451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(false, object.get(0)); 46551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 46651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 46751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testStringConstructorWrongType() throws JSONException { 46851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 46951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray("{\"foo\": false}"); 47051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 47151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (JSONException e) { 47251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 47351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 47451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 47551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testStringConstructorNull() throws JSONException { 47651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 47751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray((String) null); 47851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 47951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (NullPointerException e) { 48051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 48151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 48251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 48351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testStringConstructorParseFail() { 48451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 48551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray("["); 48651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 48751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (JSONException e) { 48851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (StackOverflowError e) { 48951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail("Stack overflowed on input: \"[\""); 49051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 49151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 49251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 49367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testCreate() throws JSONException { 49467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5.5, true)); 49567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(2, array.length()); 49667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5, array.getDouble(0)); 49767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.get(1)); 49867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[5.5,true]", array.toString()); 49967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 50067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 50153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testAccessOutOfBounds() throws JSONException { 50253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray array = new JSONArray(); 50353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.put("foo"); 50453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(null, array.opt(3)); 50553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(null, array.opt(-3)); 50653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals("", array.optString(3)); 50753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals("", array.optString(-3)); 50853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson try { 50953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(3); 51053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson fail(); 51153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } catch (JSONException e) { 51253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 51353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson try { 51453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(-3); 51553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson fail(); 51653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } catch (JSONException e) { 51753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 51853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson try { 51953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.getString(3); 52053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson fail(); 52153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } catch (JSONException e) { 52253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 52353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson try { 52453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.getString(-3); 52553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson fail(); 52653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } catch (JSONException e) { 52753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 52853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 52967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson} 530