http://git-wip-us.apache.org/repos/asf/geode/blob/eac0bb8c/geode-json/src/test/java/org/json/ParsingTest.java ---------------------------------------------------------------------- diff --git a/geode-json/src/test/java/org/json/ParsingTest.java b/geode-json/src/test/java/org/json/ParsingTest.java index 4a0837a..a49aba7 100755 --- a/geode-json/src/test/java/org/json/ParsingTest.java +++ b/geode-json/src/test/java/org/json/ParsingTest.java @@ -1,17 +1,15 @@ /* * Copyright (C) 2010 The Android Open Source Project * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. */ package org.json; @@ -25,270 +23,269 @@ import static org.junit.Assert.fail; public class ParsingTest { - @Test - public void testParsingNoObjects() { - try { - new JSONTokener("").nextValue(); - fail(); - } catch (JSONException ignored) { - } + @Test + public void testParsingNoObjects() { + try { + new JSONTokener("").nextValue(); + fail(); + } catch (JSONException ignored) { } - - @Test - public void testParsingLiterals() throws JSONException { - assertParsed(Boolean.TRUE, "true"); - assertParsed(Boolean.FALSE, "false"); - assertParsed(JSONObject.NULL, "null"); - assertParsed(JSONObject.NULL, "NULL"); - assertParsed(Boolean.FALSE, "False"); - assertParsed(Boolean.TRUE, "truE"); - } - - @Test - public void testParsingQuotedStrings() throws JSONException { - assertParsed("abc", "\"abc\""); - assertParsed("123", "\"123\""); - assertParsed("foo\nbar", "\"foo\\nbar\""); - assertParsed("foo bar", "\"foo\\u0020bar\""); - assertParsed("\"{}[]/\\:,=;#", "\"\\\"{}[]/\\\\:,=;#\""); - } - - @Test - public void testParsingSingleQuotedStrings() throws JSONException { - assertParsed("abc", "'abc'"); - assertParsed("123", "'123'"); - assertParsed("foo\nbar", "'foo\\nbar'"); - assertParsed("foo bar", "'foo\\u0020bar'"); - assertParsed("\"{}[]/\\:,=;#", "'\\\"{}[]/\\\\:,=;#'"); - } - - @Test - public void testParsingUnquotedStrings() throws JSONException { - assertParsed("abc", "abc"); - assertParsed("123abc", "123abc"); - assertParsed("123e0x", "123e0x"); - assertParsed("123e", "123e"); - assertParsed("123ee21", "123ee21"); - assertParsed("0xFFFFFFFFFFFFFFFFF", "0xFFFFFFFFFFFFFFFFF"); - } - - /** - * Unfortunately the original implementation attempts to figure out what - * Java number type best suits an input value. - */ - @Test - public void testParsingNumbersThatAreBestRepresentedAsLongs() throws JSONException { - assertParsed(9223372036854775807L, "9223372036854775807"); - assertParsed(9223372036854775806L, "9223372036854775806"); - assertParsed(-9223372036854775808L, "-9223372036854775808"); - assertParsed(-9223372036854775807L, "-9223372036854775807"); - } - - @Test - public void testParsingNumbersThatAreBestRepresentedAsIntegers() throws JSONException { - assertParsed(0, "0"); - assertParsed(5, "5"); - assertParsed(-2147483648, "-2147483648"); - assertParsed(2147483647, "2147483647"); - } - - @Test - public void testParsingNegativeZero() throws JSONException { - assertParsed(0, "-0"); - } - - @Test - public void testParsingIntegersWithAdditionalPrecisionYieldDoubles() throws JSONException { - assertParsed(1d, "1.00"); - assertParsed(1d, "1.0"); - assertParsed(0d, "0.0"); - assertParsed(-0d, "-0.0"); - } - - @Test - public void testParsingNumbersThatAreBestRepresentedAsDoubles() throws JSONException { - assertParsed(9.223372036854776E18, "9223372036854775808"); - assertParsed(-9.223372036854776E18, "-9223372036854775809"); - assertParsed(1.7976931348623157E308, "1.7976931348623157e308"); - assertParsed(2.2250738585072014E-308, "2.2250738585072014E-308"); - assertParsed(4.9E-324, "4.9E-324"); - assertParsed(4.9E-324, "4.9e-324"); - } - - @Test - public void testParsingOctalNumbers() throws JSONException { - assertParsed(5, "05"); - assertParsed(8, "010"); - assertParsed(1046, "02026"); - } - - @Test - public void testParsingHexNumbers() throws JSONException { - assertParsed(5, "0x5"); - assertParsed(16, "0x10"); - assertParsed(8230, "0x2026"); - assertParsed(180150010, "0xABCDEFA"); - assertParsed(2077093803, "0x7BCDEFAB"); + } + + @Test + public void testParsingLiterals() throws JSONException { + assertParsed(Boolean.TRUE, "true"); + assertParsed(Boolean.FALSE, "false"); + assertParsed(JSONObject.NULL, "null"); + assertParsed(JSONObject.NULL, "NULL"); + assertParsed(Boolean.FALSE, "False"); + assertParsed(Boolean.TRUE, "truE"); + } + + @Test + public void testParsingQuotedStrings() throws JSONException { + assertParsed("abc", "\"abc\""); + assertParsed("123", "\"123\""); + assertParsed("foo\nbar", "\"foo\\nbar\""); + assertParsed("foo bar", "\"foo\\u0020bar\""); + assertParsed("\"{}[]/\\:,=;#", "\"\\\"{}[]/\\\\:,=;#\""); + } + + @Test + public void testParsingSingleQuotedStrings() throws JSONException { + assertParsed("abc", "'abc'"); + assertParsed("123", "'123'"); + assertParsed("foo\nbar", "'foo\\nbar'"); + assertParsed("foo bar", "'foo\\u0020bar'"); + assertParsed("\"{}[]/\\:,=;#", "'\\\"{}[]/\\\\:,=;#'"); + } + + @Test + public void testParsingUnquotedStrings() throws JSONException { + assertParsed("abc", "abc"); + assertParsed("123abc", "123abc"); + assertParsed("123e0x", "123e0x"); + assertParsed("123e", "123e"); + assertParsed("123ee21", "123ee21"); + assertParsed("0xFFFFFFFFFFFFFFFFF", "0xFFFFFFFFFFFFFFFFF"); + } + + /** + * Unfortunately the original implementation attempts to figure out what Java number type best + * suits an input value. + */ + @Test + public void testParsingNumbersThatAreBestRepresentedAsLongs() throws JSONException { + assertParsed(9223372036854775807L, "9223372036854775807"); + assertParsed(9223372036854775806L, "9223372036854775806"); + assertParsed(-9223372036854775808L, "-9223372036854775808"); + assertParsed(-9223372036854775807L, "-9223372036854775807"); + } + + @Test + public void testParsingNumbersThatAreBestRepresentedAsIntegers() throws JSONException { + assertParsed(0, "0"); + assertParsed(5, "5"); + assertParsed(-2147483648, "-2147483648"); + assertParsed(2147483647, "2147483647"); + } + + @Test + public void testParsingNegativeZero() throws JSONException { + assertParsed(0, "-0"); + } + + @Test + public void testParsingIntegersWithAdditionalPrecisionYieldDoubles() throws JSONException { + assertParsed(1d, "1.00"); + assertParsed(1d, "1.0"); + assertParsed(0d, "0.0"); + assertParsed(-0d, "-0.0"); + } + + @Test + public void testParsingNumbersThatAreBestRepresentedAsDoubles() throws JSONException { + assertParsed(9.223372036854776E18, "9223372036854775808"); + assertParsed(-9.223372036854776E18, "-9223372036854775809"); + assertParsed(1.7976931348623157E308, "1.7976931348623157e308"); + assertParsed(2.2250738585072014E-308, "2.2250738585072014E-308"); + assertParsed(4.9E-324, "4.9E-324"); + assertParsed(4.9E-324, "4.9e-324"); + } + + @Test + public void testParsingOctalNumbers() throws JSONException { + assertParsed(5, "05"); + assertParsed(8, "010"); + assertParsed(1046, "02026"); + } + + @Test + public void testParsingHexNumbers() throws JSONException { + assertParsed(5, "0x5"); + assertParsed(16, "0x10"); + assertParsed(8230, "0x2026"); + assertParsed(180150010, "0xABCDEFA"); + assertParsed(2077093803, "0x7BCDEFAB"); + } + + @Test + public void testParsingLargeHexValues() throws JSONException { + assertParsed(Integer.MAX_VALUE, "0x7FFFFFFF"); + String message = "Hex values are parsed as Strings if their signed " + + "value is greater than Integer.MAX_VALUE."; + assertParsed(message, 0x80000000L, "0x80000000"); + } + + @Test + public void test64BitHexValues() throws JSONException { + // note that this is different from the same test in the original Android + // this is due to the fact that Long.parseLong doesn't correctly handle + // the value -1 expressed as unsigned hex if you use the normal JDK. Presumably + // the Android equivalent program does this better. + assertParsed("Large hex longs shouldn't yield ints or strings", 0xFFFFFFFFFFFFFFFL, + "0xFFFFFFFFFFFFFFF"); + } + + @Test + public void testParsingWithCommentsAndWhitespace() throws JSONException { + assertParsed("baz", " // foo bar \n baz"); + assertParsed("baz", " // foo bar \r baz"); + assertParsed("baz", " // foo bar \r\n baz"); + assertParsed("baz", " # foo bar \n baz"); + assertParsed("baz", " # foo bar \r baz"); + assertParsed("baz", " # foo bar \r\n baz"); + assertParsed(5, " /* foo bar \n baz */ 5"); + assertParsed(5, " /* foo bar \n baz */ 5 // quux"); + assertParsed(5, " 5 "); + assertParsed(5, " 5 \r\n\t "); + assertParsed(5, "\r\n\t 5 "); + } + + @Test + public void testParsingArrays() throws JSONException { + assertParsed(array(), "[]"); + assertParsed(array(5, 6, true), "[5,6,true]"); + assertParsed(array(5, 6, array()), "[5,6,[]]"); + assertParsed(array(5, 6, 7), "[5;6;7]"); + assertParsed(array(5, 6, 7), "[5 , 6 \t; \r\n 7\n]"); + assertParsed(array(5, 6, 7, null), "[5,6,7,]"); + assertParsed(array(null, null), "[,]"); + assertParsed(array(5, null, null, null, 5), "[5,,,,5]"); + assertParsed(array(null, 5), "[,5]"); + assertParsed(array(null, null, null), "[,,]"); + assertParsed(array(null, null, null, 5), "[,,,5]"); + } + + @Test + public void testParsingObjects() throws JSONException { + assertParsed(object("foo", 5), "{\"foo\": 5}"); + assertParsed(object("foo", 5), "{foo: 5}"); + assertParsed(object("foo", 5, "bar", "baz"), "{\"foo\": 5, \"bar\": \"baz\"}"); + assertParsed(object("foo", 5, "bar", "baz"), "{\"foo\": 5; \"bar\": \"baz\"}"); + assertParsed(object("foo", 5, "bar", "baz"), "{\"foo\"= 5; \"bar\"= \"baz\"}"); + assertParsed(object("foo", 5, "bar", "baz"), "{\"foo\"=> 5; \"bar\"=> \"baz\"}"); + assertParsed(object("foo", object(), "bar", array()), "{\"foo\"=> {}; \"bar\"=> []}"); + assertParsed(object("foo", object("foo", array(5, 6))), "{\"foo\": {\"foo\": [5, 6]}}"); + assertParsed(object("foo", object("foo", array(5, 6))), "{\"foo\":\n\t{\t \"foo\":[5,\r6]}}"); + } + + @Test + public void testSyntaxProblemUnterminatedObject() { + assertParseFail("{"); + assertParseFail("{\"foo\""); + assertParseFail("{\"foo\":"); + assertParseFail("{\"foo\":bar"); + assertParseFail("{\"foo\":bar,"); + assertParseFail("{\"foo\":bar,\"baz\""); + assertParseFail("{\"foo\":bar,\"baz\":"); + assertParseFail("{\"foo\":bar,\"baz\":true"); + assertParseFail("{\"foo\":bar,\"baz\":true,"); + } + + @Test + public void testSyntaxProblemEmptyString() { + assertParseFail(""); + } + + @Test + public void testSyntaxProblemUnterminatedArray() { + assertParseFail("["); + assertParseFail("[,"); + assertParseFail("[,,"); + assertParseFail("[true"); + assertParseFail("[true,"); + assertParseFail("[true,,"); + } + + @Test + public void testSyntaxProblemMalformedObject() { + assertParseFail("{:}"); + assertParseFail("{\"key\":}"); + assertParseFail("{:true}"); + assertParseFail("{\"key\":true:}"); + assertParseFail("{null:true}"); + assertParseFail("{true:true}"); + assertParseFail("{0xFF:true}"); + } + + private void assertParseFail(String malformedJson) { + try { + new JSONTokener(malformedJson).nextValue(); + fail("Successfully parsed: \"" + malformedJson + "\""); + } catch (JSONException ignored) { + } catch (StackOverflowError e) { + fail("Stack overflowed on input: \"" + malformedJson + "\""); } + } - @Test - public void testParsingLargeHexValues() throws JSONException { - assertParsed(Integer.MAX_VALUE, "0x7FFFFFFF"); - String message = "Hex values are parsed as Strings if their signed " + - "value is greater than Integer.MAX_VALUE."; - assertParsed(message, 0x80000000L, "0x80000000"); - } - - @Test - public void test64BitHexValues() throws JSONException { - // note that this is different from the same test in the original Android - // this is due to the fact that Long.parseLong doesn't correctly handle - // the value -1 expressed as unsigned hex if you use the normal JDK. Presumably - // the Android equivalent program does this better. - assertParsed("Large hex longs shouldn't yield ints or strings", - 0xFFFFFFFFFFFFFFFL, "0xFFFFFFFFFFFFFFF"); - } + private JSONArray array(Object... elements) { + return new JSONArray(Arrays.asList(elements)); + } - @Test - public void testParsingWithCommentsAndWhitespace() throws JSONException { - assertParsed("baz", " // foo bar \n baz"); - assertParsed("baz", " // foo bar \r baz"); - assertParsed("baz", " // foo bar \r\n baz"); - assertParsed("baz", " # foo bar \n baz"); - assertParsed("baz", " # foo bar \r baz"); - assertParsed("baz", " # foo bar \r\n baz"); - assertParsed(5, " /* foo bar \n baz */ 5"); - assertParsed(5, " /* foo bar \n baz */ 5 // quux"); - assertParsed(5, " 5 "); - assertParsed(5, " 5 \r\n\t "); - assertParsed(5, "\r\n\t 5 "); + private JSONObject object(Object... keyValuePairs) throws JSONException { + JSONObject result = new JSONObject(); + for (int i = 0; i < keyValuePairs.length; i += 2) { + result.put((String) keyValuePairs[i], keyValuePairs[i + 1]); } - - @Test - public void testParsingArrays() throws JSONException { - assertParsed(array(), "[]"); - assertParsed(array(5, 6, true), "[5,6,true]"); - assertParsed(array(5, 6, array()), "[5,6,[]]"); - assertParsed(array(5, 6, 7), "[5;6;7]"); - assertParsed(array(5, 6, 7), "[5 , 6 \t; \r\n 7\n]"); - assertParsed(array(5, 6, 7, null), "[5,6,7,]"); - assertParsed(array(null, null), "[,]"); - assertParsed(array(5, null, null, null, 5), "[5,,,,5]"); - assertParsed(array(null, 5), "[,5]"); - assertParsed(array(null, null, null), "[,,]"); - assertParsed(array(null, null, null, 5), "[,,,5]"); - } - - @Test - public void testParsingObjects() throws JSONException { - assertParsed(object("foo", 5), "{\"foo\": 5}"); - assertParsed(object("foo", 5), "{foo: 5}"); - assertParsed(object("foo", 5, "bar", "baz"), "{\"foo\": 5, \"bar\": \"baz\"}"); - assertParsed(object("foo", 5, "bar", "baz"), "{\"foo\": 5; \"bar\": \"baz\"}"); - assertParsed(object("foo", 5, "bar", "baz"), "{\"foo\"= 5; \"bar\"= \"baz\"}"); - assertParsed(object("foo", 5, "bar", "baz"), "{\"foo\"=> 5; \"bar\"=> \"baz\"}"); - assertParsed(object("foo", object(), "bar", array()), "{\"foo\"=> {}; \"bar\"=> []}"); - assertParsed(object("foo", object("foo", array(5, 6))), "{\"foo\": {\"foo\": [5, 6]}}"); - assertParsed(object("foo", object("foo", array(5, 6))), "{\"foo\":\n\t{\t \"foo\":[5,\r6]}}"); - } - - @Test - public void testSyntaxProblemUnterminatedObject() { - assertParseFail("{"); - assertParseFail("{\"foo\""); - assertParseFail("{\"foo\":"); - assertParseFail("{\"foo\":bar"); - assertParseFail("{\"foo\":bar,"); - assertParseFail("{\"foo\":bar,\"baz\""); - assertParseFail("{\"foo\":bar,\"baz\":"); - assertParseFail("{\"foo\":bar,\"baz\":true"); - assertParseFail("{\"foo\":bar,\"baz\":true,"); - } - - @Test - public void testSyntaxProblemEmptyString() { - assertParseFail(""); - } - - @Test - public void testSyntaxProblemUnterminatedArray() { - assertParseFail("["); - assertParseFail("[,"); - assertParseFail("[,,"); - assertParseFail("[true"); - assertParseFail("[true,"); - assertParseFail("[true,,"); - } - - @Test - public void testSyntaxProblemMalformedObject() { - assertParseFail("{:}"); - assertParseFail("{\"key\":}"); - assertParseFail("{:true}"); - assertParseFail("{\"key\":true:}"); - assertParseFail("{null:true}"); - assertParseFail("{true:true}"); - assertParseFail("{0xFF:true}"); - } - - private void assertParseFail(String malformedJson) { - try { - new JSONTokener(malformedJson).nextValue(); - fail("Successfully parsed: \"" + malformedJson + "\""); - } catch (JSONException ignored) { - } catch (StackOverflowError e) { - fail("Stack overflowed on input: \"" + malformedJson + "\""); - } - } - - private JSONArray array(Object... elements) { - return new JSONArray(Arrays.asList(elements)); - } - - private JSONObject object(Object... keyValuePairs) throws JSONException { - JSONObject result = new JSONObject(); - for (int i = 0; i < keyValuePairs.length; i += 2) { - result.put((String) keyValuePairs[i], keyValuePairs[i + 1]); - } - return result; - } - - private void assertParsed(String message, Object expected, String json) throws JSONException { - Object actual = new JSONTokener(json).nextValue(); - actual = canonicalize(actual); - expected = canonicalize(expected); - assertEquals("For input \"" + json + "\" " + message, expected, actual); - } - - private void assertParsed(Object expected, String json) throws JSONException { - assertParsed("", expected, json); - } - - /** - * Since they don't implement equals or hashCode properly, this recursively - * replaces JSONObjects with an equivalent HashMap, and JSONArrays with the - * equivalent ArrayList. - */ - private Object canonicalize(Object input) throws JSONException { - if (input instanceof JSONArray) { - JSONArray array = (JSONArray) input; - List<Object> result = new ArrayList<Object>(); - for (int i = 0; i < array.length(); i++) { - result.add(canonicalize(array.opt(i))); - } - return result; - } else if (input instanceof JSONObject) { - JSONObject object = (JSONObject) input; - Map<String, Object> result = new HashMap<String, Object>(); - for (Iterator<?> i = object.keys(); i.hasNext(); ) { - String key = (String) i.next(); - result.put(key, canonicalize(object.get(key))); - } - return result; - } else if (input == null || input.equals(JSONObject.NULL)) { - return JSONObject.NULL; - } else { - return input; - } + return result; + } + + private void assertParsed(String message, Object expected, String json) throws JSONException { + Object actual = new JSONTokener(json).nextValue(); + actual = canonicalize(actual); + expected = canonicalize(expected); + assertEquals("For input \"" + json + "\" " + message, expected, actual); + } + + private void assertParsed(Object expected, String json) throws JSONException { + assertParsed("", expected, json); + } + + /** + * Since they don't implement equals or hashCode properly, this recursively replaces JSONObjects + * with an equivalent HashMap, and JSONArrays with the equivalent ArrayList. + */ + private Object canonicalize(Object input) throws JSONException { + if (input instanceof JSONArray) { + JSONArray array = (JSONArray) input; + List<Object> result = new ArrayList<Object>(); + for (int i = 0; i < array.length(); i++) { + result.add(canonicalize(array.opt(i))); + } + return result; + } else if (input instanceof JSONObject) { + JSONObject object = (JSONObject) input; + Map<String, Object> result = new HashMap<String, Object>(); + for (Iterator<?> i = object.keys(); i.hasNext();) { + String key = (String) i.next(); + result.put(key, canonicalize(object.get(key))); + } + return result; + } else if (input == null || input.equals(JSONObject.NULL)) { + return JSONObject.NULL; + } else { + return input; } + } }
http://git-wip-us.apache.org/repos/asf/geode/blob/eac0bb8c/geode-json/src/test/java/org/json/SelfUseTest.java ---------------------------------------------------------------------- diff --git a/geode-json/src/test/java/org/json/SelfUseTest.java b/geode-json/src/test/java/org/json/SelfUseTest.java index 0b9fb2c..78ee678 100755 --- a/geode-json/src/test/java/org/json/SelfUseTest.java +++ b/geode-json/src/test/java/org/json/SelfUseTest.java @@ -1,17 +1,15 @@ /* * Copyright (C) 2010 The Android Open Source Project * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. */ package org.json; @@ -21,256 +19,255 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; /** - * These tests checks self use calls. For the most part we doesn't attempt to - * cover self-use, except in those cases where our clean room implementation - * does it. + * These tests checks self use calls. For the most part we doesn't attempt to cover self-use, except + * in those cases where our clean room implementation does it. * <p> - * <p>This black box test was written without inspecting the non-free org.json - * sourcecode. + * <p> + * This black box test was written without inspecting the non-free org.json sourcecode. */ public class SelfUseTest { - private int objectPutCalls = 0; - private int objectGetCalls = 0; - private int objectOptCalls = 0; - private int objectOptTypeCalls = 0; - private int arrayPutCalls = 0; - private int arrayGetCalls = 0; - private int arrayOptCalls = 0; - private int arrayOptTypeCalls = 0; - private int tokenerNextCalls = 0; - private int tokenerNextValueCalls = 0; - - private final JSONObject object = new JSONObject() { - @Override - public JSONObject put(String name, Object value) throws JSONException { - objectPutCalls++; - return super.put(name, value); - } - - @Override - public Object get(String name) throws JSONException { - objectGetCalls++; - return super.get(name); - } - - @Override - public Object opt(String name) { - objectOptCalls++; - return super.opt(name); - } - - @Override - public boolean optBoolean(String key, boolean defaultValue) { - objectOptTypeCalls++; - return super.optBoolean(key, defaultValue); - } - - @Override - public double optDouble(String key, double defaultValue) { - objectOptTypeCalls++; - return super.optDouble(key, defaultValue); - } - - @Override - public int optInt(String key, int defaultValue) { - objectOptTypeCalls++; - return super.optInt(key, defaultValue); - } - - @Override - public long optLong(String key, long defaultValue) { - objectOptTypeCalls++; - return super.optLong(key, defaultValue); - } - - @Override - public String optString(String key, String defaultValue) { - objectOptTypeCalls++; - return super.optString(key, defaultValue); - } - }; - - private final JSONArray array = new JSONArray() { - @Override - public JSONArray put(int index, Object value) throws JSONException { - arrayPutCalls++; - return super.put(index, value); - } - - @Override - public Object get(int index) throws JSONException { - arrayGetCalls++; - return super.get(index); - } - - @Override - public Object opt(int index) { - arrayOptCalls++; - return super.opt(index); - } - - @Override - public boolean optBoolean(int index, boolean fallback) { - arrayOptTypeCalls++; - return super.optBoolean(index, fallback); - } - - @Override - public double optDouble(int index, double fallback) { - arrayOptTypeCalls++; - return super.optDouble(index, fallback); - } - - @Override - public long optLong(int index, long fallback) { - arrayOptTypeCalls++; - return super.optLong(index, fallback); - } - - @Override - public String optString(int index, String fallback) { - arrayOptTypeCalls++; - return super.optString(index, fallback); - } - - @Override - public int optInt(int index, int fallback) { - arrayOptTypeCalls++; - return super.optInt(index, fallback); - } - }; - - private final JSONTokener tokener = new JSONTokener("{\"foo\": [true]}") { - @Override - public char next() { - tokenerNextCalls++; - return super.next(); - } - - @Override - public Object nextValue() throws JSONException { - tokenerNextValueCalls++; - return super.nextValue(); - } - }; - - - @Test - public void testObjectPut() throws JSONException { - object.putOpt("foo", "bar"); - assertEquals(1, objectPutCalls); + private int objectPutCalls = 0; + private int objectGetCalls = 0; + private int objectOptCalls = 0; + private int objectOptTypeCalls = 0; + private int arrayPutCalls = 0; + private int arrayGetCalls = 0; + private int arrayOptCalls = 0; + private int arrayOptTypeCalls = 0; + private int tokenerNextCalls = 0; + private int tokenerNextValueCalls = 0; + + private final JSONObject object = new JSONObject() { + @Override + public JSONObject put(String name, Object value) throws JSONException { + objectPutCalls++; + return super.put(name, value); + } + + @Override + public Object get(String name) throws JSONException { + objectGetCalls++; + return super.get(name); + } + + @Override + public Object opt(String name) { + objectOptCalls++; + return super.opt(name); + } + + @Override + public boolean optBoolean(String key, boolean defaultValue) { + objectOptTypeCalls++; + return super.optBoolean(key, defaultValue); + } + + @Override + public double optDouble(String key, double defaultValue) { + objectOptTypeCalls++; + return super.optDouble(key, defaultValue); + } + + @Override + public int optInt(String key, int defaultValue) { + objectOptTypeCalls++; + return super.optInt(key, defaultValue); + } + + @Override + public long optLong(String key, long defaultValue) { + objectOptTypeCalls++; + return super.optLong(key, defaultValue); } - @Test - public void testObjectAccumulate() throws JSONException { - object.accumulate("foo", "bar"); - assertEquals(1, objectPutCalls); + @Override + public String optString(String key, String defaultValue) { + objectOptTypeCalls++; + return super.optString(key, defaultValue); } + }; - @Test - public void testObjectGetBoolean() throws JSONException { - object.put("foo", "true"); - object.getBoolean("foo"); - assertEquals(1, objectGetCalls); + private final JSONArray array = new JSONArray() { + @Override + public JSONArray put(int index, Object value) throws JSONException { + arrayPutCalls++; + return super.put(index, value); } - @Test - public void testObjectOptType() throws JSONException { - object.optBoolean("foo"); - assertEquals(1, objectOptCalls); - assertEquals(1, objectOptTypeCalls); - object.optDouble("foo"); - assertEquals(2, objectOptCalls); - assertEquals(2, objectOptTypeCalls); - object.optInt("foo"); - assertEquals(3, objectOptCalls); - assertEquals(3, objectOptTypeCalls); - object.optLong("foo"); - assertEquals(4, objectOptCalls); - assertEquals(4, objectOptTypeCalls); - object.optString("foo"); - assertEquals(5, objectOptCalls); - assertEquals(5, objectOptTypeCalls); + @Override + public Object get(int index) throws JSONException { + arrayGetCalls++; + return super.get(index); } - @Test - public void testToJSONArray() throws JSONException { - object.put("foo", 5); - object.put("bar", 10); - array.put("foo"); - array.put("baz"); - array.put("bar"); - object.toJSONArray(array); - assertEquals(3, arrayOptCalls); - assertEquals(0, arrayOptTypeCalls); - assertEquals(3, objectOptCalls); - assertEquals(0, objectOptTypeCalls); + @Override + public Object opt(int index) { + arrayOptCalls++; + return super.opt(index); } - @Test - public void testPutAtIndex() throws JSONException { - array.put(10, false); - assertEquals(1, arrayPutCalls); + @Override + public boolean optBoolean(int index, boolean fallback) { + arrayOptTypeCalls++; + return super.optBoolean(index, fallback); } - @Test - public void testIsNull() { - array.isNull(5); - assertEquals(1, arrayOptCalls); + @Override + public double optDouble(int index, double fallback) { + arrayOptTypeCalls++; + return super.optDouble(index, fallback); } - @Test - public void testArrayGetType() throws JSONException { - array.put(true); - array.getBoolean(0); - assertEquals(1, arrayGetCalls); + @Override + public long optLong(int index, long fallback) { + arrayOptTypeCalls++; + return super.optLong(index, fallback); } - @Test - public void testArrayOptType() throws JSONException { - array.optBoolean(3); - assertEquals(1, arrayOptCalls); - assertEquals(1, arrayOptTypeCalls); - array.optDouble(3); - assertEquals(2, arrayOptCalls); - assertEquals(2, arrayOptTypeCalls); - array.optInt(3); - assertEquals(3, arrayOptCalls); - assertEquals(3, arrayOptTypeCalls); - array.optLong(3); - assertEquals(4, arrayOptCalls); - assertEquals(4, arrayOptTypeCalls); - array.optString(3); - assertEquals(5, arrayOptCalls); - assertEquals(5, arrayOptTypeCalls); + @Override + public String optString(int index, String fallback) { + arrayOptTypeCalls++; + return super.optString(index, fallback); } - @Test - public void testToJSONObject() throws JSONException { - array.put("foo"); - array.put("baz"); - array.put("bar"); - JSONArray values = new JSONArray(); - values.put(5.5d); - values.put(11d); - values.put(30); - values.toJSONObject(array); - assertEquals(3, arrayOptCalls); - assertEquals(0, arrayOptTypeCalls); + @Override + public int optInt(int index, int fallback) { + arrayOptTypeCalls++; + return super.optInt(index, fallback); } + }; - @Test - public void testNextExpecting() throws JSONException { - tokener.next('{'); - assertEquals(1, tokenerNextCalls); - tokener.next('\"'); - assertEquals(2, tokenerNextCalls); + private final JSONTokener tokener = new JSONTokener("{\"foo\": [true]}") { + @Override + public char next() { + tokenerNextCalls++; + return super.next(); } - @Test - public void testNextValue() throws JSONException { - tokener.nextValue(); - assertEquals(4, tokenerNextValueCalls); + @Override + public Object nextValue() throws JSONException { + tokenerNextValueCalls++; + return super.nextValue(); } + }; + + + @Test + public void testObjectPut() throws JSONException { + object.putOpt("foo", "bar"); + assertEquals(1, objectPutCalls); + } + + @Test + public void testObjectAccumulate() throws JSONException { + object.accumulate("foo", "bar"); + assertEquals(1, objectPutCalls); + } + + @Test + public void testObjectGetBoolean() throws JSONException { + object.put("foo", "true"); + object.getBoolean("foo"); + assertEquals(1, objectGetCalls); + } + + @Test + public void testObjectOptType() throws JSONException { + object.optBoolean("foo"); + assertEquals(1, objectOptCalls); + assertEquals(1, objectOptTypeCalls); + object.optDouble("foo"); + assertEquals(2, objectOptCalls); + assertEquals(2, objectOptTypeCalls); + object.optInt("foo"); + assertEquals(3, objectOptCalls); + assertEquals(3, objectOptTypeCalls); + object.optLong("foo"); + assertEquals(4, objectOptCalls); + assertEquals(4, objectOptTypeCalls); + object.optString("foo"); + assertEquals(5, objectOptCalls); + assertEquals(5, objectOptTypeCalls); + } + + @Test + public void testToJSONArray() throws JSONException { + object.put("foo", 5); + object.put("bar", 10); + array.put("foo"); + array.put("baz"); + array.put("bar"); + object.toJSONArray(array); + assertEquals(3, arrayOptCalls); + assertEquals(0, arrayOptTypeCalls); + assertEquals(3, objectOptCalls); + assertEquals(0, objectOptTypeCalls); + } + + @Test + public void testPutAtIndex() throws JSONException { + array.put(10, false); + assertEquals(1, arrayPutCalls); + } + + @Test + public void testIsNull() { + array.isNull(5); + assertEquals(1, arrayOptCalls); + } + + @Test + public void testArrayGetType() throws JSONException { + array.put(true); + array.getBoolean(0); + assertEquals(1, arrayGetCalls); + } + + @Test + public void testArrayOptType() throws JSONException { + array.optBoolean(3); + assertEquals(1, arrayOptCalls); + assertEquals(1, arrayOptTypeCalls); + array.optDouble(3); + assertEquals(2, arrayOptCalls); + assertEquals(2, arrayOptTypeCalls); + array.optInt(3); + assertEquals(3, arrayOptCalls); + assertEquals(3, arrayOptTypeCalls); + array.optLong(3); + assertEquals(4, arrayOptCalls); + assertEquals(4, arrayOptTypeCalls); + array.optString(3); + assertEquals(5, arrayOptCalls); + assertEquals(5, arrayOptTypeCalls); + } + + @Test + public void testToJSONObject() throws JSONException { + array.put("foo"); + array.put("baz"); + array.put("bar"); + JSONArray values = new JSONArray(); + values.put(5.5d); + values.put(11d); + values.put(30); + values.toJSONObject(array); + assertEquals(3, arrayOptCalls); + assertEquals(0, arrayOptTypeCalls); + } + + @Test + public void testNextExpecting() throws JSONException { + tokener.next('{'); + assertEquals(1, tokenerNextCalls); + tokener.next('\"'); + assertEquals(2, tokenerNextCalls); + } + + @Test + public void testNextValue() throws JSONException { + tokener.nextValue(); + assertEquals(4, tokenerNextValueCalls); + } }
