From 3d241ca0a6435cbf1fa1cdaed2af8480b99fecde Mon Sep 17 00:00:00 2001 From: Wonil Date: Sun, 30 Jul 2023 06:18:45 +0900 Subject: [PATCH] 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 * Update gson/src/test/java/com/google/gson/GsonTest.java Co-authored-by: Marcono1234 * Update gson/src/test/java/com/google/gson/JsonArrayAsListTest.java Co-authored-by: Marcono1234 * Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java Co-authored-by: Marcono1234 * Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java Co-authored-by: Marcono1234 * Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java Co-authored-by: Marcono1234 * Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java Co-authored-by: Marcono1234 * Update gson/src/test/java/com/google/gson/TypeAdapterTest.java Co-authored-by: Marcono1234 * Update gson/src/test/java/com/google/gson/TypeAdapterTest.java Co-authored-by: Marcono1234 * Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java Co-authored-by: Marcono1234 * Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java Co-authored-by: Marcono1234 --------- Co-authored-by: Marcono1234 --- .../test/java/com/google/gson/GsonTest.java | 26 +--- .../com/google/gson/JsonArrayAsListTest.java | 94 +++---------- .../java/com/google/gson/JsonParserTest.java | 13 +- .../com/google/gson/JsonStreamParserTest.java | 43 ++---- .../com/google/gson/ToNumberPolicyTest.java | 131 +++++++----------- .../java/com/google/gson/TypeAdapterTest.java | 18 +-- 6 files changed, 95 insertions(+), 230 deletions(-) diff --git a/gson/src/test/java/com/google/gson/GsonTest.java b/gson/src/test/java/com/google/gson/GsonTest.java index fcdc7cbc54..dfa1e3072a 100644 --- a/gson/src/test/java/com/google/gson/GsonTest.java +++ b/gson/src/test/java/com/google/gson/GsonTest.java @@ -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 @@ private static final class TestTypeAdapter extends TypeAdapter { @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 void testNewJsonWriter_Default() throws IOException { 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 void testNewJsonWriter_Custom() throws IOException { 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(); } diff --git a/gson/src/test/java/com/google/gson/JsonArrayAsListTest.java b/gson/src/test/java/com/google/gson/JsonArrayAsListTest.java index 4998737931..99ffaef76d 100644 --- a/gson/src/test/java/com/google/gson/JsonArrayAsListTest.java +++ b/gson/src/test/java/com/google/gson/JsonArrayAsListTest.java @@ -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 void testGet() { List 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 void testSet() { 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) { - } - - 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"); - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, new JsonPrimitive(1))); + assertThrows(IndexOutOfBoundsException.class, () -> list.set(2, new JsonPrimitive(1))); + + NullPointerException e = assertThrows(NullPointerException.class, () -> list.set(0, null)); + assertThat(e).hasMessageThat().isEqualTo("Element must be non-null"); } @Test @@ -115,30 +93,14 @@ public void testAdd() { ); assertThat(list).isEqualTo(expectedList); - try { - list.set(-1, new JsonPrimitive(1)); - fail(); - } catch (IndexOutOfBoundsException e) { - } - - try { - list.set(list.size(), new JsonPrimitive(1)); - fail(); - } catch (IndexOutOfBoundsException e) { - } - - 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"); - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, new JsonPrimitive(1))); + assertThrows(IndexOutOfBoundsException.class, () -> list.set(list.size(), new JsonPrimitive(1))); + + NullPointerException e = assertThrows(NullPointerException.class, () -> list.add(0, null)); + 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 void testAddAll() { assertThat(list).isEqualTo(expectedList); assertThat(list).isEqualTo(expectedList); - try { - list.addAll(0, Collections.singletonList(null)); - fail(); - } catch (NullPointerException e) { - assertThat(e).hasMessageThat().isEqualTo("Element must be non-null"); - } - try { - list.addAll(Collections.singletonList(null)); - fail(); - } catch (NullPointerException e) { - assertThat(e).hasMessageThat().isEqualTo("Element must be non-null"); - } + NullPointerException e = assertThrows(NullPointerException.class, () -> list.addAll(0, Collections.singletonList(null))); + assertThat(e).hasMessageThat().isEqualTo("Element must be non-null"); + + e = assertThrows(NullPointerException.class, () -> list.addAll(Collections.singletonList(null))); + assertThat(e).hasMessageThat().isEqualTo("Element must be non-null"); } @Test @@ -180,11 +135,8 @@ public void testRemoveIndex() { 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 diff --git a/gson/src/test/java/com/google/gson/JsonParserTest.java b/gson/src/test/java/com/google/gson/JsonParserTest.java index 587ba35dcd..8130625227 100644 --- a/gson/src/test/java/com/google/gson/JsonParserTest.java +++ b/gson/src/test/java/com/google/gson/JsonParserTest.java @@ -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 void testParseUnquotedSingleWordStringFails() { @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 diff --git a/gson/src/test/java/com/google/gson/JsonStreamParserTest.java b/gson/src/test/java/com/google/gson/JsonStreamParserTest.java index 2fec1f1767..807b93f89d 100644 --- a/gson/src/test/java/com/google/gson/JsonStreamParserTest.java +++ b/gson/src/test/java/com/google/gson/JsonStreamParserTest.java @@ -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 void testNoSideEffectForHasNext() { 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); } } diff --git a/gson/src/test/java/com/google/gson/ToNumberPolicyTest.java b/gson/src/test/java/com/google/gson/ToNumberPolicyTest.java index 665b48bf46..4d6c7c41a4 100644 --- a/gson/src/test/java/com/google/gson/ToNumberPolicyTest.java +++ b/gson/src/test/java/com/google/gson/ToNumberPolicyTest.java @@ -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 void testDouble() throws IOException { 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 void testLongOrDouble() throws IOException { 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 void testBigDecimal() throws IOException { 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) { diff --git a/gson/src/test/java/com/google/gson/TypeAdapterTest.java b/gson/src/test/java/com/google/gson/TypeAdapterTest.java index 18af4c4bfa..ea3042eb08 100644 --- a/gson/src/test/java/com/google/gson/TypeAdapterTest.java +++ b/gson/src/test/java/com/google/gson/TypeAdapterTest.java @@ -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 void testToJson_ThrowingIOException() { } }; - 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 adapter = new TypeAdapter() {