Adding new tests (two of which are disabled, but prepping for 1.7).

This commit is contained in:
Joel Leitch 2010-11-05 22:57:44 +00:00
parent f6a332971f
commit 6a951b427e
2 changed files with 104 additions and 26 deletions

View File

@ -16,18 +16,26 @@
package com.google.gson.functional; package com.google.gson.functional;
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.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.MoreAsserts;
import com.google.gson.reflect.TypeToken;
import junit.framework.TestCase;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Set; import java.util.Set;
import junit.framework.TestCase;
import com.google.gson.Gson;
import com.google.gson.common.MoreAsserts;
import com.google.gson.reflect.TypeToken;
/** /**
* Functional tests for Java 5.0 enums. * Functional tests for Java 5.0 enums.
* *
@ -35,25 +43,25 @@ import com.google.gson.reflect.TypeToken;
* @author Joel Leitch * @author Joel Leitch
*/ */
public class EnumTest extends TestCase { public class EnumTest extends TestCase {
private Gson gson; private Gson gson;
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
gson = new Gson(); gson = new Gson();
} }
public void testTopLevelEnumSerialization() throws Exception { public void testTopLevelEnumSerialization() throws Exception {
String result = gson.toJson(MyEnum.VALUE1); String result = gson.toJson(MyEnum.VALUE1);
assertEquals('"' + MyEnum.VALUE1.toString() + '"', result); assertEquals('"' + MyEnum.VALUE1.toString() + '"', result);
} }
public void testTopLevelEnumDeserialization() throws Exception { public void testTopLevelEnumDeserialization() throws Exception {
MyEnum result = gson.fromJson('"' + MyEnum.VALUE1.toString() + '"', MyEnum.class); MyEnum result = gson.fromJson('"' + MyEnum.VALUE1.toString() + '"', MyEnum.class);
assertEquals(MyEnum.VALUE1, result); assertEquals(MyEnum.VALUE1, result);
} }
public void testTopLevelEnumInASingleElementArrayDeserialization() { public void testTopLevelEnumInASingleElementArrayDeserialization() {
String json = "[" + MyEnum.VALUE1.getExpectedJson() + "]"; String json = "[" + MyEnum.VALUE1.getExpectedJson() + "]";
MyEnum target = gson.fromJson(json, MyEnum.class); MyEnum target = gson.fromJson(json, MyEnum.class);
@ -91,7 +99,7 @@ public class EnumTest extends TestCase {
assertEquals(MyEnum.VALUE1,target.value1); assertEquals(MyEnum.VALUE1,target.value1);
assertEquals(MyEnum.VALUE2,target.value2); assertEquals(MyEnum.VALUE2,target.value2);
} }
private static enum MyEnum { private static enum MyEnum {
VALUE1, VALUE2; VALUE1, VALUE2;
@ -112,6 +120,17 @@ public class EnumTest extends TestCase {
* Test for issue 226. * Test for issue 226.
*/ */
public void testEnumSubclass() { public void testEnumSubclass() {
assertRoshambo();
}
public void disabled_testEnumSubclassWithRegisteredTypeAdapter() {
gson = new GsonBuilder()
.registerTypeHierarchyAdapter(Roshambo.class, new MyEnumTypeAdapter())
.create();
assertRoshambo();
}
private void assertRoshambo() {
assertFalse(Roshambo.class == Roshambo.ROCK.getClass()); assertFalse(Roshambo.class == Roshambo.ROCK.getClass());
assertEquals("\"ROCK\"", gson.toJson(Roshambo.ROCK)); assertEquals("\"ROCK\"", gson.toJson(Roshambo.ROCK));
assertEquals("[\"ROCK\",\"PAPER\",\"SCISSORS\"]", gson.toJson(EnumSet.allOf(Roshambo.class))); assertEquals("[\"ROCK\",\"PAPER\",\"SCISSORS\"]", gson.toJson(EnumSet.allOf(Roshambo.class)));
@ -120,7 +139,7 @@ public class EnumTest extends TestCase {
gson.fromJson("[\"ROCK\",\"PAPER\",\"SCISSORS\"]", new TypeToken<Set<Roshambo>>() {}.getType())); gson.fromJson("[\"ROCK\",\"PAPER\",\"SCISSORS\"]", new TypeToken<Set<Roshambo>>() {}.getType()));
} }
private enum Roshambo { public enum Roshambo {
ROCK { ROCK {
@Override Roshambo defeats() { @Override Roshambo defeats() {
return SCISSORS; return SCISSORS;
@ -139,4 +158,16 @@ public class EnumTest extends TestCase {
abstract Roshambo defeats(); abstract Roshambo defeats();
} }
private static class MyEnumTypeAdapter
implements JsonSerializer<Roshambo>, JsonDeserializer<Roshambo> {
public JsonElement serialize(Roshambo src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("123" + src.name());
}
public Roshambo deserialize(JsonElement json, Type classOfT, JsonDeserializationContext context)
throws JsonParseException {
return Roshambo.valueOf(json.getAsString().substring(3));
}
}
} }

View File

@ -26,16 +26,19 @@ import java.util.Map;
/** /**
* Functional test for Gson serialization and deserialization of * Functional test for Gson serialization and deserialization of
* classes with type variables.
*
* @author Joel Leitch * @author Joel Leitch
*/ */
public class TypeVariableTest extends TestCase { public class TypeVariableTest extends TestCase {
public void testSingle() throws Exception { public void disabled_testAdvancedTypeVariables() throws Exception {
Gson gson = new Gson(); Gson gson = new Gson();
Bar bar1 = new Bar("someString", 1); Bar bar1 = new Bar("someString", 1, true);
ArrayList<Integer> arrayList = new ArrayList<Integer>(); ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(1); arrayList.add(1);
arrayList.add(2); arrayList.add(2);
arrayList.add(3);
bar1.map.put("key1", arrayList); bar1.map.put("key1", arrayList);
bar1.map.put("key2", new ArrayList<Integer>()); bar1.map.put("key2", new ArrayList<Integer>());
String json = gson.toJson(bar1); String json = gson.toJson(bar1);
@ -44,36 +47,80 @@ public class TypeVariableTest extends TestCase {
assertEquals(bar1, bar2); assertEquals(bar1, bar2);
} }
public static class Foo<S, T> { public void testBasicTypeVariables() throws Exception {
Gson gson = new Gson();
Blue blue1 = new Blue(true);
String json = gson.toJson(blue1);
Blue blue2 = gson.fromJson(json, Blue.class);
assertEquals(blue1, blue2);
}
public static class Blue extends Red<Boolean> {
public Blue() {
super(false);
}
public Blue(boolean value) {
super(value);
}
// Technically, we should implement hashcode too
@Override
public boolean equals(Object o) {
if (!(o instanceof Blue)) {
return false;
} else {
Blue blue = (Blue) o;
return redField.equals(blue.redField);
}
}
}
public static class Red<S> {
protected final S redField;
public Red(S redField) {
this.redField = redField;
}
}
public static class Foo<S, T> extends Red<Boolean> {
private final S someSField; private final S someSField;
private final T someTField; private final T someTField;
public final Map<S, List<T>> map = new HashMap<S, List<T>>(); public final Map<S, List<T>> map = new HashMap<S, List<T>>();
public Foo(S sValue, T tValue) { public Foo(S sValue, T tValue, Boolean redField) {
super(redField);
this.someSField = sValue; this.someSField = sValue;
this.someTField = tValue; this.someTField = tValue;
} }
@SuppressWarnings("unchecked") // Technically, we should implement hashcode too
@Override @Override
@SuppressWarnings("unchecked")
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof Foo)) { if (!(o instanceof Foo<?, ?>)) {
return false; return false;
} else {
Foo<S, T> realFoo = (Foo<S, T>) o;
return redField.equals(realFoo.redField)
&& someTField.equals(realFoo.someTField)
&& someSField.equals(realFoo.someSField)
&& map.equals(realFoo.map);
} }
Foo<S, T> realFoo = (Foo<S, T>) o;
return someTField.equals(realFoo.someTField)
&& someSField.equals(realFoo.someSField)
&& map.equals(realFoo.map);
} }
} }
public static class Bar extends Foo<String, Integer> { public static class Bar extends Foo<String, Integer> {
public Bar() { public Bar() {
this("", 0); this("", 0, false);
} }
public Bar(String s, Integer i) { public Bar(String s, Integer i, boolean b) {
super(s, i); super(s, i, b);
} }
} }
} }