From 5a2668150a5aa2b1a5f8e6e80cbde8edf97b1c1b Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Wed, 5 Jan 2022 22:15:54 +0100 Subject: [PATCH] Switch to JUnit 5 POM has been configured to compile and run unit tests with Java 11 (because JUnit 5 requires at least Java 8). However, IDEs might not support different Java versions for main code and test code. Another issue is that the proto module uses Google Truth which has JUnit 4 as transitive dependency. --- UserGuide.md | 8 +- codegen/pom.xml | 4 +- .../functional/CodeGenFunctionalTest.java | 7 +- extras/pom.xml | 4 +- .../gson/graph/GraphAdapterBuilderTest.java | 30 +- .../gson/interceptors/InterceptorTest.java | 43 +- .../PostConstructAdapterFactoryTest.java | 18 +- .../RuntimeTypeAdapterFactoryTest.java | 52 ++- .../typeadapters/UtcDateTypeAdapterTest.java | 33 +- gson/pom.xml | 5 +- .../com/google/gson/internal/JavaVersion.java | 2 +- .../java/com/google/gson/CommentsTest.java | 9 +- .../DefaultInetAddressTypeAdapterTest.java | 24 +- .../gson/DefaultMapJsonSerializerTest.java | 15 +- ...ExposeAnnotationExclusionStrategyTest.java | 27 +- .../com/google/gson/FieldAttributesTest.java | 36 +- .../google/gson/FieldNamingPolicyTest.java | 26 +- .../com/google/gson/GenericArrayTypeTest.java | 27 +- .../java/com/google/gson/GsonBuilderTest.java | 75 ++-- .../test/java/com/google/gson/GsonTest.java | 25 +- .../com/google/gson/GsonTypeAdapterTest.java | 32 +- .../gson/InnerClassExclusionStrategyTest.java | 21 +- .../google/gson/JavaSerializationTest.java | 15 +- .../java/com/google/gson/JsonArrayTest.java | 53 +-- .../java/com/google/gson/JsonNullTest.java | 12 +- .../java/com/google/gson/JsonObjectTest.java | 57 ++- .../java/com/google/gson/JsonParserTest.java | 44 +- .../com/google/gson/JsonPrimitiveTest.java | 129 +++--- .../com/google/gson/JsonStreamParserTest.java | 26 +- .../gson/LongSerializationPolicyTest.java | 26 +- .../java/com/google/gson/MixedStreamTest.java | 45 +- .../google/gson/ObjectTypeAdapterTest.java | 21 +- .../gson/OverrideCoreTypeAdaptersTest.java | 15 +- .../gson/ParameterizedTypeFixtures.java | 1 - .../google/gson/ParameterizedTypeTest.java | 22 +- .../com/google/gson/ToNumberPolicyTest.java | 28 +- .../gson/VersionExclusionStrategyTest.java | 16 +- .../com/google/gson/common/MoreAsserts.java | 17 +- .../com/google/gson/common/TestTypes.java | 5 +- .../com/google/gson/functional/ArrayTest.java | 99 +++-- .../functional/CircularReferenceTest.java | 40 +- .../gson/functional/CollectionTest.java | 138 +++--- .../gson/functional/ConcurrencyTest.java | 45 +- .../functional/CustomDeserializerTest.java | 38 +- .../gson/functional/CustomSerializerTest.java | 25 +- .../functional/CustomTypeAdaptersTest.java | 94 ++-- .../functional/DefaultTypeAdaptersTest.java | 194 +++++--- .../functional/DelegateTypeAdapterTest.java | 24 +- .../com/google/gson/functional/EnumTest.java | 57 ++- .../functional/EnumWithObfuscatedTest.java | 19 +- .../google/gson/functional/EscapingTest.java | 29 +- .../ExclusionStrategyFunctionalTest.java | 42 +- .../gson/functional/ExposeFieldsTest.java | 57 ++- .../gson/functional/FieldExclusionTest.java | 22 +- .../gson/functional/FieldNamingTest.java | 23 +- .../GsonVersionDiagnosticsTest.java | 25 +- .../gson/functional/InheritanceTest.java | 72 +-- .../gson/functional/InstanceCreatorTest.java | 36 +- .../google/gson/functional/InterfaceTest.java | 27 +- .../functional/InternationalizationTest.java | 28 +- .../JavaUtilConcurrentAtomicTest.java | 42 +- .../google/gson/functional/JavaUtilTest.java | 24 +- .../JsonAdapterAnnotationOnClassesTest.java | 40 +- .../JsonAdapterAnnotationOnFieldsTest.java | 44 +- ...JsonAdapterSerializerDeserializerTest.java | 18 +- .../google/gson/functional/JsonArrayTest.java | 31 +- .../gson/functional/JsonParserTest.java | 39 +- .../google/gson/functional/JsonTreeTest.java | 29 +- .../google/gson/functional/LeniencyTest.java | 18 +- .../functional/MapAsArrayTypeAdapterTest.java | 27 +- .../com/google/gson/functional/MapTest.java | 189 +++++--- .../MoreSpecificTypeSerializationTest.java | 43 +- .../gson/functional/NamingPolicyTest.java | 69 +-- .../functional/NullObjectAndFieldTest.java | 91 ++-- .../google/gson/functional/ObjectTest.java | 167 ++++--- .../functional/ParameterizedTypesTest.java | 76 ++-- .../gson/functional/PrettyPrintingTest.java | 51 ++- .../functional/PrimitiveCharacterTest.java | 17 +- .../google/gson/functional/PrimitiveTest.java | 322 +++++++++----- .../gson/functional/PrintFormattingTest.java | 24 +- .../gson/functional/RawSerializationTest.java | 31 +- .../gson/functional/ReadersWritersTest.java | 49 ++- .../gson/functional/ReflectionAccessTest.java | 19 +- .../ReusedTypeVariablesFullyResolveTest.java | 18 +- ...ntimeTypeAdapterFactoryFunctionalTest.java | 17 +- .../google/gson/functional/SecurityTest.java | 45 +- .../gson/functional/SerializedNameTest.java | 16 +- .../functional/StreamingTypeAdaptersTest.java | 68 ++- .../google/gson/functional/StringTest.java | 67 ++- .../ToNumberPolicyFunctionalTest.java | 40 +- .../gson/functional/TreeTypeAdaptersTest.java | 30 +- .../functional/TypeAdapterPrecedenceTest.java | 30 +- .../functional/TypeHierarchyAdapterTest.java | 15 +- .../gson/functional/TypeVariableTest.java | 27 +- .../gson/functional/UncategorizedTest.java | 37 +- .../gson/functional/VersioningTest.java | 46 +- .../gson/internal/GsonBuildConfigTest.java | 8 +- .../google/gson/internal/GsonTypesTest.java | 17 +- .../google/gson/internal/JavaVersionTest.java | 25 +- .../gson/internal/LazilyParsedNumberTest.java | 17 +- .../gson/internal/LinkedTreeMapTest.java | 50 ++- .../UnsafeAllocatorInstantiationTest.java | 22 +- .../bind/DefaultDateTypeAdapterTest.java | 58 ++- .../internal/bind/JsonElementReaderTest.java | 62 ++- .../internal/bind/JsonTreeReaderTest.java | 12 +- .../internal/bind/JsonTreeWriterTest.java | 58 ++- .../bind/RecursiveTypesResolveTest.java | 36 +- .../internal/bind/util/ISO8601UtilsTest.java | 36 +- .../gson/internal/sql/SqlTypesGsonTest.java | 55 ++- .../internal/sql/SqlTypesSupportTest.java | 10 +- .../google/gson/metrics/PerformanceTest.java | 181 ++++---- .../google/gson/reflect/TypeTokenTest.java | 26 +- .../regression/JsonAdapterNullSafeTest.java | 13 +- .../com/google/gson/regression/OSGiTest.java | 16 +- .../gson/stream/JsonReaderPathTest.java | 71 +-- .../google/gson/stream/JsonReaderTest.java | 414 ++++++++++++------ .../google/gson/stream/JsonWriterTest.java | 123 ++++-- .../test/resources/testcases-proguard.conf | 4 +- pom.xml | 41 +- proto/pom.xml | 4 +- .../functional/ProtosWithAnnotationsTest.java | 25 +- ...rotosWithComplexAndRepeatedFieldsTest.java | 25 +- .../ProtosWithPrimitiveTypesTest.java | 29 +- 123 files changed, 3462 insertions(+), 2081 deletions(-) diff --git a/UserGuide.md b/UserGuide.md index 331ef27663..429dc23ca7 100644 --- a/UserGuide.md +++ b/UserGuide.md @@ -53,13 +53,13 @@ Gson can work with arbitrary Java objects including pre-existing objects that yo Here are some metrics that we obtained on a desktop (dual opteron, 8GB RAM, 64-bit Ubuntu) running lots of other things along-with the tests. You can rerun these tests by using the class [`PerformanceTest`](gson/src/test/java/com/google/gson/metrics/PerformanceTest.java). -* Strings: Deserialized strings of over 25MB without any problems (see `disabled_testStringDeserializationPerformance` method in `PerformanceTest`) +* Strings: Deserialized strings of over 25MB without any problems (see `testStringDeserialization` method in `PerformanceTest`) * Large collections: - * Serialized a collection of 1.4 million objects (see `disabled_testLargeCollectionSerialization` method in `PerformanceTest`) - * Deserialized a collection of 87,000 objects (see `disabled_testLargeCollectionDeserialization` in `PerformanceTest`) + * Serialized a collection of 1.4 million objects (see `testLargeCollectionSerialization` method in `PerformanceTest`) + * Deserialized a collection of 87,000 objects (see `testLargeCollectionDeserialization` in `PerformanceTest`) * Gson 1.4 raised the deserialization limit for byte arrays and collection to over 11MB from 80KB. -Note: Delete the `disabled_` prefix to run these tests. We use this prefix to prevent running these tests every time we run JUnit tests. +Note: Remove the `@Disabled` annotations to run these tests. They are disabled by default to not run them every time we run JUnit tests. ## Gson Users diff --git a/codegen/pom.xml b/codegen/pom.xml index 4d232e3a92..8178ad9695 100644 --- a/codegen/pom.xml +++ b/codegen/pom.xml @@ -25,8 +25,8 @@ - junit - junit + org.junit.jupiter + junit-jupiter test diff --git a/codegen/src/test/java/com/google/gson/codegen/functional/CodeGenFunctionalTest.java b/codegen/src/test/java/com/google/gson/codegen/functional/CodeGenFunctionalTest.java index 855ee3faf5..1f66c50a27 100644 --- a/codegen/src/test/java/com/google/gson/codegen/functional/CodeGenFunctionalTest.java +++ b/codegen/src/test/java/com/google/gson/codegen/functional/CodeGenFunctionalTest.java @@ -15,11 +15,12 @@ */ package com.google.gson.codegen.functional; -import junit.framework.TestCase; +import org.junit.jupiter.api.Test; -public class CodeGenFunctionalTest extends TestCase { +class CodeGenFunctionalTest { - public void testGeneratedJson() { + @Test + void testGeneratedJson() { Order order = new Order("toy", 10); // TODO: figure out how to access the generated type adapter } diff --git a/extras/pom.xml b/extras/pom.xml index 0f83b14230..066c79511e 100644 --- a/extras/pom.xml +++ b/extras/pom.xml @@ -35,8 +35,8 @@ 1.0 - junit - junit + org.junit.jupiter + junit-jupiter test diff --git a/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java b/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java index 43fc6b6991..fa424dc943 100644 --- a/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java +++ b/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java @@ -16,23 +16,21 @@ package com.google.gson.graph; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.junit.jupiter.api.Test; -import org.junit.Test; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; - -public final class GraphAdapterBuilderTest { +class GraphAdapterBuilderTest { @Test - public void testSerialization() { + void testSerialization() { Roshambo rock = new Roshambo("ROCK"); Roshambo scissors = new Roshambo("SCISSORS"); Roshambo paper = new Roshambo("PAPER"); @@ -53,7 +51,7 @@ public void testSerialization() { } @Test - public void testDeserialization() { + void testDeserialization() { String json = "{'0x1':{'name':'ROCK','beats':'0x2'}," + "'0x2':{'name':'SCISSORS','beats':'0x3'}," + "'0x3':{'name':'PAPER','beats':'0x1'}}"; @@ -74,7 +72,7 @@ public void testDeserialization() { } @Test - public void testDeserializationDirectSelfReference() { + void testDeserializationDirectSelfReference() { String json = "{'0x1':{'name':'SUICIDE','beats':'0x1'}}"; GsonBuilder gsonBuilder = new GsonBuilder(); @@ -89,7 +87,7 @@ public void testDeserializationDirectSelfReference() { } @Test - public void testSerializeListOfLists() { + void testSerializeListOfLists() { Type listOfListsType = new TypeToken>>() {}.getType(); Type listOfAnyType = new TypeToken>() {}.getType(); @@ -109,7 +107,7 @@ public void testSerializeListOfLists() { } @Test - public void testDeserializeListOfLists() { + void testDeserializeListOfLists() { Type listOfAnyType = new TypeToken>() {}.getType(); Type listOfListsType = new TypeToken>>() {}.getType(); @@ -127,7 +125,7 @@ public void testDeserializeListOfLists() { } @Test - public void testSerializationWithMultipleTypes() { + void testSerializationWithMultipleTypes() { Company google = new Company("Google"); new Employee("Jesse", google); new Employee("Joel", google); @@ -146,7 +144,7 @@ public void testSerializationWithMultipleTypes() { } @Test - public void testDeserializationWithMultipleTypes() { + void testDeserializationWithMultipleTypes() { GsonBuilder gsonBuilder = new GsonBuilder(); new GraphAdapterBuilder() .addType(Company.class) diff --git a/extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java b/extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java index 0aab6598c3..1bf6958812 100644 --- a/extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java +++ b/extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java @@ -15,6 +15,9 @@ */ package com.google.gson.interceptors; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonParseException; @@ -29,51 +32,56 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import junit.framework.TestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * Unit tests for {@link Intercept} and {@link JsonPostDeserializer}. * * @author Inderjeet Singh */ -public final class InterceptorTest extends TestCase { +class InterceptorTest { private Gson gson; - @Override - public void setUp() throws Exception { - super.setUp(); + @BeforeEach + void setUp() throws Exception { this.gson = new GsonBuilder() .registerTypeAdapterFactory(new InterceptorFactory()) .enableComplexMapKeySerialization() .create(); } - public void testExceptionsPropagated() { + @Test + void testExceptionsPropagated() { try { gson.fromJson("{}", User.class); fail(); } catch (JsonParseException expected) {} } - public void testTopLevelClass() { + @Test + void testTopLevelClass() { User user = gson.fromJson("{name:'bob',password:'pwd'}", User.class); assertEquals(User.DEFAULT_EMAIL, user.email); } - public void testList() { + @Test + void testList() { List list = gson.fromJson("[{name:'bob',password:'pwd'}]", new TypeToken>(){}.getType()); User user = list.get(0); assertEquals(User.DEFAULT_EMAIL, user.email); } - public void testCollection() { + @Test + void testCollection() { Collection list = gson.fromJson("[{name:'bob',password:'pwd'}]", new TypeToken>(){}.getType()); User user = list.iterator().next(); assertEquals(User.DEFAULT_EMAIL, user.email); } - public void testMapKeyAndValues() { + @Test + void testMapKeyAndValues() { Type mapType = new TypeToken>(){}.getType(); try { gson.fromJson("[[{name:'bob',password:'pwd'},{}]]", mapType); @@ -86,12 +94,14 @@ public void testMapKeyAndValues() { assertEquals(Address.DEFAULT_FIRST_LINE, entry.getValue().firstLine); } - public void testField() { + @Test + void testField() { UserGroup userGroup = gson.fromJson("{user:{name:'bob',password:'pwd'}}", UserGroup.class); assertEquals(User.DEFAULT_EMAIL, userGroup.user.email); } - public void testCustomTypeAdapter() { + @Test + void testCustomTypeAdapter() { Gson gson = new GsonBuilder() .registerTypeAdapter(User.class, new TypeAdapter() { @Override public void write(JsonWriter out, User value) throws IOException { @@ -114,7 +124,8 @@ public void testCustomTypeAdapter() { assertEquals(User.DEFAULT_EMAIL, userGroup.user.email); } - public void testDirectInvocationOfTypeAdapter() throws Exception { + @Test + void testDirectInvocationOfTypeAdapter() throws Exception { TypeAdapter adapter = gson.getAdapter(UserGroup.class); UserGroup userGroup = adapter.fromJson("{\"user\":{\"name\":\"bob\",\"password\":\"pwd\"}}"); assertEquals(User.DEFAULT_EMAIL, userGroup.user.email); @@ -140,7 +151,8 @@ public User(String name, String password) { } } - public static final class UserValidator implements JsonPostDeserializer { + static final class UserValidator implements JsonPostDeserializer { + @Override public void postDeserialize(User user) { if (user.name == null || user.password == null) { throw new JsonSyntaxException("name and password are required fields."); @@ -160,7 +172,8 @@ private static final class Address { String zip; } - public static final class AddressValidator implements JsonPostDeserializer
{ + static final class AddressValidator implements JsonPostDeserializer
{ + @Override public void postDeserialize(Address address) { if (address.city == null || address.state == null || address.zip == null) { throw new JsonSyntaxException("Address city, state and zip are required fields."); diff --git a/extras/src/test/java/com/google/gson/typeadapters/PostConstructAdapterFactoryTest.java b/extras/src/test/java/com/google/gson/typeadapters/PostConstructAdapterFactoryTest.java index 7bd0a520d9..7bae1bbcfd 100644 --- a/extras/src/test/java/com/google/gson/typeadapters/PostConstructAdapterFactoryTest.java +++ b/extras/src/test/java/com/google/gson/typeadapters/PostConstructAdapterFactoryTest.java @@ -16,18 +16,19 @@ package com.google.gson.typeadapters; -import javax.annotation.PostConstruct; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; import com.google.gson.Gson; import com.google.gson.GsonBuilder; - -import junit.framework.TestCase; - import java.util.Arrays; import java.util.List; +import javax.annotation.PostConstruct; +import org.junit.jupiter.api.Test; -public class PostConstructAdapterFactoryTest extends TestCase { - public void test() throws Exception { +class PostConstructAdapterFactoryTest { + @Test + void test() throws Exception { Gson gson = new GsonBuilder() .registerTypeAdapterFactory(new PostConstructAdapterFactory()) .create(); @@ -40,7 +41,8 @@ public void test() throws Exception { } } - public void testList() { + @Test + void testList() { MultipleSandwiches sandwiches = new MultipleSandwiches(Arrays.asList( new Sandwich("white", "cheddar"), new Sandwich("whole wheat", "swiss"))); @@ -70,6 +72,7 @@ public Sandwich(String bread, String cheese) { } } + @Override public boolean equals(Object o) { if (o == this) { return true; @@ -95,6 +98,7 @@ public MultipleSandwiches(List sandwiches) { this.sandwiches = sandwiches; } + @Override public boolean equals(Object o) { if (o == this) { return true; diff --git a/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java b/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java index 8c62bef7f4..af23bba9cc 100644 --- a/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java +++ b/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java @@ -16,15 +16,21 @@ package com.google.gson.typeadapters; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonParseException; import com.google.gson.TypeAdapterFactory; -import junit.framework.TestCase; +import org.junit.jupiter.api.Test; -public final class RuntimeTypeAdapterFactoryTest extends TestCase { +class RuntimeTypeAdapterFactoryTest { - public void testRuntimeTypeAdapter() { + @Test + void testRuntimeTypeAdapter() { RuntimeTypeAdapterFactory rta = RuntimeTypeAdapterFactory.of( BillingInstrument.class) .registerSubtype(CreditCard.class); @@ -41,7 +47,8 @@ public void testRuntimeTypeAdapter() { assertTrue(deserialized instanceof CreditCard); } - public void testRuntimeTypeIsBaseType() { + @Test + void testRuntimeTypeIsBaseType() { TypeAdapterFactory rta = RuntimeTypeAdapterFactory.of( BillingInstrument.class) .registerSubtype(BillingInstrument.class); @@ -57,7 +64,8 @@ public void testRuntimeTypeIsBaseType() { assertEquals("Jesse", deserialized.ownerName); } - public void testNullBaseType() { + @Test + void testNullBaseType() { try { RuntimeTypeAdapterFactory.of(null); fail(); @@ -65,7 +73,8 @@ public void testNullBaseType() { } } - public void testNullTypeFieldName() { + @Test + void testNullTypeFieldName() { try { RuntimeTypeAdapterFactory.of(BillingInstrument.class, null); fail(); @@ -73,7 +82,8 @@ public void testNullTypeFieldName() { } } - public void testNullSubtype() { + @Test + void testNullSubtype() { RuntimeTypeAdapterFactory rta = RuntimeTypeAdapterFactory.of( BillingInstrument.class); try { @@ -83,7 +93,8 @@ public void testNullSubtype() { } } - public void testNullLabel() { + @Test + void testNullLabel() { RuntimeTypeAdapterFactory rta = RuntimeTypeAdapterFactory.of( BillingInstrument.class); try { @@ -93,7 +104,8 @@ public void testNullLabel() { } } - public void testDuplicateSubtype() { + @Test + void testDuplicateSubtype() { RuntimeTypeAdapterFactory rta = RuntimeTypeAdapterFactory.of( BillingInstrument.class); rta.registerSubtype(CreditCard.class, "CC"); @@ -104,7 +116,8 @@ public void testDuplicateSubtype() { } } - public void testDuplicateLabel() { + @Test + void testDuplicateLabel() { RuntimeTypeAdapterFactory rta = RuntimeTypeAdapterFactory.of( BillingInstrument.class); rta.registerSubtype(CreditCard.class, "CC"); @@ -115,7 +128,8 @@ public void testDuplicateLabel() { } } - public void testDeserializeMissingTypeField() { + @Test + void testDeserializeMissingTypeField() { TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class) .registerSubtype(CreditCard.class); Gson gson = new GsonBuilder() @@ -128,7 +142,8 @@ public void testDeserializeMissingTypeField() { } } - public void testDeserializeMissingSubtype() { + @Test + void testDeserializeMissingSubtype() { TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class) .registerSubtype(BankTransfer.class); Gson gson = new GsonBuilder() @@ -141,7 +156,8 @@ public void testDeserializeMissingSubtype() { } } - public void testSerializeMissingSubtype() { + @Test + void testSerializeMissingSubtype() { TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class) .registerSubtype(BankTransfer.class); Gson gson = new GsonBuilder() @@ -154,7 +170,8 @@ public void testSerializeMissingSubtype() { } } - public void testSerializeCollidingTypeFieldName() { + @Test + void testSerializeCollidingTypeFieldName() { TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class, "cvv") .registerSubtype(CreditCard.class); Gson gson = new GsonBuilder() @@ -167,13 +184,14 @@ public void testSerializeCollidingTypeFieldName() { } } - public void testSerializeWrappedNullValue() { + @Test + void testSerializeWrappedNullValue() { TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class) .registerSubtype(CreditCard.class) - .registerSubtype(BankTransfer.class); + .registerSubtype(BankTransfer.class); Gson gson = new GsonBuilder() .registerTypeAdapterFactory(billingAdapter) - .create(); + .create(); String serialized = gson.toJson(new BillingInstrumentWrapper(null), BillingInstrumentWrapper.class); BillingInstrumentWrapper deserialized = gson.fromJson(serialized, BillingInstrumentWrapper.class); assertNull(deserialized.instrument); diff --git a/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java b/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java index 56e54290cf..13c8246977 100644 --- a/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java +++ b/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java @@ -16,31 +16,34 @@ package com.google.gson.typeadapters; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; +import org.junit.jupiter.api.Test; -import com.google.gson.JsonParseException; -import junit.framework.TestCase; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -public final class UtcDateTypeAdapterTest extends TestCase { +class UtcDateTypeAdapterTest { private final Gson gson = new GsonBuilder() .registerTypeAdapter(Date.class, new UtcDateTypeAdapter()) .create(); - public void testLocalTimeZone() { + @Test + void testLocalTimeZone() { Date expected = new Date(); String json = gson.toJson(expected); Date actual = gson.fromJson(json, Date.class); assertEquals(expected.getTime(), actual.getTime()); } - public void testDifferentTimeZones() { + @Test + void testDifferentTimeZones() { for (String timeZone : TimeZone.getAvailableIDs()) { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timeZone)); Date expected = cal.getTime(); @@ -55,14 +58,16 @@ public void testDifferentTimeZones() { * JDK 1.7 introduced support for XXX format to indicate UTC date. But Android is older JDK. * We want to make sure that this date is parseable in Android. */ - public void testUtcDatesOnJdkBefore1_7() { + @Test + void testUtcDatesOnJdkBefore1_7() { Gson gson = new GsonBuilder() .registerTypeAdapter(Date.class, new UtcDateTypeAdapter()) .create(); gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class); } - public void testUtcWithJdk7Default() { + @Test + void testUtcWithJdk7Default() { Date expected = new Date(); SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US); iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); @@ -73,12 +78,14 @@ public void testUtcWithJdk7Default() { assertEquals(expected.getTime(), actual.getTime()); } - public void testNullDateSerialization() { + @Test + void testNullDateSerialization() { String json = gson.toJson(null, Date.class); assertEquals("null", json); } - public void testWellFormedParseException() { + @Test + void testWellFormedParseException() { try { gson.fromJson("2017-06-20T14:32:30", Date.class); fail("No exception"); diff --git a/gson/pom.xml b/gson/pom.xml index c1be6a920b..d78588b575 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -21,8 +21,8 @@ - junit - junit + org.junit.jupiter + junit-jupiter test @@ -50,7 +50,6 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M5 + 11 @@ -53,9 +55,9 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter + 5.8.2 test @@ -68,13 +70,41 @@ org.apache.maven.plugins maven-compiler-plugin 3.8.1 + + + + default-compile + + ${javaVersion} + + [11,) + + + + + + default-testCompile + + ${testJavaVersion} + + [${testJavaVersion},) + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 - ${javaVersion} - [11,) + [${testJavaVersion},) + org.apache.maven.plugins maven-javadoc-plugin @@ -99,6 +129,7 @@ false + org.apache.maven.plugins maven-jar-plugin diff --git a/proto/pom.xml b/proto/pom.xml index aa13c952a0..fc043c6a56 100644 --- a/proto/pom.xml +++ b/proto/pom.xml @@ -41,8 +41,8 @@ - junit - junit + org.junit.jupiter + junit-jupiter test diff --git a/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithAnnotationsTest.java b/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithAnnotationsTest.java index 0a508f9d6d..3f08c6f6df 100644 --- a/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithAnnotationsTest.java +++ b/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithAnnotationsTest.java @@ -31,21 +31,21 @@ import com.google.gson.protobuf.generated.Bag.ProtoWithAnnotations.InnerMessage.Data; import com.google.gson.protobuf.generated.Bag.ProtoWithAnnotations.InnerMessage.Type; import com.google.protobuf.GeneratedMessageV3; -import junit.framework.TestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * Functional tests for protocol buffers using annotations for field names and enum values. * * @author Emmanuel Cron */ -public class ProtosWithAnnotationsTest extends TestCase { +class ProtosWithAnnotationsTest { private Gson gson; private Gson gsonWithEnumNumbers; private Gson gsonWithLowerHyphen; - @Override - protected void setUp() throws Exception { - super.setUp(); + @BeforeEach + void setUp() throws Exception { ProtoTypeAdapter.Builder protoTypeAdapter = ProtoTypeAdapter.newBuilder() .setEnumSerialization(EnumSerialization.NAME) .addSerializedNameExtension(Annotations.serializedName) @@ -65,7 +65,8 @@ protected void setUp() throws Exception { .create(); } - public void testProtoWithAnnotations_deserialize() { + @Test + void testProtoWithAnnotations_deserialize() { String json = String.format("{ %n" + " \"id\":\"41e5e7fd6065d101b97018a465ffff01\",%n" + " \"expiration_date\":{ %n" @@ -142,7 +143,8 @@ public void testProtoWithAnnotations_deserialize() { + "}]}}"); } - public void testProtoWithAnnotations_deserializeUnknownEnumValue() { + @Test + void testProtoWithAnnotations_deserializeUnknownEnumValue() { String json = String.format("{ %n" + " \"content\":\"UNKNOWN\"%n" + "}"); @@ -150,7 +152,8 @@ public void testProtoWithAnnotations_deserializeUnknownEnumValue() { assertThat(proto.getContent()).isEqualTo(Type.UNKNOWN); } - public void testProtoWithAnnotations_deserializeUnrecognizedEnumValue() { + @Test + void testProtoWithAnnotations_deserializeUnrecognizedEnumValue() { String json = String.format("{ %n" + " \"content\":\"UNRECOGNIZED\"%n" + "}"); @@ -162,7 +165,8 @@ public void testProtoWithAnnotations_deserializeUnrecognizedEnumValue() { } } - public void testProtoWithAnnotations_deserializeWithEnumNumbers() { + @Test + void testProtoWithAnnotations_deserializeWithEnumNumbers() { String json = String.format("{ %n" + " \"content\":\"0\"%n" + "}"); @@ -180,7 +184,8 @@ public void testProtoWithAnnotations_deserializeWithEnumNumbers() { assertThat(rebuilt).isEqualTo("{\"content\":2}"); } - public void testProtoWithAnnotations_serialize() { + @Test + void testProtoWithAnnotations_serialize() { ProtoWithAnnotations proto = ProtoWithAnnotations.newBuilder() .setId("09f3j20839h032y0329hf30932h0nffn") .setOuterMessage(OuterMessage.newBuilder() diff --git a/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithComplexAndRepeatedFieldsTest.java b/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithComplexAndRepeatedFieldsTest.java index 8e59d5d1e9..a3e80bfaa3 100644 --- a/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithComplexAndRepeatedFieldsTest.java +++ b/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithComplexAndRepeatedFieldsTest.java @@ -15,6 +15,9 @@ */ package com.google.gson.protobuf.functional; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import com.google.common.base.CaseFormat; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -25,20 +28,20 @@ import com.google.gson.protobuf.generated.Bag.ProtoWithRepeatedFields; import com.google.gson.protobuf.generated.Bag.SimpleProto; import com.google.protobuf.GeneratedMessageV3; -import junit.framework.TestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * Functional tests for protocol buffers using complex and repeated fields * * @author Inderjeet Singh */ -public class ProtosWithComplexAndRepeatedFieldsTest extends TestCase { +class ProtosWithComplexAndRepeatedFieldsTest { private Gson gson; private Gson upperCamelGson; - @Override - protected void setUp() throws Exception { - super.setUp(); + @BeforeEach + void setUp() throws Exception { gson = new GsonBuilder() .registerTypeHierarchyAdapter(GeneratedMessageV3.class, @@ -56,7 +59,8 @@ protected void setUp() throws Exception { .create(); } - public void testSerializeRepeatedFields() { + @Test + void testSerializeRepeatedFields() { ProtoWithRepeatedFields proto = ProtoWithRepeatedFields.newBuilder() .addNumbers(2) .addNumbers(3) @@ -69,7 +73,8 @@ public void testSerializeRepeatedFields() { assertTrue(json.contains("count")); } - public void testDeserializeRepeatedFieldsProto() { + @Test + void testDeserializeRepeatedFieldsProto() { String json = "{numbers:[4,6],simples:[{msg:'bar'},{count:7}]}"; ProtoWithRepeatedFields proto = gson.fromJson(json, ProtoWithRepeatedFields.class); @@ -79,7 +84,8 @@ public void testDeserializeRepeatedFieldsProto() { assertEquals(7, proto.getSimples(1).getCount()); } - public void testSerializeDifferentCaseFormat() { + @Test + void testSerializeDifferentCaseFormat() { final ProtoWithDifferentCaseFormat proto = ProtoWithDifferentCaseFormat.newBuilder() .setAnotherField("foo") @@ -90,7 +96,8 @@ public void testSerializeDifferentCaseFormat() { assertEquals("bar", json.get("NameThatTestsCaseFormat").getAsJsonArray().get(0).getAsString()); } - public void testDeserializeDifferentCaseFormat() { + @Test + void testDeserializeDifferentCaseFormat() { final String json = "{NameThatTestsCaseFormat:['bar'],AnotherField:'foo'}"; ProtoWithDifferentCaseFormat proto = upperCamelGson.fromJson(json, ProtoWithDifferentCaseFormat.class); diff --git a/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithPrimitiveTypesTest.java b/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithPrimitiveTypesTest.java index 2e9d0e1733..72c880ea88 100644 --- a/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithPrimitiveTypesTest.java +++ b/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithPrimitiveTypesTest.java @@ -15,6 +15,10 @@ */ package com.google.gson.protobuf.functional; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.protobuf.ProtoTypeAdapter; @@ -22,14 +26,14 @@ import com.google.gson.protobuf.generated.Bag.SimpleProto; import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.GeneratedMessageV3; -import junit.framework.TestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -public class ProtosWithPrimitiveTypesTest extends TestCase { +class ProtosWithPrimitiveTypesTest { private Gson gson; - @Override - protected void setUp() throws Exception { - super.setUp(); + @BeforeEach + void setUp() throws Exception { gson = new GsonBuilder().registerTypeHierarchyAdapter( GeneratedMessageV3.class, ProtoTypeAdapter.newBuilder() .setEnumSerialization(EnumSerialization.NUMBER) @@ -37,19 +41,22 @@ protected void setUp() throws Exception { .create(); } - public void testSerializeEmptyProto() { + @Test + void testSerializeEmptyProto() { SimpleProto proto = SimpleProto.newBuilder().build(); String json = gson.toJson(proto); assertEquals("{}", json); } - public void testDeserializeEmptyProto() { + @Test + void testDeserializeEmptyProto() { SimpleProto proto = gson.fromJson("{}", SimpleProto.class); assertFalse(proto.hasCount()); assertFalse(proto.hasMsg()); } - public void testSerializeProto() { + @Test + void testSerializeProto() { Descriptor descriptor = SimpleProto.getDescriptor(); SimpleProto proto = SimpleProto.newBuilder() .setCount(3) @@ -60,13 +67,15 @@ public void testSerializeProto() { assertTrue(json.contains("\"count\":3")); } - public void testDeserializeProto() { + @Test + void testDeserializeProto() { SimpleProto proto = gson.fromJson("{msg:'foo',count:3}", SimpleProto.class); assertEquals("foo", proto.getMsg()); assertEquals(3, proto.getCount()); } - public void testDeserializeWithExplicitNullValue() { + @Test + void testDeserializeWithExplicitNullValue() { SimpleProto proto = gson.fromJson("{msg:'foo',count:null}", SimpleProto.class); assertEquals("foo", proto.getMsg()); assertEquals(0, proto.getCount());