Adding new tests (two of which are disabled, but prepping for 1.7).
This commit is contained in:
parent
f6a332971f
commit
6a951b427e
@ -16,18 +16,26 @@
|
||||
|
||||
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.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumSet;
|
||||
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.
|
||||
*
|
||||
@ -112,6 +120,17 @@ public class EnumTest extends TestCase {
|
||||
* Test for issue 226.
|
||||
*/
|
||||
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());
|
||||
assertEquals("\"ROCK\"", gson.toJson(Roshambo.ROCK));
|
||||
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()));
|
||||
}
|
||||
|
||||
private enum Roshambo {
|
||||
public enum Roshambo {
|
||||
ROCK {
|
||||
@Override Roshambo defeats() {
|
||||
return SCISSORS;
|
||||
@ -139,4 +158,16 @@ public class EnumTest extends TestCase {
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,16 +26,19 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* Functional test for Gson serialization and deserialization of
|
||||
* classes with type variables.
|
||||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class TypeVariableTest extends TestCase {
|
||||
|
||||
public void testSingle() throws Exception {
|
||||
public void disabled_testAdvancedTypeVariables() throws Exception {
|
||||
Gson gson = new Gson();
|
||||
Bar bar1 = new Bar("someString", 1);
|
||||
Bar bar1 = new Bar("someString", 1, true);
|
||||
ArrayList<Integer> arrayList = new ArrayList<Integer>();
|
||||
arrayList.add(1);
|
||||
arrayList.add(2);
|
||||
arrayList.add(3);
|
||||
bar1.map.put("key1", arrayList);
|
||||
bar1.map.put("key2", new ArrayList<Integer>());
|
||||
String json = gson.toJson(bar1);
|
||||
@ -44,36 +47,80 @@ public class TypeVariableTest extends TestCase {
|
||||
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 T someTField;
|
||||
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.someTField = tValue;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
// Technically, we should implement hashcode too
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof Foo)) {
|
||||
if (!(o instanceof Foo<?, ?>)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
Foo<S, T> realFoo = (Foo<S, T>) o;
|
||||
return someTField.equals(realFoo.someTField)
|
||||
return redField.equals(realFoo.redField)
|
||||
&& someTField.equals(realFoo.someTField)
|
||||
&& someSField.equals(realFoo.someSField)
|
||||
&& map.equals(realFoo.map);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Bar extends Foo<String, Integer> {
|
||||
public Bar() {
|
||||
this("", 0);
|
||||
this("", 0, false);
|
||||
}
|
||||
|
||||
public Bar(String s, Integer i) {
|
||||
super(s, i);
|
||||
public Bar(String s, Integer i, boolean b) {
|
||||
super(s, i, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user