modify test cases for testing the exceptional behavior of get... methods [use fail(...), use JsonArray methods, remove unused values, formatting, #1909, #1908]

This commit is contained in:
HiFromAjay 2021-06-14 14:31:14 -06:00
parent 55115a5ca2
commit 2d1981d39b
1 changed files with 49 additions and 46 deletions

View File

@ -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());
}
}
}