Fixed the test that broke while fixing issue 100 in r394 by using a TreeSet for SortedSets and HashSet for other kind of sets.

This commit is contained in:
Inderjeet Singh 2009-03-12 18:17:14 +00:00
parent a595032a90
commit 4fc6400f5c
2 changed files with 55 additions and 5 deletions

View File

@ -80,6 +80,7 @@ final class DefaultTypeAdapters {
private static final StringTypeAdapter STRING_TYPE_ADAPTER = new StringTypeAdapter();
private static final PropertiesCreator PROPERTIES_CREATOR = new PropertiesCreator();
private static final TreeSetCreator TREE_SET_CREATOR = new TreeSetCreator();
private static final HashSetCreator HASH_SET_CREATOR = new HashSetCreator();
private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER =
new GregorianCalendarTypeAdapter();
@ -180,7 +181,7 @@ final class DefaultTypeAdapters {
map.register(Queue.class, COLLECTION_TYPE_ADAPTER);
map.register(Set.class, HASH_SET_CREATOR);
map.register(SortedSet.class, HASH_SET_CREATOR);
map.register(SortedSet.class, TREE_SET_CREATOR);
map.register(Properties.class, PROPERTIES_CREATOR);
map.makeUnmodifiable();
return map;
@ -780,6 +781,16 @@ final class DefaultTypeAdapters {
}
}
private static class TreeSetCreator implements InstanceCreator<TreeSet<?>> {
public TreeSet<?> createInstance(Type type) {
return new TreeSet<Object>();
}
@Override
public String toString() {
return TreeSetCreator.class.getSimpleName();
}
}
private static class HashSetCreator implements InstanceCreator<HashSet<?>> {
public HashSet<?> createInstance(Type type) {
return new HashSet<Object>();

View File

@ -272,9 +272,12 @@ public class ObjectTest extends TestCase {
String json = "{\"list\":[0,1,2,3],\"queue\":[0,1,2,3],\"set\":[0.1,0.2,0.3,0.4],"
+ "\"sortedSet\":[\"a\",\"b\",\"c\",\"d\"]"
+ "}";
ClassWithSubInterfacesOfCollection target = gson.fromJson(
json, ClassWithSubInterfacesOfCollection.class);
assertEquals(json, target.getExpectedJson());
ClassWithSubInterfacesOfCollection target =
gson.fromJson(json, ClassWithSubInterfacesOfCollection.class);
assertTrue(target.listContains(0, 1, 2, 3));
assertTrue(target.queueContains(0, 1, 2, 3));
assertTrue(target.setContains(0.1F, 0.2F, 0.3F, 0.4F));
assertTrue(target.sortedSetContains('a', 'b', 'c', 'd'));
}
/**
@ -403,7 +406,7 @@ public class ObjectTest extends TestCase {
}
}
public static class ClassWithSubInterfacesOfCollection {
private static class ClassWithSubInterfacesOfCollection {
private List<Integer> list;
private Queue<Long> queue;
private Set<Float> set;
@ -420,6 +423,42 @@ public class ObjectTest extends TestCase {
this.sortedSet = sortedSet;
}
boolean listContains(int... values) {
for (int value : values) {
if (!list.contains(value)) {
return false;
}
}
return true;
}
boolean queueContains(long... values) {
for (long value : values) {
if (!queue.contains(value)) {
return false;
}
}
return true;
}
boolean setContains(float... values) {
for (float value : values) {
if (!set.contains(value)) {
return false;
}
}
return true;
}
boolean sortedSetContains(char... values) {
for (char value : values) {
if (!sortedSet.contains(value)) {
return false;
}
}
return true;
}
public String getExpectedJson() {
StringBuilder sb = new StringBuilder();
sb.append("{");