-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test cases for testing the exceptional behavior of JsonArray get... methods #1909
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,9 @@ | |
|
||
import com.google.gson.common.MoreAsserts; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Jesse Wilson | ||
*/ | ||
|
@@ -99,4 +102,68 @@ public void testDeepCopy() { | |
assertEquals(1, original.get(0).getAsJsonArray().size()); | ||
assertEquals(0, copy.get(0).getAsJsonArray().size()); | ||
} | ||
|
||
public void testFailedGetArrayValues() { | ||
JsonArray jsonArray = new JsonArray(); | ||
jsonArray.add(JsonParser.parseString("{" + "\"key1\":\"value1\"," + "\"key2\":\"value2\"," + "\"key3\":\"value3\"," + "\"key4\":\"value4\"" + "}")); | ||
try { | ||
jsonArray.getAsBoolean(); | ||
fail("expected getBoolean to fail"); | ||
} catch (UnsupportedOperationException e) { | ||
assertEquals("Expected an exception message", | ||
"JsonObject", e.getMessage()); | ||
} | ||
try { | ||
jsonArray.get(-1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Except for this test, all other ones are not specific to JsonArray, it might be better to put them to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed how the methods are used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Also my comment was incorrect, I missed that the |
||
fail("expected get to fail"); | ||
} catch (IndexOutOfBoundsException e) { | ||
assertEquals("Expected an exception message", | ||
"Index -1 out of bounds for length 1", e.getMessage()); | ||
} | ||
try { | ||
jsonArray.getAsString(); | ||
fail("expected getString to fail"); | ||
} catch (UnsupportedOperationException e) { | ||
assertEquals("Expected an exception message", | ||
"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()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me why these imports are needed. Aren't
assertEquals
etc inherited fromTestCase
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed those imports.