2011-02-15 01:14:20 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.google.gson;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
2014-07-02 20:21:36 +02:00
|
|
|
import com.google.gson.common.MoreAsserts;
|
|
|
|
|
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).
2021-06-11 18:04:32 +02:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
2011-02-15 01:14:20 +01:00
|
|
|
/**
|
|
|
|
* @author Jesse Wilson
|
|
|
|
*/
|
|
|
|
public final class JsonArrayTest extends TestCase {
|
|
|
|
|
|
|
|
public void testEqualsOnEmptyArray() {
|
|
|
|
MoreAsserts.assertEqualsAndHashCode(new JsonArray(), new JsonArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testEqualsNonEmptyArray() {
|
|
|
|
JsonArray a = new JsonArray();
|
|
|
|
JsonArray b = new JsonArray();
|
|
|
|
|
2011-04-05 00:09:51 +02:00
|
|
|
assertEquals(a, a);
|
|
|
|
|
2011-02-15 01:14:20 +01:00
|
|
|
a.add(new JsonObject());
|
|
|
|
assertFalse(a.equals(b));
|
|
|
|
assertFalse(b.equals(a));
|
|
|
|
|
|
|
|
b.add(new JsonObject());
|
|
|
|
MoreAsserts.assertEqualsAndHashCode(a, b);
|
|
|
|
|
|
|
|
a.add(new JsonObject());
|
|
|
|
assertFalse(a.equals(b));
|
|
|
|
assertFalse(b.equals(a));
|
|
|
|
|
2011-05-25 18:48:39 +02:00
|
|
|
b.add(JsonNull.INSTANCE);
|
2011-02-15 01:14:20 +01:00
|
|
|
assertFalse(a.equals(b));
|
|
|
|
assertFalse(b.equals(a));
|
|
|
|
}
|
2012-08-21 03:19:43 +02:00
|
|
|
|
2014-07-02 20:21:36 +02:00
|
|
|
public void testRemove() {
|
|
|
|
JsonArray array = new JsonArray();
|
|
|
|
try {
|
|
|
|
array.remove(0);
|
|
|
|
fail();
|
|
|
|
} catch (IndexOutOfBoundsException expected) {}
|
|
|
|
JsonPrimitive a = new JsonPrimitive("a");
|
|
|
|
array.add(a);
|
|
|
|
assertTrue(array.remove(a));
|
2014-07-04 04:03:34 +02:00
|
|
|
assertFalse(array.contains(a));
|
2014-07-02 20:21:36 +02:00
|
|
|
array.add(a);
|
|
|
|
array.add(new JsonPrimitive("b"));
|
|
|
|
assertEquals("b", array.remove(1).getAsString());
|
|
|
|
assertEquals(1, array.size());
|
2014-07-04 04:03:34 +02:00
|
|
|
assertTrue(array.contains(a));
|
2014-07-02 20:21:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-02 20:30:17 +02:00
|
|
|
public void testSet() {
|
|
|
|
JsonArray array = new JsonArray();
|
|
|
|
try {
|
|
|
|
array.set(0, new JsonPrimitive(1));
|
|
|
|
fail();
|
|
|
|
} catch (IndexOutOfBoundsException expected) {}
|
|
|
|
JsonPrimitive a = new JsonPrimitive("a");
|
|
|
|
array.add(a);
|
|
|
|
array.set(0, new JsonPrimitive("b"));
|
|
|
|
assertEquals("b", array.get(0).getAsString());
|
|
|
|
array.set(0, null);
|
|
|
|
assertNull(array.get(0));
|
|
|
|
array.set(0, new JsonPrimitive("c"));
|
|
|
|
assertEquals("c", array.get(0).getAsString());
|
|
|
|
assertEquals(1, array.size());
|
|
|
|
}
|
|
|
|
|
2012-08-21 03:19:43 +02:00
|
|
|
public void testDeepCopy() {
|
|
|
|
JsonArray original = new JsonArray();
|
|
|
|
JsonArray firstEntry = new JsonArray();
|
|
|
|
original.add(firstEntry);
|
|
|
|
|
|
|
|
JsonArray copy = original.deepCopy();
|
|
|
|
original.add(new JsonPrimitive("y"));
|
|
|
|
|
|
|
|
assertEquals(1, copy.size());
|
|
|
|
firstEntry.add(new JsonPrimitive("z"));
|
|
|
|
|
|
|
|
assertEquals(1, original.get(0).getAsJsonArray().size());
|
|
|
|
assertEquals(0, copy.get(0).getAsJsonArray().size());
|
|
|
|
}
|
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).
2021-06-11 18:04:32 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2011-02-15 01:14:20 +01:00
|
|
|
}
|