Modification in test cases (#2454)

* Fixed Typo in GsonBuilder.java

* Suggestions on Test cases

* Modified test cases using assertThrows method (JUnit)

* Update gson/src/test/java/com/google/gson/JsonArrayAsListTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/GsonTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonArrayAsListTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/TypeAdapterTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/TypeAdapterTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

---------

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
This commit is contained in:
Wonil 2023-07-30 06:18:45 +09:00 committed by GitHub
parent 5055b62463
commit 3d241ca0a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 227 deletions

View File

@ -17,7 +17,7 @@
package com.google.gson;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
import com.google.gson.Gson.FutureTypeAdapter;
import com.google.gson.internal.Excluder;
@ -104,12 +104,8 @@ public final class GsonTest {
@Test
public void testGetAdapter_Null() {
Gson gson = new Gson();
try {
gson.getAdapter((TypeToken<?>) null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("type must not be null");
}
NullPointerException e = assertThrows(NullPointerException.class, () -> gson.getAdapter((TypeToken<?>) null));
assertThat(e).hasMessageThat().isEqualTo("type must not be null");
}
@Test
@ -282,13 +278,9 @@ public final class GsonTest {
jsonWriter.value(true);
jsonWriter.endObject();
try {
// Additional top-level value
jsonWriter.value(1);
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().isEqualTo("JSON must have only one top-level value.");
}
// Additional top-level value
IllegalStateException e = assertThrows(IllegalStateException.class, () -> jsonWriter.value(1));
assertThat(e).hasMessageThat().isEqualTo("JSON must have only one top-level value.");
jsonWriter.close();
assertThat(writer.toString()).isEqualTo("{\"\\u003ctest2\":true}");
@ -323,11 +315,7 @@ public final class GsonTest {
public void testNewJsonReader_Default() throws IOException {
String json = "test"; // String without quotes
JsonReader jsonReader = new Gson().newJsonReader(new StringReader(json));
try {
jsonReader.nextString();
fail();
} catch (MalformedJsonException expected) {
}
assertThrows(MalformedJsonException.class, jsonReader::nextString);
jsonReader.close();
}

View File

@ -17,7 +17,7 @@
package com.google.gson;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
import com.google.gson.common.MoreAsserts;
import java.util.Arrays;
@ -37,17 +37,8 @@ public class JsonArrayAsListTest {
List<JsonElement> list = a.asList();
assertThat(list.get(0)).isEqualTo(new JsonPrimitive(1));
try {
list.get(-1);
fail();
} catch (IndexOutOfBoundsException e) {
}
try {
list.get(2);
fail();
} catch (IndexOutOfBoundsException e) {
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(-1));
assertThrows(IndexOutOfBoundsException.class, () -> list.get(2));
a.add((JsonElement) null);
assertThat(list.get(1)).isEqualTo(JsonNull.INSTANCE);
@ -75,24 +66,11 @@ public class JsonArrayAsListTest {
assertThat(list.get(0)).isEqualTo(new JsonPrimitive(2));
assertThat(a.get(0)).isEqualTo(new JsonPrimitive(2));
try {
list.set(-1, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, new JsonPrimitive(1)));
assertThrows(IndexOutOfBoundsException.class, () -> list.set(2, new JsonPrimitive(1)));
try {
list.set(2, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}
try {
list.set(0, null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
NullPointerException e = assertThrows(NullPointerException.class, () -> list.set(0, null));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
@Test
@ -115,30 +93,14 @@ public class JsonArrayAsListTest {
);
assertThat(list).isEqualTo(expectedList);
try {
list.set(-1, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, new JsonPrimitive(1)));
assertThrows(IndexOutOfBoundsException.class, () -> list.set(list.size(), new JsonPrimitive(1)));
try {
list.set(list.size(), new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}
NullPointerException e = assertThrows(NullPointerException.class, () -> list.add(0, null));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
try {
list.add(0, null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
try {
list.add(null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
e = assertThrows(NullPointerException.class, () -> list.add(null));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
@Test
@ -157,18 +119,11 @@ public class JsonArrayAsListTest {
assertThat(list).isEqualTo(expectedList);
assertThat(list).isEqualTo(expectedList);
try {
list.addAll(0, Collections.<JsonElement>singletonList(null));
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
try {
list.addAll(Collections.<JsonElement>singletonList(null));
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
NullPointerException e = assertThrows(NullPointerException.class, () -> list.addAll(0, Collections.<JsonElement>singletonList(null)));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
e = assertThrows(NullPointerException.class, () -> list.addAll(Collections.<JsonElement>singletonList(null)));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
@Test
@ -180,11 +135,8 @@ public class JsonArrayAsListTest {
assertThat(list.remove(0)).isEqualTo(new JsonPrimitive(1));
assertThat(list).hasSize(0);
assertThat(a).hasSize(0);
try {
list.remove(0);
fail();
} catch (IndexOutOfBoundsException e) {
}
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0));
}
@Test

View File

@ -17,7 +17,7 @@
package com.google.gson;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.internal.Streams;
@ -37,10 +37,7 @@ public class JsonParserTest {
@Test
public void testParseInvalidJson() {
try {
JsonParser.parseString("[[]");
fail();
} catch (JsonSyntaxException expected) { }
assertThrows(JsonSyntaxException.class, () -> JsonParser.parseString("[[]"));
}
@Test
@ -81,11 +78,7 @@ public class JsonParserTest {
@Test
public void testParseUnquotedMultiWordStringFails() {
String unquotedSentence = "Test is a test..blah blah";
try {
JsonParser.parseString(unquotedSentence);
fail();
} catch (JsonSyntaxException expected) { }
assertThrows(JsonSyntaxException.class, () -> JsonParser.parseString("Test is a test..blah blah"));
}
@Test

View File

@ -16,7 +16,7 @@
package com.google.gson;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
import java.io.EOFException;
import java.util.NoSuchElementException;
@ -72,57 +72,34 @@ public class JsonStreamParserTest {
public void testCallingNextBeyondAvailableInput() {
JsonElement unused1 = parser.next();
JsonElement unused2 = parser.next();
try {
parser.next();
fail("Parser should not go beyond available input");
} catch (NoSuchElementException expected) {
}
// Parser should not go beyond available input
assertThrows(NoSuchElementException.class, parser::next);
}
@Test
public void testEmptyInput() {
JsonStreamParser parser = new JsonStreamParser("");
try {
parser.next();
fail();
} catch (JsonIOException e) {
assertThat(e.getCause()).isInstanceOf(EOFException.class);
}
JsonIOException e = assertThrows(JsonIOException.class, parser::next);
assertThat(e).hasCauseThat().isInstanceOf(EOFException.class);
parser = new JsonStreamParser("");
try {
parser.hasNext();
fail();
} catch (JsonIOException e) {
assertThat(e.getCause()).isInstanceOf(EOFException.class);
}
e = assertThrows(JsonIOException.class, parser::hasNext);
assertThat(e).hasCauseThat().isInstanceOf(EOFException.class);
}
@Test
public void testIncompleteInput() {
JsonStreamParser parser = new JsonStreamParser("[");
assertThat(parser.hasNext()).isTrue();
try {
parser.next();
fail();
} catch (JsonSyntaxException e) {
}
assertThrows(JsonSyntaxException.class, parser::next);
}
@Test
public void testMalformedInput() {
JsonStreamParser parser = new JsonStreamParser(":");
try {
parser.hasNext();
fail();
} catch (JsonSyntaxException e) {
}
assertThrows(JsonSyntaxException.class, parser::hasNext);
parser = new JsonStreamParser(":");
try {
parser.next();
fail();
} catch (JsonSyntaxException e) {
}
assertThrows(JsonSyntaxException.class, parser::next);
}
}

View File

@ -17,7 +17,7 @@
package com.google.gson;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
import com.google.gson.internal.LazilyParsedNumber;
import com.google.gson.stream.JsonReader;
@ -33,19 +33,14 @@ public class ToNumberPolicyTest {
ToNumberStrategy strategy = ToNumberPolicy.DOUBLE;
assertThat(strategy.readNumber(fromString("10.1"))).isEqualTo(10.1);
assertThat(strategy.readNumber(fromString("3.141592653589793238462643383279"))).isEqualTo(3.141592653589793D);
try {
strategy.readNumber(fromString("1e400"));
fail();
} catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo(
"JSON forbids NaN and infinities: Infinity at line 1 column 6 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
}
try {
strategy.readNumber(fromString("\"not-a-number\""));
fail();
} catch (NumberFormatException expected) {
}
MalformedJsonException e = assertThrows(MalformedJsonException.class, () -> strategy.readNumber(fromString("1e400")));
assertThat(e).hasMessageThat().isEqualTo(
"JSON forbids NaN and infinities: Infinity at line 1 column 6 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"
);
assertThrows(NumberFormatException.class, () -> strategy.readNumber(fromString("\"not-a-number\"")));
}
@Test
@ -62,46 +57,31 @@ public class ToNumberPolicyTest {
assertThat(strategy.readNumber(fromString("10"))).isEqualTo(10L);
assertThat(strategy.readNumber(fromString("10.1"))).isEqualTo(10.1);
assertThat(strategy.readNumber(fromString("3.141592653589793238462643383279"))).isEqualTo(3.141592653589793D);
try {
strategy.readNumber(fromString("1e400"));
fail();
} catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("JSON forbids NaN and infinities: Infinity; at path $");
}
try {
strategy.readNumber(fromString("\"not-a-number\""));
fail();
} catch (JsonParseException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Cannot parse not-a-number; at path $");
}
Exception e = assertThrows(MalformedJsonException.class, () -> strategy.readNumber(fromString("1e400")));
assertThat(e).hasMessageThat().isEqualTo("JSON forbids NaN and infinities: Infinity; at path $");
e = assertThrows(JsonParseException.class, () -> strategy.readNumber(fromString("\"not-a-number\"")));
assertThat(e).hasMessageThat().isEqualTo("Cannot parse not-a-number; at path $");
assertThat(strategy.readNumber(fromStringLenient("NaN"))).isEqualTo(Double.NaN);
assertThat(strategy.readNumber(fromStringLenient("Infinity"))).isEqualTo(Double.POSITIVE_INFINITY);
assertThat(strategy.readNumber(fromStringLenient("-Infinity"))).isEqualTo(Double.NEGATIVE_INFINITY);
try {
strategy.readNumber(fromString("NaN"));
fail();
} catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo(
"Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
}
try {
strategy.readNumber(fromString("Infinity"));
fail();
} catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo(
"Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
}
try {
strategy.readNumber(fromString("-Infinity"));
fail();
} catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo(
"Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
}
e = assertThrows(MalformedJsonException.class, () -> strategy.readNumber(fromString("NaN")));
assertThat(e).hasMessageThat().isEqualTo(
"Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
e = assertThrows(MalformedJsonException.class, () -> strategy.readNumber(fromString("Infinity")));
assertThat(e).hasMessageThat().isEqualTo(
"Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
e = assertThrows(MalformedJsonException.class, () -> strategy.readNumber(fromString("-Infinity")));
assertThat(e).hasMessageThat().isEqualTo(
"Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
}
@Test
@ -111,44 +91,27 @@ public class ToNumberPolicyTest {
assertThat(strategy.readNumber(fromString("3.141592653589793238462643383279"))).isEqualTo(new BigDecimal("3.141592653589793238462643383279"));
assertThat(strategy.readNumber(fromString("1e400"))).isEqualTo(new BigDecimal("1e400"));
try {
strategy.readNumber(fromString("\"not-a-number\""));
fail();
} catch (JsonParseException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Cannot parse not-a-number; at path $");
}
JsonParseException e = assertThrows(JsonParseException.class, () -> strategy.readNumber(fromString("\"not-a-number\"")));
assertThat(e).hasMessageThat().isEqualTo("Cannot parse not-a-number; at path $");
}
@Test
public void testNullsAreNeverExpected() throws IOException {
try {
ToNumberPolicy.DOUBLE.readNumber(fromString("null"));
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected a double but was NULL at line 1 column 5 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
}
try {
ToNumberPolicy.LAZILY_PARSED_NUMBER.readNumber(fromString("null"));
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
}
try {
ToNumberPolicy.LONG_OR_DOUBLE.readNumber(fromString("null"));
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
}
try {
ToNumberPolicy.BIG_DECIMAL.readNumber(fromString("null"));
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
}
IllegalStateException e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.DOUBLE.readNumber(fromString("null")));
assertThat(e).hasMessageThat().isEqualTo("Expected a double but was NULL at line 1 column 5 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.LAZILY_PARSED_NUMBER.readNumber(fromString("null")));
assertThat(e).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.LONG_OR_DOUBLE.readNumber(fromString("null")));
assertThat(e).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.BIG_DECIMAL.readNumber(fromString("null")));
assertThat(e).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
}
private static JsonReader fromString(String json) {

View File

@ -17,7 +17,7 @@
package com.google.gson;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
@ -59,19 +59,11 @@ public class TypeAdapterTest {
}
};
try {
adapter.toJson(1);
fail();
} catch (JsonIOException e) {
assertThat(e.getCause()).isEqualTo(exception);
}
JsonIOException e = assertThrows(JsonIOException.class, () -> adapter.toJson(1));
assertThat(e).hasCauseThat().isEqualTo(exception);
try {
adapter.toJsonTree(1);
fail();
} catch (JsonIOException e) {
assertThat(e.getCause()).isEqualTo(exception);
}
e = assertThrows(JsonIOException.class, () -> adapter.toJsonTree(1));
assertThat(e).hasCauseThat().isEqualTo(exception);
}
private static final TypeAdapter<String> adapter = new TypeAdapter<String>() {