Moved enum-related tests under EnumTest class.
This commit is contained in:
parent
4f727df749
commit
e340801d25
@ -295,14 +295,6 @@ public class TestTypes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static enum MyEnum {
|
|
||||||
VALUE1, VALUE2;
|
|
||||||
|
|
||||||
public String getExpectedJson() {
|
|
||||||
return "\"" + toString() + "\"";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ClassOverridingEquals {
|
public static class ClassOverridingEquals {
|
||||||
public ClassOverridingEquals ref;
|
public ClassOverridingEquals ref;
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@ import com.google.gson.Gson;
|
|||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.common.MoreAsserts;
|
import com.google.gson.common.MoreAsserts;
|
||||||
import com.google.gson.common.TestTypes.CrazyLongTypeAdapter;
|
import com.google.gson.common.TestTypes.CrazyLongTypeAdapter;
|
||||||
import com.google.gson.common.TestTypes.MyEnum;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
@ -104,12 +103,6 @@ public class ArrayTest extends TestCase {
|
|||||||
assertEquals("World", target[1]);
|
assertEquals("World", target[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testTopLevelEnumInASingleElementArrayDeserialization() {
|
|
||||||
String json = "[" + MyEnum.VALUE1.getExpectedJson() + "]";
|
|
||||||
MyEnum target = gson.fromJson(json, MyEnum.class);
|
|
||||||
assertEquals(json, "[" + target.getExpectedJson() + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void testArrayOfCollectionSerialization() throws Exception {
|
public void testArrayOfCollectionSerialization() throws Exception {
|
||||||
StringBuilder sb = new StringBuilder("[");
|
StringBuilder sb = new StringBuilder("[");
|
||||||
|
@ -20,7 +20,6 @@ import com.google.gson.Gson;
|
|||||||
import com.google.gson.JsonParseException;
|
import com.google.gson.JsonParseException;
|
||||||
import com.google.gson.common.MoreAsserts;
|
import com.google.gson.common.MoreAsserts;
|
||||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||||
import com.google.gson.common.TestTypes.MyEnum;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
@ -177,26 +176,6 @@ public class CollectionTest extends TestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCollectionOfEnumsSerialization() {
|
|
||||||
Type type = new TypeToken<Collection<MyEnum>>() {}.getType();
|
|
||||||
Collection<MyEnum> target = new ArrayList<MyEnum>();
|
|
||||||
target.add(MyEnum.VALUE1);
|
|
||||||
target.add(MyEnum.VALUE2);
|
|
||||||
String expectedJson = "[\"VALUE1\",\"VALUE2\"]";
|
|
||||||
String actualJson = gson.toJson(target);
|
|
||||||
assertEquals(expectedJson, actualJson);
|
|
||||||
actualJson = gson.toJson(target, type);
|
|
||||||
assertEquals(expectedJson, actualJson);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testCollectionOfEnumsDeserialization() {
|
|
||||||
Type type = new TypeToken<Collection<MyEnum>>() {}.getType();
|
|
||||||
String json = "[\"VALUE1\",\"VALUE2\"]";
|
|
||||||
Collection<MyEnum> target = gson.fromJson(json, type);
|
|
||||||
MoreAsserts.assertContains(target, MyEnum.VALUE1);
|
|
||||||
MoreAsserts.assertContains(target, MyEnum.VALUE2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testCollectionOfStringsDeserialization() {
|
public void testCollectionOfStringsDeserialization() {
|
||||||
String json = "[\"Hello\",\"World\"]";
|
String json = "[\"Hello\",\"World\"]";
|
||||||
Type collectionType = new TypeToken<Collection<String>>() { }.getType();
|
Type collectionType = new TypeToken<Collection<String>>() { }.getType();
|
||||||
|
@ -17,9 +17,15 @@
|
|||||||
package com.google.gson.functional;
|
package com.google.gson.functional;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.common.MoreAsserts;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Functional tests for Java 5.0 enums.
|
* Functional tests for Java 5.0 enums.
|
||||||
*
|
*
|
||||||
@ -36,40 +42,67 @@ public class EnumTest extends TestCase {
|
|||||||
gson = new Gson();
|
gson = new Gson();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEnumSerialization() throws Exception {
|
public void testTopLevelEnumSerialization() throws Exception {
|
||||||
String result = gson.toJson(TestEnum.TEST_1);
|
String result = gson.toJson(MyEnum.VALUE1);
|
||||||
assertEquals('"' + TestEnum.TEST_1.toString() + '"', result);
|
assertEquals('"' + MyEnum.VALUE1.toString() + '"', result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEnumDeserialization() throws Exception {
|
public void testTopLevelEnumDeserialization() throws Exception {
|
||||||
TestEnum result = gson.fromJson('"' + TestEnum.TEST_1.toString() + '"', TestEnum.class);
|
MyEnum result = gson.fromJson('"' + MyEnum.VALUE1.toString() + '"', MyEnum.class);
|
||||||
assertEquals(TestEnum.TEST_1, result);
|
assertEquals(MyEnum.VALUE1, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEnumFieldSerialization() throws Exception {
|
public void testTopLevelEnumInASingleElementArrayDeserialization() {
|
||||||
String result = gson.toJson(TestEnum.TEST_1);
|
String json = "[" + MyEnum.VALUE1.getExpectedJson() + "]";
|
||||||
assertEquals('"' + TestEnum.TEST_1.toString() + '"', result);
|
MyEnum target = gson.fromJson(json, MyEnum.class);
|
||||||
}
|
assertEquals(json, "[" + target.getExpectedJson() + "]");
|
||||||
|
|
||||||
public void testEnumFieldDeserialization() throws Exception {
|
|
||||||
Foo result = gson.fromJson("{\"f\":\"TEST_1\"}", Foo.class);
|
|
||||||
assertEquals(TestEnum.TEST_1, result.f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static enum TestEnum {
|
public void testCollectionOfEnumsSerialization() {
|
||||||
TEST_1,
|
Type type = new TypeToken<Collection<MyEnum>>() {}.getType();
|
||||||
TEST_2;
|
Collection<MyEnum> target = new ArrayList<MyEnum>();
|
||||||
|
target.add(MyEnum.VALUE1);
|
||||||
|
target.add(MyEnum.VALUE2);
|
||||||
|
String expectedJson = "[\"VALUE1\",\"VALUE2\"]";
|
||||||
|
String actualJson = gson.toJson(target);
|
||||||
|
assertEquals(expectedJson, actualJson);
|
||||||
|
actualJson = gson.toJson(target, type);
|
||||||
|
assertEquals(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCollectionOfEnumsDeserialization() {
|
||||||
|
Type type = new TypeToken<Collection<MyEnum>>() {}.getType();
|
||||||
|
String json = "[\"VALUE1\",\"VALUE2\"]";
|
||||||
|
Collection<MyEnum> target = gson.fromJson(json, type);
|
||||||
|
MoreAsserts.assertContains(target, MyEnum.VALUE1);
|
||||||
|
MoreAsserts.assertContains(target, MyEnum.VALUE2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testClassWithEnumFieldSerialization() throws Exception {
|
||||||
|
ClassWithEnumFields target = new ClassWithEnumFields();
|
||||||
|
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testClassWithEnumFieldDeserialization() throws Exception {
|
||||||
|
String json = "{value1:'VALUE1',value2:'VALUE2'}";
|
||||||
|
ClassWithEnumFields target = gson.fromJson(json, ClassWithEnumFields.class);
|
||||||
|
assertEquals(MyEnum.VALUE1,target.value1);
|
||||||
|
assertEquals(MyEnum.VALUE2,target.value2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Foo {
|
private static enum MyEnum {
|
||||||
private TestEnum f;
|
VALUE1, VALUE2;
|
||||||
|
|
||||||
public Foo() {
|
public String getExpectedJson() {
|
||||||
this(TestEnum.TEST_1);
|
return "\"" + toString() + "\"";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public Foo(TestEnum f) {
|
|
||||||
this.f = f;
|
private static class ClassWithEnumFields {
|
||||||
|
private final MyEnum value1 = MyEnum.VALUE1;
|
||||||
|
private final MyEnum value2 = MyEnum.VALUE2;
|
||||||
|
public String getExpectedJson() {
|
||||||
|
return "{\"value1\":\"" + value1 + "\",\"value2\":\"" + value2 + "\"}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,6 @@ import com.google.gson.common.TestTypes.ClassWithArray;
|
|||||||
import com.google.gson.common.TestTypes.ClassWithNoFields;
|
import com.google.gson.common.TestTypes.ClassWithNoFields;
|
||||||
import com.google.gson.common.TestTypes.ClassWithObjects;
|
import com.google.gson.common.TestTypes.ClassWithObjects;
|
||||||
import com.google.gson.common.TestTypes.ClassWithTransientFields;
|
import com.google.gson.common.TestTypes.ClassWithTransientFields;
|
||||||
import com.google.gson.common.TestTypes.MyEnum;
|
|
||||||
import com.google.gson.common.TestTypes.Nested;
|
import com.google.gson.common.TestTypes.Nested;
|
||||||
import com.google.gson.common.TestTypes.PrimitiveArray;
|
import com.google.gson.common.TestTypes.PrimitiveArray;
|
||||||
|
|
||||||
@ -220,28 +219,6 @@ public class ObjectTest extends TestCase {
|
|||||||
assertEquals(json, target.getExpectedJson());
|
assertEquals(json, target.getExpectedJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testClassWithEnumFieldSerialization() throws Exception {
|
|
||||||
ClassWithEnumFields target = new ClassWithEnumFields();
|
|
||||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testClassWithEnumFieldDeserialization() throws Exception {
|
|
||||||
String json = new ClassWithEnumFields().getExpectedJson();
|
|
||||||
ClassWithEnumFields target = gson.fromJson(json, ClassWithEnumFields.class);
|
|
||||||
assertEquals(json, target.getExpectedJson());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testTopLevelEnumSerialization() throws Exception {
|
|
||||||
MyEnum target = MyEnum.VALUE1;
|
|
||||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testTopLevelEnumDeserialization() throws Exception {
|
|
||||||
String json = MyEnum.VALUE1.getExpectedJson();
|
|
||||||
MyEnum target = gson.fromJson(json, MyEnum.class);
|
|
||||||
assertEquals(json, target.getExpectedJson());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testSubInterfacesOfCollectionSerialization() throws Exception {
|
public void testSubInterfacesOfCollectionSerialization() throws Exception {
|
||||||
List<Integer> list = new LinkedList<Integer>();
|
List<Integer> list = new LinkedList<Integer>();
|
||||||
list.add(0);
|
list.add(0);
|
||||||
@ -556,14 +533,6 @@ public class ObjectTest extends TestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ClassWithEnumFields {
|
|
||||||
private final MyEnum value1 = MyEnum.VALUE1;
|
|
||||||
private final MyEnum value2 = MyEnum.VALUE2;
|
|
||||||
public String getExpectedJson() {
|
|
||||||
return "{\"value1\":\"" + value1 + "\",\"value2\":\"" + value2 + "\"}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ClassWithPrivateNoArgsConstructor {
|
private static class ClassWithPrivateNoArgsConstructor {
|
||||||
public int a;
|
public int a;
|
||||||
private ClassWithPrivateNoArgsConstructor() {
|
private ClassWithPrivateNoArgsConstructor() {
|
||||||
|
Loading…
Reference in New Issue
Block a user