Skip to content

Commit

Permalink
Switch to JUnit 5
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Marcono1234 committed Jan 5, 2022
1 parent e0fb02c commit 5a26681
Show file tree
Hide file tree
Showing 123 changed files with 3,462 additions and 2,081 deletions.
8 changes: 4 additions & 4 deletions UserGuide.md
Expand Up @@ -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.

## <a name="TOC-Gson-Users"></a>Gson Users

Expand Down
4 changes: 2 additions & 2 deletions codegen/pom.xml
Expand Up @@ -25,8 +25,8 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions extras/pom.xml
Expand Up @@ -35,8 +35,8 @@
<version>1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Expand Up @@ -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");
Expand All @@ -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'}}";
Expand All @@ -74,7 +72,7 @@ public void testDeserialization() {
}

@Test
public void testDeserializationDirectSelfReference() {
void testDeserializationDirectSelfReference() {
String json = "{'0x1':{'name':'SUICIDE','beats':'0x1'}}";

GsonBuilder gsonBuilder = new GsonBuilder();
Expand All @@ -89,7 +87,7 @@ public void testDeserializationDirectSelfReference() {
}

@Test
public void testSerializeListOfLists() {
void testSerializeListOfLists() {
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();

Expand All @@ -109,7 +107,7 @@ public void testSerializeListOfLists() {
}

@Test
public void testDeserializeListOfLists() {
void testDeserializeListOfLists() {
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();

Expand All @@ -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);
Expand All @@ -146,7 +144,7 @@ public void testSerializationWithMultipleTypes() {
}

@Test
public void testDeserializationWithMultipleTypes() {
void testDeserializationWithMultipleTypes() {
GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder()
.addType(Company.class)
Expand Down
Expand Up @@ -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;
Expand All @@ -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<User> list = gson.fromJson("[{name:'bob',password:'pwd'}]", new TypeToken<List<User>>(){}.getType());
User user = list.get(0);
assertEquals(User.DEFAULT_EMAIL, user.email);
}

public void testCollection() {
@Test
void testCollection() {
Collection<User> list = gson.fromJson("[{name:'bob',password:'pwd'}]", new TypeToken<Collection<User>>(){}.getType());
User user = list.iterator().next();
assertEquals(User.DEFAULT_EMAIL, user.email);
}

public void testMapKeyAndValues() {
@Test
void testMapKeyAndValues() {
Type mapType = new TypeToken<Map<User, Address>>(){}.getType();
try {
gson.fromJson("[[{name:'bob',password:'pwd'},{}]]", mapType);
Expand All @@ -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<User>() {
@Override public void write(JsonWriter out, User value) throws IOException {
Expand All @@ -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<UserGroup> adapter = gson.getAdapter(UserGroup.class);
UserGroup userGroup = adapter.fromJson("{\"user\":{\"name\":\"bob\",\"password\":\"pwd\"}}");
assertEquals(User.DEFAULT_EMAIL, userGroup.user.email);
Expand All @@ -140,7 +151,8 @@ public User(String name, String password) {
}
}

public static final class UserValidator implements JsonPostDeserializer<User> {
static final class UserValidator implements JsonPostDeserializer<User> {
@Override
public void postDeserialize(User user) {
if (user.name == null || user.password == null) {
throw new JsonSyntaxException("name and password are required fields.");
Expand All @@ -160,7 +172,8 @@ private static final class Address {
String zip;
}

public static final class AddressValidator implements JsonPostDeserializer<Address> {
static final class AddressValidator implements JsonPostDeserializer<Address> {
@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.");
Expand Down
Expand Up @@ -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();
Expand All @@ -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")));
Expand Down Expand Up @@ -70,6 +72,7 @@ public Sandwich(String bread, String cheese) {
}
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
Expand All @@ -95,6 +98,7 @@ public MultipleSandwiches(List<Sandwich> sandwiches) {
this.sandwiches = sandwiches;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
Expand Down

0 comments on commit 5a26681

Please sign in to comment.