From 55115a5ca259787ce8d4deb7952d198f50591f92 Mon Sep 17 00:00:00 2001 From: HiFromAjay Date: Fri, 11 Jun 2021 10:04:32 -0600 Subject: [PATCH 1/3] Test cases for testing the exceptional behavior of get, getAsBoolean, getAsDouble, getAsInt, getAsJsonArray, getAsJsonObject, getAsLong, and getAsString methods of JsonArray class. These test cases, which we wrote according to the specified behavior of each method, that helped us in identifying the documentation bugs in JsonArray and JsonElement classes, which we submitted issues for (Issue #1908). Note that we have adapted these test cases based on similar tests from the JSON-java project (https://github.com/stleary/JSON-java). --- .../java/com/google/gson/JsonArrayTest.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/gson/src/test/java/com/google/gson/JsonArrayTest.java b/gson/src/test/java/com/google/gson/JsonArrayTest.java index b77d6f1b..7ad4f5de 100644 --- a/gson/src/test/java/com/google/gson/JsonArrayTest.java +++ b/gson/src/test/java/com/google/gson/JsonArrayTest.java @@ -20,6 +20,9 @@ import junit.framework.TestCase; import com.google.gson.common.MoreAsserts; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** * @author Jesse Wilson */ @@ -99,4 +102,65 @@ public final class JsonArrayTest extends TestCase { assertEquals(1, original.get(0).getAsJsonArray().size()); assertEquals(0, copy.get(0).getAsJsonArray().size()); } + + public void testFailedGetArrayValues() { + String arrayStr = "[" + "true," + "false," + "\"true\"," + "\"false\"," + "\"hello\"," + "23.45e-4," + "\"23.45\"," + "42," + "\"43\"," + "[" + "\"world\"" + "]," + "{" + "\"key1\":\"value1\"," + "\"key2\":\"value2\"," + "\"key3\":\"value3\"," + "\"key4\":\"value4\"" + "}," + "0," + "\"-1\"" + "]"; + JsonArray jsonArray = (JsonArray) JsonParser.parseString(arrayStr); + try { + jsonArray.get(10).getAsBoolean(); + assertTrue("expected getBoolean to fail", false); + } catch (UnsupportedOperationException e) { + assertEquals("Expected an exception message", + "JsonObject",e.getMessage()); + } + try { + jsonArray.get(-1); + assertTrue("expected get to fail", false); + } catch (IndexOutOfBoundsException e) { + assertEquals("Expected an exception message", + "Index -1 out of bounds for length 13",e.getMessage()); + } + try { + jsonArray.get(4).getAsDouble(); + assertTrue("expected getDouble to fail", false); + } catch (NumberFormatException e) { + assertEquals("Expected an exception message", + "For input string: \"hello\"",e.getMessage()); + } + try { + jsonArray.get(4).getAsInt(); + assertTrue("expected getInt to fail", false); + } catch (NumberFormatException e) { + assertEquals("Expected an exception message", + "For input string: \"hello\"",e.getMessage()); + } + try { + jsonArray.get(4).getAsJsonArray(); + assertTrue("expected getJSONArray to fail", false); + } catch (IllegalStateException e) { + assertEquals("Expected an exception message", + "Not a JSON Array: \"hello\"",e.getMessage()); + } + try { + jsonArray.get(4).getAsJsonObject(); + assertTrue("expected getJSONObject to fail", false); + } catch (IllegalStateException e) { + assertEquals("Expected an exception message", + "Not a JSON Object: \"hello\"",e.getMessage()); + } + try { + jsonArray.get(4).getAsLong(); + assertTrue("expected getLong to fail", false); + } catch (NumberFormatException e) { + assertEquals("Expected an exception message", + "For input string: \"hello\"",e.getMessage()); + } + try { + jsonArray.get(10).getAsString(); + assertTrue("expected getString to fail", false); + } catch (UnsupportedOperationException e) { + assertEquals("Expected an exception message", + "JsonObject",e.getMessage()); + } + } } From 2d1981d39bfcadfeac553582494abaec2fc5d737 Mon Sep 17 00:00:00 2001 From: HiFromAjay Date: Mon, 14 Jun 2021 14:31:14 -0600 Subject: [PATCH 2/3] modify test cases for testing the exceptional behavior of get... methods [use fail(...), use JsonArray methods, remove unused values, formatting, #1909, #1908] --- .../java/com/google/gson/JsonArrayTest.java | 95 ++++++++++--------- 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/gson/src/test/java/com/google/gson/JsonArrayTest.java b/gson/src/test/java/com/google/gson/JsonArrayTest.java index 7ad4f5de..86ef03c8 100644 --- a/gson/src/test/java/com/google/gson/JsonArrayTest.java +++ b/gson/src/test/java/com/google/gson/JsonArrayTest.java @@ -20,8 +20,8 @@ import junit.framework.TestCase; import com.google.gson.common.MoreAsserts; +import static org.junit.Assert.*; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; /** * @author Jesse Wilson @@ -104,63 +104,66 @@ public final class JsonArrayTest extends TestCase { } public void testFailedGetArrayValues() { - String arrayStr = "[" + "true," + "false," + "\"true\"," + "\"false\"," + "\"hello\"," + "23.45e-4," + "\"23.45\"," + "42," + "\"43\"," + "[" + "\"world\"" + "]," + "{" + "\"key1\":\"value1\"," + "\"key2\":\"value2\"," + "\"key3\":\"value3\"," + "\"key4\":\"value4\"" + "}," + "0," + "\"-1\"" + "]"; - JsonArray jsonArray = (JsonArray) JsonParser.parseString(arrayStr); + JsonArray jsonArray = new JsonArray(); + jsonArray.add(JsonParser.parseString("{" + "\"key1\":\"value1\"," + "\"key2\":\"value2\"," + "\"key3\":\"value3\"," + "\"key4\":\"value4\"" + "}")); try { - jsonArray.get(10).getAsBoolean(); - assertTrue("expected getBoolean to fail", false); + jsonArray.getAsBoolean(); + fail("expected getBoolean to fail"); } catch (UnsupportedOperationException e) { assertEquals("Expected an exception message", - "JsonObject",e.getMessage()); + "JsonObject", e.getMessage()); } try { jsonArray.get(-1); - assertTrue("expected get to fail", false); + fail("expected get to fail"); } catch (IndexOutOfBoundsException e) { assertEquals("Expected an exception message", - "Index -1 out of bounds for length 13",e.getMessage()); + "Index -1 out of bounds for length 1", e.getMessage()); } try { - jsonArray.get(4).getAsDouble(); - assertTrue("expected getDouble to fail", false); - } catch (NumberFormatException e) { - assertEquals("Expected an exception message", - "For input string: \"hello\"",e.getMessage()); - } - try { - jsonArray.get(4).getAsInt(); - assertTrue("expected getInt to fail", false); - } catch (NumberFormatException e) { - assertEquals("Expected an exception message", - "For input string: \"hello\"",e.getMessage()); - } - try { - jsonArray.get(4).getAsJsonArray(); - assertTrue("expected getJSONArray to fail", false); - } catch (IllegalStateException e) { - assertEquals("Expected an exception message", - "Not a JSON Array: \"hello\"",e.getMessage()); - } - try { - jsonArray.get(4).getAsJsonObject(); - assertTrue("expected getJSONObject to fail", false); - } catch (IllegalStateException e) { - assertEquals("Expected an exception message", - "Not a JSON Object: \"hello\"",e.getMessage()); - } - try { - jsonArray.get(4).getAsLong(); - assertTrue("expected getLong to fail", false); - } catch (NumberFormatException e) { - assertEquals("Expected an exception message", - "For input string: \"hello\"",e.getMessage()); - } - try { - jsonArray.get(10).getAsString(); - assertTrue("expected getString to fail", false); + jsonArray.getAsString(); + fail("expected getString to fail"); } catch (UnsupportedOperationException e) { assertEquals("Expected an exception message", - "JsonObject",e.getMessage()); + "JsonObject", e.getMessage()); + } + + jsonArray.remove(0); + jsonArray.add("hello"); + try { + jsonArray.getAsDouble(); + fail("expected getDouble to fail"); + } catch (NumberFormatException e) { + assertEquals("Expected an exception message", + "For input string: \"hello\"", e.getMessage()); + } + try { + jsonArray.getAsInt(); + fail("expected getInt to fail"); + } catch (NumberFormatException e) { + assertEquals("Expected an exception message", + "For input string: \"hello\"", e.getMessage()); + } + try { + jsonArray.get(0).getAsJsonArray(); + fail("expected getJSONArray to fail"); + } catch (IllegalStateException e) { + assertEquals("Expected an exception message", + "Not a JSON Array: \"hello\"", e.getMessage()); + } + try { + jsonArray.getAsJsonObject(); + fail("expected getJSONObject to fail"); + } catch (IllegalStateException e) { + assertEquals("Expected an exception message", + "Not a JSON Object: [\"hello\"]", e.getMessage()); + } + try { + jsonArray.getAsLong(); + fail("expected getLong to fail"); + } catch (NumberFormatException e) { + assertEquals("Expected an exception message", + "For input string: \"hello\"", e.getMessage()); } } } From 01ab13f701e6db84bdf37f602ef7af3c8d5c2f35 Mon Sep 17 00:00:00 2001 From: HiFromAjay Date: Thu, 5 Aug 2021 17:23:28 -0600 Subject: [PATCH 3/3] Remove unused imports [#1909, #1908] --- gson/src/test/java/com/google/gson/JsonArrayTest.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/gson/src/test/java/com/google/gson/JsonArrayTest.java b/gson/src/test/java/com/google/gson/JsonArrayTest.java index 86ef03c8..3975ce2c 100644 --- a/gson/src/test/java/com/google/gson/JsonArrayTest.java +++ b/gson/src/test/java/com/google/gson/JsonArrayTest.java @@ -16,12 +16,8 @@ package com.google.gson; -import junit.framework.TestCase; - import com.google.gson.common.MoreAsserts; - -import static org.junit.Assert.*; -import static org.junit.Assert.assertEquals; +import junit.framework.TestCase; /** * @author Jesse Wilson