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());