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 java.util.Arrays; 20609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughesimport java.util.Collection; 2151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilsonimport java.util.Collections; 227365de1056414750d0a7d1fdd26025fd247f0d04Jesse Wilsonimport java.util.List; 237365de1056414750d0a7d1fdd26025fd247f0d04Jesse Wilsonimport junit.framework.TestCase; 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 125661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson // http://code.google.com/p/android/issues/detail?id=16411 126661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson public void testCoerceStringToBoolean() throws JSONException { 127661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson JSONArray array = new JSONArray(); 128661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson array.put("maybe"); 129661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson try { 130661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson array.getBoolean(0); 131661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson fail(); 132661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson } catch (JSONException expected) { 133661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson } 134661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson assertEquals(false, array.optBoolean(0)); 135661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson assertEquals(true, array.optBoolean(0, true)); 136661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson } 137661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson 13867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testNulls() throws JSONException { 13967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 140609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes array.put(3, (Collection) null); 14167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(0, JSONObject.NULL); 14267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(4, array.length()); 14367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[null,null,null,null]", array.toString()); 14467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 14551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson // there's 2 ways to represent null; each behaves differently! 14667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(JSONObject.NULL, array.get(0)); 14767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 14853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(1); 14967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 15067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 15167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 15267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 15353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(2); 15467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 15567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 15667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 15767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 15853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(3); 15967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 16067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 16167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 16267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(JSONObject.NULL, array.opt(0)); 16367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(null, array.opt(1)); 16467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(null, array.opt(2)); 16567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(null, array.opt(3)); 16667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.isNull(0)); 16767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.isNull(1)); 16867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.isNull(2)); 16967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.isNull(3)); 17067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("null", array.optString(0)); 17167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("", array.optString(1)); 17267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("", array.optString(2)); 17367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("", array.optString(3)); 17467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 17567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 176adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson /** 177adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson * Our behaviour is questioned by this bug: 178adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson * http://code.google.com/p/android/issues/detail?id=7257 179adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson */ 180adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson public void testParseNullYieldsJSONObjectNull() throws JSONException { 181adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson JSONArray array = new JSONArray("[\"null\",null]"); 182609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes array.put((Collection) null); 183adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson assertEquals("null", array.get(0)); 184adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson assertEquals(JSONObject.NULL, array.get(1)); 185adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson try { 186adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson array.get(2); 187adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson fail(); 188adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } catch (JSONException e) { 189adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } 190adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson assertEquals("null", array.getString(0)); 191adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson assertEquals("null", array.getString(1)); 192adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson try { 193adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson array.getString(2); 194adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson fail(); 195adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } catch (JSONException e) { 196adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } 197adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson } 198adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson 19967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testNumbers() throws JSONException { 20067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 20167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(Double.MIN_VALUE); 20267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(9223372036854775806L); 20367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(Double.MAX_VALUE); 20467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(-0d); 20567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(4, array.length()); 20667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 20751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson // toString() and getString(int) return different values for -0d 20867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[4.9E-324,9223372036854775806,1.7976931348623157E308,-0]", array.toString()); 20967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 21067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MIN_VALUE, array.get(0)); 21167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9223372036854775806L, array.get(1)); 21267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MAX_VALUE, array.get(2)); 21367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-0d, array.get(3)); 21467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MIN_VALUE, array.getDouble(0)); 21567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9.223372036854776E18, array.getDouble(1)); 21667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MAX_VALUE, array.getDouble(2)); 21767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-0d, array.getDouble(3)); 21867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.getLong(0)); 21967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9223372036854775806L, array.getLong(1)); 22067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Long.MAX_VALUE, array.getLong(2)); 22167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.getLong(3)); 22267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.getInt(0)); 22367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-2, array.getInt(1)); 22467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Integer.MAX_VALUE, array.getInt(2)); 22567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.getInt(3)); 22667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MIN_VALUE, array.opt(0)); 22767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.MIN_VALUE, array.optDouble(0)); 22867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.optLong(0, 1L)); 22967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.optInt(0, 1)); 23067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("4.9E-324", array.getString(0)); 23167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("9223372036854775806", array.getString(1)); 23267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("1.7976931348623157E308", array.getString(2)); 23367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("-0.0", array.getString(3)); 23467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 23567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray other = new JSONArray(); 23667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(Double.MIN_VALUE); 23767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(9223372036854775806L); 23867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(Double.MAX_VALUE); 23967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(-0d); 24067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertTrue(array.equals(other)); 24167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson other.put(0, 0L); 24267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.equals(other)); 24367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 24467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 24567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testStrings() throws JSONException { 24667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 24767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("true"); 24867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("5.5"); 24967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("9223372036854775806"); 25067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("null"); 25167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put("5\"8' tall"); 25267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5, array.length()); 25367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[\"true\",\"5.5\",\"9223372036854775806\",\"null\",\"5\\\"8' tall\"]", 25467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.toString()); 25567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 25667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson // although the documentation doesn't mention it, join() escapes text and wraps 25767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson // strings in quotes 25867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("\"true\" \"5.5\" \"9223372036854775806\" \"null\" \"5\\\"8' tall\"", 25967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.join(" ")); 26067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 26167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("true", array.get(0)); 26267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("null", array.getString(3)); 26367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("5\"8' tall", array.getString(4)); 26467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("true", array.opt(0)); 26567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("5.5", array.optString(1)); 26667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("9223372036854775806", array.optString(2, null)); 26767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("null", array.optString(3, "-1")); 26867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(0)); 26967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(3)); 27067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 27167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.getBoolean(0)); 27267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.optBoolean(0)); 27367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.optBoolean(0, false)); 27467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(0, array.optInt(0)); 27567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-2, array.optInt(0, -2)); 27667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 27767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5d, array.getDouble(1)); 27853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5L, array.getLong(1)); 27967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5, array.getInt(1)); 28067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5, array.optInt(1, 3)); 28167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 28267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson // The last digit of the string is a 6 but getLong returns a 7. It's probably parsing as a 28367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson // double and then converting that to a long. This is consistent with JavaScript. 28467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9223372036854775807L, array.getLong(2)); 28567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(9.223372036854776E18, array.getDouble(2)); 28667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Integer.MAX_VALUE, array.getInt(2)); 28767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 28867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(array.isNull(3)); 28967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 29067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.getDouble(3); 29167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 29267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 29367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 29467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.NaN, array.optDouble(3)); 29567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(-1.0d, array.optDouble(3, -1.0d)); 29667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 29767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 29851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testJoin() throws JSONException { 29951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray array = new JSONArray(); 300609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes array.put((Collection) null); 30151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null", array.join(" & ")); 30251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put("\""); 30351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\"", array.join(" & ")); 30451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(5); 30551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\" & 5", array.join(" & ")); 30651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(true); 30751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\" & 5 & true", array.join(" & ")); 30851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(new JSONArray(Arrays.asList(true, false))); 30951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\" & 5 & true & [true,false]", array.join(" & ")); 31051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(new JSONObject(Collections.singletonMap("x", 6))); 31151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("null & \"\\\"\" & 5 & true & [true,false] & {\"x\":6}", array.join(" & ")); 31251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 31351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 31451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testJoinWithNull() throws JSONException { 31551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5, 6)); 31651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("5null6", array.join(null)); 31751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 31851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 31951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testJoinWithSpecialCharacters() throws JSONException { 32051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5, 6)); 32151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals("5\"6", array.join("\"")); 32251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 32351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 32467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testToJSONObject() throws JSONException { 32567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray keys = new JSONArray(); 32667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put("a"); 32767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put("b"); 32867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 32967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray values = new JSONArray(); 33067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson values.put(5.5d); 33167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson values.put(false); 33267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 33367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONObject object = values.toJSONObject(keys); 33467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5d, object.get("a")); 33567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(false, object.get("b")); 33667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 33767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put(0, "a"); 33867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson values.put(0, 11.0d); 33967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5d, object.get("a")); 34067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 34167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 34267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testToJSONObjectWithNulls() throws JSONException { 34367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray keys = new JSONArray(); 34467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put("a"); 34567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson keys.put("b"); 34667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 34767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray values = new JSONArray(); 34867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson values.put(5.5d); 349609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes values.put((Collection) null); 35067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 35151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson // null values are stripped! 35267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONObject object = values.toJSONObject(keys); 35367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(1, object.length()); 35467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertFalse(object.has("b")); 35567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("{\"a\":5.5}", object.toString()); 35667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 35767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 35853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testToJSONObjectMoreNamesThanValues() throws JSONException { 35953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray keys = new JSONArray(); 36053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson keys.put("a"); 36153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson keys.put("b"); 36253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray values = new JSONArray(); 36353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson values.put(5.5d); 36453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONObject object = values.toJSONObject(keys); 36553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(1, object.length()); 36653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5.5d, object.get("a")); 36753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 36853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson 36953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testToJSONObjectMoreValuesThanNames() throws JSONException { 37053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray keys = new JSONArray(); 37153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson keys.put("a"); 37253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray values = new JSONArray(); 37353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson values.put(5.5d); 37453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson values.put(11.0d); 37553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONObject object = values.toJSONObject(keys); 37653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(1, object.length()); 37753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5.5d, object.get("a")); 37853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 37953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson 38053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testToJSONObjectNullKey() throws JSONException { 38153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray keys = new JSONArray(); 38253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson keys.put(JSONObject.NULL); 38353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray values = new JSONArray(); 38453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson values.put(5.5d); 38553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONObject object = values.toJSONObject(keys); 38653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(1, object.length()); 38753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5.5d, object.get("null")); 38853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 38953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson 39067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testPutUnsupportedNumbers() throws JSONException { 39167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(); 39267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 39367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 39467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(Double.NaN); 39567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 39667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 39767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 39867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 39967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(0, Double.NEGATIVE_INFINITY); 40067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 40167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 40267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 40367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson try { 40467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson array.put(0, Double.POSITIVE_INFINITY); 40567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson fail(); 40667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } catch (JSONException e) { 40767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 40867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 40967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 41051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testPutUnsupportedNumbersAsObject() throws JSONException { 41151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray array = new JSONArray(); 41251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(Double.valueOf(Double.NaN)); 41351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(Double.valueOf(Double.NEGATIVE_INFINITY)); 41451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson array.put(Double.valueOf(Double.POSITIVE_INFINITY)); 41551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(null, array.toString()); 41651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 41751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 41853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson /** 41953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson * Although JSONArray is usually defensive about which numbers it accepts, 42053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson * it doesn't check inputs in its constructor. 42153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson */ 42267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testCreateWithUnsupportedNumbers() throws JSONException { 42367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5.5, Double.NaN)); 42467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(2, array.length()); 42567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5, array.getDouble(0)); 42667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(Double.NaN, array.getDouble(1)); 42767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 42867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 42967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testToStringWithUnsupportedNumbers() throws JSONException { 43051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson // when the array contains an unsupported number, toString returns null! 43167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5.5, Double.NaN)); 43267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertNull(array.toString()); 43367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 434f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 43553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testListConstructorCopiesContents() throws JSONException { 43653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson List<Object> contents = Arrays.<Object>asList(5); 43753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray array = new JSONArray(contents); 43853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson contents.set(0, 10); 43953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(5, array.get(0)); 44053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 44167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 44251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testTokenerConstructor() throws JSONException { 44351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray object = new JSONArray(new JSONTokener("[false]")); 44451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(1, object.length()); 44551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(false, object.get(0)); 44651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 44751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 44851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testTokenerConstructorWrongType() throws JSONException { 44951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 45051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray(new JSONTokener("{\"foo\": false}")); 45151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 45251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (JSONException e) { 45351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 45451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 45551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 45651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testTokenerConstructorNull() throws JSONException { 45751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 45851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray((JSONTokener) null); 45951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 46051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (NullPointerException e) { 46151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 46251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 46351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 46451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testTokenerConstructorParseFail() { 46551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 46651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray(new JSONTokener("[")); 46751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 46851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (JSONException e) { 46951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (StackOverflowError e) { 47051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail("Stack overflowed on input: \"[\""); 47151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 47251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 47351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 47451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testStringConstructor() throws JSONException { 47551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson JSONArray object = new JSONArray("[false]"); 47651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(1, object.length()); 47751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson assertEquals(false, object.get(0)); 47851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 47951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 48051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testStringConstructorWrongType() throws JSONException { 48151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 48251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray("{\"foo\": false}"); 48351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 48451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (JSONException e) { 48551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 48651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 48751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 48851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testStringConstructorNull() throws JSONException { 48951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 49051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray((String) null); 49151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 49251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (NullPointerException e) { 49351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 49451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 49551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 49651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson public void testStringConstructorParseFail() { 49751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson try { 49851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson new JSONArray("["); 49951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail(); 50051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (JSONException e) { 50151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } catch (StackOverflowError e) { 50251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson fail("Stack overflowed on input: \"[\""); 50351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 50451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson } 50551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson 50667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson public void testCreate() throws JSONException { 50767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson JSONArray array = new JSONArray(Arrays.asList(5.5, true)); 50867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(2, array.length()); 50967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(5.5, array.getDouble(0)); 51067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals(true, array.get(1)); 51167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson assertEquals("[5.5,true]", array.toString()); 51267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson } 51367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson 51453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson public void testAccessOutOfBounds() throws JSONException { 51553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson JSONArray array = new JSONArray(); 51653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.put("foo"); 51753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(null, array.opt(3)); 51853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals(null, array.opt(-3)); 51953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals("", array.optString(3)); 52053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson assertEquals("", array.optString(-3)); 52153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson try { 52253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(3); 52353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson fail(); 52453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } catch (JSONException e) { 52553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 52653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson try { 52753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.get(-3); 52853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson fail(); 52953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } catch (JSONException e) { 53053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 53153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson try { 53253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.getString(3); 53353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson fail(); 53453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } catch (JSONException e) { 53553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 53653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson try { 53753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson array.getString(-3); 53853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson fail(); 53953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } catch (JSONException e) { 54053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 54153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson } 542609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes 543609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes public void test_remove() throws Exception { 544609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes JSONArray a = new JSONArray(); 545609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes assertEquals(null, a.remove(-1)); 546609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes assertEquals(null, a.remove(0)); 547609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes 548609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes a.put("hello"); 549609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes assertEquals(null, a.remove(-1)); 550609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes assertEquals(null, a.remove(1)); 551609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes assertEquals("hello", a.remove(0)); 552609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes assertEquals(null, a.remove(0)); 553609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes } 554c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes 555c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes enum MyEnum { A, B, C; } 556c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes 557c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes // https://code.google.com/p/android/issues/detail?id=62539 558c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes public void testEnums() throws Exception { 559c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes // This works because it's in java.* and any class in there falls back to toString. 560c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes JSONArray a1 = new JSONArray(java.lang.annotation.RetentionPolicy.values()); 561c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes assertEquals("[\"SOURCE\",\"CLASS\",\"RUNTIME\"]", a1.toString()); 562c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes 563c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes // This doesn't because it's not. 564c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes JSONArray a2 = new JSONArray(MyEnum.values()); 565d62e296abe060ff7557e731fe70f240dbaa142e8Narayan Kamath assertEquals("[null,null,null]", a2.toString()); 566c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes } 56767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson} 568