Moved enum-related tests under EnumTest class.

This commit is contained in:
Inderjeet Singh 2009-03-30 18:36:55 +00:00
parent 4f727df749
commit e340801d25
5 changed files with 58 additions and 92 deletions

View File

@ -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;

View File

@ -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("[");

View File

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

View File

@ -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 { public void testCollectionOfEnumsSerialization() {
Foo result = gson.fromJson("{\"f\":\"TEST_1\"}", Foo.class); Type type = new TypeToken<Collection<MyEnum>>() {}.getType();
assertEquals(TestEnum.TEST_1, result.f); 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);
} }
private static enum TestEnum { public void testCollectionOfEnumsDeserialization() {
TEST_1, Type type = new TypeToken<Collection<MyEnum>>() {}.getType();
TEST_2; String json = "[\"VALUE1\",\"VALUE2\"]";
Collection<MyEnum> target = gson.fromJson(json, type);
MoreAsserts.assertContains(target, MyEnum.VALUE1);
MoreAsserts.assertContains(target, MyEnum.VALUE2);
} }
private static class Foo { public void testClassWithEnumFieldSerialization() throws Exception {
private TestEnum f; ClassWithEnumFields target = new ClassWithEnumFields();
assertEquals(target.getExpectedJson(), gson.toJson(target));
}
public Foo() { public void testClassWithEnumFieldDeserialization() throws Exception {
this(TestEnum.TEST_1); 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 enum MyEnum {
VALUE1, VALUE2;
public String getExpectedJson() {
return "\"" + toString() + "\"";
} }
}
public Foo(TestEnum f) { private static class ClassWithEnumFields {
this.f = f; private final MyEnum value1 = MyEnum.VALUE1;
private final MyEnum value2 = MyEnum.VALUE2;
public String getExpectedJson() {
return "{\"value1\":\"" + value1 + "\",\"value2\":\"" + value2 + "\"}";
} }
} }
} }

View File

@ -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() {