diff --git a/gson/src/test/java/com/google/gson/ParameterizedTypeFixtures.java b/gson/src/test/java/com/google/gson/ParameterizedTypeFixtures.java index fc49f6a45a..b3fce37c59 100644 --- a/gson/src/test/java/com/google/gson/ParameterizedTypeFixtures.java +++ b/gson/src/test/java/com/google/gson/ParameterizedTypeFixtures.java @@ -102,8 +102,6 @@ public static class MyParameterizedTypeInstanceCreator * This means that the fields of the same objects will be overwritten by Gson. * This is usually fine in tests since there we deserialize just once, but quite * dangerous in practice. - * - * @param instanceOfT Instance of Type */ public MyParameterizedTypeInstanceCreator(T instanceOfT) { this.instanceOfT = instanceOfT; diff --git a/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java b/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java index 02a4a18490..2813032b60 100644 --- a/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java +++ b/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java @@ -18,7 +18,6 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.errorprone.annotations.Immutable; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; diff --git a/gson/src/test/java/com/google/gson/functional/EnumTest.java b/gson/src/test/java/com/google/gson/functional/EnumTest.java index cf0b14e868..0ca4acdf28 100644 --- a/gson/src/test/java/com/google/gson/functional/EnumTest.java +++ b/gson/src/test/java/com/google/gson/functional/EnumTest.java @@ -120,8 +120,9 @@ public String getExpectedJson() { * Test for issue 226. */ @Test + @SuppressWarnings("GetClassOnEnum") public void testEnumSubclass() { - assertThat(Roshambo.ROCK.getDeclaringClass()).isAssignableTo(Roshambo.class); + assertThat(Roshambo.ROCK.getClass()).isNotEqualTo(Roshambo.class); assertThat(gson.toJson(Roshambo.ROCK)).isEqualTo("\"ROCK\""); assertThat(gson.toJson(EnumSet.allOf(Roshambo.class))).isEqualTo("[\"ROCK\",\"PAPER\",\"SCISSORS\"]"); assertThat(gson.fromJson("\"ROCK\"", Roshambo.class)).isEqualTo(Roshambo.ROCK); @@ -131,11 +132,12 @@ public void testEnumSubclass() { } @Test + @SuppressWarnings("GetClassOnEnum") public void testEnumSubclassWithRegisteredTypeAdapter() { gson = new GsonBuilder() .registerTypeHierarchyAdapter(Roshambo.class, new MyEnumTypeAdapter()) .create(); - assertThat(Roshambo.ROCK.getDeclaringClass()).isAssignableTo(Roshambo.class); + assertThat(Roshambo.ROCK.getClass()).isNotEqualTo(Roshambo.class); assertThat(gson.toJson(Roshambo.ROCK)).isEqualTo("\"123ROCK\""); assertThat(gson.toJson(EnumSet.allOf(Roshambo.class))).isEqualTo("[\"123ROCK\",\"123PAPER\",\"123SCISSORS\"]"); assertThat(gson.fromJson("\"123ROCK\"", Roshambo.class)).isEqualTo(Roshambo.ROCK); diff --git a/gson/src/test/java/com/google/gson/functional/GsonVersionDiagnosticsTest.java b/gson/src/test/java/com/google/gson/functional/GsonVersionDiagnosticsTest.java index 937a9065bf..b7029487e8 100644 --- a/gson/src/test/java/com/google/gson/functional/GsonVersionDiagnosticsTest.java +++ b/gson/src/test/java/com/google/gson/functional/GsonVersionDiagnosticsTest.java @@ -16,14 +16,13 @@ package com.google.gson.functional; 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; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; -import java.io.IOException; import java.util.regex.Pattern; import org.junit.Before; import org.junit.Test; @@ -46,7 +45,7 @@ public void setUp() { @Override public void write(JsonWriter out, TestType value) { throw new AssertionError("Expected during serialization"); } - @Override public TestType read(JsonReader in) throws IOException { + @Override public TestType read(JsonReader in) { throw new AssertionError("Expected during deserialization"); } }).create(); @@ -59,25 +58,17 @@ public void testVersionPattern() { } @Test - @SuppressWarnings("AssertionFailureIgnored") public void testAssertionErrorInSerializationPrintsVersion() { - try { - gson.toJson(new TestType()); - fail(); - } catch (AssertionError expected) { - ensureAssertionErrorPrintsGsonVersion(expected); - } + AssertionError e = assertThrows(AssertionError.class, () -> gson.toJson(new TestType())); + ensureAssertionErrorPrintsGsonVersion(e); } @Test - @SuppressWarnings("AssertionFailureIgnored") public void testAssertionErrorInDeserializationPrintsVersion() { - try { - gson.fromJson("{'a':'abc'}", TestType.class); - fail(); - } catch (AssertionError expected) { - ensureAssertionErrorPrintsGsonVersion(expected); - } + AssertionError e = assertThrows(AssertionError.class, + () -> gson.fromJson("{'a':'abc'}", TestType.class)); + + ensureAssertionErrorPrintsGsonVersion(e); } private void ensureAssertionErrorPrintsGsonVersion(AssertionError expected) { diff --git a/gson/src/test/java/com/google/gson/functional/ReflectionAccessTest.java b/gson/src/test/java/com/google/gson/functional/ReflectionAccessTest.java index f41876c94a..756603607b 100644 --- a/gson/src/test/java/com/google/gson/functional/ReflectionAccessTest.java +++ b/gson/src/test/java/com/google/gson/functional/ReflectionAccessTest.java @@ -119,7 +119,6 @@ public Object read(JsonReader in) throws IOException { *

See https://github.com/google/gson/issues/1875 */ @Test - @SuppressWarnings("CatchFail") public void testSerializeInternalImplementationObject() { Gson gson = new Gson(); String json = gson.toJson(Collections.emptyList()); @@ -131,7 +130,7 @@ public void testSerializeInternalImplementationObject() { gson.fromJson("[]", internalClass); fail("Missing exception; test has to be run with `--illegal-access=deny`"); } catch (JsonSyntaxException e) { - fail("Unexpected exception; test has to be run with `--illegal-access=deny`"); + throw new AssertionError("Unexpected exception; test has to be run with `--illegal-access=deny`", e); } catch (JsonIOException expected) { assertThat(expected).hasMessageThat().startsWith("Failed making constructor 'java.util.Collections$EmptyList()' accessible;" + " either increase its visibility or write a custom InstanceCreator or TypeAdapter for its declaring type: "); diff --git a/gson/src/test/java/com/google/gson/functional/StringTest.java b/gson/src/test/java/com/google/gson/functional/StringTest.java index a3d18701e8..b702ef5cfe 100644 --- a/gson/src/test/java/com/google/gson/functional/StringTest.java +++ b/gson/src/test/java/com/google/gson/functional/StringTest.java @@ -149,13 +149,12 @@ public void testAssignmentCharSerialization() { * Created in response to http://groups.google.com/group/google-gson/browse_thread/thread/2431d4a3d0d6cb23 */ @Test - @SuppressWarnings("UnicodeEscape") public void testAssignmentCharDeserialization() { String json = "\"abc=\""; String value = gson.fromJson(json, String.class); assertThat(value).isEqualTo("abc="); - json = "'abc\u003d'"; + json = "'abc\\u003d'"; value = gson.fromJson(json, String.class); assertThat(value).isEqualTo("abc="); } diff --git a/gson/src/test/java/com/google/gson/functional/ToNumberPolicyFunctionalTest.java b/gson/src/test/java/com/google/gson/functional/ToNumberPolicyFunctionalTest.java index adebcb8226..aba9edd5e4 100644 --- a/gson/src/test/java/com/google/gson/functional/ToNumberPolicyFunctionalTest.java +++ b/gson/src/test/java/com/google/gson/functional/ToNumberPolicyFunctionalTest.java @@ -28,9 +28,7 @@ import com.google.gson.stream.JsonReader; import java.lang.reflect.Type; import java.math.BigDecimal; -import java.util.ArrayList; import java.util.Collection; -import java.util.LinkedList; import java.util.List; import org.junit.Test; @@ -104,16 +102,12 @@ public void testAsListOfLongsOrDoubles() { .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) .setNumberToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) .create(); - List expected = new ArrayList<>(); - expected.add(null); - expected.add(10L); - expected.add(10.0); Type objectCollectionType = new TypeToken>() { }.getType(); Collection objects = gson.fromJson("[null,10,10.0]", objectCollectionType); - assertThat(objects).containsExactlyElementsIn(expected); + assertThat(objects).containsExactly(null, 10L, 10.0).inOrder(); Type numberCollectionType = new TypeToken>() { }.getType(); Collection numbers = gson.fromJson("[null,10,10.0]", numberCollectionType); - assertThat(numbers).containsExactlyElementsIn(expected); + assertThat(numbers).containsExactly(null, 10L, 10.0).inOrder(); } @Test diff --git a/gson/src/test/java/com/google/gson/internal/UnsafeAllocatorInstantiationTest.java b/gson/src/test/java/com/google/gson/internal/UnsafeAllocatorInstantiationTest.java index 016deb6478..72f830c6ad 100644 --- a/gson/src/test/java/com/google/gson/internal/UnsafeAllocatorInstantiationTest.java +++ b/gson/src/test/java/com/google/gson/internal/UnsafeAllocatorInstantiationTest.java @@ -17,7 +17,6 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; -import static org.junit.Assert.fail; import org.junit.Test; @@ -41,14 +40,11 @@ public static class ConcreteClass { * to instantiate an interface */ @Test - @SuppressWarnings("AssertionFailureIgnored") - public void testInterfaceInstantiation() throws Exception { - try { - UnsafeAllocator.INSTANCE.newInstance(Interface.class); - fail(); - } catch (AssertionError e) { - assertThat(e).hasMessageThat().startsWith("UnsafeAllocator is used for non-instantiable type"); - } + public void testInterfaceInstantiation() { + AssertionError e = assertThrows(AssertionError.class, + () -> UnsafeAllocator.INSTANCE.newInstance(Interface.class)); + + assertThat(e).hasMessageThat().startsWith("UnsafeAllocator is used for non-instantiable type"); } /** @@ -56,14 +52,11 @@ public void testInterfaceInstantiation() throws Exception { * to instantiate an abstract class */ @Test - @SuppressWarnings("AssertionFailureIgnored") - public void testAbstractClassInstantiation() throws Exception { - try { - UnsafeAllocator.INSTANCE.newInstance(AbstractClass.class); - fail(); - } catch (AssertionError e) { - assertThat(e).hasMessageThat().startsWith("UnsafeAllocator is used for non-instantiable type"); - } + public void testAbstractClassInstantiation() { + AssertionError e = assertThrows(AssertionError.class, + () -> UnsafeAllocator.INSTANCE.newInstance(AbstractClass.class)); + + assertThat(e).hasMessageThat().startsWith("UnsafeAllocator is used for non-instantiable type"); } /** diff --git a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java index 9fdb514a1e..8ebe20b571 100644 --- a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java +++ b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java @@ -642,7 +642,7 @@ private void assertNotANumber(String s) throws IOException { strictReader.nextDouble(); fail("Should have failed reading " + s + " as double"); } catch (MalformedJsonException e) { - // OK: Should fail because `s` can't be read as double + assertThat(e).hasMessageThat().startsWith("Use JsonReader.setLenient(true) to accept malformed JSON"); } } @@ -786,9 +786,8 @@ public void testPeekMuchLargerThanLongMinValue() throws IOException { } @Test - @SuppressWarnings("UnicodeEscape") public void testQuotedNumberWithEscape() throws IOException { - JsonReader reader = new JsonReader(reader("[\"12\u00334\"]")); + JsonReader reader = new JsonReader(reader("[\"12\\u00334\"]")); reader.setLenient(true); reader.beginArray(); assertThat(reader.peek()).isEqualTo(STRING);