Skip to content

Commit

Permalink
Port tests from JUnit 3 to JUnit 4 (#2294)
Browse files Browse the repository at this point in the history
* Port tests from JUnit 3 to JUnit 4

* Port tests from JUnit 3 to JUnit 4

* Add `@Test` above `@Ignore`
  • Loading branch information
MaicolAntali authored Dec 22, 2022
1 parent 4aaf138 commit 1a2170b
Show file tree
Hide file tree
Showing 97 changed files with 1,903 additions and 543 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.google.gson.interceptors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
Expand All @@ -29,50 +32,55 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;

/**
* Unit tests for {@link Intercept} and {@link JsonPostDeserializer}.
*
* @author Inderjeet Singh
*/
public final class InterceptorTest extends TestCase {
public final class InterceptorTest {

private Gson gson;

@Override
@Before
public void setUp() throws Exception {
super.setUp();
this.gson = new GsonBuilder()
.registerTypeAdapterFactory(new InterceptorFactory())
.enableComplexMapKeySerialization()
.create();
}

@Test
public void testExceptionsPropagated() {
try {
gson.fromJson("{}", User.class);
fail();
} catch (JsonParseException expected) {}
}

@Test
public void testTopLevelClass() {
User user = gson.fromJson("{name:'bob',password:'pwd'}", User.class);
assertEquals(User.DEFAULT_EMAIL, user.email);
}

@Test
public 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);
}

@Test
public 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);
}

