diff --git a/gson/src/test/java/com/google/gson/JsonArrayTest.java b/gson/src/test/java/com/google/gson/JsonArrayTest.java index 7ad4f5de8e..86ef03c889 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 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 void testDeepCopy() { } 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) { + jsonArray.getAsString(); + fail("expected getString to fail"); + } catch (UnsupportedOperationException e) { assertEquals("Expected an exception message", - "For input string: \"hello\"",e.getMessage()); + "JsonObject", e.getMessage()); } + + jsonArray.remove(0); + jsonArray.add("hello"); try { - jsonArray.get(4).getAsInt(); - assertTrue("expected getInt to fail", false); + jsonArray.getAsDouble(); + fail("expected getDouble to fail"); } catch (NumberFormatException e) { assertEquals("Expected an exception message", - "For input string: \"hello\"",e.getMessage()); + "For input string: \"hello\"", e.getMessage()); } try { - jsonArray.get(4).getAsJsonArray(); - assertTrue("expected getJSONArray to fail", false); - } catch (IllegalStateException e) { + jsonArray.getAsInt(); + fail("expected getInt to fail"); + } catch (NumberFormatException e) { assertEquals("Expected an exception message", - "Not a JSON Array: \"hello\"",e.getMessage()); + "For input string: \"hello\"", e.getMessage()); } try { - jsonArray.get(4).getAsJsonObject(); - assertTrue("expected getJSONObject to fail", false); + jsonArray.get(0).getAsJsonArray(); + fail("expected getJSONArray to fail"); } catch (IllegalStateException e) { assertEquals("Expected an exception message", - "Not a JSON Object: \"hello\"",e.getMessage()); + "Not a JSON Array: \"hello\"", e.getMessage()); } try { - jsonArray.get(4).getAsLong(); - assertTrue("expected getLong to fail", false); - } catch (NumberFormatException e) { + jsonArray.getAsJsonObject(); + fail("expected getJSONObject to fail"); + } catch (IllegalStateException e) { assertEquals("Expected an exception message", - "For input string: \"hello\"",e.getMessage()); + "Not a JSON Object: [\"hello\"]", e.getMessage()); } try { - jsonArray.get(10).getAsString(); - assertTrue("expected getString to fail", false); - } catch (UnsupportedOperationException e) { + jsonArray.getAsLong(); + fail("expected getLong to fail"); + } catch (NumberFormatException e) { assertEquals("Expected an exception message", - "JsonObject",e.getMessage()); + "For input string: \"hello\"", e.getMessage()); } } }