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;
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
|
|
import static com.google.common.truth.Truth.assertWithMessage;
|
2022-10-16 21:30:49 +02:00
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
2023-02-15 14:18:43 +01:00
|
|
|
import com.google.common.testing.EqualsTester;
|
2014-07-02 20:21:36 +02:00
|
|
|
import com.google.gson.common.MoreAsserts;
|
2022-10-16 21:30:49 +02:00
|
|
|
import java.math.BigInteger;
|
|
|
|
import org.junit.Test;
|
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
|
|
|
|
2011-02-15 01:14:20 +01:00
|
|
|
/**
|
|
|
|
* @author Jesse Wilson
|
|
|
|
*/
|
2022-10-16 21:30:49 +02:00
|
|
|
public final class JsonArrayTest {
|
2011-02-15 01:14:20 +01:00
|
|
|
|
2022-10-16 21:30:49 +02:00
|
|
|
@Test
|
2011-02-15 01:14:20 +01:00
|
|
|
public void testEqualsOnEmptyArray() {
|
|
|
|
MoreAsserts.assertEqualsAndHashCode(new JsonArray(), new JsonArray());
|
|
|
|
}
|
|
|
|
|
2022-10-16 21:30:49 +02:00
|
|
|
@Test
|
2011-02-15 01:14:20 +01:00
|
|
|
public void testEqualsNonEmptyArray() {
|
|
|
|
JsonArray a = new JsonArray();
|
|
|
|
JsonArray b = new JsonArray();
|
|
|
|
|
2023-02-15 14:18:43 +01:00
|
|
|
new EqualsTester().addEqualityGroup(a).testEquals();
|
2011-04-05 00:09:51 +02:00
|
|
|
|
2011-02-15 01:14:20 +01:00
|
|
|
a.add(new JsonObject());
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(a.equals(b)).isFalse();
|
|
|
|
assertThat(b.equals(a)).isFalse();
|
2011-02-15 01:14:20 +01:00
|
|
|
|
|
|
|
b.add(new JsonObject());
|
|
|
|
MoreAsserts.assertEqualsAndHashCode(a, b);
|
|
|
|
|
|
|
|
a.add(new JsonObject());
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(a.equals(b)).isFalse();
|
|
|
|
assertThat(b.equals(a)).isFalse();
|
2011-02-15 01:14:20 +01:00
|
|
|
|
2011-05-25 18:48:39 +02:00
|
|
|
b.add(JsonNull.INSTANCE);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(a.equals(b)).isFalse();
|
|
|
|
assertThat(b.equals(a)).isFalse();
|
2011-02-15 01:14:20 +01:00
|
|
|
}
|
2012-08-21 03:19:43 +02:00
|
|
|
|
2022-10-16 21:30:49 +02:00
|
|
|
@Test
|
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);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(array.remove(a)).isTrue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(array).doesNotContain(a);
|
2014-07-02 20:21:36 +02:00
|
|
|
array.add(a);
|
|
|
|
array.add(new JsonPrimitive("b"));
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(array.remove(1).getAsString()).isEqualTo("b");
|
|
|
|
assertThat(array).hasSize(1);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(array).contains(a);
|
2014-07-02 20:21:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 21:30:49 +02:00
|
|
|
@Test
|
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);
|
2022-08-06 18:57:00 +02:00
|
|
|
|
|
|
|
JsonPrimitive b = new JsonPrimitive("b");
|
|
|
|
JsonElement oldValue = array.set(0, b);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(oldValue).isEqualTo(a);
|
|
|
|
assertThat(array.get(0).getAsString()).isEqualTo("b");
|
2022-08-06 18:57:00 +02:00
|
|
|
|
|
|
|
oldValue = array.set(0, null);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(oldValue).isEqualTo(b);
|
|
|
|
assertThat(array.get(0)).isEqualTo(JsonNull.INSTANCE);
|
2022-08-06 18:57:00 +02:00
|
|
|
|
|
|
|
oldValue = array.set(0, new JsonPrimitive("c"));
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(oldValue).isEqualTo(JsonNull.INSTANCE);
|
|
|
|
assertThat(array.get(0).getAsString()).isEqualTo("c");
|
|
|
|
assertThat(array).hasSize(1);
|
2014-07-02 20:30:17 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 21:30:49 +02:00
|
|
|
@Test
|
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"));
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(copy).hasSize(1);
|
2012-08-21 03:19:43 +02:00
|
|
|
firstEntry.add(new JsonPrimitive("z"));
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(original.get(0).getAsJsonArray()).hasSize(1);
|
|
|
|
assertThat(copy.get(0).getAsJsonArray()).hasSize(0);
|
2012-08-21 03:19:43 +02:00
|
|
|
}
|
2022-08-21 22:29:40 +02:00
|
|
|
|
2022-10-16 21:30:49 +02:00
|
|
|
@Test
|
2022-08-08 17:38:56 +02:00
|
|
|
public void testIsEmpty() {
|
|
|
|
JsonArray array = new JsonArray();
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(array).isEmpty();
|
2022-08-08 17:38:56 +02:00
|
|
|
|
|
|
|
JsonPrimitive a = new JsonPrimitive("a");
|
|
|
|
array.add(a);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(array).isNotEmpty();
|
2022-08-08 17:38:56 +02:00
|
|
|
|
|
|
|
array.remove(0);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(array).isEmpty();
|
2022-08-08 17:38:56 +02:00
|
|
|
}
|
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
|
|
|
|
2022-10-16 21:30:49 +02:00
|
|
|
@Test
|
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() {
|
2021-06-14 22:31:14 +02:00
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
jsonArray.add(JsonParser.parseString("{" + "\"key1\":\"value1\"," + "\"key2\":\"value2\"," + "\"key3\":\"value3\"," + "\"key4\":\"value4\"" + "}"));
|
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
|
|
|
try {
|
2021-06-14 22:31:14 +02:00
|
|
|
jsonArray.getAsBoolean();
|
|
|
|
fail("expected getBoolean to fail");
|
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
|
|
|
} catch (UnsupportedOperationException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertWithMessage("Expected an exception message")
|
|
|
|
.that(e).hasMessageThat().isEqualTo("JsonObject");
|
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
|
|
|
}
|
|
|
|
try {
|
|
|
|
jsonArray.get(-1);
|
2021-06-14 22:31:14 +02:00
|
|
|
fail("expected get to fail");
|
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
|
|
|
} catch (IndexOutOfBoundsException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertWithMessage("Expected an exception message")
|
|
|
|
.that(e).hasMessageThat().isEqualTo("Index -1 out of bounds for length 1");
|
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
|
|
|
}
|
|
|
|
try {
|
2021-06-14 22:31:14 +02:00
|
|
|
jsonArray.getAsString();
|
|
|
|
fail("expected getString to fail");
|
|
|
|
} catch (UnsupportedOperationException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertWithMessage("Expected an exception message")
|
|
|
|
.that(e).hasMessageThat().isEqualTo("JsonObject");
|
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
|
|
|
}
|
2021-06-14 22:31:14 +02:00
|
|
|
|
|
|
|
jsonArray.remove(0);
|
|
|
|
jsonArray.add("hello");
|
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
|
|
|
try {
|
2021-06-14 22:31:14 +02:00
|
|
|
jsonArray.getAsDouble();
|
|
|
|
fail("expected getDouble to fail");
|
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
|
|
|
} catch (NumberFormatException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertWithMessage("Expected an exception message")
|
|
|
|
.that(e).hasMessageThat().isEqualTo("For input string: \"hello\"");
|
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
|
|
|
}
|
|
|
|
try {
|
2021-06-14 22:31:14 +02:00
|
|
|
jsonArray.getAsInt();
|
|
|
|
fail("expected getInt to fail");
|
|
|
|
} catch (NumberFormatException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertWithMessage("Expected an exception message")
|
|
|
|
.that(e).hasMessageThat().isEqualTo("For input string: \"hello\"");
|
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
|
|
|
}
|
|
|
|
try {
|
2021-06-14 22:31:14 +02:00
|
|
|
jsonArray.get(0).getAsJsonArray();
|
|
|
|
fail("expected getJSONArray to fail");
|
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
|
|
|
} catch (IllegalStateException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertWithMessage("Expected an exception message")
|
|
|
|
.that(e).hasMessageThat().isEqualTo("Not a JSON Array: \"hello\"");
|
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
|
|
|
}
|
|
|
|
try {
|
2021-06-14 22:31:14 +02:00
|
|
|
jsonArray.getAsJsonObject();
|
|
|
|
fail("expected getJSONObject to fail");
|
|
|
|
} catch (IllegalStateException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertWithMessage("Expected an exception message")
|
|
|
|
.that(e).hasMessageThat().isEqualTo( "Not a JSON Object: [\"hello\"]");
|
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
|
|
|
}
|
|
|
|
try {
|
2021-06-14 22:31:14 +02:00
|
|
|
jsonArray.getAsLong();
|
|
|
|
fail("expected getLong to fail");
|
|
|
|
} catch (NumberFormatException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertWithMessage("Expected an exception message")
|
|
|
|
.that(e).hasMessageThat().isEqualTo("For input string: \"hello\"");
|
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
|
|
|
}
|
|
|
|
}
|
2022-08-21 22:29:40 +02:00
|
|
|
|
2022-10-16 21:30:49 +02:00
|
|
|
@Test
|
2022-08-21 22:29:40 +02:00
|
|
|
public void testGetAs_WrongArraySize() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
try {
|
|
|
|
jsonArray.getAsByte();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(e).hasMessageThat().isEqualTo("Array must have size 1, but has size 0");
|
2022-08-21 22:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonArray.add(true);
|
|
|
|
jsonArray.add(false);
|
|
|
|
try {
|
|
|
|
jsonArray.getAsByte();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException e) {
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(e).hasMessageThat().isEqualTo("Array must have size 1, but has size 2");
|
2022-08-21 22:29:40 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-16 21:30:49 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testStringPrimitiveAddition() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
|
|
|
|
jsonArray.add("Hello");
|
|
|
|
jsonArray.add("Goodbye");
|
|
|
|
jsonArray.add("Thank you");
|
|
|
|
jsonArray.add((String) null);
|
|
|
|
jsonArray.add("Yes");
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.toString()).isEqualTo("[\"Hello\",\"Goodbye\",\"Thank you\",null,\"Yes\"]");
|
2022-10-16 21:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIntegerPrimitiveAddition() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
|
|
|
|
int x = 1;
|
|
|
|
jsonArray.add(x);
|
|
|
|
|
|
|
|
x = 2;
|
|
|
|
jsonArray.add(x);
|
|
|
|
|
|
|
|
x = -3;
|
|
|
|
jsonArray.add(x);
|
|
|
|
|
|
|
|
jsonArray.add((Integer) null);
|
|
|
|
|
|
|
|
x = 4;
|
|
|
|
jsonArray.add(x);
|
|
|
|
|
|
|
|
x = 0;
|
|
|
|
jsonArray.add(x);
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.toString()).isEqualTo("[1,2,-3,null,4,0]");
|
2022-10-16 21:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testDoublePrimitiveAddition() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
|
|
|
|
double x = 1.0;
|
|
|
|
jsonArray.add(x);
|
|
|
|
|
|
|
|
x = 2.13232;
|
|
|
|
jsonArray.add(x);
|
|
|
|
|
|
|
|
x = 0.121;
|
|
|
|
jsonArray.add(x);
|
|
|
|
|
|
|
|
jsonArray.add((Double) null);
|
|
|
|
|
|
|
|
x = -0.00234;
|
|
|
|
jsonArray.add(x);
|
|
|
|
|
|
|
|
jsonArray.add((Double) null);
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.toString()).isEqualTo("[1.0,2.13232,0.121,null,-0.00234,null]");
|
2022-10-16 21:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testBooleanPrimitiveAddition() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
|
|
|
|
jsonArray.add(true);
|
|
|
|
jsonArray.add(true);
|
|
|
|
jsonArray.add(false);
|
|
|
|
jsonArray.add(false);
|
|
|
|
jsonArray.add((Boolean) null);
|
|
|
|
jsonArray.add(true);
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.toString()).isEqualTo("[true,true,false,false,null,true]");
|
2022-10-16 21:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCharPrimitiveAddition() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
|
|
|
|
jsonArray.add('a');
|
|
|
|
jsonArray.add('e');
|
|
|
|
jsonArray.add('i');
|
|
|
|
jsonArray.add((char) 111);
|
|
|
|
jsonArray.add((Character) null);
|
|
|
|
jsonArray.add('u');
|
|
|
|
jsonArray.add("and sometimes Y");
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.toString()).isEqualTo("[\"a\",\"e\",\"i\",\"o\",null,\"u\",\"and sometimes Y\"]");
|
2022-10-16 21:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testMixedPrimitiveAddition() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
|
|
|
|
jsonArray.add('a');
|
|
|
|
jsonArray.add("apple");
|
|
|
|
jsonArray.add(12121);
|
|
|
|
jsonArray.add((char) 111);
|
|
|
|
|
|
|
|
jsonArray.add((Boolean) null);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.get(jsonArray.size() - 1)).isEqualTo(JsonNull.INSTANCE);
|
2022-10-16 21:30:49 +02:00
|
|
|
|
|
|
|
jsonArray.add((Character) null);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.get(jsonArray.size() - 1)).isEqualTo(JsonNull.INSTANCE);
|
2022-10-16 21:30:49 +02:00
|
|
|
|
|
|
|
jsonArray.add(12.232);
|
|
|
|
jsonArray.add(BigInteger.valueOf(2323));
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.toString()).isEqualTo("[\"a\",\"apple\",12121,\"o\",null,null,12.232,2323]");
|
2022-10-16 21:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNullPrimitiveAddition() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
|
|
|
|
jsonArray.add((Character) null);
|
|
|
|
jsonArray.add((Boolean) null);
|
|
|
|
jsonArray.add((Integer) null);
|
|
|
|
jsonArray.add((Double) null);
|
|
|
|
jsonArray.add((Float) null);
|
|
|
|
jsonArray.add((BigInteger) null);
|
|
|
|
jsonArray.add((String) null);
|
|
|
|
jsonArray.add((Boolean) null);
|
|
|
|
jsonArray.add((Number) null);
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.toString()).isEqualTo("[null,null,null,null,null,null,null,null,null]");
|
2022-10-16 21:30:49 +02:00
|
|
|
for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
|
// Verify that they are actually a JsonNull and not a Java null
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.get(i)).isEqualTo(JsonNull.INSTANCE);
|
2022-10-16 21:30:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNullJsonElementAddition() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
jsonArray.add((JsonElement) null);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.get(0)).isEqualTo(JsonNull.INSTANCE);
|
2022-10-16 21:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSameAddition() {
|
|
|
|
JsonArray jsonArray = new JsonArray();
|
|
|
|
|
|
|
|
jsonArray.add('a');
|
|
|
|
jsonArray.add('a');
|
|
|
|
jsonArray.add(true);
|
|
|
|
jsonArray.add(true);
|
|
|
|
jsonArray.add(1212);
|
|
|
|
jsonArray.add(1212);
|
|
|
|
jsonArray.add(34.34);
|
|
|
|
jsonArray.add(34.34);
|
|
|
|
jsonArray.add((Boolean) null);
|
|
|
|
jsonArray.add((Boolean) null);
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonArray.toString()).isEqualTo("[\"a\",\"a\",true,true,1212,1212,34.34,34.34,null,null]");
|
2022-10-16 21:30:49 +02:00
|
|
|
}
|
2011-02-15 01:14:20 +01:00
|
|
|
}
|