@Test
public void testMapKeyAndValues() {
Type mapType = new TypeToken<Map<User, Address>>(){}.getType();
try {
Expand All @@ -86,11 +94,13 @@ public void testMapKeyAndValues() {
assertEquals(Address.DEFAULT_FIRST_LINE, entry.getValue().firstLine);
}

@Test
public void testField() {
UserGroup userGroup = gson.fromJson("{user:{name:'bob',password:'pwd'}}", UserGroup.class);
assertEquals(User.DEFAULT_EMAIL, userGroup.user.email);
}

@Test
public void testCustomTypeAdapter() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(User.class, new TypeAdapter<User>() {
Expand All @@ -114,6 +124,7 @@ public void testCustomTypeAdapter() {
assertEquals(User.DEFAULT_EMAIL, userGroup.user.email);
}

@Test
public void testDirectInvocationOfTypeAdapter() throws Exception {
TypeAdapter<UserGroup> adapter = gson.getAdapter(UserGroup.class);
UserGroup userGroup = adapter.fromJson("{\"user\":{\"name\":\"bob\",\"password\":\"pwd\"}}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

package com.google.gson.typeadapters;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import junit.framework.TestCase;
import org.junit.Test;

public class PostConstructAdapterFactoryTest extends TestCase {
public void test() throws Exception {
public class PostConstructAdapterFactoryTest {
@Test
public void test() throws Exception {
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new PostConstructAdapterFactory())
.create();
Expand All @@ -37,7 +41,8 @@ public void test() throws Exception {
}
}

public void testList() {
@Test
public void testList() {
MultipleSandwiches sandwiches = new MultipleSandwiches(Arrays.asList(
new Sandwich("white", "cheddar"),
new Sandwich("whole wheat", "swiss")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@

package com.google.gson.typeadapters;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.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.Test;

public final class RuntimeTypeAdapterFactoryTest extends TestCase {
public final class RuntimeTypeAdapterFactoryTest {

@Test
public void testRuntimeTypeAdapter() {
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
BillingInstrument.class)
Expand All @@ -41,6 +47,7 @@ public void testRuntimeTypeAdapter() {
assertTrue(deserialized instanceof CreditCard);
}

@Test
public void testRuntimeTypeAdapterRecognizeSubtypes() {
// We don't have an explicit factory for CreditCard.class, but we do have one for
// BillingInstrument.class that has recognizeSubtypes(). So it should recognize CreditCard, and
Expand All @@ -62,6 +69,7 @@ public void testRuntimeTypeAdapterRecognizeSubtypes() {
assertTrue(deserialized instanceof CreditCard);
}

@Test
public void testRuntimeTypeIsBaseType() {
TypeAdapterFactory rta = RuntimeTypeAdapterFactory.of(
BillingInstrument.class)
Expand All @@ -78,6 +86,7 @@ public void testRuntimeTypeIsBaseType() {
assertEquals("Jesse", deserialized.ownerName);
}

@Test
public void testNullBaseType() {
try {
RuntimeTypeAdapterFactory.of(null);
Expand All @@ -86,6 +95,7 @@ public void testNullBaseType() {
}
}

@Test
public void testNullTypeFieldName() {
try {
RuntimeTypeAdapterFactory.of(BillingInstrument.class, null);
Expand All @@ -94,6 +104,7 @@ public void testNullTypeFieldName() {
}
}

@Test
public void testNullSubtype() {
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
BillingInstrument.class);
Expand All @@ -104,6 +115,7 @@ public void testNullSubtype() {
}
}

@Test
public void testNullLabel() {
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
BillingInstrument.class);
Expand All @@ -114,6 +126,7 @@ public void testNullLabel() {
}
}

@Test
public void testDuplicateSubtype() {
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
BillingInstrument.class);
Expand All @@ -125,6 +138,7 @@ public void testDuplicateSubtype() {
}
}

@Test
public void testDuplicateLabel() {
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
BillingInstrument.class);
Expand All @@ -136,6 +150,7 @@ public void testDuplicateLabel() {
}
}

@Test
public void testDeserializeMissingTypeField() {
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
.registerSubtype(CreditCard.class);
Expand All @@ -149,6 +164,7 @@ public void testDeserializeMissingTypeField() {
}
}

@Test
public void testDeserializeMissingSubtype() {
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
.registerSubtype(BankTransfer.class);
Expand All @@ -162,6 +178,7 @@ public void testDeserializeMissingSubtype() {
}
}

@Test
public void testSerializeMissingSubtype() {
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
.registerSubtype(BankTransfer.class);
Expand All @@ -175,6 +192,7 @@ public void testSerializeMissingSubtype() {
}
}

@Test
public void testSerializeCollidingTypeFieldName() {
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class, "cvv")
.registerSubtype(CreditCard.class);
Expand All @@ -188,6 +206,7 @@ public void testSerializeCollidingTypeFieldName() {
}
}

@Test
public void testSerializeWrappedNullValue() {
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
.registerSubtype(CreditCard.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.google.gson.typeadapters;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
Expand All @@ -24,20 +27,22 @@
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import junit.framework.TestCase;
import org.junit.Test;

public final class UtcDateTypeAdapterTest extends TestCase {
public final class UtcDateTypeAdapterTest {
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
.create();

@Test
public void testLocalTimeZone() {
Date expected = new Date();
String json = gson.toJson(expected);
Date actual = gson.fromJson(json, Date.class);
assertEquals(expected.getTime(), actual.getTime());
}

@Test
public void testDifferentTimeZones() {
for (String timeZone : TimeZone.getAvailableIDs()) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
Expand All @@ -53,13 +58,15 @@ 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.
*/
@Test
public void testUtcDatesOnJdkBefore1_7() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
.create();
gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class);
}

@Test
public void testUtcWithJdk7Default() {
Date expected = new Date();
SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US);
Expand All @@ -71,11 +78,13 @@ public void testUtcWithJdk7Default() {
assertEquals(expected.getTime(), actual.getTime());
}

@Test
public void testNullDateSerialization() {
String json = gson.toJson(null, Date.class);
assertEquals("null", json);
}

@Test
public void testWellFormedParseException() {
try {
gson.fromJson("2017-06-20T14:32:30", Date.class);
Expand Down
7 changes: 5 additions & 2 deletions gson/src/test/java/com/google/gson/CommentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@

package com.google.gson;

import static org.junit.Assert.assertEquals;

import com.google.gson.reflect.TypeToken;
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Test;

/**
* @author Jesse Wilson
*/
public final class CommentsTest extends TestCase {
public final class CommentsTest {

/**
* Test for issue 212.
*/
@Test
public void testParseComments() {
String json = "[\n"
+ " // this is a comment\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@

package com.google.gson;

import java.net.InetAddress;
import static org.junit.Assert.assertEquals;

import junit.framework.TestCase;
import java.net.InetAddress;
import org.junit.Before;
import org.junit.Test;

/**
* Unit tests for the default serializer/deserializer for the {@code InetAddress} type.
*
* @author Joel Leitch
*/
public class DefaultInetAddressTypeAdapterTest extends TestCase {
public class DefaultInetAddressTypeAdapterTest {
private Gson gson;

@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
gson = new Gson();
}

@Test
public void testInetAddressSerializationAndDeserialization() throws Exception {
InetAddress address = InetAddress.getByName("8.8.8.8");
String jsonAddress = gson.toJson(address);
Expand Down
Loading

0 comments on commit 1a2170b

Please sign in to comment.