Skip to content

Commit

Permalink
modify test cases for testing the exceptional behavior of get... meth…
Browse files Browse the repository at this point in the history
…ods [use fail(...), use JsonArray methods, remove unused values, formatting, #1909, #1908]
  • Loading branch information
HiFromAjay committed Jun 14, 2021
1 parent 55115a5 commit 2d1981d
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions gson/src/test/java/com/google/gson/JsonArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 2d1981d

Please sign in to comment.