From e340801d258e18f733620b0fd03d65fffb8df6c3 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Mon, 30 Mar 2009 18:36:55 +0000 Subject: [PATCH] Moved enum-related tests under EnumTest class. --- .../com/google/gson/common/TestTypes.java | 8 -- .../com/google/gson/functional/ArrayTest.java | 7 -- .../gson/functional/CollectionTest.java | 21 ----- .../com/google/gson/functional/EnumTest.java | 83 +++++++++++++------ .../google/gson/functional/ObjectTest.java | 31 ------- 5 files changed, 58 insertions(+), 92 deletions(-) diff --git a/gson/src/test/java/com/google/gson/common/TestTypes.java b/gson/src/test/java/com/google/gson/common/TestTypes.java index d03b746f..38d37b4d 100644 --- a/gson/src/test/java/com/google/gson/common/TestTypes.java +++ b/gson/src/test/java/com/google/gson/common/TestTypes.java @@ -295,14 +295,6 @@ public class TestTypes { } } - public static enum MyEnum { - VALUE1, VALUE2; - - public String getExpectedJson() { - return "\"" + toString() + "\""; - } - } - public static class ClassOverridingEquals { public ClassOverridingEquals ref; diff --git a/gson/src/test/java/com/google/gson/functional/ArrayTest.java b/gson/src/test/java/com/google/gson/functional/ArrayTest.java index 81feb7f4..5d85ae4b 100644 --- a/gson/src/test/java/com/google/gson/functional/ArrayTest.java +++ b/gson/src/test/java/com/google/gson/functional/ArrayTest.java @@ -20,7 +20,6 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.common.MoreAsserts; import com.google.gson.common.TestTypes.CrazyLongTypeAdapter; -import com.google.gson.common.TestTypes.MyEnum; import com.google.gson.reflect.TypeToken; import junit.framework.TestCase; @@ -104,12 +103,6 @@ public class ArrayTest extends TestCase { 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") public void testArrayOfCollectionSerialization() throws Exception { StringBuilder sb = new StringBuilder("["); diff --git a/gson/src/test/java/com/google/gson/functional/CollectionTest.java b/gson/src/test/java/com/google/gson/functional/CollectionTest.java index a4442963..e0203e11 100644 --- a/gson/src/test/java/com/google/gson/functional/CollectionTest.java +++ b/gson/src/test/java/com/google/gson/functional/CollectionTest.java @@ -20,7 +20,6 @@ import com.google.gson.Gson; import com.google.gson.JsonParseException; import com.google.gson.common.MoreAsserts; import com.google.gson.common.TestTypes.BagOfPrimitives; -import com.google.gson.common.TestTypes.MyEnum; import com.google.gson.reflect.TypeToken; import junit.framework.TestCase; @@ -177,26 +176,6 @@ public class CollectionTest extends TestCase { } } - public void testCollectionOfEnumsSerialization() { - Type type = new TypeToken>() {}.getType(); - Collection target = new ArrayList(); - 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>() {}.getType(); - String json = "[\"VALUE1\",\"VALUE2\"]"; - Collection target = gson.fromJson(json, type); - MoreAsserts.assertContains(target, MyEnum.VALUE1); - MoreAsserts.assertContains(target, MyEnum.VALUE2); - } - public void testCollectionOfStringsDeserialization() { String json = "[\"Hello\",\"World\"]"; Type collectionType = new TypeToken>() { }.getType(); diff --git a/gson/src/test/java/com/google/gson/functional/EnumTest.java b/gson/src/test/java/com/google/gson/functional/EnumTest.java index 536407f2..525fd4b2 100644 --- a/gson/src/test/java/com/google/gson/functional/EnumTest.java +++ b/gson/src/test/java/com/google/gson/functional/EnumTest.java @@ -17,9 +17,15 @@ package com.google.gson.functional; import com.google.gson.Gson; +import com.google.gson.common.MoreAsserts; +import com.google.gson.reflect.TypeToken; import junit.framework.TestCase; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; + /** * Functional tests for Java 5.0 enums. * @@ -36,40 +42,67 @@ public class EnumTest extends TestCase { gson = new Gson(); } - public void testEnumSerialization() throws Exception { - String result = gson.toJson(TestEnum.TEST_1); - assertEquals('"' + TestEnum.TEST_1.toString() + '"', result); + public void testTopLevelEnumSerialization() throws Exception { + String result = gson.toJson(MyEnum.VALUE1); + assertEquals('"' + MyEnum.VALUE1.toString() + '"', result); } - public void testEnumDeserialization() throws Exception { - TestEnum result = gson.fromJson('"' + TestEnum.TEST_1.toString() + '"', TestEnum.class); - assertEquals(TestEnum.TEST_1, result); + public void testTopLevelEnumDeserialization() throws Exception { + MyEnum result = gson.fromJson('"' + MyEnum.VALUE1.toString() + '"', MyEnum.class); + assertEquals(MyEnum.VALUE1, result); } - public void testEnumFieldSerialization() throws Exception { - String result = gson.toJson(TestEnum.TEST_1); - assertEquals('"' + TestEnum.TEST_1.toString() + '"', result); - } - - public void testEnumFieldDeserialization() throws Exception { - Foo result = gson.fromJson("{\"f\":\"TEST_1\"}", Foo.class); - assertEquals(TestEnum.TEST_1, result.f); + public void testTopLevelEnumInASingleElementArrayDeserialization() { + String json = "[" + MyEnum.VALUE1.getExpectedJson() + "]"; + MyEnum target = gson.fromJson(json, MyEnum.class); + assertEquals(json, "[" + target.getExpectedJson() + "]"); } - private static enum TestEnum { - TEST_1, - TEST_2; + public void testCollectionOfEnumsSerialization() { + Type type = new TypeToken>() {}.getType(); + Collection target = new ArrayList(); + 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>() {}.getType(); + String json = "[\"VALUE1\",\"VALUE2\"]"; + Collection 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 TestEnum f; - - public Foo() { - this(TestEnum.TEST_1); + private static enum MyEnum { + VALUE1, VALUE2; + + public String getExpectedJson() { + 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 + "\"}"; } } } diff --git a/gson/src/test/java/com/google/gson/functional/ObjectTest.java b/gson/src/test/java/com/google/gson/functional/ObjectTest.java index 530b0304..040d175f 100644 --- a/gson/src/test/java/com/google/gson/functional/ObjectTest.java +++ b/gson/src/test/java/com/google/gson/functional/ObjectTest.java @@ -28,7 +28,6 @@ import com.google.gson.common.TestTypes.ClassWithArray; import com.google.gson.common.TestTypes.ClassWithNoFields; import com.google.gson.common.TestTypes.ClassWithObjects; 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.PrimitiveArray; @@ -220,28 +219,6 @@ public class ObjectTest extends TestCase { 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 { List list = new LinkedList(); 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 { public int a; private ClassWithPrivateNoArgsConstructor() {