Port tests from JUnit 3 to JUnit 4 (#2294)
* Port tests from JUnit 3 to JUnit 4 * Port tests from JUnit 3 to JUnit 4 * Add `@Test` above `@Ignore`
This commit is contained in:
parent
4aaf1385db
commit
1a2170b99c
|
@ -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;
|
||||
|
@ -29,26 +32,27 @@ import java.util.Collection;
|
|||
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);
|
||||
|
@ -56,23 +60,27 @@ public final class InterceptorTest extends TestCase {
|
|||
} 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 {
|
||||
|
@ -86,11 +94,13 @@ public final class InterceptorTest extends TestCase {
|
|||
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>() {
|
||||
|
@ -114,6 +124,7 @@ public final class InterceptorTest extends TestCase {
|
|||
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\"}}");
|
||||
|
|
|
@ -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();
|
||||
|
@ -37,7 +41,8 @@ public class PostConstructAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testList() {
|
||||
@Test
|
||||
public void testList() {
|
||||
MultipleSandwiches sandwiches = new MultipleSandwiches(Arrays.asList(
|
||||
new Sandwich("white", "cheddar"),
|
||||
new Sandwich("whole wheat", "swiss")));
|
||||
|
|
|
@ -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)
|
||||
|
@ -41,6 +47,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
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
|
||||
|
@ -62,6 +69,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
assertTrue(deserialized instanceof CreditCard);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuntimeTypeIsBaseType() {
|
||||
TypeAdapterFactory rta = RuntimeTypeAdapterFactory.of(
|
||||
BillingInstrument.class)
|
||||
|
@ -78,6 +86,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
assertEquals("Jesse", deserialized.ownerName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBaseType() {
|
||||
try {
|
||||
RuntimeTypeAdapterFactory.of(null);
|
||||
|
@ -86,6 +95,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullTypeFieldName() {
|
||||
try {
|
||||
RuntimeTypeAdapterFactory.of(BillingInstrument.class, null);
|
||||
|
@ -94,6 +104,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSubtype() {
|
||||
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
|
||||
BillingInstrument.class);
|
||||
|
@ -104,6 +115,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullLabel() {
|
||||
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
|
||||
BillingInstrument.class);
|
||||
|
@ -114,6 +126,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDuplicateSubtype() {
|
||||
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
|
||||
BillingInstrument.class);
|
||||
|
@ -125,6 +138,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDuplicateLabel() {
|
||||
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
|
||||
BillingInstrument.class);
|
||||
|
@ -136,6 +150,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeMissingTypeField() {
|
||||
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
|
||||
.registerSubtype(CreditCard.class);
|
||||
|
@ -149,6 +164,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeMissingSubtype() {
|
||||
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
|
||||
.registerSubtype(BankTransfer.class);
|
||||
|
@ -162,6 +178,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeMissingSubtype() {
|
||||
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
|
||||
.registerSubtype(BankTransfer.class);
|
||||
|
@ -175,6 +192,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeCollidingTypeFieldName() {
|
||||
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class, "cvv")
|
||||
.registerSubtype(CreditCard.class);
|
||||
|
@ -188,6 +206,7 @@ public final class RuntimeTypeAdapterFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeWrappedNullValue() {
|
||||
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
|
||||
.registerSubtype(CreditCard.class)
|
||||
|
|
|
@ -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;
|
||||
|
@ -24,13 +27,14 @@ import java.util.Calendar;
|
|||
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);
|
||||
|
@ -38,6 +42,7 @@ public final class UtcDateTypeAdapterTest extends TestCase {
|
|||
assertEquals(expected.getTime(), actual.getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferentTimeZones() {
|
||||
for (String timeZone : TimeZone.getAvailableIDs()) {
|
||||
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
|
||||
|
@ -53,6 +58,7 @@ public final class UtcDateTypeAdapterTest extends TestCase {
|
|||
* 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())
|
||||
|
@ -60,6 +66,7 @@ public final class UtcDateTypeAdapterTest extends TestCase {
|
|||
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);
|
||||
|
@ -71,11 +78,13 @@ public final class UtcDateTypeAdapterTest extends TestCase {
|
|||
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);
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -16,11 +16,13 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for the default JSON map serialization object located in the
|
||||
|
@ -28,9 +30,10 @@ import junit.framework.TestCase;
|
|||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class DefaultMapJsonSerializerTest extends TestCase {
|
||||
public class DefaultMapJsonSerializerTest {
|
||||
private Gson gson = new Gson();
|
||||
|
||||
@Test
|
||||
public void testEmptyMapNoTypeSerialization() {
|
||||
Map<String, String> emptyMap = new HashMap<>();
|
||||
JsonElement element = gson.toJsonTree(emptyMap, emptyMap.getClass());
|
||||
|
@ -39,6 +42,7 @@ public class DefaultMapJsonSerializerTest extends TestCase {
|
|||
assertTrue(emptyMapJsonObject.entrySet().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyMapSerialization() {
|
||||
Type mapType = new TypeToken<Map<String, String>>() { }.getType();
|
||||
Map<String, String> emptyMap = new HashMap<>();
|
||||
|
@ -49,6 +53,7 @@ public class DefaultMapJsonSerializerTest extends TestCase {
|
|||
assertTrue(emptyMapJsonObject.entrySet().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonEmptyMapSerialization() {
|
||||
Type mapType = new TypeToken<Map<String, String>>() { }.getType();
|
||||
Map<String, String> myMap = new HashMap<>();
|
||||
|
|
|
@ -16,50 +16,57 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
||||
import com.google.gson.internal.Excluder;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for GsonBuilder.REQUIRE_EXPOSE_DESERIALIZE.
|
||||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class ExposeAnnotationExclusionStrategyTest extends TestCase {
|
||||
public class ExposeAnnotationExclusionStrategyTest {
|
||||
private Excluder excluder = Excluder.DEFAULT.excludeFieldsWithoutExposeAnnotation();
|
||||
|
||||
@Test
|
||||
public void testNeverSkipClasses() throws Exception {
|
||||
assertFalse(excluder.excludeClass(MockObject.class, true));
|
||||
assertFalse(excluder.excludeClass(MockObject.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipNonAnnotatedFields() throws Exception {
|
||||
Field f = createFieldAttributes("hiddenField");
|
||||
assertTrue(excluder.excludeField(f, true));
|
||||
assertTrue(excluder.excludeField(f, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipExplicitlySkippedFields() throws Exception {
|
||||
Field f = createFieldAttributes("explicitlyHiddenField");
|
||||
assertTrue(excluder.excludeField(f, true));
|
||||
assertTrue(excluder.excludeField(f, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNeverSkipExposedAnnotatedFields() throws Exception {
|
||||
Field f = createFieldAttributes("exposedField");
|
||||
assertFalse(excluder.excludeField(f, true));
|
||||
assertFalse(excluder.excludeField(f, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNeverSkipExplicitlyExposedAnnotatedFields() throws Exception {
|
||||
Field f = createFieldAttributes("explicitlyExposedField");
|
||||
assertFalse(excluder.excludeField(f, true));
|
||||
assertFalse(excluder.excludeField(f, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferentSerializeAndDeserializeField() throws Exception {
|
||||
Field f = createFieldAttributes("explicitlyDifferentModeField");
|
||||
assertFalse(excluder.excludeField(f, true));
|
||||
|
|
|
@ -16,11 +16,17 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link FieldAttributes} class.
|
||||
|
@ -28,16 +34,16 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class FieldAttributesTest extends TestCase {
|
||||
public class FieldAttributesTest {
|
||||
private FieldAttributes fieldAttributes;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fieldAttributes = new FieldAttributes(Foo.class.getField("bar"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Test
|
||||
public void testNullField() throws Exception {
|
||||
try {
|
||||
new FieldAttributes(null);
|
||||
|
@ -45,10 +51,12 @@ public class FieldAttributesTest extends TestCase {
|
|||
} catch (NullPointerException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeclaringClass() throws Exception {
|
||||
assertEquals(Foo.class, fieldAttributes.getDeclaringClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testModifiers() throws Exception {
|
||||
assertFalse(fieldAttributes.hasModifier(Modifier.STATIC));
|
||||
assertFalse(fieldAttributes.hasModifier(Modifier.FINAL));
|
||||
|
@ -60,10 +68,12 @@ public class FieldAttributesTest extends TestCase {
|
|||
assertTrue(fieldAttributes.hasModifier(Modifier.TRANSIENT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testName() throws Exception {
|
||||
assertEquals("bar", fieldAttributes.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeclaredTypeAndClass() throws Exception {
|
||||
Type expectedType = new TypeToken<List<String>>() {}.getType();
|
||||
assertEquals(expectedType, fieldAttributes.getDeclaredType());
|
||||
|
|
|
@ -16,14 +16,16 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import com.google.gson.internal.$Gson$Types;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@code GenericArrayType}s created by the {@link $Gson$Types} class.
|
||||
|
@ -31,15 +33,15 @@ import java.util.List;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class GenericArrayTypeTest extends TestCase {
|
||||
public class GenericArrayTypeTest {
|
||||
private GenericArrayType ourType;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
ourType = $Gson$Types.arrayOf($Gson$Types.newParameterizedTypeWithOwner(null, List.class, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOurTypeFunctionality() throws Exception {
|
||||
Type parameterizedType = new TypeToken<List<String>>() {}.getType();
|
||||
Type genericArrayType = new TypeToken<List<String>[]>() {}.getType();
|
||||
|
@ -49,6 +51,7 @@ public class GenericArrayTypeTest extends TestCase {
|
|||
assertEquals(genericArrayType.hashCode(), ourType.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals() throws Exception {
|
||||
Type differentGenericArrayType = new TypeToken<List<String>[][]>() {}.getType();
|
||||
assertFalse(differentGenericArrayType.equals(ourType));
|
||||
|
|
|
@ -16,11 +16,16 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigInteger;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Contains numerous tests involving registered type converters with a Gson instance.
|
||||
|
@ -28,18 +33,18 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class GsonTypeAdapterTest extends TestCase {
|
||||
public class GsonTypeAdapterTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapter(AtomicLong.class, new ExceptionTypeAdapter())
|
||||
.registerTypeAdapter(AtomicInteger.class, new AtomicIntegerTypeAdapter())
|
||||
.create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultTypeAdapterThrowsParseException() throws Exception {
|
||||
try {
|
||||
gson.fromJson("{\"abc\":123}", BigInteger.class);
|
||||
|
@ -47,6 +52,7 @@ public class GsonTypeAdapterTest extends TestCase {
|
|||
} catch (JsonParseException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeAdapterThrowsException() throws Exception {
|
||||
try {
|
||||
gson.toJson(new AtomicLong(0));
|
||||
|
@ -65,6 +71,7 @@ public class GsonTypeAdapterTest extends TestCase {
|
|||
assertNull(gson.fromJson(JsonNull.INSTANCE, AtomicLong.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeAdapterProperlyConvertsTypes() throws Exception {
|
||||
int intialValue = 1;
|
||||
AtomicInteger atomicInt = new AtomicInteger(intialValue);
|
||||
|
@ -75,6 +82,7 @@ public class GsonTypeAdapterTest extends TestCase {
|
|||
assertEquals(intialValue, atomicInt.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeAdapterDoesNotAffectNonAdaptedTypes() throws Exception {
|
||||
String expected = "blah";
|
||||
String actual = gson.toJson(expected);
|
||||
|
@ -119,6 +127,7 @@ public class GsonTypeAdapterTest extends TestCase {
|
|||
}
|
||||
|
||||
// https://groups.google.com/d/topic/google-gson/EBmOCa8kJPE/discussion
|
||||
@Test
|
||||
public void testDeserializerForAbstractClass() {
|
||||
Concrete instance = new Concrete();
|
||||
instance.a = "android";
|
||||
|
|
|
@ -16,35 +16,42 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.internal.Excluder;
|
||||
import java.lang.reflect.Field;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for GsonBuilder.EXCLUDE_INNER_CLASSES.
|
||||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class InnerClassExclusionStrategyTest extends TestCase {
|
||||
public class InnerClassExclusionStrategyTest {
|
||||
public InnerClass innerClass = new InnerClass();
|
||||
public StaticNestedClass staticNestedClass = new StaticNestedClass();
|
||||
private Excluder excluder = Excluder.DEFAULT.disableInnerClassSerialization();
|
||||
|
||||
@Test
|
||||
public void testExcludeInnerClassObject() throws Exception {
|
||||
Class<?> clazz = innerClass.getClass();
|
||||
assertTrue(excluder.excludeClass(clazz, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcludeInnerClassField() throws Exception {
|
||||
Field f = getClass().getField("innerClass");
|
||||
assertTrue(excluder.excludeField(f, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncludeStaticNestedClassObject() throws Exception {
|
||||
Class<?> clazz = staticNestedClass.getClass();
|
||||
assertFalse(excluder.excludeClass(clazz, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncludeStaticNestedClassField() throws Exception {
|
||||
Field f = getClass().getField("staticNestedClass");
|
||||
assertFalse(excluder.excludeField(f, true));
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
@ -27,16 +29,17 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Check that Gson doesn't return non-serializable data types.
|
||||
*
|
||||
* @author Jesse Wilson
|
||||
*/
|
||||
public final class JavaSerializationTest extends TestCase {
|
||||
public final class JavaSerializationTest {
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
@Test
|
||||
public void testMapIsSerializable() throws Exception {
|
||||
Type type = new TypeToken<Map<String, Integer>>() {}.getType();
|
||||
Map<String, Integer> map = gson.fromJson("{\"b\":1,\"c\":2,\"a\":3}", type);
|
||||
|
@ -46,6 +49,7 @@ public final class JavaSerializationTest extends TestCase {
|
|||
assertEquals(Arrays.asList("b", "c", "a"), new ArrayList<>(serialized.keySet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListIsSerializable() throws Exception {
|
||||
Type type = new TypeToken<List<String>>() {}.getType();
|
||||
List<String> list = gson.fromJson("[\"a\",\"b\",\"c\"]", type);
|
||||
|
@ -53,13 +57,14 @@ public final class JavaSerializationTest extends TestCase {
|
|||
assertEquals(list, serialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberIsSerializable() throws Exception {
|
||||
Type type = new TypeToken<List<Number>>() {}.getType();
|
||||
List<Number> list = gson.fromJson("[1,3.14,6.673e-11]", type);
|
||||
List<Number> serialized = serializedCopy(list);
|
||||
assertEquals(1.0, serialized.get(0).doubleValue());
|
||||
assertEquals(3.14, serialized.get(1).doubleValue());
|
||||
assertEquals(6.673e-11, serialized.get(2).doubleValue());
|
||||
assertEquals(1.0, serialized.get(0).doubleValue(), 0);
|
||||
assertEquals(3.14, serialized.get(1).doubleValue(), 0);
|
||||
assertEquals(6.673e-11, serialized.get(2).doubleValue(), 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // Serialization promises to return the same type.
|
||||
|
|
|
@ -16,21 +16,25 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import com.google.gson.common.MoreAsserts;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Jesse Wilson
|
||||
*/
|
||||
public final class JsonNullTest extends TestCase {
|
||||
public final class JsonNullTest {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void testEqualsAndHashcode() {
|
||||
MoreAsserts.assertEqualsAndHashCode(new JsonNull(), new JsonNull());
|
||||
MoreAsserts.assertEqualsAndHashCode(new JsonNull(), JsonNull.INSTANCE);
|
||||
MoreAsserts.assertEqualsAndHashCode(JsonNull.INSTANCE, JsonNull.INSTANCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeepCopy() {
|
||||
@SuppressWarnings("deprecation")
|
||||
JsonNull a = new JsonNull();
|
||||
|
|
|
@ -16,23 +16,27 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import java.io.CharArrayReader;
|
||||
import java.io.CharArrayWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import java.io.CharArrayReader;
|
||||
import java.io.CharArrayWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for {@link JsonParser}
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class JsonParserTest extends TestCase {
|
||||
public class JsonParserTest {
|
||||
|
||||
@Test
|
||||
public void testParseInvalidJson() {
|
||||
try {
|
||||
JsonParser.parseString("[[]");
|
||||
|
@ -40,6 +44,7 @@ public class JsonParserTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseUnquotedStringArrayFails() {
|
||||
JsonElement element = JsonParser.parseString("[a,b,c]");
|
||||
assertEquals("a", element.getAsJsonArray().get(0).getAsString());
|
||||
|
@ -48,6 +53,7 @@ public class JsonParserTest extends TestCase {
|
|||
assertEquals(3, element.getAsJsonArray().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseString() {
|
||||
String json = "{a:10,b:'c'}";
|
||||
JsonElement e = JsonParser.parseString(json);
|
||||
|
@ -56,21 +62,25 @@ public class JsonParserTest extends TestCase {
|
|||
assertEquals("c", e.getAsJsonObject().get("b").getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseEmptyString() {
|
||||
JsonElement e = JsonParser.parseString("\" \"");
|
||||
assertTrue(e.isJsonPrimitive());
|
||||
assertEquals(" ", e.getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseEmptyWhitespaceInput() {
|
||||
JsonElement e = JsonParser.parseString(" ");
|
||||
assertTrue(e.isJsonNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseUnquotedSingleWordStringFails() {
|
||||
assertEquals("Test", JsonParser.parseString("Test").getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseUnquotedMultiWordStringFails() {
|
||||
String unquotedSentence = "Test is a test..blah blah";
|
||||
try {
|
||||
|
@ -79,6 +89,7 @@ public class JsonParserTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseMixedArray() {
|
||||
String json = "[{},13,\"stringValue\"]";
|
||||
JsonElement e = JsonParser.parseString(json);
|
||||
|
@ -99,6 +110,7 @@ public class JsonParserTest extends TestCase {
|
|||
}
|
||||
|
||||
/** Deeply nested JSON arrays should not cause {@link StackOverflowError} */
|
||||
@Test
|
||||
public void testParseDeeplyNestedArrays() throws IOException {
|
||||
int times = 10000;
|
||||
// [[[ ... ]]]
|
||||
|
@ -118,6 +130,7 @@ public class JsonParserTest extends TestCase {
|
|||
}
|
||||
|
||||
/** Deeply nested JSON objects should not cause {@link StackOverflowError} */
|
||||
@Test
|
||||
public void testParseDeeplyNestedObjects() throws IOException {
|
||||
int times = 10000;
|
||||
// {"a":{"a": ... {"a":null} ... }}
|
||||
|
@ -138,6 +151,7 @@ public class JsonParserTest extends TestCase {
|
|||
assertEquals(times, actualTimes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseReader() {
|
||||
StringReader reader = new StringReader("{a:10,b:'c'}");
|
||||
JsonElement e = JsonParser.parseReader(reader);
|
||||
|
@ -146,6 +160,7 @@ public class JsonParserTest extends TestCase {
|
|||
assertEquals("c", e.getAsJsonObject().get("b").getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadWriteTwoObjects() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
CharArrayWriter writer = new CharArrayWriter();
|
||||
|
|
|
@ -16,19 +16,26 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.common.MoreAsserts;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for the {@link JsonPrimitive} class.
|
||||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class JsonPrimitiveTest extends TestCase {
|
||||
public class JsonPrimitiveTest {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Test
|
||||
public void testNulls() {
|
||||
try {
|
||||
new JsonPrimitive((Boolean) null);
|
||||
|
@ -52,6 +59,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoolean() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive(Boolean.TRUE);
|
||||
|
||||
|
@ -75,6 +83,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertFalse(json.getAsBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParsingStringAsBoolean() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive("true");
|
||||
|
||||
|
@ -82,6 +91,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertTrue(json.getAsBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParsingStringAsNumber() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive("1");
|
||||
|
||||
|
@ -96,6 +106,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(new BigDecimal("1"), json.getAsBigDecimal());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsNumber_Boolean() {
|
||||
JsonPrimitive json = new JsonPrimitive(true);
|
||||
try {
|
||||
|
@ -107,6 +118,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void testStringsAndChar() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive("abc");
|
||||
assertTrue(json.isString());
|
||||
|
@ -131,6 +143,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExponential() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive("1E+7");
|
||||
|
||||
|
@ -144,6 +157,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
} catch (NumberFormatException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteEqualsShort() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Byte.valueOf((byte)10));
|
||||
JsonPrimitive p2 = new JsonPrimitive(Short.valueOf((short)10));
|
||||
|
@ -151,6 +165,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteEqualsInteger() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Byte.valueOf((byte)10));
|
||||
JsonPrimitive p2 = new JsonPrimitive(Integer.valueOf(10));
|
||||
|
@ -158,6 +173,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteEqualsLong() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Byte.valueOf((byte)10));
|
||||
JsonPrimitive p2 = new JsonPrimitive(Long.valueOf(10L));
|
||||
|
@ -165,6 +181,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteEqualsBigInteger() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Byte.valueOf((byte)10));
|
||||
JsonPrimitive p2 = new JsonPrimitive(new BigInteger("10"));
|
||||
|
@ -172,6 +189,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortEqualsInteger() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Short.valueOf((short)10));
|
||||
JsonPrimitive p2 = new JsonPrimitive(Integer.valueOf(10));
|
||||
|
@ -179,6 +197,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortEqualsLong() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Short.valueOf((short)10));
|
||||
JsonPrimitive p2 = new JsonPrimitive(Long.valueOf(10));
|
||||
|
@ -186,6 +205,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortEqualsBigInteger() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Short.valueOf((short)10));
|
||||
JsonPrimitive p2 = new JsonPrimitive(new BigInteger("10"));
|
||||
|
@ -193,6 +213,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntegerEqualsLong() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Integer.valueOf(10));
|
||||
JsonPrimitive p2 = new JsonPrimitive(Long.valueOf(10L));
|
||||
|
@ -200,6 +221,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntegerEqualsBigInteger() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Integer.valueOf(10));
|
||||
JsonPrimitive p2 = new JsonPrimitive(new BigInteger("10"));
|
||||
|
@ -207,6 +229,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongEqualsBigInteger() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Long.valueOf(10L));
|
||||
JsonPrimitive p2 = new JsonPrimitive(new BigInteger("10"));
|
||||
|
@ -214,6 +237,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatEqualsDouble() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Float.valueOf(10.25F));
|
||||
JsonPrimitive p2 = new JsonPrimitive(Double.valueOf(10.25D));
|
||||
|
@ -221,6 +245,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatEqualsBigDecimal() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Float.valueOf(10.25F));
|
||||
JsonPrimitive p2 = new JsonPrimitive(new BigDecimal("10.25"));
|
||||
|
@ -228,6 +253,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleEqualsBigDecimal() {
|
||||
JsonPrimitive p1 = new JsonPrimitive(Double.valueOf(10.25D));
|
||||
JsonPrimitive p2 = new JsonPrimitive(new BigDecimal("10.25"));
|
||||
|
@ -235,6 +261,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals(p1.hashCode(), p2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidJsonOnToString() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive("Some\nEscaped\nValue");
|
||||
assertEquals("\"Some\\nEscaped\\nValue\"", json.toString());
|
||||
|
@ -243,6 +270,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertEquals("1.333", json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive("A"), new JsonPrimitive("A"));
|
||||
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(true), new JsonPrimitive(true));
|
||||
|
@ -263,6 +291,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertFalse(new JsonPrimitive(0).equals(new JsonPrimitive(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAcrossTypes() {
|
||||
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive("a"), new JsonPrimitive('a'));
|
||||
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(new BigInteger("0")), new JsonPrimitive(0));
|
||||
|
@ -271,6 +300,7 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(Float.NaN), new JsonPrimitive(Double.NaN));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsIntegerAndBigInteger() {
|
||||
JsonPrimitive a = new JsonPrimitive(5L);
|
||||
JsonPrimitive b = new JsonPrimitive(new BigInteger("18446744073709551621")); // 2^64 + 5
|
||||
|
@ -279,12 +309,14 @@ public class JsonPrimitiveTest extends TestCase {
|
|||
assertTrue(a + " equals " + b, a.equals(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsDoesNotEquateStringAndNonStringTypes() {
|
||||
assertFalse(new JsonPrimitive("true").equals(new JsonPrimitive(true)));
|
||||
assertFalse(new JsonPrimitive("0").equals(new JsonPrimitive(0)));
|
||||
assertFalse(new JsonPrimitive("NaN").equals(new JsonPrimitive(Float.NaN)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeepCopy() {
|
||||
JsonPrimitive a = new JsonPrimitive("a");
|
||||
assertSame(a, a.deepCopy()); // Primitives are immutable!
|
||||
|
|
|
@ -16,7 +16,11 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for the {@link LongSerializationPolicy} class.
|
||||
|
@ -24,8 +28,9 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class LongSerializationPolicyTest extends TestCase {
|
||||
public class LongSerializationPolicyTest {
|
||||
|
||||
@Test
|
||||
public void testDefaultLongSerialization() throws Exception {
|
||||
JsonElement element = LongSerializationPolicy.DEFAULT.serialize(1556L);
|
||||
assertTrue(element.isJsonPrimitive());
|
||||
|
@ -36,6 +41,7 @@ public class LongSerializationPolicyTest extends TestCase {
|
|||
assertEquals(1556L, element.getAsLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultLongSerializationIntegration() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setLongSerializationPolicy(LongSerializationPolicy.DEFAULT)
|
||||
|
@ -44,6 +50,7 @@ public class LongSerializationPolicyTest extends TestCase {
|
|||
assertEquals("[1]", gson.toJson(new Long[] { 1L }, Long[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultLongSerializationNull() {
|
||||
LongSerializationPolicy policy = LongSerializationPolicy.DEFAULT;
|
||||
assertTrue(policy.serialize(null).isJsonNull());
|
||||
|
@ -54,6 +61,7 @@ public class LongSerializationPolicyTest extends TestCase {
|
|||
assertEquals("null", gson.toJson(null, Long.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringLongSerialization() throws Exception {
|
||||
JsonElement element = LongSerializationPolicy.STRING.serialize(1556L);
|
||||
assertTrue(element.isJsonPrimitive());
|
||||
|
@ -64,6 +72,7 @@ public class LongSerializationPolicyTest extends TestCase {
|
|||
assertEquals("1556", element.getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringLongSerializationIntegration() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
|
||||
|
@ -72,6 +81,7 @@ public class LongSerializationPolicyTest extends TestCase {
|
|||
assertEquals("[\"1\"]", gson.toJson(new Long[] { 1L }, Long[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringLongSerializationNull() {
|
||||
LongSerializationPolicy policy = LongSerializationPolicy.STRING;
|
||||
assertTrue(policy.serialize(null).isJsonNull());
|
||||
|
|
|
@ -16,6 +16,11 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
@ -25,9 +30,9 @@ import java.io.StringWriter;
|
|||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public final class MixedStreamTest extends TestCase {
|
||||
public final class MixedStreamTest {
|
||||
|
||||
private static final Car BLUE_MUSTANG = new Car("mustang", 0x0000FF);
|
||||
private static final Car BLACK_BMW = new Car("bmw", 0x000000);
|
||||
|
@ -47,6 +52,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
+ " }\n"
|
||||
+ "]";
|
||||
|
||||
@Test
|
||||
public void testWriteMixedStreamed() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
|
@ -62,6 +68,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
assertEquals(CARS_JSON, stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadMixedStreamed() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
StringReader stringReader = new StringReader(CARS_JSON);
|
||||
|
@ -74,6 +81,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
jsonReader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReaderDoesNotMutateState() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON));
|
||||
|
@ -88,6 +96,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
assertTrue(jsonReader.isLenient());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteDoesNotMutateState() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
JsonWriter jsonWriter = new JsonWriter(new StringWriter());
|
||||
|
@ -106,6 +115,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
assertFalse(jsonWriter.isLenient());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadInvalidState() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON));
|
||||
|
@ -118,6 +128,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadClosed() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON));
|
||||
|
@ -129,6 +140,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteInvalidState() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
JsonWriter jsonWriter = new JsonWriter(new StringWriter());
|
||||
|
@ -140,6 +152,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteClosed() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
JsonWriter jsonWriter = new JsonWriter(new StringWriter());
|
||||
|
@ -153,6 +166,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteNulls() {
|
||||
Gson gson = new Gson();
|
||||
try {
|
||||
|
@ -166,6 +180,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
assertEquals("null", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadNulls() {
|
||||
Gson gson = new Gson();
|
||||
try {
|
||||
|
@ -180,6 +195,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteHtmlSafe() {
|
||||
List<String> contents = Arrays.asList("<", ">", "&", "=", "'");
|
||||
Type type = new TypeToken<List<String>>() {}.getType();
|
||||
|
@ -196,6 +212,7 @@ public final class MixedStreamTest extends TestCase {
|
|||
writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteLenient() {
|
||||
List<Double> doubles = Arrays.asList(Double.NaN, Double.NEGATIVE_INFINITY,
|
||||
Double.POSITIVE_INFINITY, -0.0d, 0.5d, 0.0d);
|
||||
|
|
|
@ -16,18 +16,21 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public final class ObjectTypeAdapterTest extends TestCase {
|
||||
public final class ObjectTypeAdapterTest {
|
||||
private final Gson gson = new GsonBuilder().create();
|
||||
private final TypeAdapter<Object> adapter = gson.getAdapter(Object.class);
|
||||
|
||||
@Test
|
||||
public void testDeserialize() throws Exception {
|
||||
Map<?, ?> map = (Map<?, ?>) adapter.fromJson("{\"a\":5,\"b\":[1,2,null],\"c\":{\"x\":\"y\"}}");
|
||||
assertEquals(5.0, map.get("a"));
|
||||
|
@ -36,23 +39,27 @@ public final class ObjectTypeAdapterTest extends TestCase {
|
|||
assertEquals(3, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialize() throws Exception {
|
||||
Object object = new RuntimeType();
|
||||
assertEquals("{'a':5,'b':[1,2,null]}", adapter.toJson(object).replace("\"", "'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeNullValue() throws Exception {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("a", null);
|
||||
assertEquals("{'a':null}", adapter.toJson(map).replace('"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeNullValue() throws Exception {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("a", null);
|
||||
assertEquals(map, adapter.fromJson("{\"a\":null}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeObject() throws Exception {
|
||||
assertEquals("{}", adapter.toJson(new Object()));
|
||||
}
|
||||
|
@ -67,6 +74,7 @@ public final class ObjectTypeAdapterTest extends TestCase {
|
|||
|
||||
/** Deeply nested JSON arrays should not cause {@link StackOverflowError} */
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testDeserializeDeeplyNestedArrays() throws IOException {
|
||||
int times = 10000;
|
||||
// [[[ ... ]]]
|
||||
|
@ -87,6 +95,7 @@ public final class ObjectTypeAdapterTest extends TestCase {
|
|||
|
||||
/** Deeply nested JSON objects should not cause {@link StackOverflowError} */
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testDeserializeDeeplyNestedObjects() throws IOException {
|
||||
int times = 10000;
|
||||
// {"a":{"a": ... {"a":null} ... }}
|
||||
|
|
|
@ -16,16 +16,18 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Jesse Wilson
|
||||
*/
|
||||
public class OverrideCoreTypeAdaptersTest extends TestCase {
|
||||
public class OverrideCoreTypeAdaptersTest {
|
||||
private static final TypeAdapter<Boolean> booleanAsIntAdapter = new TypeAdapter<Boolean>() {
|
||||
@Override public void write(JsonWriter out, Boolean value) throws IOException {
|
||||
out.value(value ? 1 : 0);
|
||||
|
@ -45,6 +47,7 @@ public class OverrideCoreTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
public void testOverrideWrapperBooleanAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Boolean.class, booleanAsIntAdapter)
|
||||
|
@ -56,6 +59,7 @@ public class OverrideCoreTypeAdaptersTest extends TestCase {
|
|||
assertEquals(Boolean.FALSE, gson.fromJson("0", Boolean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverridePrimitiveBooleanAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(boolean.class, booleanAsIntAdapter)
|
||||
|
@ -67,6 +71,7 @@ public class OverrideCoreTypeAdaptersTest extends TestCase {
|
|||
assertEquals("0", gson.toJson(false, boolean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverrideStringAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(String.class, swapCaseStringAdapter)
|
||||
|
|
|
@ -16,14 +16,17 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import com.google.gson.internal.$Gson$Types;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@code ParameterizedType}s created by the {@link $Gson$Types} class.
|
||||
|
@ -31,15 +34,15 @@ import java.util.List;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class ParameterizedTypeTest extends TestCase {
|
||||
public class ParameterizedTypeTest {
|
||||
private ParameterizedType ourType;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
ourType = $Gson$Types.newParameterizedTypeWithOwner(null, List.class, String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOurTypeFunctionality() throws Exception {
|
||||
Type parameterizedType = new TypeToken<List<String>>() {}.getType();
|
||||
assertNull(ourType.getOwnerType());
|
||||
|
@ -49,6 +52,7 @@ public class ParameterizedTypeTest extends TestCase {
|
|||
assertEquals(parameterizedType.hashCode(), ourType.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals() throws Exception {
|
||||
Type differentParameterizedType = new TypeToken<List<Integer>>() {}.getType();
|
||||
assertFalse(differentParameterizedType.equals(ourType));
|
||||
|
|
|
@ -16,15 +16,19 @@
|
|||
|
||||
package com.google.gson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.math.BigDecimal;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.internal.LazilyParsedNumber;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.MalformedJsonException;
|
||||
import junit.framework.TestCase;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.math.BigDecimal;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ToNumberPolicyTest extends TestCase {
|
||||
public class ToNumberPolicyTest {
|
||||
@Test
|
||||
public void testDouble() throws IOException {
|
||||
ToNumberStrategy strategy = ToNumberPolicy.DOUBLE;
|
||||
assertEquals(10.1, strategy.readNumber(fromString("10.1")));
|
||||
|
@ -42,6 +46,7 @@ public class ToNumberPolicyTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazilyParsedNumber() throws IOException {
|
||||
ToNumberStrategy strategy = ToNumberPolicy.LAZILY_PARSED_NUMBER;
|
||||
assertEquals(new LazilyParsedNumber("10.1"), strategy.readNumber(fromString("10.1")));
|
||||
|
@ -49,6 +54,7 @@ public class ToNumberPolicyTest extends TestCase {
|
|||
assertEquals(new LazilyParsedNumber("1e400"), strategy.readNumber(fromString("1e400")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongOrDouble() throws IOException {
|
||||
ToNumberStrategy strategy = ToNumberPolicy.LONG_OR_DOUBLE;
|
||||
assertEquals(10L, strategy.readNumber(fromString("10")));
|
||||
|
@ -90,6 +96,7 @@ public class ToNumberPolicyTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimal() throws IOException {
|
||||
ToNumberStrategy strategy = ToNumberPolicy.BIG_DECIMAL;
|
||||
assertEquals(new BigDecimal("10.1"), strategy.readNumber(fromString("10.1")));
|
||||
|
@ -104,6 +111,7 @@ public class ToNumberPolicyTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullsAreNeverExpected() throws IOException {
|
||||
try {
|
||||
ToNumberPolicy.DOUBLE.readNumber(fromString("null"));
|
||||
|
|
|
@ -17,6 +17,10 @@
|
|||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
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;
|
||||
|
@ -28,33 +32,36 @@ import java.lang.reflect.Type;
|
|||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
/**
|
||||
* Functional tests for Json serialization and deserialization of arrays.
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class ArrayTest extends TestCase {
|
||||
public class ArrayTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelArrayOfIntsSerialization() {
|
||||
int[] target = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
assertEquals("[1,2,3,4,5,6,7,8,9]", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelArrayOfIntsDeserialization() {
|
||||
int[] expected = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
int[] actual = gson.fromJson("[1,2,3,4,5,6,7,8,9]", int[].class);
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidArrayDeserialization() {
|
||||
String json = "[1, 2 3, 4, 5]";
|
||||
try {
|
||||
|
@ -64,11 +71,13 @@ public class ArrayTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyArraySerialization() {
|
||||
int[] target = {};
|
||||
assertEquals("[]", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyArrayDeserialization() {
|
||||
int[] actualObject = gson.fromJson("[]", int[].class);
|
||||
assertTrue(actualObject.length == 0);
|
||||
|
@ -80,6 +89,7 @@ public class ArrayTest extends TestCase {
|
|||
assertTrue(actualObject.length == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullsInArraySerialization() {
|
||||
String[] array = {"foo", null, "bar"};
|
||||
String expected = "[\"foo\",null,\"bar\"]";
|
||||
|
@ -87,6 +97,7 @@ public class ArrayTest extends TestCase {
|
|||
assertEquals(expected, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullsInArrayDeserialization() {
|
||||
String json = "[\"foo\",null,\"bar\"]";
|
||||
String[] expected = {"foo", null, "bar"};
|
||||
|
@ -96,6 +107,7 @@ public class ArrayTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleNullInArraySerialization() {
|
||||
BagOfPrimitives[] array = new BagOfPrimitives[1];
|
||||
array[0] = null;
|
||||
|
@ -103,11 +115,13 @@ public class ArrayTest extends TestCase {
|
|||
assertEquals("[null]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleNullInArrayDeserialization() {
|
||||
BagOfPrimitives[] array = gson.fromJson("[null]", BagOfPrimitives[].class);
|
||||
assertNull(array[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullsInArrayWithSerializeNullPropertySetSerialization() {
|
||||
gson = new GsonBuilder().serializeNulls().create();
|
||||
String[] array = {"foo", null, "bar"};
|
||||
|
@ -116,11 +130,13 @@ public class ArrayTest extends TestCase {
|
|||
assertEquals(expected, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfStringsSerialization() {
|
||||
String[] target = {"Hello", "World"};
|
||||
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfStringsDeserialization() {
|
||||
String json = "[\"Hello\",\"World\"]";
|
||||
String[] target = gson.fromJson(json, String[].class);
|
||||
|
@ -128,12 +144,14 @@ public class ArrayTest extends TestCase {
|
|||
assertEquals("World", target[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleStringArraySerialization() throws Exception {
|
||||
String[] s = { "hello" };
|
||||
String output = gson.toJson(s);
|
||||
assertEquals("[\"hello\"]", output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleStringArrayDeserialization() throws Exception {
|
||||
String json = "[\"hello\"]";
|
||||
String[] arrayType = gson.fromJson(json, String[].class);
|
||||
|
@ -141,6 +159,7 @@ public class ArrayTest extends TestCase {
|
|||
assertEquals("hello", arrayType[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfCollectionSerialization() throws Exception {
|
||||
StringBuilder sb = new StringBuilder("[");
|
||||
int arraySize = 3;
|
||||
|
@ -166,6 +185,7 @@ public class ArrayTest extends TestCase {
|
|||
assertEquals(sb.toString(), json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfCollectionDeserialization() throws Exception {
|
||||
String json = "[[1,2],[3,4]]";
|
||||
Type type = new TypeToken<Collection<Integer>[]>() {}.getType();
|
||||
|
@ -176,6 +196,7 @@ public class ArrayTest extends TestCase {
|
|||
assertArrayEquals(new Integer[] {3, 4}, target[1].toArray(new Integer[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfPrimitivesAsObjectsSerialization() throws Exception {
|
||||
Object[] objs = new Object[] {1, "abc", 0.3f, 5L};
|
||||
String json = gson.toJson(objs);
|
||||
|
@ -184,16 +205,18 @@ public class ArrayTest extends TestCase {
|
|||
assertTrue(json.contains("5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfPrimitivesAsObjectsDeserialization() throws Exception {
|
||||
String json = "[1,'abc',0.3,1.1,5]";
|
||||
Object[] objs = gson.fromJson(json, Object[].class);
|
||||
assertEquals(1, ((Number)objs[0]).intValue());
|
||||
assertEquals("abc", objs[1]);
|
||||
assertEquals(0.3, ((Number)objs[2]).doubleValue());
|
||||
assertEquals(0.3, ((Number)objs[2]).doubleValue(), 0);
|
||||
assertEquals(new BigDecimal("1.1"), new BigDecimal(objs[3].toString()));
|
||||
assertEquals(5, ((Number)objs[4]).shortValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectArrayWithNonPrimitivesSerialization() throws Exception {
|
||||
ClassWithObjects classWithObjects = new ClassWithObjects();
|
||||
BagOfPrimitives bagOfPrimitives = new BagOfPrimitives();
|
||||
|
@ -207,12 +230,14 @@ public class ArrayTest extends TestCase {
|
|||
assertTrue(json.contains(bagOfPrimitivesJson));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfNullSerialization() {
|
||||
Object[] array = {null};
|
||||
String json = gson.toJson(array);
|
||||
assertEquals("[null]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfNullDeserialization() {
|
||||
String[] values = gson.fromJson("[null]", String[].class);
|
||||
assertNull(values[0]);
|
||||
|
@ -221,6 +246,7 @@ public class ArrayTest extends TestCase {
|
|||
/**
|
||||
* Regression tests for Issue 272
|
||||
*/
|
||||
@Test
|
||||
public void testMultidimensionalArraysSerialization() {
|
||||
String[][] items = {
|
||||
{"3m Co", "71.72", "0.02", "0.03", "4/2 12:00am", "Manufacturing"},
|
||||
|
@ -231,11 +257,13 @@ public class ArrayTest extends TestCase {
|
|||
assertTrue(json.contains("Manufacturing\"]]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultidimensionalObjectArraysSerialization() {
|
||||
Object[][] array = {new Object[] { 1, 2 }};
|
||||
assertEquals("[[1,2]]", gson.toJson(array));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultidimensionalPrimitiveArraysSerialization() {
|
||||
int[][] array = {{1, 2}, {3, 4}};
|
||||
assertEquals("[[1,2],[3,4]]", gson.toJson(array));
|
||||
|
@ -244,6 +272,7 @@ public class ArrayTest extends TestCase {
|
|||
/**
|
||||
* Regression test for Issue 205
|
||||
*/
|
||||
@Test
|
||||
public void testMixingTypesInObjectArraySerialization() {
|
||||
Object[] array = {1, 2, new Object[] {"one", "two", 3}};
|
||||
assertEquals("[1,2,[\"one\",\"two\",3]]", gson.toJson(array));
|
||||
|
@ -252,6 +281,7 @@ public class ArrayTest extends TestCase {
|
|||
/**
|
||||
* Regression tests for Issue 272
|
||||
*/
|
||||
@Test
|
||||
public void testMultidimensionalArraysDeserialization() {
|
||||
String json = "[['3m Co','71.72','0.02','0.03','4/2 12:00am','Manufacturing'],"
|
||||
+ "['Alcoa Inc','29.01','0.42','1.47','4/1 12:00am','Manufacturing']]";
|
||||
|
@ -260,6 +290,7 @@ public class ArrayTest extends TestCase {
|
|||
assertEquals("Manufacturing", items[1][5]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultidimensionalPrimitiveArraysDeserialization() {
|
||||
String json = "[[1,2],[3,4]]";
|
||||
int[][] expected = {{1, 2}, {3, 4}};
|
||||
|
@ -267,6 +298,7 @@ public class ArrayTest extends TestCase {
|
|||
}
|
||||
|
||||
/** http://code.google.com/p/google-gson/issues/detail?id=342 */
|
||||
@Test
|
||||
public void testArrayElementsAreArrays() {
|
||||
Object[] stringArrays = {
|
||||
new String[] {"test1", "test2"},
|
||||
|
|
|
@ -15,11 +15,10 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
@ -28,6 +27,11 @@ import com.google.gson.JsonObject;
|
|||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.common.TestTypes.ClassOverridingEquals;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests related to circular reference detection and error reporting.
|
||||
|
@ -35,15 +39,15 @@ import com.google.gson.common.TestTypes.ClassOverridingEquals;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class CircularReferenceTest extends TestCase {
|
||||
public class CircularReferenceTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCircularSerialization() throws Exception {
|
||||
ContainsReferenceToSelfType a = new ContainsReferenceToSelfType();
|
||||
ContainsReferenceToSelfType b = new ContainsReferenceToSelfType();
|
||||
|
@ -56,6 +60,7 @@ public class CircularReferenceTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelfReferenceIgnoredInSerialization() throws Exception {
|
||||
ClassOverridingEquals objA = new ClassOverridingEquals();
|
||||
objA.ref = objA;
|
||||
|
@ -64,6 +69,7 @@ public class CircularReferenceTest extends TestCase {
|
|||
assertFalse(json.contains("ref")); // self-reference is ignored
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelfReferenceArrayFieldSerialization() throws Exception {
|
||||
ClassWithSelfReferenceArray objA = new ClassWithSelfReferenceArray();
|
||||
objA.children = new ClassWithSelfReferenceArray[]{objA};
|
||||
|
@ -75,6 +81,7 @@ public class CircularReferenceTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelfReferenceCustomHandlerSerialization() throws Exception {
|
||||
ClassWithSelfReference obj = new ClassWithSelfReference();
|
||||
obj.child = obj;
|
||||
|
@ -94,6 +101,7 @@ public class CircularReferenceTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedAcyclicGraphSerialization() throws Exception {
|
||||
ContainsReferenceToSelfType a = new ContainsReferenceToSelfType();
|
||||
ContainsReferenceToSelfType b = new ContainsReferenceToSelfType();
|
||||
|
@ -104,6 +112,7 @@ public class CircularReferenceTest extends TestCase {
|
|||
assertNotNull(gson.toJson(a));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedAcyclicGraphDeserialization() throws Exception {
|
||||
String json = "{\"children\":[{\"children\":[{\"children\":[]}]},{\"children\":[]}]}";
|
||||
ContainsReferenceToSelfType target = gson.fromJson(json, ContainsReferenceToSelfType.class);
|
||||
|
|
|
@ -17,6 +17,9 @@
|
|||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
@ -40,7 +43,8 @@ import java.util.Queue;
|
|||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
import java.util.Vector;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for Json serialization and deserialization of collections.
|
||||
|
@ -48,15 +52,15 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class CollectionTest extends TestCase {
|
||||
public class CollectionTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelCollectionOfIntegersSerialization() {
|
||||
Collection<Integer> target = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
Type targetType = new TypeToken<Collection<Integer>>() {}.getType();
|
||||
|
@ -64,6 +68,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("[1,2,3,4,5,6,7,8,9]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelCollectionOfIntegersDeserialization() {
|
||||
String json = "[0,1,2,3,4,5,6,7,8,9]";
|
||||
Type collectionType = new TypeToken<Collection<Integer>>() { }.getType();
|
||||
|
@ -72,6 +77,7 @@ public class CollectionTest extends TestCase {
|
|||
assertArrayEquals(expected, toIntArray(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelListOfIntegerCollectionsDeserialization() throws Exception {
|
||||
String json = "[[1,2,3],[4,5,6],[7,8,9]]";
|
||||
Type collectionType = new TypeToken<Collection<Collection<Integer>>>() {}.getType();
|
||||
|
@ -89,6 +95,7 @@ public class CollectionTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLinkedListSerialization() {
|
||||
List<String> list = new LinkedList<>();
|
||||
list.add("a1");
|
||||
|
@ -99,6 +106,7 @@ public class CollectionTest extends TestCase {
|
|||
assertTrue(json.contains("a2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLinkedListDeserialization() {
|
||||
String json = "['a1','a2']";
|
||||
Type linkedListType = new TypeToken<LinkedList<String>>() {}.getType();
|
||||
|
@ -107,6 +115,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("a2", list.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueueSerialization() {
|
||||
Queue<String> queue = new LinkedList<>();
|
||||
queue.add("a1");
|
||||
|
@ -117,6 +126,7 @@ public class CollectionTest extends TestCase {
|
|||
assertTrue(json.contains("a2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueueDeserialization() {
|
||||
String json = "['a1','a2']";
|
||||
Type queueType = new TypeToken<Queue<String>>() {}.getType();
|
||||
|
@ -126,6 +136,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("a2", queue.element());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPriorityQueue() throws Exception {
|
||||
Type type = new TypeToken<PriorityQueue<Integer>>(){}.getType();
|
||||
PriorityQueue<Integer> queue = gson.fromJson("[10, 20, 22]", type);
|
||||
|
@ -137,6 +148,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("[10,20,22]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVector() {
|
||||
Type type = new TypeToken<Vector<Integer>>(){}.getType();
|
||||
Vector<Integer> target = gson.fromJson("[10, 20, 31]", type);
|
||||
|
@ -148,6 +160,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("[10,20,31]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStack() {
|
||||
Type type = new TypeToken<Stack<Integer>>(){}.getType();
|
||||
Stack<Integer> target = gson.fromJson("[11, 13, 17]", type);
|
||||
|
@ -159,6 +172,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("[11,13,17]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullsInListSerialization() {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("foo");
|
||||
|
@ -170,6 +184,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals(expected, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullsInListDeserialization() {
|
||||
List<String> expected = new ArrayList<>();
|
||||
expected.add("foo");
|
||||
|
@ -183,6 +198,7 @@ public class CollectionTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfObjectSerialization() {
|
||||
List<Object> target = new ArrayList<>();
|
||||
target.add("Hello");
|
||||
|
@ -193,6 +209,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target, type));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfObjectWithNullSerialization() {
|
||||
List<Object> target = new ArrayList<>();
|
||||
target.add("Hello");
|
||||
|
@ -204,6 +221,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("[\"Hello\",null,\"World\"]", gson.toJson(target, type));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfStringsSerialization() {
|
||||
List<String> target = new ArrayList<>();
|
||||
target.add("Hello");
|
||||
|
@ -211,6 +229,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfBagOfPrimitivesSerialization() {
|
||||
List<BagOfPrimitives> target = new ArrayList<>();
|
||||
BagOfPrimitives objA = new BagOfPrimitives(3L, 1, true, "blah");
|
||||
|
@ -226,6 +245,7 @@ public class CollectionTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfStringsDeserialization() {
|
||||
String json = "[\"Hello\",\"World\"]";
|
||||
Type collectionType = new TypeToken<Collection<String>>() { }.getType();
|
||||
|
@ -235,11 +255,13 @@ public class CollectionTest extends TestCase {
|
|||
assertTrue(target.contains("World"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRawCollectionOfIntegersSerialization() {
|
||||
Collection<Integer> target = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
assertEquals("[1,2,3,4,5,6,7,8,9]", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectCollectionSerialization() {
|
||||
BagOfPrimitives bag1 = new BagOfPrimitives();
|
||||
Collection<?> target = Arrays.asList(bag1, bag1, "test");
|
||||
|
@ -247,6 +269,7 @@ public class CollectionTest extends TestCase {
|
|||
assertTrue(json.contains(bag1.getExpectedJson()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRawCollectionDeserializationNotAlllowed() {
|
||||
String json = "[0,1,2,3,4,5,6,7,8,9]";
|
||||
Collection<?> integers = gson.fromJson(json, Collection.class);
|
||||
|
@ -259,6 +282,7 @@ public class CollectionTest extends TestCase {
|
|||
assertTrue(strings.contains("World"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRawCollectionOfBagOfPrimitivesNotAllowed() {
|
||||
BagOfPrimitives bag = new BagOfPrimitives(10, 20, false, "stringValue");
|
||||
String json = '[' + bag.getExpectedJson() + ',' + bag.getExpectedJson() + ']';
|
||||
|
@ -274,6 +298,7 @@ public class CollectionTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWildcardPrimitiveCollectionSerilaization() throws Exception {
|
||||
Collection<? extends Integer> target = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
Type collectionType = new TypeToken<Collection<? extends Integer>>() { }.getType();
|
||||
|
@ -284,6 +309,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals("[1,2,3,4,5,6,7,8,9]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWildcardPrimitiveCollectionDeserilaization() throws Exception {
|
||||
String json = "[1,2,3,4,5,6,7,8,9]";
|
||||
Type collectionType = new TypeToken<Collection<? extends Integer>>() { }.getType();
|
||||
|
@ -293,6 +319,7 @@ public class CollectionTest extends TestCase {
|
|||
assertTrue(target.contains(9));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWildcardCollectionField() throws Exception {
|
||||
Collection<BagOfPrimitives> collection = new ArrayList<>();
|
||||
BagOfPrimitives objA = new BagOfPrimitives(3L, 1, true, "blah");
|
||||
|
@ -312,6 +339,7 @@ public class CollectionTest extends TestCase {
|
|||
assertTrue(deserializedCollection.contains(objB));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldIsArrayList() {
|
||||
HasArrayListField object = new HasArrayListField();
|
||||
object.longs.add(1L);
|
||||
|
@ -322,6 +350,7 @@ public class CollectionTest extends TestCase {
|
|||
assertEquals(Arrays.asList(1L, 3L), copy.longs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserCollectionTypeAdapter() {
|
||||
Type listOfString = new TypeToken<List<String>>() {}.getType();
|
||||
Object stringListSerializer = new JsonSerializer<List<String>>() {
|
||||
|
@ -372,6 +401,7 @@ public class CollectionTest extends TestCase {
|
|||
this.value = value;
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testSetSerialization() {
|
||||
Set<Entry> set = new HashSet<>();
|
||||
set.add(new Entry(1));
|
||||
|
@ -380,6 +410,7 @@ public class CollectionTest extends TestCase {
|
|||
assertTrue(json.contains("1"));
|
||||
assertTrue(json.contains("2"));
|
||||
}
|
||||
@Test
|
||||
public void testSetDeserialization() {
|
||||
String json = "[{value:1},{value:2}]";
|
||||
Type type = new TypeToken<Set<Entry>>() {}.getType();
|
||||
|
@ -394,6 +425,7 @@ public class CollectionTest extends TestCase {
|
|||
|
||||
private class SmallClass { private String inSmall; }
|
||||
|
||||
@Test
|
||||
public void testIssue1107() {
|
||||
String json = "{\n" +
|
||||
" \"inBig\": {\n" +
|
||||
|
|
|
@ -15,14 +15,15 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests for ensuring Gson thread-safety.
|
||||
|
@ -30,12 +31,11 @@ import com.google.gson.Gson;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class ConcurrencyTest extends TestCase {
|
||||
public class ConcurrencyTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
|
@ -43,6 +43,7 @@ public class ConcurrencyTest extends TestCase {
|
|||
* Source-code based on
|
||||
* http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081
|
||||
*/
|
||||
@Test
|
||||
public void testSingleThreadSerialization() {
|
||||
MyObject myObj = new MyObject();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
|
@ -54,6 +55,7 @@ public class ConcurrencyTest extends TestCase {
|
|||
* Source-code based on
|
||||
* http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081
|
||||
*/
|
||||
@Test
|
||||
public void testSingleThreadDeserialization() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class);
|
||||
|
@ -64,6 +66,7 @@ public class ConcurrencyTest extends TestCase {
|
|||
* Source-code based on
|
||||
* http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081
|
||||
*/
|
||||
@Test
|
||||
public void testMultiThreadSerialization() throws InterruptedException {
|
||||
final CountDownLatch startLatch = new CountDownLatch(1);
|
||||
final CountDownLatch finishedLatch = new CountDownLatch(10);
|
||||
|
@ -95,6 +98,7 @@ public class ConcurrencyTest extends TestCase {
|
|||
* Source-code based on
|
||||
* http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081
|
||||
*/
|
||||
@Test
|
||||
public void testMultiThreadDeserialization() throws InterruptedException {
|
||||
final CountDownLatch startLatch = new CountDownLatch(1);
|
||||
final CountDownLatch finishedLatch = new CountDownLatch(10);
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
|
@ -25,10 +28,9 @@ import com.google.gson.JsonObject;
|
|||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.common.TestTypes.Base;
|
||||
import com.google.gson.common.TestTypes.ClassWithBaseField;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional Test exercising custom deserialization only. When test applies to both
|
||||
|
@ -36,18 +38,18 @@ import java.lang.reflect.Type;
|
|||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class CustomDeserializerTest extends TestCase {
|
||||
public class CustomDeserializerTest {
|
||||
private static final String DEFAULT_VALUE = "test123";
|
||||
private static final String SUFFIX = "blah";
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new GsonBuilder().registerTypeAdapter(DataHolder.class, new DataHolderDeserializer()).create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultConstructorNotCalledOnObject() throws Exception {
|
||||
DataHolder data = new DataHolder(DEFAULT_VALUE);
|
||||
String json = gson.toJson(data);
|
||||
|
@ -56,6 +58,7 @@ public class CustomDeserializerTest extends TestCase {
|
|||
assertEquals(DEFAULT_VALUE + SUFFIX, actual.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultConstructorNotCalledOnField() throws Exception {
|
||||
DataHolderWrapper dataWrapper = new DataHolderWrapper(new DataHolder(DEFAULT_VALUE));
|
||||
String json = gson.toJson(dataWrapper);
|
||||
|
@ -110,6 +113,7 @@ public class CustomDeserializerTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonTypeFieldBasedDeserialization() {
|
||||
String json = "{field1:'abc',field2:'def',__type__:'SUB_TYPE1'}";
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(MyBase.class, new JsonDeserializer<MyBase>() {
|
||||
|
@ -148,6 +152,7 @@ public class CustomDeserializerTest extends TestCase {
|
|||
String field2;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomDeserializerReturnsNullForTopLevelObject() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new JsonDeserializer<Base>() {
|
||||
|
@ -162,6 +167,7 @@ public class CustomDeserializerTest extends TestCase {
|
|||
assertNull(target);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomDeserializerReturnsNull() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new JsonDeserializer<Base>() {
|
||||
|
@ -176,6 +182,7 @@ public class CustomDeserializerTest extends TestCase {
|
|||
assertNull(target.base);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomDeserializerReturnsNullForArrayElements() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new JsonDeserializer<Base>() {
|
||||
|
@ -191,6 +198,7 @@ public class CustomDeserializerTest extends TestCase {
|
|||
assertNull(target[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomDeserializerReturnsNullForArrayElementsForArrayField() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new JsonDeserializer<Base>() {
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
|
@ -29,10 +32,8 @@ import com.google.gson.common.TestTypes.ClassWithBaseArrayField;
|
|||
import com.google.gson.common.TestTypes.ClassWithBaseField;
|
||||
import com.google.gson.common.TestTypes.Sub;
|
||||
import com.google.gson.common.TestTypes.SubSerializer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional Test exercising custom serialization only. When test applies to both
|
||||
|
@ -40,9 +41,10 @@ import java.lang.reflect.Type;
|
|||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class CustomSerializerTest extends TestCase {
|
||||
public class CustomSerializerTest {
|
||||
|
||||
public void testBaseClassSerializerInvokedForBaseClassFields() {
|
||||
@Test
|
||||
public void testBaseClassSerializerInvokedForBaseClassFields() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new BaseSerializer())
|
||||
.registerTypeAdapter(Sub.class, new SubSerializer())
|
||||
|
@ -53,7 +55,8 @@ public class CustomSerializerTest extends TestCase {
|
|||
assertEquals(BaseSerializer.NAME, base.get(Base.SERIALIZER_KEY).getAsString());
|
||||
}
|
||||
|
||||
public void testSubClassSerializerInvokedForBaseClassFieldsHoldingSubClassInstances() {
|
||||
@Test
|
||||
public void testSubClassSerializerInvokedForBaseClassFieldsHoldingSubClassInstances() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new BaseSerializer())
|
||||
.registerTypeAdapter(Sub.class, new SubSerializer())
|
||||
|
@ -64,7 +67,8 @@ public class CustomSerializerTest extends TestCase {
|
|||
assertEquals(SubSerializer.NAME, base.get(Base.SERIALIZER_KEY).getAsString());
|
||||
}
|
||||
|
||||
public void testSubClassSerializerInvokedForBaseClassFieldsHoldingArrayOfSubClassInstances() {
|
||||
@Test
|
||||
public void testSubClassSerializerInvokedForBaseClassFieldsHoldingArrayOfSubClassInstances() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new BaseSerializer())
|
||||
.registerTypeAdapter(Sub.class, new SubSerializer())
|
||||
|
@ -78,7 +82,8 @@ public class CustomSerializerTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testBaseClassSerializerInvokedForBaseClassFieldsHoldingSubClassInstances() {
|
||||
@Test
|
||||
public void testBaseClassSerializerInvokedForBaseClassFieldsHoldingSubClassInstances() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new BaseSerializer())
|
||||
.create();
|
||||
|
@ -88,7 +93,8 @@ public class CustomSerializerTest extends TestCase {
|
|||
assertEquals(BaseSerializer.NAME, base.get(Base.SERIALIZER_KEY).getAsString());
|
||||
}
|
||||
|
||||
public void testSerializerReturnsNull() {
|
||||
@Test
|
||||
public void testSerializerReturnsNull() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new JsonSerializer<Base>() {
|
||||
@Override public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
|
|
|
@ -15,6 +15,11 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.InstanceCreator;
|
||||
|
@ -35,7 +40,9 @@ import java.util.HashMap;
|
|||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for the support of custom serializer and deserializers.
|
||||
|
@ -43,15 +50,15 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class CustomTypeAdaptersTest extends TestCase {
|
||||
public class CustomTypeAdaptersTest {
|
||||
private GsonBuilder builder;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
builder = new GsonBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomSerializers() {
|
||||
Gson gson = builder.registerTypeAdapter(
|
||||
ClassWithCustomTypeConverter.class, new JsonSerializer<ClassWithCustomTypeConverter>() {
|
||||
|
@ -67,6 +74,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals("{\"bag\":5,\"value\":25}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomDeserializers() {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(
|
||||
ClassWithCustomTypeConverter.class, new JsonDeserializer<ClassWithCustomTypeConverter>() {
|
||||
|
@ -83,6 +91,8 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals(5, target.getBag().getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disable_testCustomSerializersOfSelf() {
|
||||
Gson gson = createGsonObjectWithFooTypeAdapter();
|
||||
Gson basicGson = new Gson();
|
||||
|
@ -93,6 +103,8 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals(jsonFromGson, jsonFromCustomSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disable_testCustomDeserializersOfSelf() {
|
||||
Gson gson = createGsonObjectWithFooTypeAdapter();
|
||||
Gson basicGson = new Gson();
|
||||
|
@ -104,6 +116,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals(expectedFoo.value, newFooObject.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomNestedSerializers() {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(
|
||||
BagOfPrimitives.class, new JsonSerializer<BagOfPrimitives>() {
|
||||
|
@ -116,6 +129,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals("{\"bag\":6,\"value\":10}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomNestedDeserializers() {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(
|
||||
BagOfPrimitives.class, new JsonDeserializer<BagOfPrimitives>() {
|
||||
|
@ -130,6 +144,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals(7, target.getBag().getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomTypeAdapterDoesNotAppliesToSubClasses() {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(Base.class, new JsonSerializer<Base> () {
|
||||
@Override
|
||||
|
@ -147,6 +162,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertTrue(json.contains("derivedValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomTypeAdapterAppliesToSubClassesSerializedAsBaseClass() {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(Base.class, new JsonSerializer<Base> () {
|
||||
@Override
|
||||
|
@ -206,6 +222,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomSerializerInvokedForPrimitives() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(boolean.class, new JsonSerializer<Boolean>() {
|
||||
|
@ -218,6 +235,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals("true", gson.toJson(true, Boolean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomDeserializerInvokedForPrimitives() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(boolean.class, new JsonDeserializer<Boolean>() {
|
||||
|
@ -231,6 +249,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals(Boolean.TRUE, gson.fromJson("true", Boolean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomByteArraySerializer() {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(byte[].class, new JsonSerializer<byte[]>() {
|
||||
@Override
|
||||
|
@ -247,6 +266,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals("\"0123456789\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomByteArrayDeserializerAndInstanceCreator() {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(byte[].class,
|
||||
new JsonDeserializer<byte[]>() {
|
||||
|
@ -305,6 +325,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
|
||||
// Test created from Issue 70
|
||||
@Test
|
||||
public void testCustomAdapterInvokedForCollectionElementSerializationWithType() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(StringHolder.class, new StringHolderTypeAdapter())
|
||||
|
@ -318,6 +339,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
|
||||
// Test created from Issue 70
|
||||
@Test
|
||||
public void testCustomAdapterInvokedForCollectionElementSerialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(StringHolder.class, new StringHolderTypeAdapter())
|
||||
|
@ -330,6 +352,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
|
||||
// Test created from Issue 70
|
||||
@Test
|
||||
public void testCustomAdapterInvokedForCollectionElementDeserialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(StringHolder.class, new StringHolderTypeAdapter())
|
||||
|
@ -343,6 +366,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
|
||||
// Test created from Issue 70
|
||||
@Test
|
||||
public void testCustomAdapterInvokedForMapElementSerializationWithType() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(StringHolder.class, new StringHolderTypeAdapter())
|
||||
|
@ -356,6 +380,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
|
||||
// Test created from Issue 70
|
||||
@Test
|
||||
public void testCustomAdapterInvokedForMapElementSerialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(StringHolder.class, new StringHolderTypeAdapter())
|
||||
|
@ -368,6 +393,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
|
||||
// Test created from Issue 70
|
||||
@Test
|
||||
public void testCustomAdapterInvokedForMapElementDeserialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(StringHolder.class, new StringHolderTypeAdapter())
|
||||
|
@ -380,6 +406,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals("Tomaw", foo.part2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnsureCustomSerializerNotInvokedForNullValues() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(DataHolder.class, new DataHolderSerializer())
|
||||
|
@ -389,6 +416,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
assertEquals("{\"wrappedData\":{\"myData\":\"abc\"}}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnsureCustomDeserializerNotInvokedForNullValues() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(DataHolder.class, new DataHolderDeserializer())
|
||||
|
@ -399,6 +427,7 @@ public class CustomTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
|
||||
// Test created from Issue 352
|
||||
@Test
|
||||
public void testRegisterHierarchyAdapterForDate() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(Date.class, new DateTypeAdapter())
|
||||
|
|
|
@ -15,6 +15,11 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
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.JsonArray;
|
||||
|
@ -54,7 +59,9 @@ import java.util.Set;
|
|||
import java.util.TimeZone;
|
||||
import java.util.TreeSet;
|
||||
import java.util.UUID;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional test for Json serialization and deserialization for common classes for which default
|
||||
|
@ -63,14 +70,13 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class DefaultTypeAdaptersTest extends TestCase {
|
||||
public class DefaultTypeAdaptersTest {
|
||||
private Gson gson;
|
||||
private TimeZone oldTimeZone;
|
||||
private Locale oldLocale;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.oldTimeZone = TimeZone.getDefault();
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
|
||||
this.oldLocale = Locale.getDefault();
|
||||
|
@ -78,13 +84,13 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
TimeZone.setDefault(oldTimeZone);
|
||||
Locale.setDefault(oldLocale);
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassSerialization() {
|
||||
try {
|
||||
gson.toJson(String.class);
|
||||
|
@ -96,6 +102,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals("\"java.lang.String\"", gson.toJson(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassDeserialization() {
|
||||
try {
|
||||
gson.fromJson("String.class", String.class.getClass());
|
||||
|
@ -107,12 +114,14 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(String.class, gson.fromJson("java.lang.String", Class.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrlSerialization() throws Exception {
|
||||
String urlValue = "http://google.com/";
|
||||
URL url = new URL(urlValue);
|
||||
assertEquals("\"http://google.com/\"", gson.toJson(url));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrlDeserialization() {
|
||||
String urlValue = "http://google.com/";
|
||||
String json = "'http:\\/\\/google.com\\/'";
|
||||
|
@ -123,11 +132,13 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(urlValue, target.toExternalForm());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrlNullSerialization() throws Exception {
|
||||
ClassWithUrlField target = new ClassWithUrlField();
|
||||
assertEquals("{}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrlNullDeserialization() {
|
||||
String json = "{}";
|
||||
ClassWithUrlField target = gson.fromJson(json, ClassWithUrlField.class);
|
||||
|
@ -138,12 +149,14 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
URL url;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUriSerialization() throws Exception {
|
||||
String uriValue = "http://google.com/";
|
||||
URI uri = new URI(uriValue);
|
||||
assertEquals("\"http://google.com/\"", gson.toJson(uri));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUriDeserialization() {
|
||||
String uriValue = "http://google.com/";
|
||||
String json = '"' + uriValue + '"';
|
||||
|
@ -151,6 +164,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(uriValue, target.toASCIIString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSerialization() throws Exception {
|
||||
testNullSerializationAndDeserialization(Boolean.class);
|
||||
testNullSerializationAndDeserialization(Byte.class);
|
||||
|
@ -191,12 +205,14 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(null, gson.fromJson("null", c));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUuidSerialization() throws Exception {
|
||||
String uuidValue = "c237bec1-19ef-4858-a98e-521cf0aad4c0";
|
||||
UUID uuid = UUID.fromString(uuidValue);
|
||||
assertEquals('"' + uuidValue + '"', gson.toJson(uuid));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUuidDeserialization() {
|
||||
String uuidValue = "c237bec1-19ef-4858-a98e-521cf0aad4c0";
|
||||
String json = '"' + uuidValue + '"';
|
||||
|
@ -204,34 +220,40 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(uuidValue, target.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocaleSerializationWithLanguage() {
|
||||
Locale target = new Locale("en");
|
||||
assertEquals("\"en\"", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocaleDeserializationWithLanguage() {
|
||||
String json = "\"en\"";
|
||||
Locale locale = gson.fromJson(json, Locale.class);
|
||||
assertEquals("en", locale.getLanguage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocaleSerializationWithLanguageCountry() {
|
||||
Locale target = Locale.CANADA_FRENCH;
|
||||
assertEquals("\"fr_CA\"", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocaleDeserializationWithLanguageCountry() {
|
||||
String json = "\"fr_CA\"";
|
||||
Locale locale = gson.fromJson(json, Locale.class);
|
||||
assertEquals(Locale.CANADA_FRENCH, locale);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocaleSerializationWithLanguageCountryVariant() {
|
||||
Locale target = new Locale("de", "DE", "EURO");
|
||||
String json = gson.toJson(target);
|
||||
assertEquals("\"de_DE_EURO\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocaleDeserializationWithLanguageCountryVariant() {
|
||||
String json = "\"de_DE_EURO\"";
|
||||
Locale locale = gson.fromJson(json, Locale.class);
|
||||
|
@ -240,6 +262,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals("EURO", locale.getVariant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalFieldSerialization() {
|
||||
ClassWithBigDecimal target = new ClassWithBigDecimal("-122.01e-21");
|
||||
String json = gson.toJson(target);
|
||||
|
@ -247,6 +270,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(target.value, new BigDecimal(actual));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalFieldDeserialization() {
|
||||
ClassWithBigDecimal expected = new ClassWithBigDecimal("-122.01e-21");
|
||||
String json = expected.getExpectedJson();
|
||||
|
@ -254,6 +278,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(expected.value, actual.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadValueForBigDecimalDeserialization() {
|
||||
try {
|
||||
gson.fromJson("{\"value\"=1.5e-1.0031}", ClassWithBigDecimal.class);
|
||||
|
@ -261,12 +286,14 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
} catch (JsonParseException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigIntegerFieldSerialization() {
|
||||
ClassWithBigInteger target = new ClassWithBigInteger("23232323215323234234324324324324324324");
|
||||
String json = gson.toJson(target);
|
||||
assertEquals(target.getExpectedJson(), json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigIntegerFieldDeserialization() {
|
||||
ClassWithBigInteger expected = new ClassWithBigInteger("879697697697697697697697697697697697");
|
||||
String json = expected.getExpectedJson();
|
||||
|
@ -274,6 +301,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(expected.value, actual.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverrideBigIntegerTypeAdapter() throws Exception {
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapter(BigInteger.class, new NumberAsStringAdapter(BigInteger.class))
|
||||
|
@ -282,6 +310,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(new BigInteger("123"), gson.fromJson("\"123\"", BigInteger.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverrideBigDecimalTypeAdapter() throws Exception {
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapter(BigDecimal.class, new NumberAsStringAdapter(BigDecimal.class))
|
||||
|
@ -290,6 +319,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(new BigDecimal("1.1"), gson.fromJson("\"1.1\"", BigDecimal.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSerialization() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
HashSet<String> s = new HashSet<>();
|
||||
|
@ -301,6 +331,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals("[\"blah\"]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBitSetSerialization() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
BitSet bits = new BitSet();
|
||||
|
@ -311,6 +342,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals("[0,1,0,1,1,1,0,0,0,1]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBitSetDeserialization() throws Exception {
|
||||
BitSet expected = new BitSet();
|
||||
expected.set(0);
|
||||
|
@ -345,6 +377,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultDateSerialization() {
|
||||
Date now = new Date(1315806903103L);
|
||||
String json = gson.toJson(now);
|
||||
|
@ -355,6 +388,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultDateDeserialization() {
|
||||
String json = "'Dec 13, 2009 07:18:02 AM'";
|
||||
Date extracted = gson.fromJson(json, Date.class);
|
||||
|
@ -378,6 +412,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(seconds, date.getSeconds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultDateSerializationUsingBuilder() throws Exception {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
Date now = new Date(1315806903103L);
|
||||
|
@ -389,6 +424,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultDateDeserializationUsingBuilder() throws Exception {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
Date now = new Date(1315806903103L);
|
||||
|
@ -397,6 +433,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(now.toString(), extracted.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultCalendarSerialization() throws Exception {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
String json = gson.toJson(Calendar.getInstance());
|
||||
|
@ -408,6 +445,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertTrue(json.contains("second"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultCalendarDeserialization() throws Exception {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
String json = "{year:2009,month:2,dayOfMonth:11,hourOfDay:14,minute:29,second:23}";
|
||||
|
@ -420,6 +458,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(23, cal.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultGregorianCalendarSerialization() throws Exception {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
GregorianCalendar cal = new GregorianCalendar();
|
||||
|
@ -432,6 +471,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertTrue(json.contains("second"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultGregorianCalendarDeserialization() throws Exception {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
String json = "{year:2009,month:2,dayOfMonth:11,hourOfDay:14,minute:29,second:23}";
|
||||
|
@ -444,6 +484,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(23, cal.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateSerializationWithPattern() throws Exception {
|
||||
String pattern = "yyyy-MM-dd";
|
||||
Gson gson = new GsonBuilder().setDateFormat(DateFormat.FULL).setDateFormat(pattern).create();
|
||||
|
@ -453,6 +494,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void testDateDeserializationWithPattern() throws Exception {
|
||||
String pattern = "yyyy-MM-dd";
|
||||
Gson gson = new GsonBuilder().setDateFormat(DateFormat.FULL).setDateFormat(pattern).create();
|
||||
|
@ -464,6 +506,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(now.getDay(), extracted.getDay());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateSerializationWithPatternNotOverridenByTypeAdapter() throws Exception {
|
||||
String pattern = "yyyy-MM-dd";
|
||||
Gson gson = new GsonBuilder()
|
||||
|
@ -483,6 +526,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
|
||||
// http://code.google.com/p/google-gson/issues/detail?id=230
|
||||
@Test
|
||||
public void testDateSerializationInCollection() throws Exception {
|
||||
Type listOfDates = new TypeToken<List<Date>>() {}.getType();
|
||||
TimeZone defaultTimeZone = TimeZone.getDefault();
|
||||
|
@ -501,6 +545,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonPrimitiveSerialization() {
|
||||
assertEquals("5", gson.toJson(new JsonPrimitive(5), JsonElement.class));
|
||||
assertEquals("true", gson.toJson(new JsonPrimitive(true), JsonElement.class));
|
||||
|
@ -508,6 +553,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals("\"a\"", gson.toJson(new JsonPrimitive('a'), JsonElement.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonPrimitiveDeserialization() {
|
||||
assertEquals(new JsonPrimitive(5), gson.fromJson("5", JsonElement.class));
|
||||
assertEquals(new JsonPrimitive(5), gson.fromJson("5", JsonPrimitive.class));
|
||||
|
@ -519,16 +565,19 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(new JsonPrimitive('a'), gson.fromJson("\"a\"", JsonPrimitive.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonNullSerialization() {
|
||||
assertEquals("null", gson.toJson(JsonNull.INSTANCE, JsonElement.class));
|
||||
assertEquals("null", gson.toJson(JsonNull.INSTANCE, JsonNull.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullJsonElementSerialization() {
|
||||
assertEquals("null", gson.toJson(null, JsonElement.class));
|
||||
assertEquals("null", gson.toJson(null, JsonNull.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonArraySerialization() {
|
||||
JsonArray array = new JsonArray();
|
||||
array.add(new JsonPrimitive(1));
|
||||
|
@ -537,6 +586,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals("[1,2,3]", gson.toJson(array, JsonElement.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonArrayDeserialization() {
|
||||
JsonArray array = new JsonArray();
|
||||
array.add(new JsonPrimitive(1));
|
||||
|
@ -548,6 +598,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(array, gson.fromJson(json, JsonArray.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonObjectSerialization() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.add("foo", new JsonPrimitive(1));
|
||||
|
@ -555,6 +606,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals("{\"foo\":1,\"bar\":2}", gson.toJson(object, JsonElement.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonObjectDeserialization() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.add("foo", new JsonPrimitive(1));
|
||||
|
@ -568,11 +620,13 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(object, actualObj);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonNullDeserialization() {
|
||||
assertEquals(JsonNull.INSTANCE, gson.fromJson("null", JsonElement.class));
|
||||
assertEquals(JsonNull.INSTANCE, gson.fromJson("null", JsonNull.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonElementTypeMismatch() {
|
||||
try {
|
||||
gson.fromJson("\"abc\"", JsonObject.class);
|
||||
|
@ -603,6 +657,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesSerialization() {
|
||||
Properties props = new Properties();
|
||||
props.setProperty("foo", "bar");
|
||||
|
@ -611,12 +666,14 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals(expected, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesDeserialization() {
|
||||
String json = "{foo:'bar'}";
|
||||
Properties props = gson.fromJson(json, Properties.class);
|
||||
assertEquals("bar", props.getProperty("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTreeSetSerialization() {
|
||||
TreeSet<String> treeSet = new TreeSet<>();
|
||||
treeSet.add("Value1");
|
||||
|
@ -624,6 +681,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertEquals("[\"Value1\"]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTreeSetDeserialization() {
|
||||
String json = "['Value1']";
|
||||
Type type = new TypeToken<TreeSet<String>>() {}.getType();
|
||||
|
@ -631,23 +689,27 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
|||
assertTrue(treeSet.contains("Value1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringBuilderSerialization() {
|
||||
StringBuilder sb = new StringBuilder("abc");
|
||||
String json = gson.toJson(sb);
|
||||
assertEquals("\"abc\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringBuilderDeserialization() {
|
||||
StringBuilder sb = gson.fromJson("'abc'", StringBuilder.class);
|
||||
assertEquals("abc", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringBufferSerialization() {
|
||||
StringBuffer sb = new StringBuffer("abc");
|
||||
String json = gson.toJson(sb);
|
||||
assertEquals("\"abc\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringBufferDeserialization() {
|
||||
StringBuffer sb = gson.fromJson("'abc'", StringBuffer.class);
|
||||
assertEquals("abc", sb.toString());
|
||||
|
|
|
@ -15,11 +15,7 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
@ -29,26 +25,31 @@ import com.google.gson.common.TestTypes.BagOfPrimitives;
|
|||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for {@link Gson#getDelegateAdapter(TypeAdapterFactory, TypeToken)} method.
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class DelegateTypeAdapterTest extends TestCase {
|
||||
public class DelegateTypeAdapterTest {
|
||||
|
||||
private StatsTypeAdapterFactory stats;
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
stats = new StatsTypeAdapterFactory();
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapterFactory(stats)
|
||||
.create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelegateInvoked() {
|
||||
List<BagOfPrimitives> bags = new ArrayList<>();
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
|
@ -61,6 +62,7 @@ public class DelegateTypeAdapterTest extends TestCase {
|
|||
assertEquals(51, stats.numWrites);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelegateInvokedOnStrings() {
|
||||
String[] bags = {"1", "2", "3", "4"};
|
||||
String json = gson.toJson(bags);
|
||||
|
|
|
@ -16,6 +16,11 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
|
@ -36,7 +41,8 @@ import java.util.EnumMap;
|
|||
import java.util.EnumSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for Java 5.0 enums.
|
||||
|
@ -44,26 +50,28 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class EnumTest extends TestCase {
|
||||
public class EnumTest {
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelEnumSerialization() throws Exception {
|
||||
String result = gson.toJson(MyEnum.VALUE1);
|
||||
assertEquals('"' + MyEnum.VALUE1.toString() + '"', result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelEnumDeserialization() throws Exception {
|
||||
MyEnum result = gson.fromJson('"' + MyEnum.VALUE1.toString() + '"', MyEnum.class);
|
||||
assertEquals(MyEnum.VALUE1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfEnumsSerialization() {
|
||||
Type type = new TypeToken<Collection<MyEnum>>() {}.getType();
|
||||
Collection<MyEnum> target = new ArrayList<>();
|
||||
|
@ -76,6 +84,7 @@ public class EnumTest extends TestCase {
|
|||
assertEquals(expectedJson, actualJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfEnumsDeserialization() {
|
||||
Type type = new TypeToken<Collection<MyEnum>>() {}.getType();
|
||||
String json = "[\"VALUE1\",\"VALUE2\"]";
|
||||
|
@ -84,11 +93,13 @@ public class EnumTest extends TestCase {
|
|||
MoreAsserts.assertContains(target, MyEnum.VALUE2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithEnumFieldSerialization() throws Exception {
|
||||
ClassWithEnumFields target = new ClassWithEnumFields();
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithEnumFieldDeserialization() throws Exception {
|
||||
String json = "{value1:'VALUE1',value2:'VALUE2'}";
|
||||
ClassWithEnumFields target = gson.fromJson(json, ClassWithEnumFields.class);
|
||||
|
@ -111,6 +122,7 @@ public class EnumTest extends TestCase {
|
|||
/**
|
||||
* Test for issue 226.
|
||||
*/
|
||||
@Test
|
||||
public void testEnumSubclass() {
|
||||
assertFalse(Roshambo.class == Roshambo.ROCK.getClass());
|
||||
assertEquals("\"ROCK\"", gson.toJson(Roshambo.ROCK));
|
||||
|
@ -120,6 +132,7 @@ public class EnumTest extends TestCase {
|
|||
gson.fromJson("[\"ROCK\",\"PAPER\",\"SCISSORS\"]", new TypeToken<Set<Roshambo>>() {}.getType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumSubclassWithRegisteredTypeAdapter() {
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(Roshambo.class, new MyEnumTypeAdapter())
|
||||
|
@ -132,6 +145,7 @@ public class EnumTest extends TestCase {
|
|||
gson.fromJson("[\"123ROCK\",\"123PAPER\",\"123SCISSORS\"]", new TypeToken<Set<Roshambo>>() {}.getType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumSubclassAsParameterizedType() {
|
||||
Collection<Roshambo> list = new ArrayList<>();
|
||||
list.add(Roshambo.ROCK);
|
||||
|
@ -146,11 +160,13 @@ public class EnumTest extends TestCase {
|
|||
MoreAsserts.assertContains(actualJsonList, Roshambo.PAPER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumCaseMapping() {
|
||||
assertEquals(Gender.MALE, gson.fromJson("\"boy\"", Gender.class));
|
||||
assertEquals("\"boy\"", gson.toJson(Gender.MALE, Gender.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumSet() {
|
||||
EnumSet<Roshambo> foo = EnumSet.of(Roshambo.ROCK, Roshambo.PAPER);
|
||||
String json = gson.toJson(foo);
|
||||
|
@ -163,6 +179,7 @@ public class EnumTest extends TestCase {
|
|||
assertFalse(bar.contains(Roshambo.SCISSORS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumMap() throws Exception {
|
||||
EnumMap<MyEnum, String> map = new EnumMap<>(MyEnum.class);
|
||||
map.put(MyEnum.VALUE1, "test");
|
||||
|
@ -215,6 +232,7 @@ public class EnumTest extends TestCase {
|
|||
FEMALE
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumClassWithFields() {
|
||||
assertEquals("\"RED\"", gson.toJson(Color.RED));
|
||||
assertEquals("red", gson.fromJson("RED", Color.class).value);
|
||||
|
@ -231,6 +249,7 @@ public class EnumTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumToStringRead() {
|
||||
// Should still be able to read constant name
|
||||
assertEquals(CustomToString.A, gson.fromJson("\"A\"", CustomToString.class));
|
||||
|
@ -253,6 +272,7 @@ public class EnumTest extends TestCase {
|
|||
* Test that enum constant names have higher precedence than {@code toString()}
|
||||
* result.
|
||||
*/
|
||||
@Test
|
||||
public void testEnumToStringReadInterchanged() {
|
||||
assertEquals(InterchangedToString.A, gson.fromJson("\"A\"", InterchangedToString.class));
|
||||
assertEquals(InterchangedToString.B, gson.fromJson("\"B\"", InterchangedToString.class));
|
||||
|
|
|
@ -16,22 +16,24 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for enums with Proguard.
|
||||
*
|
||||
* @author Young Cha
|
||||
*/
|
||||
public class EnumWithObfuscatedTest extends TestCase {
|
||||
public class EnumWithObfuscatedTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
|
@ -43,6 +45,7 @@ public class EnumWithObfuscatedTest extends TestCase {
|
|||
FEMALE
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumClassWithObfuscated() {
|
||||
for (Gender enumConstant: Gender.class.getEnumConstants()) {
|
||||
try {
|
||||
|
|
|
@ -16,12 +16,17 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Performs some functional test involving JSON output escaping.
|
||||
|
@ -29,15 +34,15 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class EscapingTest extends TestCase {
|
||||
public class EscapingTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapingQuotesInStringArray() throws Exception {
|
||||
String[] valueWithQuotes = { "beforeQuote\"afterQuote" };
|
||||
String jsonRepresentation = gson.toJson(valueWithQuotes);
|
||||
|
@ -46,6 +51,7 @@ public class EscapingTest extends TestCase {
|
|||
assertEquals(valueWithQuotes[0], target[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapeAllHtmlCharacters() {
|
||||
List<String> strings = new ArrayList<>();
|
||||
strings.add("<");
|
||||
|
@ -58,6 +64,7 @@ public class EscapingTest extends TestCase {
|
|||
gson.toJson(strings));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapingObjectFields() throws Exception {
|
||||
BagOfPrimitives objWithPrimitives = new BagOfPrimitives(1L, 1, true, "test with\" <script>");
|
||||
String jsonRepresentation = gson.toJson(objWithPrimitives);
|
||||
|
@ -69,6 +76,7 @@ public class EscapingTest extends TestCase {
|
|||
assertEquals(objWithPrimitives.getExpectedJson(), expectedObject.getExpectedJson());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonAcceptsEscapedAndNonEscapedJsonDeserialization() throws Exception {
|
||||
Gson escapeHtmlGson = new GsonBuilder().create();
|
||||
Gson noEscapeHtmlGson = new GsonBuilder().disableHtmlEscaping().create();
|
||||
|
@ -82,6 +90,7 @@ public class EscapingTest extends TestCase {
|
|||
assertEquals(target, escapeHtmlGson.fromJson(nonEscapedJsonForm, BagOfPrimitives.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonDoubleDeserialization() {
|
||||
BagOfPrimitives expected = new BagOfPrimitives(3L, 4, true, "value1");
|
||||
String json = gson.toJson(gson.toJson(expected));
|
||||
|
|
|
@ -16,6 +16,11 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.ExclusionStrategy;
|
||||
import com.google.gson.FieldAttributes;
|
||||
import com.google.gson.Gson;
|
||||
|
@ -26,7 +31,8 @@ import java.lang.annotation.ElementType;
|
|||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Performs some functional tests when Gson is instantiated with some common user defined
|
||||
|
@ -35,7 +41,7 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class ExclusionStrategyFunctionalTest extends TestCase {
|
||||
public class ExclusionStrategyFunctionalTest {
|
||||
private static final ExclusionStrategy EXCLUDE_SAMPLE_OBJECT_FOR_TEST = new ExclusionStrategy() {
|
||||
@Override public boolean shouldSkipField(FieldAttributes f) {
|
||||
return false;
|
||||
|
@ -47,12 +53,12 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
|
|||
|
||||
private SampleObjectForTest src;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
src = new SampleObjectForTest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionStrategySerialization() throws Exception {
|
||||
Gson gson = createGson(new MyExclusionStrategy(String.class), true);
|
||||
String json = gson.toJson(src);
|
||||
|
@ -61,6 +67,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
|
|||
assertTrue(json.contains("\"longField\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionStrategySerializationDoesNotImpactDeserialization() {
|
||||
String json = "{\"annotatedField\":1,\"stringField\":\"x\",\"longField\":2}";
|
||||
Gson gson = createGson(new MyExclusionStrategy(String.class), true);
|
||||
|
@ -70,6 +77,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
|
|||
assertEquals(2, value.longField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionStrategyDeserialization() throws Exception {
|
||||
Gson gson = createGson(new MyExclusionStrategy(String.class), false);
|
||||
JsonObject json = new JsonObject();
|
||||
|
@ -85,6 +93,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
|
|||
assertEquals(src.stringField, target.stringField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionStrategySerializationDoesNotImpactSerialization() throws Exception {
|
||||
Gson gson = createGson(new MyExclusionStrategy(String.class), false);
|
||||
String json = gson.toJson(src);
|
||||
|
@ -93,6 +102,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
|
|||
assertTrue(json.contains("\"longField\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionStrategyWithMode() throws Exception {
|
||||
SampleObjectForTest testObj = new SampleObjectForTest(
|
||||
src.annotatedField + 5, src.stringField + "blah,blah",
|
||||
|
@ -112,6 +122,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
|
|||
assertEquals(src.stringField, target.stringField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcludeTopLevelClassSerialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.addSerializationExclusionStrategy(EXCLUDE_SAMPLE_OBJECT_FOR_TEST)
|
||||
|
@ -119,6 +130,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
|
|||
assertEquals("null", gson.toJson(new SampleObjectForTest(), SampleObjectForTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcludeTopLevelClassSerializationDoesNotImpactDeserialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.addSerializationExclusionStrategy(EXCLUDE_SAMPLE_OBJECT_FOR_TEST)
|
||||
|
@ -130,6 +142,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
|
|||
assertEquals(2, value.longField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcludeTopLevelClassDeserialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.addDeserializationExclusionStrategy(EXCLUDE_SAMPLE_OBJECT_FOR_TEST)
|
||||
|
@ -139,6 +152,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
|
|||
assertNull(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcludeTopLevelClassDeserializationDoesNotImpactSerialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.addDeserializationExclusionStrategy(EXCLUDE_SAMPLE_OBJECT_FOR_TEST)
|
||||
|
|
|
@ -16,33 +16,37 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.InstanceCreator;
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import java.lang.reflect.Type;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the regarding functional "@Expose" type tests.
|
||||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class ExposeFieldsTest extends TestCase {
|
||||
public class ExposeFieldsTest {
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new GsonBuilder()
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
.registerTypeAdapter(SomeInterface.class, new SomeInterfaceInstanceCreator())
|
||||
.create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullExposeFieldSerialization() throws Exception {
|
||||
ClassWithExposedFields object = new ClassWithExposedFields(null, 1);
|
||||
String json = gson.toJson(object);
|
||||
|
@ -50,6 +54,7 @@ public class ExposeFieldsTest extends TestCase {
|
|||
assertEquals(object.getExpectedJson(), json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayWithOneNullExposeFieldObjectSerialization() throws Exception {
|
||||
ClassWithExposedFields object1 = new ClassWithExposedFields(1, 1);
|
||||
ClassWithExposedFields object2 = new ClassWithExposedFields(null, 1);
|
||||
|
@ -66,11 +71,13 @@ public class ExposeFieldsTest extends TestCase {
|
|||
assertEquals(expected, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExposeAnnotationSerialization() throws Exception {
|
||||
ClassWithExposedFields target = new ClassWithExposedFields(1, 2);
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExposeAnnotationDeserialization() throws Exception {
|
||||
String json = "{a:3,b:4,d:20.0}";
|
||||
ClassWithExposedFields target = gson.fromJson(json, ClassWithExposedFields.class);
|
||||
|
@ -80,6 +87,7 @@ public class ExposeFieldsTest extends TestCase {
|
|||
assertFalse(target.d == 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoExposedFieldSerialization() throws Exception {
|
||||
ClassWithNoExposedFields obj = new ClassWithNoExposedFields();
|
||||
String json = gson.toJson(obj);
|
||||
|
@ -87,6 +95,7 @@ public class ExposeFieldsTest extends TestCase {
|
|||
assertEquals("{}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoExposedFieldDeserialization() throws Exception {
|
||||
String json = "{a:4,b:5}";
|
||||
ClassWithNoExposedFields obj = gson.fromJson(json, ClassWithNoExposedFields.class);
|
||||
|
@ -95,6 +104,7 @@ public class ExposeFieldsTest extends TestCase {
|
|||
assertEquals(1, obj.b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExposedInterfaceFieldSerialization() throws Exception {
|
||||
String expected = "{\"interfaceField\":{}}";
|
||||
ClassWithInterfaceField target = new ClassWithInterfaceField(new SomeObject());
|
||||
|
@ -103,6 +113,7 @@ public class ExposeFieldsTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExposedInterfaceFieldDeserialization() throws Exception {
|
||||
String json = "{\"interfaceField\":{}}";
|
||||
ClassWithInterfaceField obj = gson.fromJson(json, ClassWithInterfaceField.class);
|
||||
|
|
|
@ -16,10 +16,12 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Performs some functional testing to ensure GSON infrastructure properly serializes/deserializes
|
||||
|
@ -28,17 +30,17 @@ import junit.framework.TestCase;
|
|||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class FieldExclusionTest extends TestCase {
|
||||
public class FieldExclusionTest {
|
||||
private static final String VALUE = "blah_1234";
|
||||
|
||||
private Outer outer;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
outer = new Outer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultInnerClassExclusion() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
Outer.Inner target = outer.new Inner(VALUE);
|
||||
|
@ -51,6 +53,7 @@ public class FieldExclusionTest extends TestCase {
|
|||
assertEquals(target.toJson(), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInnerClassExclusion() throws Exception {
|
||||
Gson gson = new GsonBuilder().disableInnerClassSerialization().create();
|
||||
Outer.Inner target = outer.new Inner(VALUE);
|
||||
|
@ -58,6 +61,7 @@ public class FieldExclusionTest extends TestCase {
|
|||
assertEquals("null", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultNestedStaticClassIncluded() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
Outer.Inner target = outer.new Inner(VALUE);
|
||||
|
|
|
@ -22,14 +22,16 @@ import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES;
|
|||
import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE;
|
||||
import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES;
|
||||
import static com.google.gson.FieldNamingPolicy.UPPER_CASE_WITH_UNDERSCORES;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.FieldNamingPolicy;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public final class FieldNamingTest extends TestCase {
|
||||
public final class FieldNamingTest {
|
||||
@Test
|
||||
public void testIdentity() {
|
||||
Gson gson = getGsonWithNamingPolicy(IDENTITY);
|
||||
assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," +
|
||||
|
@ -38,6 +40,7 @@ public final class FieldNamingTest extends TestCase {
|
|||
gson.toJson(new TestNames()).replace('\"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpperCamelCase() {
|
||||
Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE);
|
||||
assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," +
|
||||
|
@ -46,6 +49,7 @@ public final class FieldNamingTest extends TestCase {
|
|||
gson.toJson(new TestNames()).replace('\"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpperCamelCaseWithSpaces() {
|
||||
Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES);
|
||||
assertEquals("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," +
|
||||
|
@ -54,6 +58,7 @@ public final class FieldNamingTest extends TestCase {
|
|||
gson.toJson(new TestNames()).replace('\"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpperCaseWithUnderscores() {
|
||||
Gson gson = getGsonWithNamingPolicy(UPPER_CASE_WITH_UNDERSCORES);
|
||||
assertEquals("{'LOWER_CAMEL':1,'UPPER_CAMEL':2,'_LOWER_CAMEL_LEADING_UNDERSCORE':3," +
|
||||
|
@ -62,6 +67,7 @@ public final class FieldNamingTest extends TestCase {
|
|||
gson.toJson(new TestNames()).replace('\"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLowerCaseWithUnderscores() {
|
||||
Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
|
||||
assertEquals("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," +
|
||||
|
@ -70,6 +76,7 @@ public final class FieldNamingTest extends TestCase {
|
|||
gson.toJson(new TestNames()).replace('\"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLowerCaseWithDashes() {
|
||||
Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES);
|
||||
assertEquals("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," +
|
||||
|
|
|
@ -15,26 +15,25 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
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.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import java.io.IOException;
|
||||
import java.util.regex.Pattern;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests to validate printing of Gson version on AssertionErrors
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class GsonVersionDiagnosticsTest extends TestCase {
|
||||
public class GsonVersionDiagnosticsTest {
|
||||
// We require a patch number, even if it is .0, consistent with https://semver.org/#spec-item-2.
|
||||
private static final Pattern GSON_VERSION_PATTERN =
|
||||
Pattern.compile("(\\(GSON \\d\\.\\d+\\.\\d)(?:[-.][A-Z]+)?\\)$");
|
||||
|
@ -42,7 +41,6 @@ public class GsonVersionDiagnosticsTest extends TestCase {
|
|||
private Gson gson;
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp() {
|
||||
gson = new GsonBuilder().registerTypeAdapter(TestType.class, new TypeAdapter<TestType>() {
|
||||
@Override public void write(JsonWriter out, TestType value) {
|
||||
|
|
|
@ -15,6 +15,11 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
|
@ -26,9 +31,6 @@ import com.google.gson.common.TestTypes.ClassWithBaseCollectionField;
|
|||
import com.google.gson.common.TestTypes.ClassWithBaseField;
|
||||
import com.google.gson.common.TestTypes.Nested;
|
||||
import com.google.gson.common.TestTypes.Sub;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
|
@ -37,6 +39,8 @@ import java.util.Queue;
|
|||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for Json serialization and deserialization of classes with
|
||||
|
@ -45,21 +49,22 @@ import java.util.TreeSet;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class InheritanceTest extends TestCase {
|
||||
public class InheritanceTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubClassSerialization() throws Exception {
|
||||
SubTypeOfNested target = new SubTypeOfNested(new BagOfPrimitives(10, 20, false, "stringValue"),
|
||||
new BagOfPrimitives(30, 40, true, "stringValue"));
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubClassDeserialization() throws Exception {
|
||||
String json = "{\"value\":5,\"primitive1\":{\"longValue\":10,\"intValue\":20,"
|
||||
+ "\"booleanValue\":false,\"stringValue\":\"stringValue\"},\"primitive2\":"
|
||||
|
@ -69,6 +74,7 @@ public class InheritanceTest extends TestCase {
|
|||
assertEquals(json, target.getExpectedJson());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithBaseFieldSerialization() {
|
||||
ClassWithBaseField sub = new ClassWithBaseField(new Sub());
|
||||
JsonObject json = (JsonObject) gson.toJsonTree(sub);
|
||||
|
@ -76,6 +82,7 @@ public class InheritanceTest extends TestCase {
|
|||
assertEquals(Sub.SUB_NAME, base.getAsJsonObject().get(Sub.SUB_FIELD_KEY).getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithBaseArrayFieldSerialization() {
|
||||
Base[] baseClasses = new Base[]{ new Sub(), new Sub()};
|
||||
ClassWithBaseArrayField sub = new ClassWithBaseArrayField(baseClasses);
|
||||
|
@ -86,6 +93,7 @@ public class InheritanceTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithBaseCollectionFieldSerialization() {
|
||||
Collection<Base> baseClasses = new ArrayList<>();
|
||||
baseClasses.add(new Sub());
|
||||
|
@ -98,18 +106,21 @@ public class InheritanceTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseSerializedAsSub() {
|
||||
Base base = new Sub();
|
||||
JsonObject json = gson.toJsonTree(base).getAsJsonObject();
|
||||
assertEquals(Sub.SUB_NAME, json.get(Sub.SUB_FIELD_KEY).getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseSerializedAsSubForToJsonMethod() {
|
||||
Base base = new Sub();
|
||||
String json = gson.toJson(base);
|
||||
assertTrue(json.contains(Sub.SUB_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseSerializedAsBaseWhenSpecifiedWithExplicitType() {
|
||||
Base base = new Sub();
|
||||
JsonObject json = gson.toJsonTree(base, Base.class).getAsJsonObject();
|
||||
|
@ -117,6 +128,7 @@ public class InheritanceTest extends TestCase {
|
|||
assertNull(json.get(Sub.SUB_FIELD_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseSerializedAsBaseWhenSpecifiedWithExplicitTypeForToJsonMethod() {
|
||||
Base base = new Sub();
|
||||
String json = gson.toJson(base, Base.class);
|
||||
|
@ -124,12 +136,14 @@ public class InheritanceTest extends TestCase {
|
|||
assertFalse(json.contains(Sub.SUB_FIELD_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseSerializedAsSubWhenSpecifiedWithExplicitType() {
|
||||
Base base = new Sub();
|
||||
JsonObject json = gson.toJsonTree(base, Sub.class).getAsJsonObject();
|
||||
assertEquals(Sub.SUB_NAME, json.get(Sub.SUB_FIELD_KEY).getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseSerializedAsSubWhenSpecifiedWithExplicitTypeForToJsonMethod() {
|
||||
Base base = new Sub();
|
||||
String json = gson.toJson(base, Sub.class);
|
||||
|
@ -150,6 +164,7 @@ public class InheritanceTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubInterfacesOfCollectionSerialization() throws Exception {
|
||||
List<Integer> list = new LinkedList<>();
|
||||
list.add(0);
|
||||
|
@ -176,6 +191,7 @@ public class InheritanceTest extends TestCase {
|
|||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubInterfacesOfCollectionDeserialization() throws Exception {
|
||||
String json = "{\"list\":[0,1,2,3],\"queue\":[0,1,2,3],\"set\":[0.1,0.2,0.3,0.4],"
|
||||
+ "\"sortedSet\":[\"a\",\"b\",\"c\",\"d\"]"
|
||||
|
|
|
@ -16,6 +16,10 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.InstanceCreator;
|
||||
|
@ -28,7 +32,7 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional Test exercising custom serialization only. When test applies to both
|
||||
|
@ -36,8 +40,9 @@ import junit.framework.TestCase;
|
|||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class InstanceCreatorTest extends TestCase {
|
||||
public class InstanceCreatorTest {
|
||||
|
||||
@Test
|
||||
public void testInstanceCreatorReturnsBaseType() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new InstanceCreator<Base>() {
|
||||
|
@ -51,6 +56,7 @@ public class InstanceCreatorTest extends TestCase {
|
|||
assertEquals("BaseRevised", base.baseName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstanceCreatorReturnsSubTypeForTopLevelObject() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new InstanceCreator<Base>() {
|
||||
|
@ -69,6 +75,7 @@ public class InstanceCreatorTest extends TestCase {
|
|||
assertEquals(Sub.SUB_NAME, sub.subName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstanceCreatorReturnsSubTypeForField() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Base.class, new InstanceCreator<Base>() {
|
||||
|
@ -84,6 +91,7 @@ public class InstanceCreatorTest extends TestCase {
|
|||
}
|
||||
|
||||
// This regressed in Gson 2.0 and 2.1
|
||||
@Test
|
||||
public void testInstanceCreatorForCollectionType() {
|
||||
@SuppressWarnings("serial")
|
||||
class SubArrayList<T> extends ArrayList<T> {}
|
||||
|
@ -101,6 +109,7 @@ public class InstanceCreatorTest extends TestCase {
|
|||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testInstanceCreatorForParametrizedType() throws Exception {
|
||||
@SuppressWarnings("serial")
|
||||
class SubTreeSet<T> extends TreeSet<T> {}
|
||||
|
|
|
@ -16,9 +16,11 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests involving interfaces.
|
||||
|
@ -26,23 +28,24 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class InterfaceTest extends TestCase {
|
||||
public class InterfaceTest {
|
||||
private static final String OBJ_JSON = "{\"someStringValue\":\"StringValue\"}";
|
||||
|
||||
private Gson gson;
|
||||
private TestObject obj;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
obj = new TestObject("StringValue");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializingObjectImplementingInterface() throws Exception {
|
||||
assertEquals(OBJ_JSON, gson.toJson(obj));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializingInterfaceObjectField() throws Exception {
|
||||
TestObjectWrapper objWrapper = new TestObjectWrapper(obj);
|
||||
assertEquals("{\"obj\":" + OBJ_JSON + "}", gson.toJson(objWrapper));
|
||||
|
|
|
@ -16,23 +16,26 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for internationalized strings.
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class InternationalizationTest extends TestCase {
|
||||
public class InternationalizationTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringsWithUnicodeChineseCharactersSerialization() throws Exception {
|
||||
String target = "\u597d\u597d\u597d";
|
||||
String json = gson.toJson(target);
|
||||
|
@ -40,6 +43,7 @@ public class InternationalizationTest extends TestCase {
|
|||
assertEquals(expected, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringsWithUnicodeChineseCharactersDeserialization() throws Exception {
|
||||
String expected = "\u597d\u597d\u597d";
|
||||
String json = '"' + expected + '"';
|
||||
|
@ -47,11 +51,13 @@ public class InternationalizationTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringsWithUnicodeChineseCharactersEscapedDeserialization() throws Exception {
|
||||
String actual = gson.fromJson("'\\u597d\\u597d\\u597d'", String.class);
|
||||
assertEquals("\u597d\u597d\u597d", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSupplementaryUnicodeSerialization() throws Exception {
|
||||
// Supplementary code point U+1F60A
|
||||
String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1);
|
||||
|
@ -59,6 +65,7 @@ public class InternationalizationTest extends TestCase {
|
|||
assertEquals('"' + supplementaryCodePoint + '"', json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSupplementaryUnicodeDeserialization() throws Exception {
|
||||
// Supplementary code point U+1F60A
|
||||
String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1);
|
||||
|
@ -66,6 +73,7 @@ public class InternationalizationTest extends TestCase {
|
|||
assertEquals(supplementaryCodePoint, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSupplementaryUnicodeEscapedDeserialization() throws Exception {
|
||||
// Supplementary code point U+1F60A
|
||||
String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1);
|
||||
|
|
|
@ -16,30 +16,32 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.LongSerializationPolicy;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicLongArray;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.LongSerializationPolicy;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional test for Json serialization and deserialization for classes in java.util.concurrent.atomic
|
||||
*/
|
||||
public class JavaUtilConcurrentAtomicTest extends TestCase {
|
||||
public class JavaUtilConcurrentAtomicTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtomicBoolean() throws Exception {
|
||||
AtomicBoolean target = gson.fromJson("true", AtomicBoolean.class);
|
||||
assertTrue(target.get());
|
||||
|
@ -47,6 +49,7 @@ public class JavaUtilConcurrentAtomicTest extends TestCase {
|
|||
assertEquals("true", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtomicInteger() throws Exception {
|
||||
AtomicInteger target = gson.fromJson("10", AtomicInteger.class);
|
||||
assertEquals(10, target.get());
|
||||
|
@ -54,6 +57,7 @@ public class JavaUtilConcurrentAtomicTest extends TestCase {
|
|||
assertEquals("10", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtomicLong() throws Exception {
|
||||
AtomicLong target = gson.fromJson("10", AtomicLong.class);
|
||||
assertEquals(10, target.get());
|
||||
|
@ -61,6 +65,7 @@ public class JavaUtilConcurrentAtomicTest extends TestCase {
|
|||
assertEquals("10", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtomicLongWithStringSerializationPolicy() throws Exception {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
|
||||
|
@ -71,6 +76,7 @@ public class JavaUtilConcurrentAtomicTest extends TestCase {
|
|||
assertEquals("{\"value\":\"10\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtomicIntegerArray() throws Exception {
|
||||
AtomicIntegerArray target = gson.fromJson("[10, 13, 14]", AtomicIntegerArray.class);
|
||||
assertEquals(3, target.length());
|
||||
|
@ -81,6 +87,7 @@ public class JavaUtilConcurrentAtomicTest extends TestCase {
|
|||
assertEquals("[10,13,14]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtomicLongArray() throws Exception {
|
||||
AtomicLongArray target = gson.fromJson("[10, 13, 14]", AtomicLongArray.class);
|
||||
assertEquals(3, target.length());
|
||||
|
@ -91,6 +98,7 @@ public class JavaUtilConcurrentAtomicTest extends TestCase {
|
|||
assertEquals("[10,13,14]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtomicLongArrayWithStringSerializationPolicy() throws Exception {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
|
||||
|
|
|
@ -16,25 +16,28 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.util.Currency;
|
||||
import java.util.Properties;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import java.util.Currency;
|
||||
import java.util.Properties;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional test for Json serialization and deserialization for classes in java.util
|
||||
*/
|
||||
public class JavaUtilTest extends TestCase {
|
||||
public class JavaUtilTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCurrency() throws Exception {
|
||||
CurrencyHolder target = gson.fromJson("{'value':'USD'}", CurrencyHolder.class);
|
||||
assertEquals("USD", target.value.getCurrencyCode());
|
||||
|
@ -51,6 +54,7 @@ public class JavaUtilTest extends TestCase {
|
|||
Currency value;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProperties() {
|
||||
Properties props = gson.fromJson("{'a':'v1','b':'v2'}", Properties.class);
|
||||
assertEquals("v1", props.getProperty("a"));
|
||||
|
|
|
@ -16,6 +16,11 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
|
@ -34,13 +39,14 @@ import com.google.gson.stream.JsonWriter;
|
|||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Locale;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for the {@link com.google.gson.annotations.JsonAdapter} annotation on classes.
|
||||
*/
|
||||
public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
||||
public final class JsonAdapterAnnotationOnClassesTest {
|
||||
|
||||
@Test
|
||||
public void testJsonAdapterInvoked() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new A("bar"));
|
||||
|
@ -59,6 +65,7 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
|||
assertEquals(Foo.BAZ, baz);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonAdapterFactoryInvoked() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new C("bar"));
|
||||
|
@ -67,6 +74,7 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
|||
assertEquals("jsonAdapterFactory", c.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisteredAdapterOverridesJsonAdapter() {
|
||||
TypeAdapter<A> typeAdapter = new TypeAdapter<A>() {
|
||||
@Override public void write(JsonWriter out, A value) throws IOException {
|
||||
|
@ -86,6 +94,7 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
|||
/**
|
||||
* The serializer overrides field adapter, but for deserializer the fieldAdapter is used.
|
||||
*/
|
||||
@Test
|
||||
public void testRegisteredSerializerOverridesJsonAdapter() {
|
||||
JsonSerializer<A> serializer = new JsonSerializer<A>() {
|
||||
@Override public JsonElement serialize(A src, Type typeOfSrc,
|
||||
|
@ -105,6 +114,7 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
|||
/**
|
||||
* The deserializer overrides Json adapter, but for serializer the jsonAdapter is used.
|
||||
*/
|
||||
@Test
|
||||
public void testRegisteredDeserializerOverridesJsonAdapter() {
|
||||
JsonDeserializer<A> deserializer = new JsonDeserializer<A>() {
|
||||
@Override public A deserialize(JsonElement json, Type typeOfT,
|
||||
|
@ -121,6 +131,7 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
|||
assertEquals("registeredDeserializer", target.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncorrectTypeAdapterFails() {
|
||||
try {
|
||||
String json = new Gson().toJson(new ClassWithIncorrectJsonAdapter("bar"));
|
||||
|
@ -128,11 +139,13 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
|||
} catch (ClassCastException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuperclassTypeAdapterNotInvoked() {
|
||||
String json = new Gson().toJson(new B("bar"));
|
||||
assertFalse(json.contains("jsonAdapter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSafeObjectFromJson() {
|
||||
Gson gson = new Gson();
|
||||
NullableClass fromJson = gson.fromJson("null", NullableClass.class);
|
||||
|
@ -250,6 +263,7 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncorrectJsonAdapterType() {
|
||||
try {
|
||||
new Gson().toJson(new D());
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
@ -28,13 +28,16 @@ import com.google.gson.annotations.JsonAdapter;
|
|||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for the {@link com.google.gson.annotations.JsonAdapter} annotation on fields.
|
||||
*/
|
||||
public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
||||
public final class JsonAdapterAnnotationOnFieldsTest {
|
||||
@Test
|
||||
public void testClassAnnotationAdapterTakesPrecedenceOverDefault() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new Computer(new User("Inderjeet Singh")));
|
||||
|
@ -43,6 +46,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
|||
assertEquals("UserClassAnnotationAdapter", computer.user.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassAnnotationAdapterFactoryTakesPrecedenceOverDefault() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new Gizmo(new Part("Part")));
|
||||
|
@ -51,6 +55,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
|||
assertEquals("GizmoPartTypeAdapterFactory", computer.part.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisteredTypeAdapterTakesPrecedenceOverClassAnnotationAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(User.class, new RegisteredUserAdapter())
|
||||
|
@ -61,6 +66,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
|||
assertEquals("RegisteredUserAdapter", computer.user.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldAnnotationTakesPrecedenceOverRegisteredTypeAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Part.class, new TypeAdapter<Part>() {
|
||||
|
@ -77,6 +83,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
|||
assertEquals("PartJsonFieldAnnotationAdapter", gadget.part.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldAnnotationTakesPrecedenceOverClassAnnotation() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new Computer2(new User("Inderjeet Singh")));
|
||||
|
@ -187,6 +194,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonAdapterInvokedOnlyForAnnotatedFields() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{'part1':'name','part2':{'name':'name2'}}";
|
||||
|
@ -204,6 +212,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonAdapterWrappedInNullSafeAsRequested() {
|
||||
Gson gson = new Gson();
|
||||
String fromJson = "{'part':null}";
|
||||
|
@ -225,6 +234,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
|||
}
|
||||
|
||||
/** Regression test contributed through https://github.com/google/gson/issues/831 */
|
||||
@Test
|
||||
public void testNonPrimitiveFieldAnnotationTakesPrecedenceOverDefault() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new GadgetWithOptionalPart(new Part("foo")));
|
||||
|
@ -234,6 +244,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
|||
}
|
||||
|
||||
/** Regression test contributed through https://github.com/google/gson/issues/831 */
|
||||
@Test
|
||||
public void testPrimitiveFieldAnnotationTakesPrecedenceOverDefault() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new GadgetWithPrimitivePart(42));
|
||||
|
@ -273,6 +284,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldAnnotationWorksForParameterizedType() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new Gizmo2(Arrays.asList(new Part("Part"))));
|
||||
|
|
|
@ -16,7 +16,9 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
|
@ -27,15 +29,16 @@ import com.google.gson.JsonPrimitive;
|
|||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.annotations.JsonAdapter;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import java.lang.reflect.Type;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for the {@link JsonAdapter} annotation on fields where the value is of
|
||||
* type {@link JsonSerializer} or {@link JsonDeserializer}.
|
||||
*/
|
||||
public final class JsonAdapterSerializerDeserializerTest extends TestCase {
|
||||
public final class JsonAdapterSerializerDeserializerTest {
|
||||
|
||||
@Test
|
||||
public void testJsonSerializerDeserializerBasedJsonAdapterOnFields() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new Computer(new User("Inderjeet Singh"), null, new User("Jesse Wilson")));
|
||||
|
@ -90,6 +93,7 @@ public final class JsonAdapterSerializerDeserializerTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonSerializerDeserializerBasedJsonAdapterOnClass() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new Computer2(new User2("Inderjeet Singh")));
|
||||
|
@ -125,6 +129,7 @@ public final class JsonAdapterSerializerDeserializerTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferentJsonAdaptersForGenericFieldsOfSameRawType() {
|
||||
Container c = new Container("Foo", 10);
|
||||
Gson gson = new Gson();
|
||||
|
@ -162,6 +167,7 @@ public final class JsonAdapterSerializerDeserializerTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonAdapterNullSafe() {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new Computer3(null, null));
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
@ -26,14 +29,13 @@ import com.google.gson.JsonSyntaxException;
|
|||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||
import com.google.gson.common.TestTypes.Nested;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for that use JsonParser and related Gson methods
|
||||
|
@ -41,15 +43,15 @@ import java.util.Map;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class JsonParserTest extends TestCase {
|
||||
public class JsonParserTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseInvalidJson() {
|
||||
try {
|
||||
gson.fromJson("[[]", Object[].class);
|
||||
|
@ -57,6 +59,7 @@ public class JsonParserTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingCustomTree() {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty("stringValue", "foo");
|
||||
|
@ -66,6 +69,7 @@ public class JsonParserTest extends TestCase {
|
|||
assertEquals("foo", target.stringValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadTypeForDeserializingCustomTree() {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty("stringValue", "foo");
|
||||
|
@ -78,6 +82,7 @@ public class JsonParserTest extends TestCase {
|
|||
} catch (JsonParseException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadFieldTypeForCustomDeserializerCustomTree() {
|
||||
JsonArray array = new JsonArray();
|
||||
array.add(new JsonPrimitive("blah"));
|
||||
|
@ -92,6 +97,7 @@ public class JsonParserTest extends TestCase {
|
|||
} catch (JsonParseException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadFieldTypeForDeserializingCustomTree() {
|
||||
JsonArray array = new JsonArray();
|
||||
array.add(new JsonPrimitive("blah"));
|
||||
|
@ -109,6 +115,7 @@ public class JsonParserTest extends TestCase {
|
|||
} catch (JsonParseException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangingCustomTreeAndDeserializing() {
|
||||
StringReader json =
|
||||
new StringReader("{'stringValue':'no message','intValue':10,'longValue':20}");
|
||||
|
@ -121,6 +128,7 @@ public class JsonParserTest extends TestCase {
|
|||
assertEquals("fooBar", target.stringValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtraCommasInArrays() {
|
||||
Type type = new TypeToken<List<String>>() {}.getType();
|
||||
assertEquals(Arrays.asList("a", null, "b", null, null), gson.fromJson("[a,,b,,]", type));
|
||||
|
@ -128,6 +136,7 @@ public class JsonParserTest extends TestCase {
|
|||
assertEquals(Arrays.asList("a", null), gson.fromJson("[a,]", type));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtraCommasInMaps() {
|
||||
Type type = new TypeToken<Map<String, String>>() {}.getType();
|
||||
try {
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
@ -8,7 +13,8 @@ import com.google.gson.common.TestTypes.BagOfPrimitives;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for {@link Gson#toJsonTree(Object)} and
|
||||
|
@ -17,15 +23,15 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class JsonTreeTest extends TestCase {
|
||||
public class JsonTreeTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJsonTree() {
|
||||
BagOfPrimitives bag = new BagOfPrimitives(10L, 5, false, "foo");
|
||||
JsonElement json = gson.toJsonTree(bag);
|
||||
|
@ -39,6 +45,7 @@ public class JsonTreeTest extends TestCase {
|
|||
assertContains(obj, new JsonPrimitive("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJsonTreeObjectType() {
|
||||
SubTypeOfBagOfPrimitives bag = new SubTypeOfBagOfPrimitives(10L, 5, false, "foo", 1.4F);
|
||||
JsonElement json = gson.toJsonTree(bag, BagOfPrimitives.class);
|
||||
|
@ -52,6 +59,7 @@ public class JsonTreeTest extends TestCase {
|
|||
assertContains(obj, new JsonPrimitive("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonTreeToString() {
|
||||
SubTypeOfBagOfPrimitives bag = new SubTypeOfBagOfPrimitives(10L, 5, false, "foo", 1.4F);
|
||||
String json1 = gson.toJson(bag);
|
||||
|
@ -60,6 +68,7 @@ public class JsonTreeTest extends TestCase {
|
|||
assertEquals(json1, json2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonTreeNull() {
|
||||
BagOfPrimitives bag = new BagOfPrimitives(10L, 5, false, null);
|
||||
JsonObject jsonElement = (JsonObject) gson.toJsonTree(bag, BagOfPrimitives.class);
|
||||
|
|
|
@ -15,27 +15,29 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for leniency option.
|
||||
*/
|
||||
public class LeniencyTest extends TestCase {
|
||||
public class LeniencyTest {
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new GsonBuilder().setLenient().create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLenientFromJson() {
|
||||
List<String> json = gson.fromJson(""
|
||||
+ "[ # One!\n"
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
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.JsonSyntaxException;
|
||||
|
@ -24,10 +27,12 @@ import java.lang.reflect.Type;
|
|||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MapAsArrayTypeAdapterTest extends TestCase {
|
||||
public class MapAsArrayTypeAdapterTest {
|
||||
|
||||
@Test
|
||||
public void testSerializeComplexMapWithTypeAdapter() {
|
||||
Type type = new TypeToken<Map<Point, String>>() {}.getType();
|
||||
Gson gson = new GsonBuilder()
|
||||
|
@ -53,6 +58,8 @@ public class MapAsArrayTypeAdapterTest extends TestCase {
|
|||
new TypeToken<Map<String, Boolean>>() {}.getType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testTwoTypesCollapseToOneSerialize() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.enableComplexMapKeySerialization()
|
||||
|
@ -68,6 +75,7 @@ public class MapAsArrayTypeAdapterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoTypesCollapseToOneDeserialize() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.enableComplexMapKeySerialization()
|
||||
|
@ -81,6 +89,7 @@ public class MapAsArrayTypeAdapterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleEnableComplexKeyRegistrationHasNoEffect() throws Exception {
|
||||
Type type = new TypeToken<Map<Point, String>>() {}.getType();
|
||||
Gson gson = new GsonBuilder()
|
||||
|
@ -96,6 +105,7 @@ public class MapAsArrayTypeAdapterTest extends TestCase {
|
|||
assertEquals(original, gson.<Map<Point, String>>fromJson(json, type));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapWithTypeVariableSerialization() {
|
||||
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
|
||||
PointWithProperty<Point> map = new PointWithProperty<>();
|
||||
|
@ -105,6 +115,7 @@ public class MapAsArrayTypeAdapterTest extends TestCase {
|
|||
assertEquals("{\"map\":[[{\"x\":2,\"y\":3},{\"x\":4,\"y\":5}]]}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapWithTypeVariableDeserialization() {
|
||||
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
|
||||
String json = "{map:[[{x:2,y:3},{x:4,y:5}]]}";
|
||||
|
|
|
@ -16,6 +16,12 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
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.InstanceCreator;
|
||||
|
@ -41,7 +47,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.ConcurrentNavigableMap;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional test for Json serialization and deserialization for Maps
|
||||
|
@ -49,15 +56,15 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class MapTest extends TestCase {
|
||||
public class MapTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapSerialization() {
|
||||
Map<String, Integer> map = new LinkedHashMap<>();
|
||||
map.put("a", 1);
|
||||
|
@ -68,6 +75,7 @@ public class MapTest extends TestCase {
|
|||
assertTrue(json.contains("\"b\":2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserialization() {
|
||||
String json = "{\"a\":1,\"b\":2}";
|
||||
Type typeOfMap = new TypeToken<Map<String,Integer>>(){}.getType();
|
||||
|
@ -76,6 +84,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals(2, target.get("b").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectMapSerialization() {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("a", 1);
|
||||
|
@ -85,6 +94,7 @@ public class MapTest extends TestCase {
|
|||
assertTrue(json.contains("\"b\":\"string\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapSerializationEmpty() {
|
||||
Map<String, Integer> map = new LinkedHashMap<>();
|
||||
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
||||
|
@ -92,12 +102,14 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserializationEmpty() {
|
||||
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
||||
Map<String, Integer> map = gson.fromJson("{}", typeOfMap);
|
||||
assertTrue(map.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapSerializationWithNullValue() {
|
||||
Map<String, Integer> map = new LinkedHashMap<>();
|
||||
map.put("abc", null);
|
||||
|
@ -108,6 +120,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserializationWithNullValue() {
|
||||
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
||||
Map<String, Integer> map = gson.fromJson("{\"abc\":null}", typeOfMap);
|
||||
|
@ -115,6 +128,7 @@ public class MapTest extends TestCase {
|
|||
assertNull(map.get("abc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapSerializationWithNullValueButSerializeNulls() {
|
||||
gson = new GsonBuilder().serializeNulls().create();
|
||||
Map<String, Integer> map = new LinkedHashMap<>();
|
||||
|
@ -125,6 +139,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{\"abc\":null}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapSerializationWithNullKey() {
|
||||
Map<String, Integer> map = new LinkedHashMap<>();
|
||||
map.put(null, 123);
|
||||
|
@ -134,6 +149,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{\"null\":123}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserializationWithNullKey() {
|
||||
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
||||
Map<String, Integer> map = gson.fromJson("{\"null\":123}", typeOfMap);
|
||||
|
@ -147,6 +163,7 @@ public class MapTest extends TestCase {
|
|||
assertNull(map.get(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapSerializationWithIntegerKeys() {
|
||||
Map<Integer, String> map = new LinkedHashMap<>();
|
||||
map.put(123, "456");
|
||||
|
@ -156,6 +173,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{\"123\":\"456\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserializationWithIntegerKeys() {
|
||||
Type typeOfMap = new TypeToken<Map<Integer, String>>() {}.getType();
|
||||
Map<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
|
||||
|
@ -164,6 +182,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("456", map.get(123));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserializationWithUnquotedIntegerKeys() {
|
||||
Type typeOfMap = new TypeToken<Map<Integer, String>>() {}.getType();
|
||||
Map<Integer, String> map = gson.fromJson("{123:\"456\"}", typeOfMap);
|
||||
|
@ -172,6 +191,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("456", map.get(123));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserializationWithLongKeys() {
|
||||
long longValue = 9876543210L;
|
||||
String json = String.format("{\"%d\":\"456\"}", longValue);
|
||||
|
@ -182,6 +202,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("456", map.get(longValue));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserializationWithUnquotedLongKeys() {
|
||||
long longKey = 9876543210L;
|
||||
String json = String.format("{%d:\"456\"}", longKey);
|
||||
|
@ -192,6 +213,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("456", map.get(longKey));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashMapDeserialization() throws Exception {
|
||||
Type typeOfMap = new TypeToken<HashMap<Integer, String>>() {}.getType();
|
||||
HashMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
|
||||
|
@ -200,6 +222,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("456", map.get(123));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortedMap() throws Exception {
|
||||
Type typeOfMap = new TypeToken<SortedMap<Integer, String>>() {}.getType();
|
||||
SortedMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
|
||||
|
@ -208,6 +231,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("456", map.get(123));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcurrentMap() throws Exception {
|
||||
Type typeOfMap = new TypeToken<ConcurrentMap<Integer, String>>() {}.getType();
|
||||
ConcurrentMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
|
||||
|
@ -218,6 +242,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{\"123\":\"456\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcurrentHashMap() throws Exception {
|
||||
Type typeOfMap = new TypeToken<ConcurrentHashMap<Integer, String>>() {}.getType();
|
||||
ConcurrentHashMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
|
||||
|
@ -228,6 +253,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{\"123\":\"456\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcurrentNavigableMap() throws Exception {
|
||||
Type typeOfMap = new TypeToken<ConcurrentNavigableMap<Integer, String>>() {}.getType();
|
||||
ConcurrentNavigableMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
|
||||
|
@ -238,6 +264,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{\"123\":\"456\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcurrentSkipListMap() throws Exception {
|
||||
Type typeOfMap = new TypeToken<ConcurrentSkipListMap<Integer, String>>() {}.getType();
|
||||
ConcurrentSkipListMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
|
||||
|
@ -248,6 +275,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{\"123\":\"456\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParameterizedMapSubclassSerialization() {
|
||||
MyParameterizedMap<String, String> map = new MyParameterizedMap<>(10);
|
||||
map.put("a", "b");
|
||||
|
@ -264,6 +292,7 @@ public class MapTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapSubclassSerialization() {
|
||||
MyMap map = new MyMap();
|
||||
map.put("a", "b");
|
||||
|
@ -271,6 +300,7 @@ public class MapTest extends TestCase {
|
|||
assertTrue(json.contains("\"a\":\"b\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapStandardSubclassDeserialization() {
|
||||
String json = "{a:'1',b:'2'}";
|
||||
Type type = new TypeToken<LinkedHashMap<String, String>>() {}.getType();
|
||||
|
@ -279,6 +309,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("2", map.get("b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapSubclassDeserialization() {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(MyMap.class, new InstanceCreator<MyMap>() {
|
||||
@Override public MyMap createInstance(Type type) {
|
||||
|
@ -291,6 +322,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("2", map.get("b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomSerializerForSpecificMapType() {
|
||||
Type type = $Gson$Types.newParameterizedTypeWithOwner(
|
||||
null, Map.class, String.class, Long.class);
|
||||
|
@ -324,6 +356,7 @@ public class MapTest extends TestCase {
|
|||
/**
|
||||
* Created in response to http://code.google.com/p/google-gson/issues/detail?id=99
|
||||
*/
|
||||
@Test
|
||||
public void testMapSerializationWithNullValues() {
|
||||
ClassWithAMap target = new ClassWithAMap();
|
||||
target.map.put("name1", null);
|
||||
|
@ -336,6 +369,7 @@ public class MapTest extends TestCase {
|
|||
/**
|
||||
* Created in response to http://code.google.com/p/google-gson/issues/detail?id=99
|
||||
*/
|
||||
@Test
|
||||
public void testMapSerializationWithNullValuesSerialized() {
|
||||
Gson gson = new GsonBuilder().serializeNulls().create();
|
||||
ClassWithAMap target = new ClassWithAMap();
|
||||
|
@ -346,6 +380,7 @@ public class MapTest extends TestCase {
|
|||
assertTrue(json.contains("name2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapSerializationWithWildcardValues() {
|
||||
Map<String, ? extends Collection<? extends Integer>> map = new LinkedHashMap<>();
|
||||
map.put("test", null);
|
||||
|
@ -356,6 +391,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals("{}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserializationWithWildcardValues() {
|
||||
Type typeOfMap = new TypeToken<Map<String, ? extends Long>>() {}.getType();
|
||||
Map<String, ? extends Long> map = gson.fromJson("{\"test\":123}", typeOfMap);
|
||||
|
@ -374,6 +410,7 @@ public class MapTest extends TestCase {
|
|||
/**
|
||||
* From bug report http://code.google.com/p/google-gson/issues/detail?id=95
|
||||
*/
|
||||
@Test
|
||||
public void testMapOfMapSerialization() {
|
||||
Map<String, Map<String, String>> map = new HashMap<>();
|
||||
Map<String, String> nestedMap = new HashMap<>();
|
||||
|
@ -389,6 +426,7 @@ public class MapTest extends TestCase {
|
|||
/**
|
||||
* From bug report http://code.google.com/p/google-gson/issues/detail?id=95
|
||||
*/
|
||||
@Test
|
||||
public void testMapOfMapDeserialization() {
|
||||
String json = "{nestedMap:{'2':'2','1':'1'}}";
|
||||
Type type = new TypeToken<Map<String, Map<String, String>>>(){}.getType();
|
||||
|
@ -401,6 +439,7 @@ public class MapTest extends TestCase {
|
|||
/**
|
||||
* From bug report http://code.google.com/p/google-gson/issues/detail?id=178
|
||||
*/
|
||||
@Test
|
||||
public void testMapWithQuotes() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("a\"b", "c\"d");
|
||||
|
@ -411,6 +450,7 @@ public class MapTest extends TestCase {
|
|||
/**
|
||||
* From issue 227.
|
||||
*/
|
||||
@Test
|
||||
public void testWriteMapsWithEmptyStringKey() {
|
||||
Map<String, Boolean> map = new HashMap<>();
|
||||
map.put("", true);
|
||||
|
@ -418,6 +458,7 @@ public class MapTest extends TestCase {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadMapsWithEmptyStringKey() {
|
||||
Map<String, Boolean> map = gson.fromJson("{\"\":true}", new TypeToken<Map<String, Boolean>>() {}.getType());
|
||||
assertEquals(Boolean.TRUE, map.get(""));
|
||||
|
@ -426,6 +467,7 @@ public class MapTest extends TestCase {
|
|||
/**
|
||||
* From bug report http://code.google.com/p/google-gson/issues/detail?id=204
|
||||
*/
|
||||
@Test
|
||||
public void testSerializeMaps() {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("a", 12);
|
||||
|
@ -454,6 +496,7 @@ public class MapTest extends TestCase {
|
|||
new Gson().toJson(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testInterfaceTypeMap() {
|
||||
MapClass element = new MapClass();
|
||||
TestTypes.Sub subType = new TestTypes.Sub();
|
||||
|
@ -475,6 +518,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals(expected, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testInterfaceTypeMapWithSerializer() {
|
||||
MapClass element = new MapClass();
|
||||
TestTypes.Sub subType = new TestTypes.Sub();
|
||||
|
@ -509,6 +553,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals(expected, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralMapField() throws Exception {
|
||||
MapWithGeneralMapParameters map = new MapWithGeneralMapParameters();
|
||||
map.map.put("string", "testString");
|
||||
|
@ -525,6 +570,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals(expected, gson.toJson(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexKeysSerialization() {
|
||||
Map<Point, String> map = new LinkedHashMap<>();
|
||||
map.put(new Point(2, 3), "a");
|
||||
|
@ -534,6 +580,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals(json, gson.toJson(map, Map.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexKeysDeserialization() {
|
||||
String json = "{'2,3':'a','5,7':'b'}";
|
||||
try {
|
||||
|
@ -543,6 +590,7 @@ public class MapTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringKeyDeserialization() {
|
||||
String json = "{'2,3':'a','5,7':'b'}";
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
|
@ -551,6 +599,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals(map, gson.fromJson(json, new TypeToken<Map<String, String>>() {}.getType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberKeyDeserialization() {
|
||||
String json = "{'2.3':'a','5.7':'b'}";
|
||||
Map<Double, String> map = new LinkedHashMap<>();
|
||||
|
@ -559,6 +608,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals(map, gson.fromJson(json, new TypeToken<Map<Double, String>>() {}.getType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleanKeyDeserialization() {
|
||||
String json = "{'true':'a','false':'b'}";
|
||||
Map<Boolean, String> map = new LinkedHashMap<>();
|
||||
|
@ -567,6 +617,7 @@ public class MapTest extends TestCase {
|
|||
assertEquals(map, gson.fromJson(json, new TypeToken<Map<Boolean, String>>() {}.getType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDeserializationWithDuplicateKeys() {
|
||||
try {
|
||||
gson.fromJson("{'a':1,'a':2}", new TypeToken<Map<String, Integer>>() {}.getType());
|
||||
|
@ -575,6 +626,7 @@ public class MapTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeMapOfMaps() {
|
||||
Type type = new TypeToken<Map<String, Map<String, String>>>() {}.getType();
|
||||
Map<String, Map<String, String>> map = newMap(
|
||||
|
@ -584,6 +636,7 @@ public class MapTest extends TestCase {
|
|||
gson.toJson(map, type).replace('"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeerializeMapOfMaps() {
|
||||
Type type = new TypeToken<Map<String, Map<String, String>>>() {}.getType();
|
||||
Map<String, Map<String, String>> map = newMap(
|
||||
|
@ -600,6 +653,7 @@ public class MapTest extends TestCase {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapNamePromotionWithJsonElementReader() {
|
||||
String json = "{'2.3':'a'}";
|
||||
Map<Double, String> map = new LinkedHashMap<>();
|
||||
|
|
|
@ -16,15 +16,19 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests for Gson serialization of a sub-class object while encountering a base-class type
|
||||
|
@ -32,15 +36,15 @@ import java.util.Map;
|
|||
* @author Inderjeet Singh
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class MoreSpecificTypeSerializationTest extends TestCase {
|
||||
public class MoreSpecificTypeSerializationTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubclassFields() {
|
||||
ClassWithBaseFields target = new ClassWithBaseFields(new Sub(1, 2));
|
||||
String json = gson.toJson(target);
|
||||
|
@ -48,6 +52,7 @@ public class MoreSpecificTypeSerializationTest extends TestCase {
|
|||
assertTrue(json.contains("\"s\":2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListOfSubclassFields() {
|
||||
Collection<Base> list = new ArrayList<>();
|
||||
list.add(new Base(1));
|
||||
|
@ -58,6 +63,7 @@ public class MoreSpecificTypeSerializationTest extends TestCase {
|
|||
assertTrue(json, json.contains("{\"s\":3,\"b\":2}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapOfSubclassFields() {
|
||||
Map<String, Base> map = new HashMap<>();
|
||||
map.put("base", new Base(1));
|
||||
|
@ -73,6 +79,7 @@ public class MoreSpecificTypeSerializationTest extends TestCase {
|
|||
/**
|
||||
* For parameterized type, Gson ignores the more-specific type and sticks to the declared type
|
||||
*/
|
||||
@Test
|
||||
public void testParameterizedSubclassFields() {
|
||||
ClassWithParameterizedBaseFields target = new ClassWithParameterizedBaseFields(
|
||||
new ParameterizedSub<>("one", "two"));
|
||||
|
@ -85,6 +92,7 @@ public class MoreSpecificTypeSerializationTest extends TestCase {
|
|||
* For parameterized type in a List, Gson ignores the more-specific type and sticks to
|
||||
* the declared type
|
||||
*/
|
||||
@Test
|
||||
public void testListOfParameterizedSubclassFields() {
|
||||
Collection<ParameterizedBase<String>> list = new ArrayList<>();
|
||||
list.add(new ParameterizedBase<>("one"));
|
||||
|
@ -100,6 +108,7 @@ public class MoreSpecificTypeSerializationTest extends TestCase {
|
|||
* For parameterized type in a map, Gson ignores the more-specific type and sticks to the
|
||||
* declared type
|
||||
*/
|
||||
@Test
|
||||
public void testMapOfParameterizedSubclassFields() {
|
||||
Map<String, ParameterizedBase<String>> map = new HashMap<>();
|
||||
map.put("base", new ParameterizedBase<>("one"));
|
||||
|
|
|
@ -15,6 +15,9 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.FieldNamingPolicy;
|
||||
import com.google.gson.FieldNamingStrategy;
|
||||
import com.google.gson.Gson;
|
||||
|
@ -23,7 +26,8 @@ import com.google.gson.annotations.SerializedName;
|
|||
import com.google.gson.common.TestTypes.ClassWithSerializedNameFields;
|
||||
import com.google.gson.common.TestTypes.StringWrapper;
|
||||
import java.lang.reflect.Field;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for naming policies.
|
||||
|
@ -31,15 +35,15 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class NamingPolicyTest extends TestCase {
|
||||
public class NamingPolicyTest {
|
||||
private GsonBuilder builder;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
builder = new GsonBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithNonDefaultFieldNamingPolicySerialization() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
|
||||
StringWrapper target = new StringWrapper("blah");
|
||||
|
@ -47,6 +51,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
+ target.someConstantStringInstanceField + "\"}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithNonDefaultFieldNamingPolicyDeserialiation() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
|
||||
String target = "{\"SomeConstantStringInstanceField\":\"someValue\"}";
|
||||
|
@ -54,6 +59,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
assertEquals("someValue", deserializedObject.someConstantStringInstanceField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithLowerCaseDashPolicySerialization() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
|
||||
StringWrapper target = new StringWrapper("blah");
|
||||
|
@ -61,6 +67,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
+ target.someConstantStringInstanceField + "\"}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithLowerCaseDotPolicySerialization() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DOTS).create();
|
||||
StringWrapper target = new StringWrapper("blah");
|
||||
|
@ -68,6 +75,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
+ target.someConstantStringInstanceField + "\"}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithLowerCaseDotPolicyDeserialiation() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DOTS).create();
|
||||
String target = "{\"some.constant.string.instance.field\":\"someValue\"}";
|
||||
|
@ -75,6 +83,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
assertEquals("someValue", deserializedObject.someConstantStringInstanceField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithLowerCaseDashPolicyDeserialiation() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
|
||||
String target = "{\"some-constant-string-instance-field\":\"someValue\"}";
|
||||
|
@ -82,6 +91,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
assertEquals("someValue", deserializedObject.someConstantStringInstanceField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithLowerCaseUnderscorePolicySerialization() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
|
||||
.create();
|
||||
|
@ -90,6 +100,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
+ target.someConstantStringInstanceField + "\"}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithLowerCaseUnderscorePolicyDeserialiation() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
|
||||
.create();
|
||||
|
@ -98,6 +109,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
assertEquals("someValue", deserializedObject.someConstantStringInstanceField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithSerializedNameFieldNamingPolicySerialization() {
|
||||
Gson gson = builder.create();
|
||||
ClassWithSerializedNameFields expected = new ClassWithSerializedNameFields(5, 6);
|
||||
|
@ -105,6 +117,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
assertEquals(expected.getExpectedJson(), actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithSerializedNameFieldNamingPolicyDeserialization() {
|
||||
Gson gson = builder.create();
|
||||
ClassWithSerializedNameFields expected = new ClassWithSerializedNameFields(5, 7);
|
||||
|
@ -113,6 +126,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
assertEquals(expected.f, actual.f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonDuplicateNameUsingSerializedNameFieldNamingPolicySerialization() {
|
||||
Gson gson = builder.create();
|
||||
try {
|
||||
|
@ -129,6 +143,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithUpperCamelCaseSpacesPolicySerialiation() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES)
|
||||
.create();
|
||||
|
@ -137,6 +152,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
+ target.someConstantStringInstanceField + "\"}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithUpperCamelCaseSpacesPolicyDeserialiation() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES)
|
||||
.create();
|
||||
|
@ -145,6 +161,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
assertEquals("someValue", deserializedObject.someConstantStringInstanceField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithUpperCaseUnderscorePolicySerialization() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CASE_WITH_UNDERSCORES)
|
||||
.create();
|
||||
|
@ -153,6 +170,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
+ target.someConstantStringInstanceField + "\"}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonWithUpperCaseUnderscorePolicyDeserialiation() {
|
||||
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CASE_WITH_UNDERSCORES)
|
||||
.create();
|
||||
|
@ -161,6 +179,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
assertEquals("someValue", deserializedObject.someConstantStringInstanceField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeprecatedNamingStrategy() throws Exception {
|
||||
Gson gson = builder.setFieldNamingStrategy(new UpperCaseNamingStrategy()).create();
|
||||
ClassWithDuplicateFields target = new ClassWithDuplicateFields(10);
|
||||
|
@ -168,6 +187,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
assertEquals("{\"A\":10}", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexFieldNameStrategy() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(new ClassWithComplexFieldName(10));
|
||||
|
@ -179,6 +199,7 @@ public class NamingPolicyTest extends TestCase {
|
|||
}
|
||||
|
||||
/** http://code.google.com/p/google-gson/issues/detail?id=349 */
|
||||
@Test
|
||||
public void testAtSignInSerializedName() {
|
||||
assertEquals("{\"@foo\":\"bar\"}", new Gson().toJson(new AtName()));
|
||||
}
|
||||
|
|
|
@ -16,22 +16,26 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||
import com.google.gson.common.TestTypes.ClassWithObjects;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for the different cases for serializing (or ignoring) null fields and object.
|
||||
|
@ -39,15 +43,15 @@ import java.util.Collection;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class NullObjectAndFieldTest extends TestCase {
|
||||
public class NullObjectAndFieldTest {
|
||||
private GsonBuilder gsonBuilder;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gsonBuilder = new GsonBuilder().serializeNulls();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelNullObjectSerialization() {
|
||||
Gson gson = gsonBuilder.create();
|
||||
String actual = gson.toJson(null);
|
||||
|
@ -57,12 +61,14 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
assertEquals("null", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelNullObjectDeserialization() throws Exception {
|
||||
Gson gson = gsonBuilder.create();
|
||||
String actual = gson.fromJson("null", String.class);
|
||||
assertNull(actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitSerializationOfNulls() {
|
||||
Gson gson = gsonBuilder.create();
|
||||
ClassWithObjects target = new ClassWithObjects(null);
|
||||
|
@ -71,12 +77,14 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitDeserializationOfNulls() throws Exception {
|
||||
Gson gson = gsonBuilder.create();
|
||||
ClassWithObjects target = gson.fromJson("{\"bag\":null}", ClassWithObjects.class);
|
||||
assertNull(target.bag);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitSerializationOfNullArrayMembers() {
|
||||
Gson gson = gsonBuilder.create();
|
||||
ClassWithMembers target = new ClassWithMembers();
|
||||
|
@ -87,6 +95,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
/**
|
||||
* Added to verify http://code.google.com/p/google-gson/issues/detail?id=68
|
||||
*/
|
||||
@Test
|
||||
public void testNullWrappedPrimitiveMemberSerialization() {
|
||||
Gson gson = gsonBuilder.serializeNulls().create();
|
||||
ClassWithNullWrappedPrimitive target = new ClassWithNullWrappedPrimitive();
|
||||
|
@ -97,6 +106,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
/**
|
||||
* Added to verify http://code.google.com/p/google-gson/issues/detail?id=68
|
||||
*/
|
||||
@Test
|
||||
public void testNullWrappedPrimitiveMemberDeserialization() {
|
||||
Gson gson = gsonBuilder.create();
|
||||
String json = "{'value':null}";
|
||||
|
@ -104,6 +114,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
assertNull(target.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitSerializationOfNullCollectionMembers() {
|
||||
Gson gson = gsonBuilder.create();
|
||||
ClassWithMembers target = new ClassWithMembers();
|
||||
|
@ -111,6 +122,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
assertTrue(json.contains("\"col\":null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitSerializationOfNullStringMembers() {
|
||||
Gson gson = gsonBuilder.create();
|
||||
ClassWithMembers target = new ClassWithMembers();
|
||||
|
@ -118,6 +130,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
assertTrue(json.contains("\"str\":null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomSerializationOfNulls() {
|
||||
gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer());
|
||||
Gson gson = gsonBuilder.create();
|
||||
|
@ -127,6 +140,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrintPrintingObjectWithNulls() throws Exception {
|
||||
gsonBuilder = new GsonBuilder();
|
||||
Gson gson = gsonBuilder.create();
|
||||
|
@ -138,6 +152,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
assertTrue(result.contains("\"str\":null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrintPrintingArraysWithNulls() throws Exception {
|
||||
gsonBuilder = new GsonBuilder();
|
||||
Gson gson = gsonBuilder.create();
|
||||
|
@ -150,6 +165,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
}
|
||||
|
||||
// test for issue 389
|
||||
@Test
|
||||
public void testAbsentJsonElementsAreSetToNull() {
|
||||
Gson gson = new Gson();
|
||||
ClassWithInitializedMembers target =
|
||||
|
@ -200,6 +216,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitNullSetsFieldToNullDuringDeserialization() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{value:null}";
|
||||
|
@ -207,6 +224,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
assertNull(obj.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomTypeAdapterPassesNullSerialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(ObjectWithField.class, new JsonSerializer<ObjectWithField>() {
|
||||
|
@ -221,6 +239,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||
assertFalse(json.contains("value1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomTypeAdapterPassesNullDesrialization() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(ObjectWithField.class, new JsonDeserializer<ObjectWithField>() {
|
||||
|
|
|
@ -16,6 +16,14 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
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.InstanceCreator;
|
||||
|
@ -45,7 +53,9 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for Json serialization and deserialization of regular classes.
|
||||
|
@ -53,14 +63,13 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class ObjectTest extends TestCase {
|
||||
public class ObjectTest {
|
||||
private Gson gson;
|
||||
private TimeZone oldTimeZone;
|
||||
private Locale oldLocale;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
|
||||
oldTimeZone = TimeZone.getDefault();
|
||||
|
@ -69,13 +78,13 @@ public class ObjectTest extends TestCase {
|
|||
Locale.setDefault(Locale.US);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
TimeZone.setDefault(oldTimeZone);
|
||||
Locale.setDefault(oldLocale);
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonInSingleQuotesDeserialization() {
|
||||
String json = "{'stringValue':'no message','intValue':10,'longValue':20}";
|
||||
BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class);
|
||||
|
@ -84,6 +93,7 @@ public class ObjectTest extends TestCase {
|
|||
assertEquals(20, target.longValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonInMixedQuotesDeserialization() {
|
||||
String json = "{\"stringValue\":'no message','intValue':10,'longValue':20}";
|
||||
BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class);
|
||||
|
@ -92,11 +102,13 @@ public class ObjectTest extends TestCase {
|
|||
assertEquals(20, target.longValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBagOfPrimitivesSerialization() throws Exception {
|
||||
BagOfPrimitives target = new BagOfPrimitives(10, 20, false, "stringValue");
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBagOfPrimitivesDeserialization() throws Exception {
|
||||
BagOfPrimitives src = new BagOfPrimitives(10, 20, false, "stringValue");
|
||||
String json = src.getExpectedJson();
|
||||
|
@ -104,11 +116,13 @@ public class ObjectTest extends TestCase {
|
|||
assertEquals(json, target.getExpectedJson());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBagOfPrimitiveWrappersSerialization() throws Exception {
|
||||
BagOfPrimitiveWrappers target = new BagOfPrimitiveWrappers(10L, 20, false);
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBagOfPrimitiveWrappersDeserialization() throws Exception {
|
||||
BagOfPrimitiveWrappers target = new BagOfPrimitiveWrappers(10L, 20, false);
|
||||
String jsonString = target.getExpectedJson();
|
||||
|
@ -116,17 +130,20 @@ public class ObjectTest extends TestCase {
|
|||
assertEquals(jsonString, target.getExpectedJson());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithTransientFieldsSerialization() throws Exception {
|
||||
ClassWithTransientFields<Long> target = new ClassWithTransientFields<>(1L);
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithTransientFieldsDeserialization() throws Exception {
|
||||
String json = "{\"longValue\":[1]}";
|
||||
ClassWithTransientFields<?> target = gson.fromJson(json, ClassWithTransientFields.class);
|
||||
assertEquals(json, target.getExpectedJson());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithTransientFieldsDeserializationTransientFieldsPassedInJsonAreIgnored()
|
||||
throws Exception {
|
||||
String json = "{\"transientLongValue\":1,\"longValue\":[1]}";
|
||||
|
@ -134,10 +151,12 @@ public class ObjectTest extends TestCase {
|
|||
assertFalse(target.transientLongValue != 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithNoFieldsSerialization() throws Exception {
|
||||
assertEquals("{}", gson.toJson(new ClassWithNoFields()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithNoFieldsDeserialization() throws Exception {
|
||||
String json = "{}";
|
||||
ClassWithNoFields target = gson.fromJson(json, ClassWithNoFields.class);
|
||||
|
@ -156,6 +175,7 @@ public class ObjectTest extends TestCase {
|
|||
String s;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassWithDuplicateFields() {
|
||||
try {
|
||||
gson.getAdapter(Subclass.class);
|
||||
|
@ -170,12 +190,14 @@ public class ObjectTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedSerialization() throws Exception {
|
||||
Nested target = new Nested(new BagOfPrimitives(10, 20, false, "stringValue"),
|
||||
new BagOfPrimitives(30, 40, true, "stringValue"));
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedDeserialization() throws Exception {
|
||||
String json = "{\"primitive1\":{\"longValue\":10,\"intValue\":20,\"booleanValue\":false,"
|
||||
+ "\"stringValue\":\"stringValue\"},\"primitive2\":{\"longValue\":30,\"intValue\":40,"
|
||||
|
@ -183,15 +205,18 @@ public class ObjectTest extends TestCase {
|
|||
Nested target = gson.fromJson(json, Nested.class);
|
||||
assertEquals(json, target.getExpectedJson());
|
||||
}
|
||||
@Test
|
||||
public void testNullSerialization() throws Exception {
|
||||
assertEquals("null", gson.toJson(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyStringDeserialization() throws Exception {
|
||||
Object object = gson.fromJson("", Object.class);
|
||||
assertNull(object);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTruncatedDeserialization() {
|
||||
try {
|
||||
gson.fromJson("[\"a\", \"b\",", new TypeToken<List<String>>() {}.getType());
|
||||
|
@ -200,17 +225,20 @@ public class ObjectTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullDeserialization() throws Exception {
|
||||
String myNullObject = null;
|
||||
Object object = gson.fromJson(myNullObject, Object.class);
|
||||
assertNull(object);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullFieldsSerialization() throws Exception {
|
||||
Nested target = new Nested(new BagOfPrimitives(10, 20, false, "stringValue"), null);
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullFieldsDeserialization() throws Exception {
|
||||
String json = "{\"primitive1\":{\"longValue\":10,\"intValue\":20,\"booleanValue\":false"
|
||||
+ ",\"stringValue\":\"stringValue\"}}";
|
||||
|
@ -218,28 +246,33 @@ public class ObjectTest extends TestCase {
|
|||
assertEquals(json, target.getExpectedJson());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfObjectsSerialization() throws Exception {
|
||||
ArrayOfObjects target = new ArrayOfObjects();
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfObjectsDeserialization() throws Exception {
|
||||
String json = new ArrayOfObjects().getExpectedJson();
|
||||
ArrayOfObjects target = gson.fromJson(json, ArrayOfObjects.class);
|
||||
assertEquals(json, target.getExpectedJson());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfArraysSerialization() throws Exception {
|
||||
ArrayOfArrays target = new ArrayOfArrays();
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfArraysDeserialization() throws Exception {
|
||||
String json = new ArrayOfArrays().getExpectedJson();
|
||||
ArrayOfArrays target = gson.fromJson(json, ArrayOfArrays.class);
|
||||
assertEquals(json, target.getExpectedJson());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOfObjectsAsFields() throws Exception {
|
||||
ClassWithObjects classWithObjects = new ClassWithObjects();
|
||||
BagOfPrimitives bagOfPrimitives = new BagOfPrimitives();
|
||||
|
@ -259,6 +292,7 @@ public class ObjectTest extends TestCase {
|
|||
/**
|
||||
* Created in response to Issue 14: http://code.google.com/p/google-gson/issues/detail?id=14
|
||||
*/
|
||||
@Test
|
||||
public void testNullArraysDeserialization() throws Exception {
|
||||
String json = "{\"array\": null}";
|
||||
ClassWithArray target = gson.fromJson(json, ClassWithArray.class);
|
||||
|
@ -268,12 +302,14 @@ public class ObjectTest extends TestCase {
|
|||
/**
|
||||
* Created in response to Issue 14: http://code.google.com/p/google-gson/issues/detail?id=14
|
||||
*/
|
||||
@Test
|
||||
public void testNullObjectFieldsDeserialization() throws Exception {
|
||||
String json = "{\"bag\": null}";
|
||||
ClassWithObjects target = gson.fromJson(json, ClassWithObjects.class);
|
||||
assertNull(target.bag);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyCollectionInAnObjectDeserialization() throws Exception {
|
||||
String json = "{\"children\":[]}";
|
||||
ClassWithCollectionField target = gson.fromJson(json, ClassWithCollectionField.class);
|
||||
|
@ -285,6 +321,7 @@ public class ObjectTest extends TestCase {
|
|||
Collection<String> children = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveArrayInAnObjectDeserialization() throws Exception {
|
||||
String json = "{\"longArray\":[0,1,2,3,4,5,6,7,8,9]}";
|
||||
PrimitiveArray target = gson.fromJson(json, PrimitiveArray.class);
|
||||
|
@ -294,29 +331,34 @@ public class ObjectTest extends TestCase {
|
|||
/**
|
||||
* Created in response to Issue 14: http://code.google.com/p/google-gson/issues/detail?id=14
|
||||
*/
|
||||
@Test
|
||||
public void testNullPrimitiveFieldsDeserialization() throws Exception {
|
||||
String json = "{\"longValue\":null}";
|
||||
BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class);
|
||||
assertEquals(BagOfPrimitives.DEFAULT_VALUE, target.longValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyCollectionInAnObjectSerialization() throws Exception {
|
||||
ClassWithCollectionField target = new ClassWithCollectionField();
|
||||
assertEquals("{\"children\":[]}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrivateNoArgConstructorDeserialization() throws Exception {
|
||||
ClassWithPrivateNoArgsConstructor target =
|
||||
gson.fromJson("{\"a\":20}", ClassWithPrivateNoArgsConstructor.class);
|
||||
assertEquals(20, target.a);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnonymousLocalClassesSerialization() throws Exception {
|
||||
assertEquals("null", gson.toJson(new ClassWithNoFields() {
|
||||
// empty anonymous class
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnonymousLocalClassesCustomSerialization() throws Exception {
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(ClassWithNoFields.class,
|
||||
|
@ -332,6 +374,7 @@ public class ObjectTest extends TestCase {
|
|||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveArrayFieldSerialization() {
|
||||
PrimitiveArray target = new PrimitiveArray(new long[] { 1L, 2L, 3L });
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
|
@ -341,6 +384,7 @@ public class ObjectTest extends TestCase {
|
|||
* Tests that a class field with type Object can be serialized properly.
|
||||
* See issue 54
|
||||
*/
|
||||
@Test
|
||||
public void testClassWithObjectFieldSerialization() {
|
||||
ClassWithObjectField obj = new ClassWithObjectField();
|
||||
obj.member = "abc";
|
||||
|
@ -353,6 +397,7 @@ public class ObjectTest extends TestCase {
|
|||
Object member;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInnerClassSerialization() {
|
||||
Parent p = new Parent();
|
||||
Parent.Child c = p.new Child();
|
||||
|
@ -361,6 +406,7 @@ public class ObjectTest extends TestCase {
|
|||
assertFalse(json.contains("value1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInnerClassDeserialization() {
|
||||
final Parent p = new Parent();
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(
|
||||
|
@ -429,6 +475,7 @@ public class ObjectTest extends TestCase {
|
|||
/**
|
||||
* In response to Issue 41 http://code.google.com/p/google-gson/issues/detail?id=41
|
||||
*/
|
||||
@Test
|
||||
public void testObjectFieldNamesWithoutQuotesDeserialization() {
|
||||
String json = "{longValue:1,'booleanValue':true,\"stringValue\":'bar'}";
|
||||
BagOfPrimitives bag = gson.fromJson(json, BagOfPrimitives.class);
|
||||
|
@ -437,6 +484,7 @@ public class ObjectTest extends TestCase {
|
|||
assertEquals("bar", bag.stringValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringFieldWithNumberValueDeserialization() {
|
||||
String json = "{\"stringValue\":1}";
|
||||
BagOfPrimitives bag = gson.fromJson(json, BagOfPrimitives.class);
|
||||
|
@ -454,6 +502,7 @@ public class ObjectTest extends TestCase {
|
|||
/**
|
||||
* Created to reproduce issue 140
|
||||
*/
|
||||
@Test
|
||||
public void testStringFieldWithEmptyValueSerialization() {
|
||||
ClassWithEmptyStringFields target = new ClassWithEmptyStringFields();
|
||||
target.a = "5794749";
|
||||
|
@ -466,6 +515,7 @@ public class ObjectTest extends TestCase {
|
|||
/**
|
||||
* Created to reproduce issue 140
|
||||
*/
|
||||
@Test
|
||||
public void testStringFieldWithEmptyValueDeserialization() {
|
||||
String json = "{a:\"5794749\",b:\"\",c:\"\"}";
|
||||
ClassWithEmptyStringFields target = gson.fromJson(json, ClassWithEmptyStringFields.class);
|
||||
|
@ -480,6 +530,7 @@ public class ObjectTest extends TestCase {
|
|||
String c = "";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonObjectSerialization() {
|
||||
Gson gson = new GsonBuilder().serializeNulls().create();
|
||||
JsonObject obj = new JsonObject();
|
||||
|
@ -490,6 +541,7 @@ public class ObjectTest extends TestCase {
|
|||
/**
|
||||
* Test for issue 215.
|
||||
*/
|
||||
@Test
|
||||
public void testSingletonLists() {
|
||||
Gson gson = new Gson();
|
||||
Product product = new Product();
|
||||
|
@ -519,6 +571,7 @@ public class ObjectTest extends TestCase {
|
|||
}
|
||||
|
||||
// http://code.google.com/p/google-gson/issues/detail?id=270
|
||||
@Test
|
||||
public void testDateAsMapObjectField() {
|
||||
HasObjectMap a = new HasObjectMap();
|
||||
a.map.put("date", new Date(0));
|
||||
|
@ -539,6 +592,7 @@ public class ObjectTest extends TestCase {
|
|||
* <p>Important: It is not documented that this is officially supported; this
|
||||
* test just checks the current behavior.
|
||||
*/
|
||||
@Test
|
||||
public void testStaticFieldSerialization() {
|
||||
// By default Gson should ignore static fields
|
||||
assertEquals("{}", gson.toJson(new ClassWithStaticField()));
|
||||
|
@ -561,6 +615,7 @@ public class ObjectTest extends TestCase {
|
|||
* <p>Important: It is not documented that this is officially supported; this
|
||||
* test just checks the current behavior.
|
||||
*/
|
||||
@Test
|
||||
public void testStaticFieldDeserialization() {
|
||||
// By default Gson should ignore static fields
|
||||
gson.fromJson("{\"s\":\"custom\"}", ClassWithStaticField.class);
|
||||
|
@ -597,6 +652,7 @@ public class ObjectTest extends TestCase {
|
|||
static final String s = "initial";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThrowingDefaultConstructor() {
|
||||
try {
|
||||
gson.fromJson("{}", ClassWithThrowingConstructor.class);
|
||||
|
|
|
@ -15,20 +15,22 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.common.TestTypes.ArrayOfObjects;
|
||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for pretty printing option.
|
||||
|
@ -36,18 +38,18 @@ import com.google.gson.reflect.TypeToken;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class PrettyPrintingTest extends TestCase {
|
||||
public class PrettyPrintingTest {
|
||||
|
||||
private static final boolean DEBUG = false;
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrettyPrintList() {
|
||||
BagOfPrimitives b = new BagOfPrimitives();
|
||||
List<BagOfPrimitives> listOfB = new LinkedList<>();
|
||||
|
@ -59,18 +61,21 @@ public class PrettyPrintingTest extends TestCase {
|
|||
print(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrettyPrintArrayOfObjects() {
|
||||
ArrayOfObjects target = new ArrayOfObjects();
|
||||
String json = gson.toJson(target);
|
||||
print(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrettyPrintArrayOfPrimitives() {
|
||||
int[] ints = new int[] { 1, 2, 3, 4, 5 };
|
||||
String json = gson.toJson(ints);
|
||||
assertEquals("[\n 1,\n 2,\n 3,\n 4,\n 5\n]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrettyPrintArrayOfPrimitiveArrays() {
|
||||
int[][] ints = new int[][] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 },
|
||||
{ 9, 0 }, { 10 } };
|
||||
|
@ -79,6 +84,7 @@ public class PrettyPrintingTest extends TestCase {
|
|||
+ "\n [\n 7,\n 8\n ],\n [\n 9,\n 0\n ],\n [\n 10\n ]\n]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrettyPrintListOfPrimitiveArrays() {
|
||||
List<Integer[]> list = Arrays.asList(new Integer[][] { { 1, 2 }, { 3, 4 },
|
||||
{ 5, 6 }, { 7, 8 }, { 9, 0 }, { 10 } });
|
||||
|
@ -87,6 +93,7 @@ public class PrettyPrintingTest extends TestCase {
|
|||
+ "\n [\n 7,\n 8\n ],\n [\n 9,\n 0\n ],\n [\n 10\n ]\n]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMap() {
|
||||
Map<String, Integer> map = new LinkedHashMap<>();
|
||||
map.put("abc", 1);
|
||||
|
@ -96,6 +103,7 @@ public class PrettyPrintingTest extends TestCase {
|
|||
}
|
||||
|
||||
// In response to bug 153
|
||||
@Test
|
||||
public void testEmptyMapField() {
|
||||
ClassWithMap obj = new ClassWithMap();
|
||||
obj.map = new LinkedHashMap<>();
|
||||
|
@ -109,6 +117,7 @@ public class PrettyPrintingTest extends TestCase {
|
|||
int value = 2;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleArrays() {
|
||||
int[][][] ints = new int[][][] { { { 1 }, { 2 } } };
|
||||
String json = gson.toJson(ints);
|
||||
|
|
|
@ -16,9 +16,11 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for Java Character values.
|
||||
|
@ -26,21 +28,22 @@ import com.google.gson.Gson;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class PrimitiveCharacterTest extends TestCase {
|
||||
public class PrimitiveCharacterTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveCharacterAutoboxedSerialization() {
|
||||
assertEquals("\"A\"", gson.toJson('A'));
|
||||
assertEquals("\"A\"", gson.toJson('A', char.class));
|
||||
assertEquals("\"A\"", gson.toJson('A', Character.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveCharacterAutoboxedDeserialization() {
|
||||
char expected = 'a';
|
||||
char actual = gson.fromJson("a", char.class);
|
||||
|
|
|
@ -17,6 +17,10 @@
|
|||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
@ -31,7 +35,8 @@ import java.math.BigDecimal;
|
|||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for Json primitive values: integers, and floating point numbers.
|
||||
|
@ -39,19 +44,20 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class PrimitiveTest extends TestCase {
|
||||
public class PrimitiveTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveIntegerAutoboxedSerialization() {
|
||||
assertEquals("1", gson.toJson(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveIntegerAutoboxedDeserialization() {
|
||||
int expected = 1;
|
||||
int actual = gson.fromJson("1", int.class);
|
||||
|
@ -61,6 +67,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteSerialization() {
|
||||
assertEquals("1", gson.toJson(1, byte.class));
|
||||
assertEquals("1", gson.toJson(1, Byte.class));
|
||||
|
@ -71,6 +78,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("1", gson.toJson(1.5, Byte.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteDeserialization() {
|
||||
Byte boxed = gson.fromJson("1", Byte.class);
|
||||
assertEquals(1, (byte)boxed);
|
||||
|
@ -81,6 +89,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertArrayEquals(new byte[] {-128, 0, 127, -1}, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteDeserializationLossy() {
|
||||
try {
|
||||
gson.fromJson("-129", byte.class);
|
||||
|
@ -104,6 +113,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortSerialization() {
|
||||
assertEquals("1", gson.toJson(1, short.class));
|
||||
assertEquals("1", gson.toJson(1, Short.class));
|
||||
|
@ -116,6 +126,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("1", gson.toJson(1.5, Short.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortDeserialization() {
|
||||
Short boxed = gson.fromJson("1", Short.class);
|
||||
assertEquals(1, (short)boxed);
|
||||
|
@ -126,6 +137,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertArrayEquals(new short[] {-32768, 0, 32767, -1}, shorts);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortDeserializationLossy() {
|
||||
try {
|
||||
gson.fromJson("-32769", short.class);
|
||||
|
@ -149,6 +161,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntSerialization() {
|
||||
assertEquals("1", gson.toJson(1, int.class));
|
||||
assertEquals("1", gson.toJson(1, Integer.class));
|
||||
|
@ -161,6 +174,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("1", gson.toJson(1.5, Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongSerialization() {
|
||||
assertEquals("1", gson.toJson(1L, long.class));
|
||||
assertEquals("1", gson.toJson(1L, Long.class));
|
||||
|
@ -172,6 +186,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("1", gson.toJson(1.5, Long.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatSerialization() {
|
||||
assertEquals("1.5", gson.toJson(1.5f, float.class));
|
||||
assertEquals("1.5", gson.toJson(1.5f, Float.class));
|
||||
|
@ -186,6 +201,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("Infinity", gson.toJson(Double.MAX_VALUE, Float.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleSerialization() {
|
||||
assertEquals("1.5", gson.toJson(1.5, double.class));
|
||||
assertEquals("1.5", gson.toJson(1.5, Double.class));
|
||||
|
@ -197,6 +213,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(Double.toString(Long.MAX_VALUE - 10L), gson.toJson(Long.MAX_VALUE - 10L, Double.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveIntegerAutoboxedInASingleElementArraySerialization() {
|
||||
int target[] = {-9332};
|
||||
assertEquals("[-9332]", gson.toJson(target));
|
||||
|
@ -204,22 +221,26 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("[-9332]", gson.toJson(target, Integer[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReallyLongValuesSerialization() {
|
||||
long value = 333961828784581L;
|
||||
assertEquals("333961828784581", gson.toJson(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReallyLongValuesDeserialization() {
|
||||
String json = "333961828784581";
|
||||
long value = gson.fromJson(json, Long.class);
|
||||
assertEquals(333961828784581L, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveLongAutoboxedSerialization() {
|
||||
assertEquals("1", gson.toJson(1L, long.class));
|
||||
assertEquals("1", gson.toJson(1L, Long.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveLongAutoboxedDeserialization() {
|
||||
long expected = 1L;
|
||||
long actual = gson.fromJson("1", long.class);
|
||||
|
@ -229,6 +250,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveLongAutoboxedInASingleElementArraySerialization() {
|
||||
long[] target = {-23L};
|
||||
assertEquals("[-23]", gson.toJson(target));
|
||||
|
@ -236,11 +258,13 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("[-23]", gson.toJson(target, Long[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveBooleanAutoboxedSerialization() {
|
||||
assertEquals("true", gson.toJson(true));
|
||||
assertEquals("false", gson.toJson(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleanDeserialization() {
|
||||
boolean value = gson.fromJson("false", boolean.class);
|
||||
assertEquals(false, value);
|
||||
|
@ -248,6 +272,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(true, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveBooleanAutoboxedInASingleElementArraySerialization() {
|
||||
boolean target[] = {false};
|
||||
assertEquals("[false]", gson.toJson(target));
|
||||
|
@ -255,6 +280,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("[false]", gson.toJson(target, Boolean[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberSerialization() {
|
||||
Number expected = 1L;
|
||||
String json = gson.toJson(expected);
|
||||
|
@ -264,6 +290,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expected.toString(), json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberDeserialization() {
|
||||
String json = "1";
|
||||
Number expected = Integer.valueOf(json);
|
||||
|
@ -280,24 +307,28 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(1L, actual.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberAsStringDeserialization() {
|
||||
Number value = gson.fromJson("\"18\"", Number.class);
|
||||
assertEquals(18, value.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveDoubleAutoboxedSerialization() {
|
||||
assertEquals("-122.08234335", gson.toJson(-122.08234335D));
|
||||
assertEquals("122.08112002", gson.toJson(122.08112002D));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveDoubleAutoboxedDeserialization() {
|
||||
double actual = gson.fromJson("-122.08858585", double.class);
|
||||
assertEquals(-122.08858585D, actual);
|
||||
assertEquals(-122.08858585D, actual, 0);
|
||||
|
||||
actual = gson.fromJson("122.023900008000", Double.class);
|
||||
assertEquals(122.023900008D, actual);
|
||||
assertEquals(122.023900008D, actual, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveDoubleAutoboxedInASingleElementArraySerialization() {
|
||||
double[] target = {-122.08D};
|
||||
assertEquals("[-122.08]", gson.toJson(target));
|
||||
|
@ -305,6 +336,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("[-122.08]", gson.toJson(target, Double[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleAsStringRepresentationDeserialization() {
|
||||
String doubleValue = "1.0043E+5";
|
||||
Double expected = Double.valueOf(doubleValue);
|
||||
|
@ -312,9 +344,10 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
|
||||
double actual1 = gson.fromJson(doubleValue, double.class);
|
||||
assertEquals(expected.doubleValue(), actual1);
|
||||
assertEquals(expected, actual1, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleNoFractAsStringRepresentationDeserialization() {
|
||||
String doubleValue = "1E+5";
|
||||
Double expected = Double.valueOf(doubleValue);
|
||||
|
@ -322,22 +355,24 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
|
||||
double actual1 = gson.fromJson(doubleValue, double.class);
|
||||
assertEquals(expected.doubleValue(), actual1);
|
||||
assertEquals(expected, actual1, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleArrayDeserialization() {
|
||||
String json = "[0.0, 0.004761904761904762, 3.4013606962703525E-4, 7.936508173034305E-4,"
|
||||
+ "0.0011904761904761906, 0.0]";
|
||||
double[] values = gson.fromJson(json, double[].class);
|
||||
assertEquals(6, values.length);
|
||||
assertEquals(0.0, values[0]);
|
||||
assertEquals(0.004761904761904762, values[1]);
|
||||
assertEquals(3.4013606962703525E-4, values[2]);
|
||||
assertEquals(7.936508173034305E-4, values[3]);
|
||||
assertEquals(0.0011904761904761906, values[4]);
|
||||
assertEquals(0.0, values[5]);
|
||||
assertEquals(6, values.length, 0);
|
||||
assertEquals(0.0, values[0], 0);
|
||||
assertEquals(0.004761904761904762, values[1], 0);
|
||||
assertEquals(3.4013606962703525E-4, values[2], 0);
|
||||
assertEquals(7.936508173034305E-4, values[3], 0);
|
||||
assertEquals(0.0011904761904761906, values[4], 0);
|
||||
assertEquals(0.0, values[5], 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLargeDoubleDeserialization() {
|
||||
String doubleValue = "1.234567899E8";
|
||||
Double expected = Double.valueOf(doubleValue);
|
||||
|
@ -345,21 +380,24 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
|
||||
double actual1 = gson.fromJson(doubleValue, double.class);
|
||||
assertEquals(expected.doubleValue(), actual1);
|
||||
assertEquals(expected, actual1, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalSerialization() {
|
||||
BigDecimal target = new BigDecimal("-122.0e-21");
|
||||
String json = gson.toJson(target);
|
||||
assertEquals(target, new BigDecimal(json));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalDeserialization() {
|
||||
BigDecimal target = new BigDecimal("-122.0e-21");
|
||||
String json = "-122.0e-21";
|
||||
assertEquals(target, gson.fromJson(json, BigDecimal.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalInASingleElementArraySerialization() {
|
||||
BigDecimal[] target = {new BigDecimal("-122.08e-21")};
|
||||
String json = gson.toJson(target);
|
||||
|
@ -371,18 +409,21 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(target[0], new BigDecimal(actual));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSmallValueForBigDecimalSerialization() {
|
||||
BigDecimal target = new BigDecimal("1.55");
|
||||
String actual = gson.toJson(target);
|
||||
assertEquals(target.toString(), actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSmallValueForBigDecimalDeserialization() {
|
||||
BigDecimal expected = new BigDecimal("1.55");
|
||||
BigDecimal actual = gson.fromJson("1.55", BigDecimal.class);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalPreservePrecisionSerialization() {
|
||||
String expectedValue = "1.000";
|
||||
BigDecimal obj = new BigDecimal(expectedValue);
|
||||
|
@ -391,6 +432,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expectedValue, actualValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalPreservePrecisionDeserialization() {
|
||||
String json = "1.000";
|
||||
BigDecimal expected = new BigDecimal(json);
|
||||
|
@ -399,6 +441,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalAsStringRepresentationDeserialization() {
|
||||
String doubleValue = "0.05E+5";
|
||||
BigDecimal expected = new BigDecimal(doubleValue);
|
||||
|
@ -406,6 +449,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalNoFractAsStringRepresentationDeserialization() {
|
||||
String doubleValue = "5E+5";
|
||||
BigDecimal expected = new BigDecimal(doubleValue);
|
||||
|
@ -413,17 +457,20 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigIntegerSerialization() {
|
||||
BigInteger target = new BigInteger("12121211243123245845384534687435634558945453489543985435");
|
||||
assertEquals(target.toString(), gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigIntegerDeserialization() {
|
||||
String json = "12121211243123245845384534687435634558945453489543985435";
|
||||
BigInteger target = new BigInteger(json);
|
||||
assertEquals(target, gson.fromJson(json, BigInteger.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigIntegerInASingleElementArraySerialization() {
|
||||
BigInteger[] target = {new BigInteger("1212121243434324323254365345367456456456465464564564")};
|
||||
String json = gson.toJson(target);
|
||||
|
@ -435,18 +482,21 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(target[0], new BigInteger(actual));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSmallValueForBigIntegerSerialization() {
|
||||
BigInteger target = new BigInteger("15");
|
||||
String actual = gson.toJson(target);
|
||||
assertEquals(target.toString(), actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSmallValueForBigIntegerDeserialization() {
|
||||
BigInteger expected = new BigInteger("15");
|
||||
BigInteger actual = gson.fromJson("15", BigInteger.class);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadValueForBigIntegerDeserialization() {
|
||||
try {
|
||||
gson.fromJson("15.099", BigInteger.class);
|
||||
|
@ -454,18 +504,21 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazilyParsedNumberSerialization() {
|
||||
LazilyParsedNumber target = new LazilyParsedNumber("1.5");
|
||||
String actual = gson.toJson(target);
|
||||
assertEquals("1.5", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazilyParsedNumberDeserialization() {
|
||||
LazilyParsedNumber expected = new LazilyParsedNumber("1.5");
|
||||
LazilyParsedNumber actual = gson.fromJson("1.5", LazilyParsedNumber.class);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoreSpecificSerialization() {
|
||||
Gson gson = new Gson();
|
||||
String expected = "This is a string";
|
||||
|
@ -480,6 +533,7 @@ public class PrimitiveTest extends TestCase {
|
|||
return json.substring(json.indexOf('[') + 1, json.indexOf(']'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleNaNSerializationNotSupportedByDefault() {
|
||||
try {
|
||||
double nan = Double.NaN;
|
||||
|
@ -494,6 +548,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleNaNSerialization() {
|
||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||
double nan = Double.NaN;
|
||||
|
@ -501,11 +556,13 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("NaN", gson.toJson(Double.NaN));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleNaNDeserialization() {
|
||||
assertTrue(Double.isNaN(gson.fromJson("NaN", Double.class)));
|
||||
assertTrue(Double.isNaN(gson.fromJson("NaN", double.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatNaNSerializationNotSupportedByDefault() {
|
||||
try {
|
||||
float nan = Float.NaN;
|
||||
|
@ -520,6 +577,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatNaNSerialization() {
|
||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||
float nan = Float.NaN;
|
||||
|
@ -527,11 +585,13 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("NaN", gson.toJson(Float.NaN));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatNaNDeserialization() {
|
||||
assertTrue(Float.isNaN(gson.fromJson("NaN", Float.class)));
|
||||
assertTrue(Float.isNaN(gson.fromJson("NaN", float.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalNaNDeserializationNotSupported() {
|
||||
try {
|
||||
gson.fromJson("NaN", BigDecimal.class);
|
||||
|
@ -540,6 +600,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleInfinitySerializationNotSupportedByDefault() {
|
||||
try {
|
||||
double infinity = Double.POSITIVE_INFINITY;
|
||||
|
@ -554,6 +615,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleInfinitySerialization() {
|
||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||
double infinity = Double.POSITIVE_INFINITY;
|
||||
|
@ -561,11 +623,13 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("Infinity", gson.toJson(Double.POSITIVE_INFINITY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleInfinityDeserialization() {
|
||||
assertTrue(Double.isInfinite(gson.fromJson("Infinity", Double.class)));
|
||||
assertTrue(Double.isInfinite(gson.fromJson("Infinity", double.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatInfinitySerializationNotSupportedByDefault() {
|
||||
try {
|
||||
float infinity = Float.POSITIVE_INFINITY;
|
||||
|
@ -580,6 +644,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatInfinitySerialization() {
|
||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||
float infinity = Float.POSITIVE_INFINITY;
|
||||
|
@ -587,11 +652,13 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("Infinity", gson.toJson(Float.POSITIVE_INFINITY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatInfinityDeserialization() {
|
||||
assertTrue(Float.isInfinite(gson.fromJson("Infinity", Float.class)));
|
||||
assertTrue(Float.isInfinite(gson.fromJson("Infinity", float.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalInfinityDeserializationNotSupported() {
|
||||
try {
|
||||
gson.fromJson("Infinity", BigDecimal.class);
|
||||
|
@ -600,6 +667,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeInfinitySerializationNotSupportedByDefault() {
|
||||
try {
|
||||
double negativeInfinity = Double.NEGATIVE_INFINITY;
|
||||
|
@ -614,6 +682,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeInfinitySerialization() {
|
||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||
double negativeInfinity = Double.NEGATIVE_INFINITY;
|
||||
|
@ -621,11 +690,13 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("-Infinity", gson.toJson(Double.NEGATIVE_INFINITY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeInfinityDeserialization() {
|
||||
assertTrue(Double.isInfinite(gson.fromJson("-Infinity", double.class)));
|
||||
assertTrue(Double.isInfinite(gson.fromJson("-Infinity", Double.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeInfinityFloatSerializationNotSupportedByDefault() {
|
||||
try {
|
||||
float negativeInfinity = Float.NEGATIVE_INFINITY;
|
||||
|
@ -640,6 +711,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeInfinityFloatSerialization() {
|
||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||
float negativeInfinity = Float.NEGATIVE_INFINITY;
|
||||
|
@ -647,11 +719,13 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("-Infinity", gson.toJson(Float.NEGATIVE_INFINITY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeInfinityFloatDeserialization() {
|
||||
assertTrue(Float.isInfinite(gson.fromJson("-Infinity", float.class)));
|
||||
assertTrue(Float.isInfinite(gson.fromJson("-Infinity", Float.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalNegativeInfinityDeserializationNotSupported() {
|
||||
try {
|
||||
gson.fromJson("-Infinity", BigDecimal.class);
|
||||
|
@ -660,6 +734,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongAsStringSerialization() throws Exception {
|
||||
gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create();
|
||||
String result = gson.toJson(15L);
|
||||
|
@ -670,6 +745,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals("2", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongAsStringDeserialization() throws Exception {
|
||||
long value = gson.fromJson("\"15\"", long.class);
|
||||
assertEquals(15, value);
|
||||
|
@ -679,6 +755,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(25, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuotedStringSerializationAndDeserialization() throws Exception {
|
||||
String value = "String Blah Blah Blah...1, 2, 3";
|
||||
String serializedForm = gson.toJson(value);
|
||||
|
@ -688,6 +765,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertEquals(value, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnquotedStringDeserializationFails() throws Exception {
|
||||
assertEquals("UnquotedSingleWord", gson.fromJson("UnquotedSingleWord", String.class));
|
||||
|
||||
|
@ -698,6 +776,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHtmlCharacterSerialization() throws Exception {
|
||||
String target = "<script>var a = 12;</script>";
|
||||
String result = gson.toJson(target);
|
||||
|
@ -708,6 +787,7 @@ public class PrimitiveTest extends TestCase {
|
|||
assertTrue(result.equals('"' + target + '"'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializePrimitiveWrapperAsObjectField() {
|
||||
String json = "{i:10}";
|
||||
ClassWithIntegerField target = gson.fromJson(json, ClassWithIntegerField.class);
|
||||
|
@ -718,12 +798,14 @@ public class PrimitiveTest extends TestCase {
|
|||
Integer i;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveClassLiteral() {
|
||||
assertEquals(1, gson.fromJson("1", int.class).intValue());
|
||||
assertEquals(1, gson.fromJson(new StringReader("1"), int.class).intValue());
|
||||
assertEquals(1, gson.fromJson(new JsonPrimitive(1), int.class).intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsLongPrimitive() {
|
||||
try {
|
||||
gson.fromJson("{'abc':1}", long.class);
|
||||
|
@ -731,6 +813,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsLongWrapper() {
|
||||
try {
|
||||
gson.fromJson("[1,2,3]", Long.class);
|
||||
|
@ -738,6 +821,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsInt() {
|
||||
try {
|
||||
gson.fromJson("[1, 2, 3, 4]", int.class);
|
||||
|
@ -745,6 +829,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsInteger() {
|
||||
try {
|
||||
gson.fromJson("{}", Integer.class);
|
||||
|
@ -752,6 +837,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsShortPrimitive() {
|
||||
try {
|
||||
gson.fromJson("{'abc':1}", short.class);
|
||||
|
@ -759,6 +845,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsShortWrapper() {
|
||||
try {
|
||||
gson.fromJson("['a','b']", Short.class);
|
||||
|
@ -766,6 +853,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsDoublePrimitive() {
|
||||
try {
|
||||
gson.fromJson("[1,2]", double.class);
|
||||
|
@ -773,6 +861,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsDoubleWrapper() {
|
||||
try {
|
||||
gson.fromJson("{'abc':1}", Double.class);
|
||||
|
@ -780,6 +869,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsFloatPrimitive() {
|
||||
try {
|
||||
gson.fromJson("{'abc':1}", float.class);
|
||||
|
@ -787,6 +877,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsFloatWrapper() {
|
||||
try {
|
||||
gson.fromJson("[1,2,3]", Float.class);
|
||||
|
@ -794,6 +885,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsBytePrimitive() {
|
||||
try {
|
||||
gson.fromJson("{'abc':1}", byte.class);
|
||||
|
@ -801,6 +893,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsByteWrapper() {
|
||||
try {
|
||||
gson.fromJson("[1,2,3,4]", Byte.class);
|
||||
|
@ -808,6 +901,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsBooleanPrimitive() {
|
||||
try {
|
||||
gson.fromJson("{'abc':1}", boolean.class);
|
||||
|
@ -815,6 +909,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsBooleanWrapper() {
|
||||
try {
|
||||
gson.fromJson("[1,2,3,4]", Boolean.class);
|
||||
|
@ -822,6 +917,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsBigDecimal() {
|
||||
try {
|
||||
gson.fromJson("[1,2,3,4]", BigDecimal.class);
|
||||
|
@ -829,6 +925,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsBigDecimal() {
|
||||
try {
|
||||
gson.fromJson("{'a':1}", BigDecimal.class);
|
||||
|
@ -836,6 +933,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsBigInteger() {
|
||||
try {
|
||||
gson.fromJson("[1,2,3,4]", BigInteger.class);
|
||||
|
@ -843,6 +941,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsBigInteger() {
|
||||
try {
|
||||
gson.fromJson("{'c':2}", BigInteger.class);
|
||||
|
@ -850,6 +949,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonArrayAsNumber() {
|
||||
try {
|
||||
gson.fromJson("[1,2,3,4]", Number.class);
|
||||
|
@ -857,6 +957,7 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeJsonObjectAsNumber() {
|
||||
try {
|
||||
gson.fromJson("{'c':2}", Number.class);
|
||||
|
@ -864,10 +965,12 @@ public class PrimitiveTest extends TestCase {
|
|||
} catch (JsonSyntaxException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingDecimalPointValueZeroSucceeds() {
|
||||
assertEquals(1, (int) gson.fromJson("1.0", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingNonZeroDecimalPointValuesAsIntegerFails() {
|
||||
try {
|
||||
gson.fromJson("1.02", Byte.class);
|
||||
|
@ -891,6 +994,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingBigDecimalAsIntegerFails() {
|
||||
try {
|
||||
gson.fromJson("-122.08e-213", Integer.class);
|
||||
|
@ -899,6 +1003,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingBigIntegerAsInteger() {
|
||||
try {
|
||||
gson.fromJson("12121211243123245845384534687435634558945453489543985435", Integer.class);
|
||||
|
@ -907,6 +1012,7 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingBigIntegerAsLong() {
|
||||
try {
|
||||
gson.fromJson("12121211243123245845384534687435634558945453489543985435", Long.class);
|
||||
|
@ -915,29 +1021,33 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueVeryCloseToZeroIsZero() {
|
||||
assertEquals(0, (byte) gson.fromJson("-122.08e-2132", byte.class));
|
||||
assertEquals(0, (short) gson.fromJson("-122.08e-2132", short.class));
|
||||
assertEquals(0, (int) gson.fromJson("-122.08e-2132", int.class));
|
||||
assertEquals(0, (long) gson.fromJson("-122.08e-2132", long.class));
|
||||
assertEquals(-0.0f, gson.fromJson("-122.08e-2132", float.class));
|
||||
assertEquals(-0.0, gson.fromJson("-122.08e-2132", double.class));
|
||||
assertEquals(0.0f, gson.fromJson("122.08e-2132", float.class));
|
||||
assertEquals(0.0, gson.fromJson("122.08e-2132", double.class));
|
||||
assertEquals(-0.0f, gson.fromJson("-122.08e-2132", float.class), 0);
|
||||
assertEquals(-0.0, gson.fromJson("-122.08e-2132", double.class), 0);
|
||||
assertEquals(0.0f, gson.fromJson("122.08e-2132", float.class), 0);
|
||||
assertEquals(0.0, gson.fromJson("122.08e-2132", double.class), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingBigDecimalAsFloat() {
|
||||
String json = "-122.08e-2132332";
|
||||
float actual = gson.fromJson(json, float.class);
|
||||
assertEquals(-0.0f, actual);
|
||||
assertEquals(-0.0f, actual, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingBigDecimalAsDouble() {
|
||||
String json = "-122.08e-2132332";
|
||||
double actual = gson.fromJson(json, double.class);
|
||||
assertEquals(-0.0d, actual);
|
||||
assertEquals(-0.0d, actual, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingBigDecimalAsBigIntegerFails() {
|
||||
try {
|
||||
gson.fromJson("-122.08e-213", BigInteger.class);
|
||||
|
@ -946,12 +1056,14 @@ public class PrimitiveTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializingBigIntegerAsBigDecimal() {
|
||||
BigDecimal actual =
|
||||
gson.fromJson("12121211243123245845384534687435634558945453489543985435", BigDecimal.class);
|
||||
assertEquals("12121211243123245845384534687435634558945453489543985435", actual.toPlainString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringsAsBooleans() {
|
||||
String json = "['true', 'false', 'TRUE', 'yes', '1']";
|
||||
assertEquals(Arrays.asList(true, false, true, false, false),
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
|
@ -25,7 +28,8 @@ import com.google.gson.common.TestTypes.Nested;
|
|||
import com.google.gson.common.TestTypes.PrimitiveArray;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for print formatting.
|
||||
|
@ -33,16 +37,16 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class PrintFormattingTest extends TestCase {
|
||||
public class PrintFormattingTest {
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompactFormattingLeavesNoWhiteSpace() {
|
||||
List<Object> list = new ArrayList<>();
|
||||
list.add(new BagOfPrimitives());
|
||||
|
@ -54,6 +58,7 @@ public class PrintFormattingTest extends TestCase {
|
|||
assertContainsNoWhiteSpace(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonObjectWithNullValues() {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty("field1", "value1");
|
||||
|
@ -63,6 +68,7 @@ public class PrintFormattingTest extends TestCase {
|
|||
assertFalse(json.contains("field2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonObjectWithNullValuesSerialized() {
|
||||
gson = new GsonBuilder().serializeNulls().create();
|
||||
JsonObject obj = new JsonObject();
|
||||
|
|
|
@ -15,41 +15,44 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests to validate serialization of parameterized types without explicit types
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class RawSerializationTest extends TestCase {
|
||||
public class RawSerializationTest {
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfPrimitives() {
|
||||
Collection<Integer> ints = Arrays.asList(1, 2, 3, 4, 5);
|
||||
String json = gson.toJson(ints);
|
||||
assertEquals("[1,2,3,4,5]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfObjects() {
|
||||
Collection<Foo> foos = Arrays.asList(new Foo(1), new Foo(2));
|
||||
String json = gson.toJson(foos);
|
||||
assertEquals("[{\"b\":1},{\"b\":2}]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParameterizedObject() {
|
||||
Bar<Foo> bar = new Bar<>(new Foo(1));
|
||||
String expectedJson = "{\"t\":{\"b\":1}}";
|
||||
|
@ -61,6 +64,7 @@ public class RawSerializationTest extends TestCase {
|
|||
assertEquals(expectedJson, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoLevelParameterizedObject() {
|
||||
Bar<Bar<Foo>> bar = new Bar<>(new Bar<>(new Foo(1)));
|
||||
String expectedJson = "{\"t\":{\"t\":{\"b\":1}}}";
|
||||
|
@ -72,6 +76,7 @@ public class RawSerializationTest extends TestCase {
|
|||
assertEquals(expectedJson, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThreeLevelParameterizedObject() {
|
||||
Bar<Bar<Bar<Foo>>> bar = new Bar<>(new Bar<>(new Bar<>(new Foo(1))));
|
||||
String expectedJson = "{\"t\":{\"t\":{\"t\":{\"b\":1}}}}";
|
||||
|
|
|
@ -15,6 +15,12 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
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.JsonStreamParser;
|
||||
|
@ -30,7 +36,8 @@ import java.io.StringWriter;
|
|||
import java.io.Writer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for the support of {@link Reader}s and {@link Writer}s.
|
||||
|
@ -38,15 +45,15 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class ReadersWritersTest extends TestCase {
|
||||
public class ReadersWritersTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriterForSerialization() throws Exception {
|
||||
Writer writer = new StringWriter();
|
||||
BagOfPrimitives src = new BagOfPrimitives();
|
||||
|
@ -54,6 +61,7 @@ public class ReadersWritersTest extends TestCase {
|
|||
assertEquals(src.getExpectedJson(), writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReaderForDeserialization() throws Exception {
|
||||
BagOfPrimitives expected = new BagOfPrimitives();
|
||||
Reader json = new StringReader(expected.getExpectedJson());
|
||||
|
@ -61,18 +69,21 @@ public class ReadersWritersTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelNullObjectSerializationWithWriter() {
|
||||
StringWriter writer = new StringWriter();
|
||||
gson.toJson(null, writer);
|
||||
assertEquals("null", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelNullObjectDeserializationWithReader() {
|
||||
StringReader reader = new StringReader("null");
|
||||
Integer nullIntObject = gson.fromJson(reader, Integer.class);
|
||||
assertNull(nullIntObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelNullObjectSerializationWithWriterAndSerializeNulls() {
|
||||
Gson gson = new GsonBuilder().serializeNulls().create();
|
||||
StringWriter writer = new StringWriter();
|
||||
|
@ -80,6 +91,7 @@ public class ReadersWritersTest extends TestCase {
|
|||
assertEquals("null", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTopLevelNullObjectDeserializationWithReaderAndSerializeNulls() {
|
||||
Gson gson = new GsonBuilder().serializeNulls().create();
|
||||
StringReader reader = new StringReader("null");
|
||||
|
@ -87,6 +99,7 @@ public class ReadersWritersTest extends TestCase {
|
|||
assertNull(nullIntObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadWriteTwoStrings() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
CharArrayWriter writer = new CharArrayWriter();
|
||||
|
@ -100,6 +113,7 @@ public class ReadersWritersTest extends TestCase {
|
|||
assertEquals("two", actualTwo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadWriteTwoObjects() throws IOException {
|
||||
Gson gson = new Gson();
|
||||
CharArrayWriter writer = new CharArrayWriter();
|
||||
|
@ -116,6 +130,7 @@ public class ReadersWritersTest extends TestCase {
|
|||
assertFalse(parser.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeMismatchThrowsJsonSyntaxExceptionForStrings() {
|
||||
try {
|
||||
gson.fromJson("true", new TypeToken<Map<String, String>>() {}.getType());
|
||||
|
@ -124,6 +139,7 @@ public class ReadersWritersTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeMismatchThrowsJsonSyntaxExceptionForReaders() {
|
||||
try {
|
||||
gson.fromJson(new StringReader("true"), new TypeToken<Map<String, String>>() {}.getType());
|
||||
|
@ -136,6 +152,7 @@ public class ReadersWritersTest extends TestCase {
|
|||
* Verifies that passing an {@link Appendable} which is not an instance of {@link Writer}
|
||||
* to {@code Gson.toJson} works correctly.
|
||||
*/
|
||||
@Test
|
||||
public void testToJsonAppendable() {
|
||||
class CustomAppendable implements Appendable {
|
||||
final StringBuilder stringBuilder = new StringBuilder();
|
||||
|
|
|
@ -36,8 +36,8 @@ public class ReflectionAccessTest {
|
|||
return classLoader.loadClass(c.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("removal") // java.lang.SecurityManager deprecation in Java 17
|
||||
@Test
|
||||
public void testRestrictiveSecurityManager() throws Exception {
|
||||
// Must use separate class loader, otherwise permission is not checked, see Class.getDeclaredFields()
|
||||
Class<?> clazz = loadClassWithDifferentClassLoader(ClassWithPrivateMembers.class);
|
||||
|
|
|
@ -15,11 +15,7 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
|
@ -33,11 +29,15 @@ import com.google.gson.internal.Streams;
|
|||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for the RuntimeTypeAdapterFactory feature in extras.
|
||||
*/
|
||||
public final class RuntimeTypeAdapterFactoryFunctionalTest extends TestCase {
|
||||
public final class RuntimeTypeAdapterFactoryFunctionalTest {
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
|
@ -45,6 +45,7 @@ public final class RuntimeTypeAdapterFactoryFunctionalTest extends TestCase {
|
|||
* This test also ensures that {@link TypeAdapterFactory} registered through {@link JsonAdapter}
|
||||
* work correctly for {@link Gson#getDelegateAdapter(TypeAdapterFactory, TypeToken)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSubclassesAutomaticallySerialized() throws Exception {
|
||||
Shape shape = new Circle(25);
|
||||
String json = gson.toJson(shape);
|
||||
|
|
|
@ -16,18 +16,21 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests for security-related aspects of Gson
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class SecurityTest extends TestCase {
|
||||
public class SecurityTest {
|
||||
/**
|
||||
* Keep this in sync with Gson.JSON_NON_EXECUTABLE_PREFIX
|
||||
*/
|
||||
|
@ -35,18 +38,19 @@ public class SecurityTest extends TestCase {
|
|||
|
||||
private GsonBuilder gsonBuilder;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gsonBuilder = new GsonBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonExecutableJsonSerialization() {
|
||||
Gson gson = gsonBuilder.generateNonExecutableJson().create();
|
||||
String json = gson.toJson(new BagOfPrimitives());
|
||||
assertTrue(json.startsWith(JSON_NON_EXECUTABLE_PREFIX));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonExecutableJsonDeserialization() {
|
||||
String json = JSON_NON_EXECUTABLE_PREFIX + "{longValue:1}";
|
||||
Gson gson = gsonBuilder.create();
|
||||
|
@ -54,6 +58,7 @@ public class SecurityTest extends TestCase {
|
|||
assertEquals(1, target.longValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonWithNonExectuableTokenSerialization() {
|
||||
Gson gson = gsonBuilder.generateNonExecutableJson().create();
|
||||
String json = gson.toJson(JSON_NON_EXECUTABLE_PREFIX);
|
||||
|
@ -64,6 +69,7 @@ public class SecurityTest extends TestCase {
|
|||
* Gson should be able to deserialize a stream with non-exectuable token even if it is created
|
||||
* without {@link GsonBuilder#generateNonExecutableJson()}.
|
||||
*/
|
||||
@Test
|
||||
public void testJsonWithNonExectuableTokenWithRegularGsonDeserialization() {
|
||||
Gson gson = gsonBuilder.create();
|
||||
String json = JSON_NON_EXECUTABLE_PREFIX + "{stringValue:')]}\\u0027\\n'}";
|
||||
|
@ -75,6 +81,7 @@ public class SecurityTest extends TestCase {
|
|||
* Gson should be able to deserialize a stream with non-exectuable token if it is created
|
||||
* with {@link GsonBuilder#generateNonExecutableJson()}.
|
||||
*/
|
||||
@Test
|
||||
public void testJsonWithNonExectuableTokenWithConfiguredGsonDeserialization() {
|
||||
// Gson should be able to deserialize a stream with non-exectuable token even if it is created
|
||||
Gson gson = gsonBuilder.generateNonExecutableJson().create();
|
||||
|
|
|
@ -15,20 +15,23 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import org.junit.Test;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public final class SerializedNameTest extends TestCase {
|
||||
public final class SerializedNameTest {
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
@Test
|
||||
public void testFirstNameIsChosenForSerialization() {
|
||||
MyClass target = new MyClass("v1", "v2");
|
||||
// Ensure name1 occurs exactly once, and name2 and name3 don't appear
|
||||
assertEquals("{\"name\":\"v1\",\"name1\":\"v2\"}", gson.toJson(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleNamesDeserializedCorrectly() {
|
||||
assertEquals("v1", gson.fromJson("{'name':'v1'}", MyClass.class).a);
|
||||
|
||||
|
@ -38,6 +41,7 @@ public final class SerializedNameTest extends TestCase {
|
|||
assertEquals("v3", gson.fromJson("{'name3':'v3'}", MyClass.class).b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleNamesInTheSameString() {
|
||||
// The last value takes precedence
|
||||
assertEquals("v3", gson.fromJson("{'name1':'v1','name2':'v2','name3':'v3'}", MyClass.class).b);
|
||||
|
|
|
@ -16,6 +16,11 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
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.JsonArray;
|
||||
|
@ -33,14 +38,15 @@ import java.util.Collections;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public final class StreamingTypeAdaptersTest extends TestCase {
|
||||
public final class StreamingTypeAdaptersTest {
|
||||
private Gson miniGson = new GsonBuilder().create();
|
||||
private TypeAdapter<Truck> truckAdapter = miniGson.getAdapter(Truck.class);
|
||||
private TypeAdapter<Map<String, Double>> mapAdapter
|
||||
= miniGson.getAdapter(new TypeToken<Map<String, Double>>() {});
|
||||
|
||||
@Test
|
||||
public void testSerialize() {
|
||||
Truck truck = new Truck();
|
||||
truck.passengers = Arrays.asList(new Person("Jesse", 29), new Person("Jodie", 29));
|
||||
|
@ -51,14 +57,16 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
truckAdapter.toJson(truck).replace('\"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialize() throws IOException {
|
||||
String json = "{'horsePower':300.0,"
|
||||
+ "'passengers':[{'age':29,'name':'Jesse'},{'age':29,'name':'Jodie'}]}";
|
||||
Truck truck = truckAdapter.fromJson(json.replace('\'', '\"'));
|
||||
assertEquals(300.0, truck.horsePower);
|
||||
assertEquals(300.0, truck.horsePower, 0);
|
||||
assertEquals(Arrays.asList(new Person("Jesse", 29), new Person("Jodie", 29)), truck.passengers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeNullField() {
|
||||
Truck truck = new Truck();
|
||||
truck.passengers = null;
|
||||
|
@ -66,11 +74,13 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
truckAdapter.toJson(truck).replace('\"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeNullField() throws IOException {
|
||||
Truck truck = truckAdapter.fromJson("{'horsePower':0.0,'passengers':null}".replace('\'', '\"'));
|
||||
assertNull(truck.passengers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeNullObject() {
|
||||
Truck truck = new Truck();
|
||||
truck.passengers = Arrays.asList((Person) null);
|
||||
|
@ -78,11 +88,13 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
truckAdapter.toJson(truck).replace('\"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeNullObject() throws IOException {
|
||||
Truck truck = truckAdapter.fromJson("{'horsePower':0.0,'passengers':[null]}".replace('\'', '\"'));
|
||||
assertEquals(Arrays.asList((Person) null), truck.passengers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeWithCustomTypeAdapter() {
|
||||
usePersonNameAdapter();
|
||||
Truck truck = new Truck();
|
||||
|
@ -91,6 +103,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
truckAdapter.toJson(truck).replace('\"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeWithCustomTypeAdapter() throws IOException {
|
||||
usePersonNameAdapter();
|
||||
Truck truck = truckAdapter.fromJson("{'horsePower':0.0,'passengers':['Jesse','Jodie']}".replace('\'', '\"'));
|
||||
|
@ -111,6 +124,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
truckAdapter = miniGson.getAdapter(Truck.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeMap() {
|
||||
Map<String, Double> map = new LinkedHashMap<>();
|
||||
map.put("a", 5.0);
|
||||
|
@ -118,6 +132,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
assertEquals("{'a':5.0,'b':10.0}", mapAdapter.toJson(map).replace('"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeMap() throws IOException {
|
||||
Map<String, Double> map = new LinkedHashMap<>();
|
||||
map.put("a", 5.0);
|
||||
|
@ -125,23 +140,27 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
assertEquals(map, mapAdapter.fromJson("{'a':5.0,'b':10.0}".replace('\'', '\"')));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialize1dArray() {
|
||||
TypeAdapter<double[]> arrayAdapter = miniGson.getAdapter(new TypeToken<double[]>() {});
|
||||
assertEquals("[1.0,2.0,3.0]", arrayAdapter.toJson(new double[]{ 1.0, 2.0, 3.0 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialize1dArray() throws IOException {
|
||||
TypeAdapter<double[]> arrayAdapter = miniGson.getAdapter(new TypeToken<double[]>() {});
|
||||
double[] array = arrayAdapter.fromJson("[1.0,2.0,3.0]");
|
||||
assertTrue(Arrays.toString(array), Arrays.equals(new double[]{1.0, 2.0, 3.0}, array));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialize2dArray() {
|
||||
TypeAdapter<double[][]> arrayAdapter = miniGson.getAdapter(new TypeToken<double[][]>() {});
|
||||
double[][] array = { {1.0, 2.0 }, { 3.0 } };
|
||||
assertEquals("[[1.0,2.0],[3.0]]", arrayAdapter.toJson(array));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialize2dArray() throws IOException {
|
||||
TypeAdapter<double[][]> arrayAdapter = miniGson.getAdapter(new TypeToken<double[][]>() {});
|
||||
double[][] array = arrayAdapter.fromJson("[[1.0,2.0],[3.0]]");
|
||||
|
@ -149,6 +168,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
assertTrue(Arrays.toString(array), Arrays.deepEquals(expected, array));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSafe() {
|
||||
TypeAdapter<Person> typeAdapter = new TypeAdapter<Person>() {
|
||||
@Override public Person read(JsonReader in) throws IOException {
|
||||
|
@ -179,11 +199,12 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
assertEquals("{\"horsePower\":1.0,\"passengers\":[null,\"jesse,30\"]}",
|
||||
gson.toJson(truck, Truck.class));
|
||||
truck = gson.fromJson(json, Truck.class);
|
||||
assertEquals(1.0D, truck.horsePower);
|
||||
assertEquals(1.0D, truck.horsePower, 0);
|
||||
assertNull(truck.passengers.get(0));
|
||||
assertEquals("jesse", truck.passengers.get(1).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeRecursive() {
|
||||
TypeAdapter<Node> nodeAdapter = miniGson.getAdapter(Node.class);
|
||||
Node root = new Node("root");
|
||||
|
@ -195,6 +216,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
nodeAdapter.toJson(root).replace('"', '\''));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromJsonTree() {
|
||||
JsonObject truckObject = new JsonObject();
|
||||
truckObject.add("horsePower", new JsonPrimitive(300));
|
||||
|
@ -206,7 +228,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||
truckObject.add("passengers", passengersArray);
|
||||
|
||||
Truck truck = truckAdapter.fromJsonTree(truckObject);
|
||||
assertEquals(300.0, truck.horsePower);
|
||||
assertEquals(300.0, truck.horsePower, 0);
|
||||
assertEquals(Arrays.asList(new Person("Jesse", 30)), truck.passengers);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package com.google.gson.functional;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for Json serialization and deserialization of strings.
|
||||
|
@ -10,73 +12,83 @@ import junit.framework.TestCase;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class StringTest extends TestCase {
|
||||
public class StringTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringValueSerialization() throws Exception {
|
||||
String value = "someRandomStringValue";
|
||||
assertEquals('"' + value + '"', gson.toJson(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringValueDeserialization() throws Exception {
|
||||
String value = "someRandomStringValue";
|
||||
String actual = gson.fromJson("\"" + value + "\"", String.class);
|
||||
assertEquals(value, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleQuoteInStringSerialization() throws Exception {
|
||||
String valueWithQuotes = "beforeQuote'afterQuote";
|
||||
String jsonRepresentation = gson.toJson(valueWithQuotes);
|
||||
assertEquals(valueWithQuotes, gson.fromJson(jsonRepresentation, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapedCtrlNInStringSerialization() throws Exception {
|
||||
String value = "a\nb";
|
||||
String json = gson.toJson(value);
|
||||
assertEquals("\"a\\nb\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapedCtrlNInStringDeserialization() throws Exception {
|
||||
String json = "'a\\nb'";
|
||||
String actual = gson.fromJson(json, String.class);
|
||||
assertEquals("a\nb", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapedCtrlRInStringSerialization() throws Exception {
|
||||
String value = "a\rb";
|
||||
String json = gson.toJson(value);
|
||||
assertEquals("\"a\\rb\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapedCtrlRInStringDeserialization() throws Exception {
|
||||
String json = "'a\\rb'";
|
||||
String actual = gson.fromJson(json, String.class);
|
||||
assertEquals("a\rb", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapedBackslashInStringSerialization() throws Exception {
|
||||
String value = "a\\b";
|
||||
String json = gson.toJson(value);
|
||||
assertEquals("\"a\\\\b\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapedBackslashInStringDeserialization() throws Exception {
|
||||
String actual = gson.fromJson("'a\\\\b'", String.class);
|
||||
assertEquals("a\\b", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleQuoteInStringDeserialization() throws Exception {
|
||||
String value = "beforeQuote'afterQuote";
|
||||
String actual = gson.fromJson("\"" + value + "\"", String.class);
|
||||
assertEquals(value, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapingQuotesInStringSerialization() throws Exception {
|
||||
String valueWithQuotes = "beforeQuote\"afterQuote";
|
||||
String jsonRepresentation = gson.toJson(valueWithQuotes);
|
||||
|
@ -84,6 +96,7 @@ public class StringTest extends TestCase {
|
|||
assertEquals(valueWithQuotes, target);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapingQuotesInStringDeserialization() throws Exception {
|
||||
String value = "beforeQuote\\\"afterQuote";
|
||||
String actual = gson.fromJson("\"" + value + "\"", String.class);
|
||||
|
@ -91,12 +104,14 @@ public class StringTest extends TestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringValueAsSingleElementArraySerialization() throws Exception {
|
||||
String[] target = {"abc"};
|
||||
assertEquals("[\"abc\"]", gson.toJson(target));
|
||||
assertEquals("[\"abc\"]", gson.toJson(target, String[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringWithEscapedSlashDeserialization() {
|
||||
String value = "/";
|
||||
String json = "'\\/'";
|
||||
|
@ -107,6 +122,7 @@ public class StringTest extends TestCase {
|
|||
/**
|
||||
* Created in response to http://groups.google.com/group/google-gson/browse_thread/thread/2431d4a3d0d6cb23
|
||||
*/
|
||||
@Test
|
||||
public void testAssignmentCharSerialization() {
|
||||
String value = "abc=";
|
||||
String json = gson.toJson(value);
|
||||
|
@ -116,6 +132,7 @@ public class StringTest extends TestCase {
|
|||
/**
|
||||
* Created in response to http://groups.google.com/group/google-gson/browse_thread/thread/2431d4a3d0d6cb23
|
||||
*/
|
||||
@Test
|
||||
public void testAssignmentCharDeserialization() {
|
||||
String json = "\"abc=\"";
|
||||
String value = gson.fromJson(json, String.class);
|
||||
|
@ -126,12 +143,14 @@ public class StringTest extends TestCase {
|
|||
assertEquals("abc=", value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavascriptKeywordsInStringSerialization() {
|
||||
String value = "null true false function";
|
||||
String json = gson.toJson(value);
|
||||
assertEquals("\"" + value + "\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavascriptKeywordsInStringDeserialization() {
|
||||
String json = "'null true false function'";
|
||||
String value = gson.fromJson(json, String.class);
|
||||
|
|
|
@ -16,12 +16,9 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
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.ToNumberPolicy;
|
||||
|
@ -29,9 +26,16 @@ import com.google.gson.ToNumberStrategy;
|
|||
import com.google.gson.internal.LazilyParsedNumber;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import junit.framework.TestCase;
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ToNumberPolicyFunctionalTest extends TestCase {
|
||||
public class ToNumberPolicyFunctionalTest {
|
||||
@Test
|
||||
public void testDefault() {
|
||||
Gson gson = new Gson();
|
||||
assertEquals(null, gson.fromJson("null", Object.class));
|
||||
|
@ -40,6 +44,7 @@ public class ToNumberPolicyFunctionalTest extends TestCase {
|
|||
assertEquals(new LazilyParsedNumber("10"), gson.fromJson("10", Number.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsDoubles() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setObjectToNumberStrategy(ToNumberPolicy.DOUBLE)
|
||||
|
@ -51,6 +56,7 @@ public class ToNumberPolicyFunctionalTest extends TestCase {
|
|||
assertEquals(10.0, gson.fromJson("10", Number.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsLazilyParsedNumbers() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setObjectToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER)
|
||||
|
@ -62,6 +68,7 @@ public class ToNumberPolicyFunctionalTest extends TestCase {
|
|||
assertEquals(new LazilyParsedNumber("10"), gson.fromJson("10", Number.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsLongsOrDoubles() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||
|
@ -75,6 +82,7 @@ public class ToNumberPolicyFunctionalTest extends TestCase {
|
|||
assertEquals(10.0, gson.fromJson("10.0", Number.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsBigDecimals() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setObjectToNumberStrategy(ToNumberPolicy.BIG_DECIMAL)
|
||||
|
@ -90,6 +98,7 @@ public class ToNumberPolicyFunctionalTest extends TestCase {
|
|||
assertEquals(new BigDecimal("1e400"), gson.fromJson("1e400", BigDecimal.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsListOfLongsOrDoubles() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
|
||||
|
@ -107,6 +116,7 @@ public class ToNumberPolicyFunctionalTest extends TestCase {
|
|||
assertEquals(expected, numbers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomStrategiesCannotAffectConcreteDeclaredNumbers() {
|
||||
ToNumberStrategy fail = new ToNumberStrategy() {
|
||||
@Override
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
|
@ -31,12 +34,13 @@ import java.lang.reflect.Type;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Collection of functional tests for DOM tree based type adapters.
|
||||
*/
|
||||
public class TreeTypeAdaptersTest extends TestCase {
|
||||
public class TreeTypeAdaptersTest {
|
||||
private static final Id<Student> STUDENT1_ID = new Id<>("5", Student.class);
|
||||
private static final Id<Student> STUDENT2_ID = new Id<>("6", Student.class);
|
||||
private static final Student STUDENT1 = new Student(STUDENT1_ID, "first");
|
||||
|
@ -49,8 +53,8 @@ public class TreeTypeAdaptersTest extends TestCase {
|
|||
private Gson gson;
|
||||
private Course<HistoryCourse> course;
|
||||
|
||||
@Override
|
||||
protected void setUp() {
|
||||
@Before
|
||||
public void setUp() {
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Id.class, new IdTreeTypeAdapter())
|
||||
.create();
|
||||
|
@ -58,6 +62,7 @@ public class TreeTypeAdaptersTest extends TestCase {
|
|||
new Assignment<HistoryCourse>(null, null), Arrays.asList(STUDENT1, STUDENT2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeId() {
|
||||
String json = gson.toJson(course, TYPE_COURSE_HISTORY);
|
||||
assertTrue(json.contains(String.valueOf(COURSE_ID.getValue())));
|
||||
|
@ -65,6 +70,7 @@ public class TreeTypeAdaptersTest extends TestCase {
|
|||
assertTrue(json.contains(String.valueOf(STUDENT2_ID.getValue())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeId() {
|
||||
String json = "{courseId:1,students:[{id:1,name:'first'},{id:6,name:'second'}],"
|
||||
+ "numAssignments:4,assignment:{}}";
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
|
@ -29,9 +31,10 @@ import com.google.gson.stream.JsonReader;
|
|||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public final class TypeAdapterPrecedenceTest extends TestCase {
|
||||
public final class TypeAdapterPrecedenceTest {
|
||||
@Test
|
||||
public void testNonstreamingFollowedByNonstreaming() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Foo.class, newSerializer("serializer 1"))
|
||||
|
@ -43,6 +46,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
|||
assertEquals("foo via deserializer 2", gson.fromJson("foo", Foo.class).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamingFollowedByStreaming() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Foo.class, newTypeAdapter("type adapter 1"))
|
||||
|
@ -52,6 +56,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
|||
assertEquals("foo via type adapter 2", gson.fromJson("foo", Foo.class).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Foo.class, newSerializer("serializer"))
|
||||
|
@ -62,6 +67,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
|||
assertEquals("foo via type adapter", gson.fromJson("foo", Foo.class).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamingFollowedByNonstreaming() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Foo.class, newTypeAdapter("type adapter"))
|
||||
|
@ -72,6 +78,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
|||
assertEquals("foo via deserializer", gson.fromJson("foo", Foo.class).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamingHierarchicalFollowedByNonstreaming() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(Foo.class, newTypeAdapter("type adapter"))
|
||||
|
@ -82,6 +89,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
|||
assertEquals("foo via deserializer", gson.fromJson("foo", Foo.class).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamingFollowedByNonstreamingHierarchical() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Foo.class, newTypeAdapter("type adapter"))
|
||||
|
@ -92,6 +100,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
|||
assertEquals("foo via type adapter", gson.fromJson("foo", Foo.class).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamingHierarchicalFollowedByNonstreamingHierarchical() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(Foo.class, newSerializer("serializer"))
|
||||
|
@ -102,6 +111,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
|||
assertEquals("foo via type adapter", gson.fromJson("foo", Foo.class).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonstreamingHierarchicalFollowedByNonstreaming() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(Foo.class, newSerializer("hierarchical"))
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
|
@ -27,13 +29,14 @@ import com.google.gson.JsonPrimitive;
|
|||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test that the hierarchy adapter works when subtypes are used.
|
||||
*/
|
||||
public final class TypeHierarchyAdapterTest extends TestCase {
|
||||
public final class TypeHierarchyAdapterTest {
|
||||
|
||||
@Test
|
||||
public void testTypeHierarchy() {
|
||||
Manager andy = new Manager();
|
||||
andy.userid = "andy";
|
||||
|
@ -116,6 +119,7 @@ public final class TypeHierarchyAdapterTest extends TestCase {
|
|||
((Manager) company.ceo.minions[2]).minions[1].userid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterSuperTypeFirst() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(Employee.class, new EmployeeAdapter())
|
||||
|
@ -132,6 +136,7 @@ public final class TypeHierarchyAdapterTest extends TestCase {
|
|||
}
|
||||
|
||||
/** This behaviour changed in Gson 2.1; it used to throw. */
|
||||
@Test
|
||||
public void testRegisterSubTypeFirstAllowed() {
|
||||
new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(Manager.class, new ManagerAdapter())
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.lang.reflect.Type;
|
||||
|
@ -23,7 +25,7 @@ import java.util.Arrays;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional test for Gson serialization and deserialization of
|
||||
|
@ -31,8 +33,9 @@ import junit.framework.TestCase;
|
|||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class TypeVariableTest extends TestCase {
|
||||
public class TypeVariableTest {
|
||||
|
||||
@Test
|
||||
public void testAdvancedTypeVariables() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
Bar bar1 = new Bar("someString", 1, true);
|
||||
|
@ -48,6 +51,7 @@ public class TypeVariableTest extends TestCase {
|
|||
assertEquals(bar1, bar2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeVariablesViaTypeParameter() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
Foo<String, Integer> original = new Foo<>("e", 5, false);
|
||||
|
@ -59,6 +63,7 @@ public class TypeVariableTest extends TestCase {
|
|||
assertEquals(original, gson.<Foo<String, Integer>>fromJson(json, type));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicTypeVariables() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
Blue blue1 = new Blue(true);
|
||||
|
|
|
@ -15,6 +15,11 @@
|
|||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
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.JsonDeserializationContext;
|
||||
|
@ -23,13 +28,12 @@ import com.google.gson.JsonElement;
|
|||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||
import com.google.gson.common.TestTypes.ClassOverridingEquals;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests that do not fall neatly into any of the existing classification.
|
||||
|
@ -37,16 +41,16 @@ import java.lang.reflect.Type;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class UncategorizedTest extends TestCase {
|
||||
public class UncategorizedTest {
|
||||
|
||||
private Gson gson = null;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidJsonDeserializationFails() throws Exception {
|
||||
try {
|
||||
gson.fromJson("adfasdf1112,,,\":", BagOfPrimitives.class);
|
||||
|
@ -59,6 +63,7 @@ public class UncategorizedTest extends TestCase {
|
|||
} catch (JsonParseException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectEqualButNotSameSerialization() throws Exception {
|
||||
ClassOverridingEquals objA = new ClassOverridingEquals();
|
||||
ClassOverridingEquals objB = new ClassOverridingEquals();
|
||||
|
@ -67,11 +72,13 @@ public class UncategorizedTest extends TestCase {
|
|||
assertEquals(objB.getExpectedJson(), json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticFieldsAreNotSerialized() {
|
||||
BagOfPrimitives target = new BagOfPrimitives();
|
||||
assertFalse(gson.toJson(target).contains("DEFAULT_VALUE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGsonInstanceReusableForSerializationAndDeserialization() {
|
||||
BagOfPrimitives bag = new BagOfPrimitives();
|
||||
String json = gson.toJson(bag);
|
||||
|
@ -84,6 +91,7 @@ public class UncategorizedTest extends TestCase {
|
|||
* base class object. For a motivation for this test, see Issue 37 and
|
||||
* http://groups.google.com/group/google-gson/browse_thread/thread/677d56e9976d7761
|
||||
*/
|
||||
@Test
|
||||
public void testReturningDerivedClassesDuringDeserialization() {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(Base.class, new BaseTypeAdapter()).create();
|
||||
String json = "{\"opType\":\"OP1\"}";
|
||||
|
@ -101,6 +109,7 @@ public class UncategorizedTest extends TestCase {
|
|||
* Test that trailing whitespace is ignored.
|
||||
* http://code.google.com/p/google-gson/issues/detail?id=302
|
||||
*/
|
||||
@Test
|
||||
public void testTrailingWhitespace() throws Exception {
|
||||
List<Integer> integers = gson.fromJson("[1,2,3] \n\n ",
|
||||
new TypeToken<List<Integer>>() {}.getType());
|
||||
|
|
|
@ -16,14 +16,18 @@
|
|||
|
||||
package com.google.gson.internal;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public final class GsonTypesTest extends TestCase {
|
||||
public final class GsonTypesTest {
|
||||
|
||||
@Test
|
||||
public void testNewParameterizedTypeWithoutOwner() throws Exception {
|
||||
// List<A>. List is a top-level class
|
||||
Type type = $Gson$Types.newParameterizedTypeWithOwner(null, List.class, A.class);
|
||||
|
@ -46,6 +50,7 @@ public final class GsonTypesTest extends TestCase {
|
|||
assertEquals(D.class, getFirstTypeArgument(type));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFirstTypeArgument() throws Exception {
|
||||
assertNull(getFirstTypeArgument(A.class));
|
||||
|
||||
|
|
|
@ -15,28 +15,33 @@
|
|||
*/
|
||||
package com.google.gson.internal;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.math.BigDecimal;
|
||||
import org.junit.Test;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class LazilyParsedNumberTest extends TestCase {
|
||||
public class LazilyParsedNumberTest {
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
LazilyParsedNumber n1 = new LazilyParsedNumber("1");
|
||||
LazilyParsedNumber n1Another = new LazilyParsedNumber("1");
|
||||
assertEquals(n1.hashCode(), n1Another.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
LazilyParsedNumber n1 = new LazilyParsedNumber("1");
|
||||
LazilyParsedNumber n1Another = new LazilyParsedNumber("1");
|
||||
assertTrue(n1.equals(n1Another));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaSerialization() throws IOException, ClassNotFoundException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(out);
|
||||
|
|
|
@ -16,6 +16,12 @@
|
|||
|
||||
package com.google.gson.internal;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.common.MoreAsserts;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
@ -29,10 +35,11 @@ import java.util.Iterator;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Random;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public final class LinkedTreeMapTest extends TestCase {
|
||||
public final class LinkedTreeMapTest {
|
||||
|
||||
@Test
|
||||
public void testIterationOrder() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
map.put("a", "android");
|
||||
|
@ -42,6 +49,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
assertIterationOrder(map.values(), "android", "cola", "bbq");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveRootDoesNotDoubleUnlink() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
map.put("a", "android");
|
||||
|
@ -55,6 +63,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
assertIterationOrder(map.keySet(), "a", "c");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutNullKeyFails() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
try {
|
||||
|
@ -64,6 +73,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutNonComparableKeyFails() {
|
||||
LinkedTreeMap<Object, String> map = new LinkedTreeMap<>();
|
||||
try {
|
||||
|
@ -72,6 +82,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
} catch (ClassCastException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutNullValue() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
map.put("a", null);
|
||||
|
@ -81,6 +92,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
assertNull(map.get("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutNullValue_Forbidden() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>(false);
|
||||
try {
|
||||
|
@ -94,6 +106,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
assertFalse(map.containsValue(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntrySetValueNull() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
map.put("a", "1");
|
||||
|
@ -110,6 +123,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEntrySetValueNull_Forbidden() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>(false);
|
||||
map.put("a", "1");
|
||||
|
@ -125,12 +139,14 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
assertFalse(map.containsValue(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsNonComparableKeyReturnsFalse() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
map.put("a", "android");
|
||||
assertFalse(map.containsKey(new Object()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsNullKeyIsAlwaysFalse() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
assertFalse(map.containsKey(null));
|
||||
|
@ -138,6 +154,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
assertFalse(map.containsKey(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutOverrides() throws Exception {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
assertNull(map.put("d", "donut"));
|
||||
|
@ -150,6 +167,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
assertEquals(3, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyStringValues() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
map.put("a", "");
|
||||
|
@ -157,6 +175,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
assertEquals("", map.get("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLargeSetOfRandomKeys() throws Exception {
|
||||
Random random = new Random(1367593214724L);
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
|
@ -173,6 +192,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
map.put("a", "android");
|
||||
|
@ -183,6 +203,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
assertEquals(0, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
LinkedTreeMap<String, Integer> map1 = new LinkedTreeMap<>();
|
||||
map1.put("A", 1);
|
||||
|
@ -199,6 +220,7 @@ public final class LinkedTreeMapTest extends TestCase {
|
|||
MoreAsserts.assertEqualsAndHashCode(map1, map2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaSerialization() throws IOException, ClassNotFoundException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(out);
|
||||
|
|
|
@ -15,13 +15,17 @@
|
|||
*/
|
||||
package com.google.gson.internal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test unsafe allocator instantiation
|
||||
* @author Ugljesa Jovanovic
|
||||
*/
|
||||
public final class UnsafeAllocatorInstantiationTest extends TestCase {
|
||||
public final class UnsafeAllocatorInstantiationTest {
|
||||
|
||||
public interface Interface {
|
||||
}
|
||||
|
@ -36,6 +40,7 @@ public final class UnsafeAllocatorInstantiationTest extends TestCase {
|
|||
* Ensure that an {@link AssertionError} is thrown when trying
|
||||
* to instantiate an interface
|
||||
*/
|
||||
@Test
|
||||
public void testInterfaceInstantiation() throws Exception {
|
||||
try {
|
||||
UnsafeAllocator.INSTANCE.newInstance(Interface.class);
|
||||
|
@ -49,6 +54,7 @@ public final class UnsafeAllocatorInstantiationTest extends TestCase {
|
|||
* Ensure that an {@link AssertionError} is thrown when trying
|
||||
* to instantiate an abstract class
|
||||
*/
|
||||
@Test
|
||||
public void testAbstractClassInstantiation() throws Exception {
|
||||
try {
|
||||
UnsafeAllocator.INSTANCE.newInstance(AbstractClass.class);
|
||||
|
@ -61,6 +67,7 @@ public final class UnsafeAllocatorInstantiationTest extends TestCase {
|
|||
/**
|
||||
* Ensure that no exception is thrown when trying to instantiate a concrete class
|
||||
*/
|
||||
@Test
|
||||
public void testConcreteClassInstantiation() throws Exception {
|
||||
ConcreteClass instance = UnsafeAllocator.INSTANCE.newInstance(ConcreteClass.class);
|
||||
assertNotNull(instance);
|
||||
|
|
|
@ -16,31 +16,38 @@
|
|||
|
||||
package com.google.gson.internal.bind;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.TypeAdapterFactory;
|
||||
import com.google.gson.internal.JavaVersion;
|
||||
import com.google.gson.internal.bind.DefaultDateTypeAdapter.DateType;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import junit.framework.TestCase;
|
||||
import java.io.IOException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* A simple unit test for the {@link DefaultDateTypeAdapter} class.
|
||||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class DefaultDateTypeAdapterTest extends TestCase {
|
||||
public class DefaultDateTypeAdapterTest {
|
||||
|
||||
@Test
|
||||
public void testFormattingInEnUs() {
|
||||
assertFormattingAlwaysEmitsUsLocale(Locale.US);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormattingInFr() {
|
||||
assertFormattingAlwaysEmitsUsLocale(Locale.FRANCE);
|
||||
}
|
||||
|
@ -73,6 +80,7 @@ public class DefaultDateTypeAdapterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParsingDatesFormattedWithSystemLocale() throws Exception {
|
||||
TimeZone defaultTimeZone = TimeZone.getDefault();
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||
|
@ -110,6 +118,7 @@ public class DefaultDateTypeAdapterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParsingDatesFormattedWithUsLocale() throws Exception {
|
||||
TimeZone defaultTimeZone = TimeZone.getDefault();
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||
|
@ -134,6 +143,7 @@ public class DefaultDateTypeAdapterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatUsesDefaultTimezone() throws Exception {
|
||||
TimeZone defaultTimeZone = TimeZone.getDefault();
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
|
||||
|
@ -150,6 +160,7 @@ public class DefaultDateTypeAdapterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateDeserializationISO8601() throws Exception {
|
||||
TypeAdapterFactory adapterFactory = DateType.DATE.createDefaultsAdapterFactory();
|
||||
assertParsed("1970-01-01T00:00:00.000Z", adapterFactory);
|
||||
|
@ -159,6 +170,7 @@ public class DefaultDateTypeAdapterTest extends TestCase {
|
|||
assertParsed("1970-01-01T01:00:00+01", adapterFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateSerialization() throws Exception {
|
||||
int dateStyle = DateFormat.LONG;
|
||||
TypeAdapter<Date> dateTypeAdapter = dateAdapter(DateType.DATE.createAdapterFactory(dateStyle));
|
||||
|
@ -169,6 +181,7 @@ public class DefaultDateTypeAdapterTest extends TestCase {
|
|||
assertEquals(toLiteral(formatter.format(currentDate)), dateString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDatePattern() throws Exception {
|
||||
String pattern = "yyyy-MM-dd";
|
||||
TypeAdapter<Date> dateTypeAdapter = dateAdapter(DateType.DATE.createAdapterFactory(pattern));
|
||||
|
@ -179,6 +192,7 @@ public class DefaultDateTypeAdapterTest extends TestCase {
|
|||
assertEquals(toLiteral(formatter.format(currentDate)), dateString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidDatePattern() throws Exception {
|
||||
try {
|
||||
DateType.DATE.createAdapterFactory("I am a bad Date pattern....");
|
||||
|
@ -186,12 +200,14 @@ public class DefaultDateTypeAdapterTest extends TestCase {
|
|||
} catch (IllegalArgumentException expected) { }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullValue() throws Exception {
|
||||
TypeAdapter<Date> adapter = dateAdapter(DateType.DATE.createDefaultsAdapterFactory());
|
||||
assertNull(adapter.fromJson("null"));
|
||||
assertEquals("null", adapter.toJson(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnexpectedToken() throws Exception {
|
||||
try {
|
||||
TypeAdapter<Date> adapter = dateAdapter(DateType.DATE.createDefaultsAdapterFactory());
|
||||
|
|
|
@ -16,38 +16,45 @@
|
|||
|
||||
package com.google.gson.internal.bind;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.MalformedJsonException;
|
||||
import java.io.IOException;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public final class JsonElementReaderTest extends TestCase {
|
||||
public final class JsonElementReaderTest {
|
||||
|
||||
@Test
|
||||
public void testNumbers() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[1, 2, 3]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
reader.beginArray();
|
||||
assertEquals(1, reader.nextInt());
|
||||
assertEquals(2L, reader.nextLong());
|
||||
assertEquals(3.0, reader.nextDouble());
|
||||
assertEquals(3.0, reader.nextDouble(), 0);
|
||||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLenientNansAndInfinities() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[NaN, -Infinity, Infinity]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
reader.setLenient(true);
|
||||
reader.beginArray();
|
||||
assertTrue(Double.isNaN(reader.nextDouble()));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, reader.nextDouble());
|
||||
assertEquals(Double.POSITIVE_INFINITY, reader.nextDouble());
|
||||
assertEquals(Double.NEGATIVE_INFINITY, reader.nextDouble(), 0);
|
||||
assertEquals(Double.POSITIVE_INFINITY, reader.nextDouble(), 0);
|
||||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrictNansAndInfinities() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[NaN, -Infinity, Infinity]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -77,16 +84,18 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumbersFromStrings() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[\"1\", \"2\", \"3\"]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
reader.beginArray();
|
||||
assertEquals(1, reader.nextInt());
|
||||
assertEquals(2L, reader.nextLong());
|
||||
assertEquals(3.0, reader.nextDouble());
|
||||
assertEquals(3.0, reader.nextDouble(), 0);
|
||||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringsFromNumbers() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[1]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -95,6 +104,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleans() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[true, false]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -104,6 +114,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNulls() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[null,null]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -113,6 +124,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrings() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[\"A\",\"B\"]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -122,6 +134,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[1, 2, 3]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -138,6 +151,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObject() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("{\"A\": 1, \"B\": 2}");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -156,6 +170,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyArray() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -163,6 +178,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedArrays() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[[],[[]]]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -176,6 +192,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedObjects() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("{\"A\":{},\"B\":{\"C\":{}}}");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -192,6 +209,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endObject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyObject() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("{}");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -199,6 +217,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endObject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipValue() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[\"A\",{\"B\":[[]]},\"C\",[[]],\"D\",null]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -212,6 +231,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrongType() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[[],\"A\"]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -303,6 +323,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
reader.endArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextJsonElement() throws IOException {
|
||||
final JsonElement element = JsonParser.parseString("{\"A\": 1, \"B\" : {}, \"C\" : []}");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
@ -338,6 +359,7 @@ public final class JsonElementReaderTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEarlyClose() throws IOException {
|
||||
JsonElement element = JsonParser.parseString("[1, 2, 3]");
|
||||
JsonTreeReader reader = new JsonTreeReader(element);
|
||||
|
|
|
@ -15,6 +15,10 @@
|
|||
*/
|
||||
package com.google.gson.internal.bind;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
|
@ -27,10 +31,11 @@ import java.io.IOException;
|
|||
import java.io.Reader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public class JsonTreeReaderTest extends TestCase {
|
||||
public class JsonTreeReaderTest {
|
||||
@Test
|
||||
public void testSkipValue_emptyJsonObject() throws IOException {
|
||||
JsonTreeReader in = new JsonTreeReader(new JsonObject());
|
||||
in.skipValue();
|
||||
|
@ -38,6 +43,7 @@ public class JsonTreeReaderTest extends TestCase {
|
|||
assertEquals("$", in.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipValue_filledJsonObject() throws IOException {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
|
@ -57,6 +63,7 @@ public class JsonTreeReaderTest extends TestCase {
|
|||
assertEquals("$", in.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipValue_name() throws IOException {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("a", "value");
|
||||
|
@ -68,6 +75,7 @@ public class JsonTreeReaderTest extends TestCase {
|
|||
assertEquals("value", in.nextString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipValue_afterEndOfDocument() throws IOException {
|
||||
JsonTreeReader reader = new JsonTreeReader(new JsonObject());
|
||||
reader.beginObject();
|
||||
|
@ -80,6 +88,7 @@ public class JsonTreeReaderTest extends TestCase {
|
|||
assertEquals("$", reader.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipValue_atArrayEnd() throws IOException {
|
||||
JsonTreeReader reader = new JsonTreeReader(new JsonArray());
|
||||
reader.beginArray();
|
||||
|
@ -88,6 +97,7 @@ public class JsonTreeReaderTest extends TestCase {
|
|||
assertEquals("$", reader.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipValue_atObjectEnd() throws IOException {
|
||||
JsonTreeReader reader = new JsonTreeReader(new JsonObject());
|
||||
reader.beginObject();
|
||||
|
@ -96,6 +106,7 @@ public class JsonTreeReaderTest extends TestCase {
|
|||
assertEquals("$", reader.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasNext_endOfDocument() throws IOException {
|
||||
JsonTreeReader reader = new JsonTreeReader(new JsonObject());
|
||||
reader.beginObject();
|
||||
|
@ -103,6 +114,7 @@ public class JsonTreeReaderTest extends TestCase {
|
|||
assertFalse(reader.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomJsonElementSubclass() throws IOException {
|
||||
@SuppressWarnings("deprecation") // superclass constructor
|
||||
class CustomSubclass extends JsonElement {
|
||||
|
@ -132,6 +144,7 @@ public class JsonTreeReaderTest extends TestCase {
|
|||
* read from a {@link JsonElement} instead of a {@link Reader}. Therefore all relevant methods of
|
||||
* {@code JsonReader} must be overridden.
|
||||
*/
|
||||
@Test
|
||||
public void testOverrides() {
|
||||
List<String> ignoredMethods = Arrays.asList("setLenient(boolean)", "isLenient()");
|
||||
MoreAsserts.assertOverridesMethods(JsonReader.class, JsonTreeReader.class, ignoredMethods);
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package com.google.gson.internal.bind;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
import com.google.gson.common.MoreAsserts;
|
||||
|
@ -24,10 +27,11 @@ import java.io.IOException;
|
|||
import java.io.Writer;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public final class JsonTreeWriterTest extends TestCase {
|
||||
public final class JsonTreeWriterTest {
|
||||
@Test
|
||||
public void testArray() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.beginArray();
|
||||
|
@ -38,6 +42,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
assertEquals("[1,2,3]", writer.get().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedArray() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.beginArray();
|
||||
|
@ -51,6 +56,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
assertEquals("[[],[[]]]", writer.get().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObject() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.beginObject();
|
||||
|
@ -60,6 +66,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
assertEquals("{\"A\":1,\"B\":2}", writer.get().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedObject() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.beginObject();
|
||||
|
@ -76,6 +83,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
assertEquals("{\"A\":{\"B\":{}},\"C\":{}}", writer.get().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteAfterClose() throws Exception {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.setLenient(true);
|
||||
|
@ -90,6 +98,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrematureClose() throws Exception {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.setLenient(true);
|
||||
|
@ -101,6 +110,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeNullsFalse() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.setSerializeNulls(false);
|
||||
|
@ -111,6 +121,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
assertEquals("{}", writer.get().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeNullsTrue() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.setSerializeNulls(true);
|
||||
|
@ -121,39 +132,46 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
assertEquals("{\"A\":null}", writer.get().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyWriter() {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
assertEquals(JsonNull.INSTANCE, writer.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeginArray() throws Exception {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
assertEquals(writer, writer.beginArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeginObject() throws Exception {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
assertEquals(writer, writer.beginObject());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueString() throws Exception {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
String n = "as";
|
||||
assertEquals(writer, writer.value(n));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoolValue() throws Exception {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
boolean bool = true;
|
||||
assertEquals(writer, writer.value(bool));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoolMaisValue() throws Exception {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
Boolean bool = true;
|
||||
assertEquals(writer, writer.value(bool));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLenientNansAndInfinities() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.setLenient(true);
|
||||
|
@ -168,6 +186,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
assertEquals("[NaN,-Infinity,Infinity,NaN,-Infinity,Infinity]", writer.get().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrictNansAndInfinities() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.setLenient(false);
|
||||
|
@ -204,6 +223,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrictBoxedNansAndInfinities() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.setLenient(false);
|
||||
|
@ -240,6 +260,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonValue() throws IOException {
|
||||
JsonTreeWriter writer = new JsonTreeWriter();
|
||||
writer.beginArray();
|
||||
|
@ -255,6 +276,7 @@ public final class JsonTreeWriterTest extends TestCase {
|
|||
* create a {@link JsonElement} tree instead of writing to a {@link Writer}. Therefore all relevant
|
||||
* methods of {@code JsonWriter} must be overridden.
|
||||
*/
|
||||
@Test
|
||||
public void testOverrides() {
|
||||
List<String> ignoredMethods = Arrays.asList("setLenient(boolean)", "isLenient()", "setIndent(java.lang.String)",
|
||||
"setHtmlSafe(boolean)", "isHtmlSafe()", "setSerializeNulls(boolean)", "getSerializeNulls()");
|
||||
|
|
|
@ -16,10 +16,13 @@
|
|||
|
||||
package com.google.gson.internal.bind;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.internal.$Gson$Types;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test fixes for infinite recursion on {@link $Gson$Types#resolve(java.lang.reflect.Type, Class,
|
||||
|
@ -29,7 +32,7 @@ import junit.framework.TestCase;
|
|||
* These tests originally caused {@link StackOverflowError} because of infinite recursion on attempts to
|
||||
* resolve generics on types, with an intermediate types like 'Foo2<? extends ? super ? extends ... ? extends A>'
|
||||
*/
|
||||
public class RecursiveTypesResolveTest extends TestCase {
|
||||
public class RecursiveTypesResolveTest {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class Foo1<A> {
|
||||
|
@ -44,6 +47,7 @@ public class RecursiveTypesResolveTest extends TestCase {
|
|||
* Test simplest case of recursion.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testRecursiveResolveSimple() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
TypeAdapter<Foo1> adapter = new Gson().getAdapter(Foo1.class);
|
||||
|
@ -54,21 +58,25 @@ public class RecursiveTypesResolveTest extends TestCase {
|
|||
* Tests belows check the behaviour of the methods changed for the fix.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testDoubleSupertype() {
|
||||
assertEquals($Gson$Types.supertypeOf(Number.class),
|
||||
$Gson$Types.supertypeOf($Gson$Types.supertypeOf(Number.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleSubtype() {
|
||||
assertEquals($Gson$Types.subtypeOf(Number.class),
|
||||
$Gson$Types.subtypeOf($Gson$Types.subtypeOf(Number.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuperSubtype() {
|
||||
assertEquals($Gson$Types.subtypeOf(Object.class),
|
||||
$Gson$Types.supertypeOf($Gson$Types.subtypeOf(Number.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubSupertype() {
|
||||
assertEquals($Gson$Types.subtypeOf(Object.class),
|
||||
$Gson$Types.subtypeOf($Gson$Types.supertypeOf(Number.class)));
|
||||
|
@ -88,12 +96,14 @@ public class RecursiveTypesResolveTest extends TestCase {
|
|||
TestType2<? super Y, ? super X> superReversedType;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecursiveTypeVariablesResolve1() throws Exception {
|
||||
@SuppressWarnings("rawtypes")
|
||||
TypeAdapter<TestType> adapter = new Gson().getAdapter(TestType.class);
|
||||
assertNotNull(adapter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecursiveTypeVariablesResolve12() throws Exception {
|
||||
@SuppressWarnings("rawtypes")
|
||||
TypeAdapter<TestType2> adapter = new Gson().getAdapter(TestType2.class);
|
||||
|
|
|
@ -28,8 +28,8 @@ public class ISO8601UtilsTest {
|
|||
return calendar;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateFormatString() {
|
||||
@Test
|
||||
public void testDateFormatString() {
|
||||
GregorianCalendar calendar = new GregorianCalendar(utcTimeZone(), Locale.US);
|
||||
// Calendar was created with current time, must clear it
|
||||
calendar.clear();
|
||||
|
@ -40,8 +40,8 @@ public class ISO8601UtilsTest {
|
|||
assertEquals(expectedDate, dateStr.substring(0, expectedDate.length()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateFormatWithMilliseconds() {
|
||||
@Test
|
||||
public void testDateFormatWithMilliseconds() {
|
||||
long time = 1530209176870L;
|
||||
Date date = new Date(time);
|
||||
String dateStr = ISO8601Utils.format(date, true);
|
||||
|
@ -49,8 +49,8 @@ public class ISO8601UtilsTest {
|
|||
assertEquals(expectedDate, dateStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateFormatWithTimezone() {
|
||||
@Test
|
||||
public void testDateFormatWithTimezone() {
|
||||
long time = 1530209176870L;
|
||||
Date date = new Date(time);
|
||||
String dateStr = ISO8601Utils.format(date, true, TimeZone.getTimeZone("Brazil/East"));
|
||||
|
@ -58,16 +58,16 @@ public class ISO8601UtilsTest {
|
|||
assertEquals(expectedDate, dateStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateParseWithDefaultTimezone() throws ParseException {
|
||||
@Test
|
||||
public void testDateParseWithDefaultTimezone() throws ParseException {
|
||||
String dateStr = "2018-06-25";
|
||||
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
||||
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25).getTime();
|
||||
assertEquals(expectedDate, date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateParseInvalidDay() {
|
||||
@Test
|
||||
public void testDateParseInvalidDay() {
|
||||
String dateStr = "2022-12-33";
|
||||
try {
|
||||
ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
||||
|
@ -76,8 +76,8 @@ public class ISO8601UtilsTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateParseInvalidMonth() {
|
||||
@Test
|
||||
public void testDateParseInvalidMonth() {
|
||||
String dateStr = "2022-14-30";
|
||||
try {
|
||||
ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
||||
|
@ -86,8 +86,8 @@ public class ISO8601UtilsTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateParseWithTimezone() throws ParseException {
|
||||
@Test
|
||||
public void testDateParseWithTimezone() throws ParseException {
|
||||
String dateStr = "2018-06-25T00:00:00-03:00";
|
||||
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
||||
GregorianCalendar calendar = createUtcCalendar();
|
||||
|
@ -96,8 +96,8 @@ public class ISO8601UtilsTest {
|
|||
assertEquals(expectedDate, date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateParseSpecialTimezone() throws ParseException {
|
||||
@Test
|
||||
public void testDateParseSpecialTimezone() throws ParseException {
|
||||
String dateStr = "2018-06-25T00:02:00-02:58";
|
||||
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
||||
GregorianCalendar calendar = createUtcCalendar();
|
||||
|
@ -106,8 +106,8 @@ public class ISO8601UtilsTest {
|
|||
assertEquals(expectedDate, date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateParseInvalidTime() throws ParseException {
|
||||
@Test
|
||||
public void testDateParseInvalidTime() throws ParseException {
|
||||
final String dateStr = "2018-06-25T61:60:62-03:00";
|
||||
assertThrows(ParseException.class, new ThrowingRunnable() {
|
||||
@Override
|
||||
|
|
|
@ -1,26 +1,27 @@
|
|||
package com.google.gson.internal.sql;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.functional.DefaultTypeAdaptersTest;
|
||||
import com.google.gson.internal.JavaVersion;
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SqlTypesGsonTest extends TestCase {
|
||||
public class SqlTypesGsonTest {
|
||||
private Gson gson;
|
||||
private TimeZone oldTimeZone;
|
||||
private Locale oldLocale;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.oldTimeZone = TimeZone.getDefault();
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
|
||||
this.oldLocale = Locale.getDefault();
|
||||
|
@ -28,13 +29,13 @@ public class SqlTypesGsonTest extends TestCase {
|
|||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
TimeZone.setDefault(oldTimeZone);
|
||||
Locale.setDefault(oldLocale);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSerializationAndDeserialization() {
|
||||
testNullSerializationAndDeserialization(Date.class);
|
||||
testNullSerializationAndDeserialization(Time.class);
|
||||
|
@ -45,12 +46,14 @@ public class SqlTypesGsonTest extends TestCase {
|
|||
DefaultTypeAdaptersTest.testNullSerializationAndDeserialization(gson, c);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSqlDateSerialization() {
|
||||
java.sql.Date instant = new java.sql.Date(1259875082000L);
|
||||
String json = gson.toJson(instant);
|
||||
assertEquals("\"Dec 3, 2009\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSqlDateDeserialization() {
|
||||
String json = "'Dec 3, 2009'";
|
||||
java.sql.Date extracted = gson.fromJson(json, java.sql.Date.class);
|
||||
|
@ -58,6 +61,7 @@ public class SqlTypesGsonTest extends TestCase {
|
|||
}
|
||||
|
||||
// http://code.google.com/p/google-gson/issues/detail?id=230
|
||||
@Test
|
||||
public void testSqlDateSerialization() throws Exception {
|
||||
TimeZone defaultTimeZone = TimeZone.getDefault();
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||
|
@ -75,18 +79,21 @@ public class SqlTypesGsonTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSqlTimeSerialization() {
|
||||
Time now = new Time(1259875082000L);
|
||||
String json = gson.toJson(now);
|
||||
assertEquals("\"01:18:02 PM\"", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSqlTimeDeserialization() {
|
||||
String json = "'1:18:02 PM'";
|
||||
Time extracted = gson.fromJson(json, Time.class);
|
||||
DefaultTypeAdaptersTest.assertEqualsTime(extracted, 13, 18, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSqlTimestampSerialization() {
|
||||
Timestamp now = new java.sql.Timestamp(1259875082000L);
|
||||
String json = gson.toJson(now);
|
||||
|
@ -97,6 +104,7 @@ public class SqlTypesGsonTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSqlTimestampDeserialization() {
|
||||
String json = "'Dec 3, 2009 1:18:02 PM'";
|
||||
Timestamp extracted = gson.fromJson(json, Timestamp.class);
|
||||
|
@ -105,6 +113,7 @@ public class SqlTypesGsonTest extends TestCase {
|
|||
}
|
||||
|
||||
// http://code.google.com/p/google-gson/issues/detail?id=230
|
||||
@Test
|
||||
public void testTimestampSerialization() throws Exception {
|
||||
TimeZone defaultTimeZone = TimeZone.getDefault();
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package com.google.gson.internal.sql;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SqlTypesSupportTest extends TestCase {
|
||||
import org.junit.Test;
|
||||
|
||||
public class SqlTypesSupportTest {
|
||||
@Test
|
||||
public void testSupported() {
|
||||
assertTrue(SqlTypesSupport.SUPPORTS_SQL_TYPES);
|
||||
|
||||
|
|
|
@ -16,19 +16,22 @@
|
|||
|
||||
package com.google.gson.metrics;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests to measure performance for Gson. All tests in this file will be disabled in code. To run
|
||||
|
@ -37,23 +40,25 @@ import java.util.Map;
|
|||
* @author Inderjeet Singh
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class PerformanceTest extends TestCase {
|
||||
public class PerformanceTest {
|
||||
private static final int COLLECTION_SIZE = 5000;
|
||||
|
||||
private static final int NUM_ITERATIONS = 100;
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDummy() {
|
||||
// This is here to prevent Junit for complaining when we disable all tests.
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testStringDeserialization() {
|
||||
StringBuilder sb = new StringBuilder(8096);
|
||||
sb.append("Error Yippie");
|
||||
|
@ -111,6 +116,8 @@ public class PerformanceTest extends TestCase {
|
|||
/**
|
||||
* Created in response to http://code.google.com/p/google-gson/issues/detail?id=96
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testLargeCollectionSerialization() {
|
||||
int count = 1400000;
|
||||
List<CollectionEntry> list = new ArrayList<>(count);
|
||||
|
@ -123,6 +130,8 @@ public class PerformanceTest extends TestCase {
|
|||
/**
|
||||
* Created in response to http://code.google.com/p/google-gson/issues/detail?id=96
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testLargeCollectionDeserialization() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int count = 87000;
|
||||
|
@ -147,6 +156,8 @@ public class PerformanceTest extends TestCase {
|
|||
* Created in response to http://code.google.com/p/google-gson/issues/detail?id=96
|
||||
*/
|
||||
// Last I tested, Gson was able to serialize upto 14MB byte array
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testByteArraySerialization() {
|
||||
for (int size = 4145152; true; size += 1036288) {
|
||||
byte[] ba = new byte[size];
|
||||
|
@ -162,6 +173,8 @@ public class PerformanceTest extends TestCase {
|
|||
* Created in response to http://code.google.com/p/google-gson/issues/detail?id=96
|
||||
*/
|
||||
// Last I tested, Gson was able to deserialize a byte array of 11MB
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testByteArrayDeserialization() {
|
||||
for (int numElements = 10639296; true; numElements += 16384) {
|
||||
StringBuilder sb = new StringBuilder(numElements*2);
|
||||
|
@ -190,7 +203,9 @@ public class PerformanceTest extends TestCase {
|
|||
// Deserialized classes avg time: 70 ms
|
||||
// Serialize exposed classes avg time: 159 ms
|
||||
// Deserialized exposed classes avg time: 173 ms
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testSerializeClasses() {
|
||||
ClassWithList c = new ClassWithList("str");
|
||||
for (int i = 0; i < COLLECTION_SIZE; ++i) {
|
||||
|
@ -206,6 +221,8 @@ public class PerformanceTest extends TestCase {
|
|||
System.out.printf("Serialize classes avg time: %d ms\n", avg);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testDeserializeClasses() {
|
||||
String json = buildJsonForClassWithList();
|
||||
ClassWithList[] target = new ClassWithList[NUM_ITERATIONS];
|
||||
|
@ -217,7 +234,9 @@ public class PerformanceTest extends TestCase {
|
|||
long avg = (t2 - t1) / NUM_ITERATIONS;
|
||||
System.out.printf("Deserialize classes avg time: %d ms\n", avg);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testLargeObjectSerializationAndDeserialization() {
|
||||
Map<String, Long> largeObject = new HashMap<>();
|
||||
for (long l = 0; l < 100000; l++) {
|
||||
|
@ -236,6 +255,8 @@ public class PerformanceTest extends TestCase {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testSerializeExposedClasses() {
|
||||
ClassWithListOfObjects c1 = new ClassWithListOfObjects("str");
|
||||
for (int i1 = 0; i1 < COLLECTION_SIZE; ++i1) {
|
||||
|
@ -252,6 +273,8 @@ public class PerformanceTest extends TestCase {
|
|||
System.out.printf("Serialize exposed classes avg time: %d ms\n", avg);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testDeserializeExposedClasses() {
|
||||
String json = buildJsonForClassWithList();
|
||||
ClassWithListOfObjects[] target = new ClassWithListOfObjects[NUM_ITERATIONS];
|
||||
|
@ -264,6 +287,8 @@ public class PerformanceTest extends TestCase {
|
|||
System.out.printf("Deserialize exposed classes avg time: %d ms\n", avg);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void disabled_testLargeGsonMapRoundTrip() throws Exception {
|
||||
Map<Long, Long> original = new HashMap<>();
|
||||
for (long i = 0; i < 1000000; i++) {
|
||||
|
|
|
@ -16,6 +16,11 @@
|
|||
|
||||
package com.google.gson.reflect;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
|
@ -23,12 +28,12 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.RandomAccess;
|
||||
import java.util.Set;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Jesse Wilson
|
||||
*/
|
||||
public final class TypeTokenTest extends TestCase {
|
||||
public final class TypeTokenTest {
|
||||
// These fields are accessed using reflection by the tests below
|
||||
List<Integer> listOfInteger = null;
|
||||
List<Number> listOfNumber = null;
|
||||
|
@ -38,6 +43,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
List<Set<?>> listOfSetOfUnknown = null;
|
||||
|
||||
@SuppressWarnings({"deprecation"})
|
||||
@Test
|
||||
public void testIsAssignableFromRawTypes() {
|
||||
assertTrue(TypeToken.get(Object.class).isAssignableFrom(String.class));
|
||||
assertFalse(TypeToken.get(String.class).isAssignableFrom(Object.class));
|
||||
|
@ -46,6 +52,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
}
|
||||
|
||||
@SuppressWarnings({"deprecation"})
|
||||
@Test
|
||||
public void testIsAssignableFromWithTypeParameters() throws Exception {
|
||||
Type a = getClass().getDeclaredField("listOfInteger").getGenericType();
|
||||
Type b = getClass().getDeclaredField("listOfNumber").getGenericType();
|
||||
|
@ -59,6 +66,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
}
|
||||
|
||||
@SuppressWarnings({"deprecation"})
|
||||
@Test
|
||||
public void testIsAssignableFromWithBasicWildcards() throws Exception {
|
||||
Type a = getClass().getDeclaredField("listOfString").getGenericType();
|
||||
Type b = getClass().getDeclaredField("listOfUnknown").getGenericType();
|
||||
|
@ -73,6 +81,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
}
|
||||
|
||||
@SuppressWarnings({"deprecation"})
|
||||
@Test
|
||||
public void testIsAssignableFromWithNestedWildcards() throws Exception {
|
||||
Type a = getClass().getDeclaredField("listOfSetOfString").getGenericType();
|
||||
Type b = getClass().getDeclaredField("listOfSetOfUnknown").getGenericType();
|
||||
|
@ -85,6 +94,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
assertFalse(TypeToken.get(b).isAssignableFrom(a));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayFactory() {
|
||||
TypeToken<?> expectedStringArray = new TypeToken<String[]>() {};
|
||||
assertEquals(expectedStringArray, TypeToken.getArray(String.class));
|
||||
|
@ -100,6 +110,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParameterizedFactory() {
|
||||
TypeToken<?> expectedListOfString = new TypeToken<List<String>>() {};
|
||||
assertEquals(expectedListOfString, TypeToken.getParameterized(List.class, String.class));
|
||||
|
@ -122,6 +133,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
assertEquals(expectedSatisfyingTwoBounds, TypeToken.getParameterized(GenericWithMultiBound.class, ClassSatisfyingBounds.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParameterizedFactory_Invalid() {
|
||||
try {
|
||||
TypeToken.getParameterized(null, new Type[0]);
|
||||
|
@ -207,6 +219,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
private static class CustomTypeToken extends TypeToken<String> {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeTokenNonAnonymousSubclass() {
|
||||
TypeToken<?> typeToken = new CustomTypeToken();
|
||||
assertEquals(String.class, typeToken.getRawType());
|
||||
|
@ -217,6 +230,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
* User must only create direct subclasses of TypeToken, but not subclasses
|
||||
* of subclasses (...) of TypeToken.
|
||||
*/
|
||||
@Test
|
||||
public void testTypeTokenSubSubClass() {
|
||||
class SubTypeToken<T> extends TypeToken<String> {}
|
||||
class SubSubTypeToken1<T> extends SubTypeToken<T> {}
|
||||
|
@ -245,6 +259,7 @@ public final class TypeTokenTest extends TestCase {
|
|||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void testTypeTokenRaw() {
|
||||
try {
|
||||
new TypeToken() {};
|
||||
|
|
|
@ -15,22 +15,25 @@
|
|||
*/
|
||||
package com.google.gson.regression;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.TypeAdapterFactory;
|
||||
import com.google.gson.annotations.JsonAdapter;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import org.junit.Test;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class JsonAdapterNullSafeTest extends TestCase {
|
||||
public class JsonAdapterNullSafeTest {
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
@Test
|
||||
public void testNullSafeBugSerialize() throws Exception {
|
||||
Device device = new Device("ec57803e");
|
||||
gson.toJson(device);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSafeBugDeserialize() throws Exception {
|
||||
Device device = gson.fromJson("{'id':'ec57803e2'}", Device.class);
|
||||
assertEquals("ec57803e2", device.id);
|
||||
|
|
|
@ -15,25 +15,29 @@
|
|||
*/
|
||||
package com.google.gson.regression;
|
||||
|
||||
import java.io.InputStream;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.jar.Manifest;
|
||||
import org.junit.Test;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class OSGiTest extends TestCase {
|
||||
public void testComGoogleGsonAnnotationsPackage() throws Exception {
|
||||
public class OSGiTest {
|
||||
@Test
|
||||
public void testComGoogleGsonAnnotationsPackage() throws Exception {
|
||||
Manifest mf = findManifest("com.google.gson");
|
||||
String importPkg = mf.getMainAttributes().getValue("Import-Package");
|
||||
assertNotNull("Import-Package statement is there", importPkg);
|
||||
assertSubstring("There should be com.google.gson.annotations dependency", importPkg, "com.google.gson.annotations");
|
||||
}
|
||||
|
||||
public void testSunMiscImportPackage() throws Exception {
|
||||
@Test
|
||||
public void testSunMiscImportPackage() throws Exception {
|
||||
Manifest mf = findManifest("com.google.gson");
|
||||
String importPkg = mf.getMainAttributes().getValue("Import-Package");
|
||||
assertNotNull("Import-Package statement is there", importPkg);
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -16,16 +16,20 @@
|
|||
|
||||
package com.google.gson.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.gson.internal.LazilyParsedNumber;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public final class JsonWriterTest extends TestCase {
|
||||
public final class JsonWriterTest {
|
||||
|
||||
@Test
|
||||
public void testTopLevelValueTypes() throws IOException {
|
||||
StringWriter string1 = new StringWriter();
|
||||
JsonWriter writer1 = new JsonWriter(string1);
|
||||
|
@ -58,6 +62,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("\"a\"", string5.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidTopLevelTypes() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -69,6 +74,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoNames() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -81,6 +87,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNameWithoutValue() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -93,6 +100,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueWithoutName() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -104,6 +112,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleTopLevelValues() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -115,6 +124,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadNestingObject() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -127,6 +137,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadNestingArray() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -139,6 +150,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullName() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -150,6 +162,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullStringValue() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -160,6 +173,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("{\"a\":null}", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonValue() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -172,6 +186,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("{\"a\":{\"b\":true},\"c\":1}", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonFiniteFloats() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -196,6 +211,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonFiniteDoubles() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -220,6 +236,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonFiniteNumbers() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -250,6 +267,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonFiniteFloatsWhenLenient() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -262,6 +280,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[NaN,-Infinity,Infinity]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonFiniteDoublesWhenLenient() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -274,6 +293,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[NaN,-Infinity,Infinity]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonFiniteNumbersWhenLenient() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -287,6 +307,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[NaN,-Infinity,Infinity,Infinity]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloats() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -317,6 +338,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubles() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -343,6 +365,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
+ "2.718281828459045]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongs() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -361,6 +384,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
+ "9223372036854775807]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumbers() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -380,6 +404,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
/**
|
||||
* Tests writing {@code Number} instances which are not one of the standard JDK ones.
|
||||
*/
|
||||
@Test
|
||||
public void testNumbersCustomClass() throws IOException {
|
||||
String[] validNumbers = {
|
||||
"-0.0",
|
||||
|
@ -413,6 +438,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMalformedNumbers() throws IOException {
|
||||
String[] malformedNumbers = {
|
||||
"some text",
|
||||
|
@ -452,6 +478,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleans() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -462,6 +489,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[true,false]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoxedBooleans() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -473,6 +501,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[true,false,null]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNulls() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -482,6 +511,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[null]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrings() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -525,6 +555,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
+ "\"\\u0019\"]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnicodeLineBreaksEscaped() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -534,6 +565,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[\"\\u2028 \\u2029\"]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyArray() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -542,6 +574,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyObject() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -550,6 +583,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("{}", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectsInArrays() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -567,6 +601,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
+ "{\"c\":6,\"d\":true}]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArraysInObjects() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -586,6 +621,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
+ "\"b\":[6,true]}", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeepNestingArrays() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -598,6 +634,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeepNestingObjects() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -615,6 +652,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
+ "}}}}}}}}}}}}}}}}}}}}}", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRepeatedName() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -626,6 +664,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("{\"a\":true,\"a\":false}", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrettyPrintObject() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -663,6 +702,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals(expected, stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrettyPrintArray() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
|
@ -700,6 +740,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals(expected, stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLenientWriterPermitsMultipleTopLevelValues() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter writer = new JsonWriter(stringWriter);
|
||||
|
@ -712,6 +753,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
assertEquals("[][]", stringWriter.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrictWriterDoesNotPermitMultipleTopLevelValues() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter writer = new JsonWriter(stringWriter);
|
||||
|
@ -724,6 +766,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClosedWriterThrowsOnStructure() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter writer = new JsonWriter(stringWriter);
|
||||
|
@ -752,6 +795,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClosedWriterThrowsOnName() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter writer = new JsonWriter(stringWriter);
|
||||
|
@ -765,6 +809,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClosedWriterThrowsOnValue() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter writer = new JsonWriter(stringWriter);
|
||||
|
@ -778,6 +823,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClosedWriterThrowsOnFlush() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter writer = new JsonWriter(stringWriter);
|
||||
|
@ -791,6 +837,7 @@ public final class JsonWriterTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriterCloseIsIdempotent() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter writer = new JsonWriter(stringWriter);
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
-keep enum com.google.gson.functional.EnumWithObfuscatedTest$Gender
|
||||
-keep class com.google.gson.functional.EnumWithObfuscatedTest {
|
||||
public void test*();
|
||||
protected void setUp();
|
||||
public void setUp();
|
||||
}
|
||||
|
||||
-dontwarn com.google.gson.functional.EnumWithObfuscatedTest
|
||||
|
|
|
@ -31,21 +31,21 @@ import com.google.gson.protobuf.generated.Bag.ProtoWithAnnotations.InnerMessage;
|
|||
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.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for protocol buffers using annotations for field names and enum values.
|
||||
*
|
||||
* @author Emmanuel Cron
|
||||
*/
|
||||
public class ProtosWithAnnotationsTest extends TestCase {
|
||||
public class ProtosWithAnnotationsTest {
|
||||
private Gson gson;
|
||||
private Gson gsonWithEnumNumbers;
|
||||
private Gson gsonWithLowerHyphen;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
ProtoTypeAdapter.Builder protoTypeAdapter = ProtoTypeAdapter.newBuilder()
|
||||
.setEnumSerialization(EnumSerialization.NAME)
|
||||
.addSerializedNameExtension(Annotations.serializedName)
|
||||
|
@ -65,6 +65,7 @@ public class ProtosWithAnnotationsTest extends TestCase {
|
|||
.create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProtoWithAnnotations_deserialize() {
|
||||
String json = String.format("{ %n"
|
||||
+ " \"id\":\"41e5e7fd6065d101b97018a465ffff01\",%n"
|
||||
|
@ -142,6 +143,7 @@ public class ProtosWithAnnotationsTest extends TestCase {
|
|||
+ "}]}}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProtoWithAnnotations_deserializeUnknownEnumValue() {
|
||||
String json = String.format("{ %n"
|
||||
+ " \"content\":\"UNKNOWN\"%n"
|
||||
|
@ -150,6 +152,7 @@ public class ProtosWithAnnotationsTest extends TestCase {
|
|||
assertThat(proto.getContent()).isEqualTo(Type.UNKNOWN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProtoWithAnnotations_deserializeUnrecognizedEnumValue() {
|
||||
String json = String.format("{ %n"
|
||||
+ " \"content\":\"UNRECOGNIZED\"%n"
|
||||
|
@ -162,6 +165,7 @@ public class ProtosWithAnnotationsTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProtoWithAnnotations_deserializeWithEnumNumbers() {
|
||||
String json = String.format("{ %n"
|
||||
+ " \"content\":\"0\"%n"
|
||||
|
@ -180,6 +184,7 @@ public class ProtosWithAnnotationsTest extends TestCase {
|
|||
assertThat(rebuilt).isEqualTo("{\"content\":2}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProtoWithAnnotations_serialize() {
|
||||
ProtoWithAnnotations proto = ProtoWithAnnotations.newBuilder()
|
||||
.setId("09f3j20839h032y0329hf30932h0nffn")
|
||||
|
|
|
@ -15,6 +15,9 @@
|
|||
*/
|
||||
package com.google.gson.protobuf.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.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.ProtoWithDifferentCaseFormat;
|
|||
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.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Functional tests for protocol buffers using complex and repeated fields
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class ProtosWithComplexAndRepeatedFieldsTest extends TestCase {
|
||||
public class ProtosWithComplexAndRepeatedFieldsTest {
|
||||
private Gson gson;
|
||||
private Gson upperCamelGson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson =
|
||||
new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(GeneratedMessageV3.class,
|
||||
|
@ -56,6 +59,7 @@ public class ProtosWithComplexAndRepeatedFieldsTest extends TestCase {
|
|||
.create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeRepeatedFields() {
|
||||
ProtoWithRepeatedFields proto = ProtoWithRepeatedFields.newBuilder()
|
||||
.addNumbers(2)
|
||||
|
@ -69,6 +73,7 @@ public class ProtosWithComplexAndRepeatedFieldsTest extends TestCase {
|
|||
assertTrue(json.contains("count"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeRepeatedFieldsProto() {
|
||||
String json = "{numbers:[4,6],simples:[{msg:'bar'},{count:7}]}";
|
||||
ProtoWithRepeatedFields proto =
|
||||
|
@ -79,6 +84,7 @@ public class ProtosWithComplexAndRepeatedFieldsTest extends TestCase {
|
|||
assertEquals(7, proto.getSimples(1).getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeDifferentCaseFormat() {
|
||||
final ProtoWithDifferentCaseFormat proto =
|
||||
ProtoWithDifferentCaseFormat.newBuilder()
|
||||
|
@ -90,6 +96,7 @@ public class ProtosWithComplexAndRepeatedFieldsTest extends TestCase {
|
|||
assertEquals("bar", json.get("NameThatTestsCaseFormat").getAsJsonArray().get(0).getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeDifferentCaseFormat() {
|
||||
final String json = "{NameThatTestsCaseFormat:['bar'],AnotherField:'foo'}";
|
||||
ProtoWithDifferentCaseFormat proto =
|
||||
|
|
|
@ -15,6 +15,10 @@
|
|||
*/
|
||||
package com.google.gson.protobuf.functional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.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.ProtoTypeAdapter.EnumSerialization;
|
|||
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.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ProtosWithPrimitiveTypesTest extends TestCase {
|
||||
public class ProtosWithPrimitiveTypesTest {
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
gson = new GsonBuilder().registerTypeHierarchyAdapter(
|
||||
GeneratedMessageV3.class, ProtoTypeAdapter.newBuilder()
|
||||
.setEnumSerialization(EnumSerialization.NUMBER)
|
||||
|
@ -37,18 +41,21 @@ public class ProtosWithPrimitiveTypesTest extends TestCase {
|
|||
.create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeEmptyProto() {
|
||||
SimpleProto proto = SimpleProto.newBuilder().build();
|
||||
String json = gson.toJson(proto);
|
||||
assertEquals("{}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeEmptyProto() {
|
||||
SimpleProto proto = gson.fromJson("{}", SimpleProto.class);
|
||||
assertFalse(proto.hasCount());
|
||||
assertFalse(proto.hasMsg());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeProto() {
|
||||
Descriptor descriptor = SimpleProto.getDescriptor();
|
||||
SimpleProto proto = SimpleProto.newBuilder()
|
||||
|
@ -60,12 +67,14 @@ public class ProtosWithPrimitiveTypesTest extends TestCase {
|
|||
assertTrue(json.contains("\"count\":3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeProto() {
|
||||
SimpleProto proto = gson.fromJson("{msg:'foo',count:3}", SimpleProto.class);
|
||||
assertEquals("foo", proto.getMsg());
|
||||
assertEquals(3, proto.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeWithExplicitNullValue() {
|
||||
SimpleProto proto = gson.fromJson("{msg:'foo',count:null}", SimpleProto.class);
|
||||
assertEquals("foo", proto.getMsg());
|
||||
|
|
Loading…
Reference in New Issue