Skip to content

Commit

Permalink
Add Additional Test Cases (googleapis#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
geri-m committed Nov 4, 2018
1 parent 17031ba commit edfafab
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public static void parseElement(XmlPullParser parser, Object destination,
private static boolean parseElementInternal(XmlPullParser parser,
ArrayList<Type> context,
Object destination,
Type valueType,
Type valueType, // will be set fo elements of collections/arrays
XmlNamespaceDictionary namespaceDictionary,
CustomizeParser customizeParser) throws IOException, XmlPullParserException {
// TODO(yanivi): method is too long; needs to be broken down into smaller methods and comment
Expand Down Expand Up @@ -258,7 +258,7 @@ private static boolean parseElementInternal(XmlPullParser parser,
destinationMap = null;
initGenericXmlWithNamespace(parser, namespaceDictionary, genericXml);
} // if the Destination is a Map generate a destination Map
else if (destination instanceof Map<?, ?>){
else if (destination instanceof Map<?, ?>) {
genericXml = null;
destinationMap = Map.class.cast(destination);
} // if the Destination is neither a Map nor a generic XML, we have a standard Object
Expand Down Expand Up @@ -288,12 +288,20 @@ else if (destination instanceof Map<?, ?>){
case XmlPullParser.TEXT:
// parse text content
if (destination != null) {
// if destination is not null, we definitely have a classInfo

/*
if (classInfo == null) {
field = null;
} else {
field = classInfo.getField(TEXT_CONTENT);
field = classInfo.getField(fieldName);
}
*/

Preconditions.checkNotNull(classInfo);
field = classInfo.getField(TEXT_CONTENT);

// we have text content and we have a field to map this content
parseAttributeOrTextContent(parser.getText(),
field,
valueType,
Expand Down Expand Up @@ -321,33 +329,45 @@ else if (destination instanceof Map<?, ?>){
// get the "real" field name of the
String fieldName = getFieldName(false, alias, namespace, parser.getName());

// fetch the field from the classInfo

// if destination is not null, we definitely have a classInfo

/*
if (classInfo == null) {
field = null;
} else {
field = classInfo.getField(fieldName);
}
*/

// fetch the field from the classInfo
Preconditions.checkNotNull(classInfo);
field = classInfo.getField(fieldName);

Type fieldType;
// if the field is not in the destination ...
if (field == null) {
// ... set the value type to a given value type, if this is a field in a collection/array
fieldType = valueType;
} else {
// ... or if the field was found in the destination/ClassInfo set the generic Datatype
fieldType = field.getGenericType();
}

fieldType = Data.resolveWildcardTypeOrTypeVariable(context, fieldType);

// field type is now class, parameterized type, or generic array type
// resolve a parameterized type to a class
Class<?> fieldClass;
if (fieldType instanceof Class<?>) {
fieldClass = (Class<?>) fieldType;
} else if (fieldType instanceof ParameterizedType) {
fieldClass = Types.getRawClass((ParameterizedType) fieldType);
} else {
// if fieldtype is null or fieldtype is an array
fieldClass = null;
}

if (fieldType instanceof ParameterizedType) {
fieldClass = Types.getRawClass((ParameterizedType) fieldType);
}
boolean isArray = Types.isArray(fieldType);
// text content
boolean ignore = field == null && destinationMap == null && genericXml == null;
Expand All @@ -368,6 +388,7 @@ else if (destination instanceof Map<?, ?>){
break;
case XmlPullParser.TEXT:
if (!ignore && level == 1) {
// handle primitive types, that are not @Key("text()") annotated
parseAttributeOrTextContent(parser.getText(),
field,
valueType,
Expand Down Expand Up @@ -579,7 +600,7 @@ private static void processAttributes(final XmlPullParser parser, final ArrayLis

final Field field;

// if destination is not null, we defintiy have a classInfo
// if destination is not null, we definitely have a classInfo

/*
if (classInfo == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public class GenericXmlListTest {
"><rep>2</rep></any>";
private static final String MULTIPLE_ENUM_ELEMENT = "<?xml version=\"1.0\"?><any xmlns" +
"=\"http://www.w3.org/2005/Atom\"><rep>ENUM_1</rep><rep>ENUM_2</rep></any>";
private static final String COLLECTION_OF_ARRAY = "<?xml version=\"1.0\"?><any xmlns" +
private static final String COLLECTION_OF_ARRAY_STRING = "<?xml version=\"1.0\"?><any xmlns" +
"=\"http://www.w3.org/2005/Atom\"><rep><a>a</a><b>b</b></rep><rep><c>c</c><d>d</d></rep></any>";
private static final String COLLECTION_OF_ARRAY_INTEGER = "<?xml version=\"1.0\"?><any xmlns" +
"=\"http://www.w3.org/2005/Atom\"><rep><a>1</a><b>2</b></rep><rep><c>3</c><d>4</d></rep></any>";

/**
* The purpose of this test is to map an XML with an Array of {@link XmlTest.AnyType} objects.
Expand Down Expand Up @@ -348,7 +350,7 @@ public void testParseArrayTypeWithEnum() throws Exception {
public void testParseToArrayOfArrayMaps() throws Exception {
ArrayOfArrayMapsTypeGeneric xml = new ArrayOfArrayMapsTypeGeneric();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(COLLECTION_OF_ARRAY));
parser.setInput(new StringReader(COLLECTION_OF_ARRAY_STRING));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
Expand All @@ -366,17 +368,18 @@ public void testParseToArrayOfArrayMaps() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(COLLECTION_OF_ARRAY, out.toString());
assertEquals(COLLECTION_OF_ARRAY_STRING, out.toString());
}

/**
* The purpose is to have a Collection of {@link java.lang.reflect.ParameterizedType} elements.
* The purpose is to have an Collection of {@link java.lang.reflect.ParameterizedType} elements,
* whereas the Value Type is a {@link String}
*/
@Test
public void testParseToCollectionOfArrayMaps() throws Exception {
CollectionOfArrayMapsTypeGeneric xml = new CollectionOfArrayMapsTypeGeneric();
public void testParseToCollectionOfArrayMapStringValue() throws Exception {
CollectionOfArrayMapStringTypeGeneric xml = new CollectionOfArrayMapStringTypeGeneric();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(COLLECTION_OF_ARRAY));
parser.setInput(new StringReader(COLLECTION_OF_ARRAY_STRING));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
Expand All @@ -394,14 +397,48 @@ public void testParseToCollectionOfArrayMaps() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(COLLECTION_OF_ARRAY, out.toString());
assertEquals(COLLECTION_OF_ARRAY_STRING, out.toString());
}

private static class CollectionOfArrayMapsTypeGeneric extends GenericXml {
/**
* The purpose is to have an Collection of {@link java.lang.reflect.ParameterizedType} elements,
* whereas the Value Type is a {@link Integer}
*/
@Test
public void testParseToCollectionOfArrayMapIntegerValues() throws Exception {
CollectionOfArrayMapIntegerTypeGeneric xml = new CollectionOfArrayMapIntegerTypeGeneric();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(COLLECTION_OF_ARRAY_INTEGER));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals(2, xml.rep.size());
assertEquals(1, xml.rep.toArray(new ArrayMap[]{})[0].getValue(0));
assertEquals("a", xml.rep.toArray(new ArrayMap[]{})[0].getKey(0));
assertEquals(2, xml.rep.toArray(new ArrayMap[]{})[0].getValue(1));
assertEquals("b", xml.rep.toArray(new ArrayMap[]{})[0].getKey(1));
assertEquals(3, xml.rep.toArray(new ArrayMap[]{})[1].getValue(0));
assertEquals("c", xml.rep.toArray(new ArrayMap[]{})[1].getKey(0));
assertEquals(4, xml.rep.toArray(new ArrayMap[]{})[1].getValue(1));
assertEquals("d", xml.rep.toArray(new ArrayMap[]{})[1].getKey(1));
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(COLLECTION_OF_ARRAY_INTEGER, out.toString());
}

private static class CollectionOfArrayMapStringTypeGeneric extends GenericXml {
@Key
public Collection<ArrayMap<String, String>> rep;
}

private static class CollectionOfArrayMapIntegerTypeGeneric extends GenericXml {
@Key
public Collection<ArrayMap<String, Integer>> rep;
}

private static class ArrayOfArrayMapsTypeGeneric extends GenericXml {
@Key
public ArrayMap<String, String>[] rep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.google.api.client.util.ArrayMap;
import com.google.api.client.util.ClassInfo;

/**
Expand All @@ -24,6 +30,19 @@ public void testInstanceOf(){
Object destination = new Object();
assertTrue(destination instanceof Object);
assertFalse(destination instanceof Void);

Object map = new ArrayMap<String, String>();
Object list = new ArrayList<String>();
Collection<String> listCollection = new ArrayList<String>();
// need to better understand this.
// assertFalse(map instanceof Class<List<String>>);
assertFalse(map instanceof ParameterizedType);
assertFalse(list instanceof ParameterizedType);
assertFalse(listCollection instanceof ParameterizedType);


Object arr = new String[]{};
assertFalse(arr instanceof ParameterizedType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public class XmlListTest {
"=\"http://www.w3.org/2005/Atom\"><rep>1</rep><rep>2</rep></any>";
private static final String MULTIPLE_ENUM_ELEMENT = "<?xml version=\"1.0\"?><any xmlns" +
"=\"http://www.w3.org/2005/Atom\"><rep>ENUM_1</rep><rep>ENUM_2</rep></any>";
private static final String COLLECTION_OF_ARRAY = "<?xml version=\"1.0\"?><any xmlns" +
private static final String COLLECTION_OF_ARRAY_STRING = "<?xml version=\"1.0\"?><any xmlns" +
"=\"http://www.w3.org/2005/Atom\"><rep><a>a</a><b>b</b></rep><rep><c>c</c><d>d</d></rep></any>";
private static final String COLLECTION_OF_ARRAY_INTEGER = "<?xml version=\"1.0\"?><any xmlns" +
"=\"http://www.w3.org/2005/Atom\"><rep><a>1</a><b>2</b></rep><rep><c>3</c><d>4</d></rep></any>";

/**
* The purpose of this test is to map an XML with an Array of {@link XmlTest.AnyType} objects.
Expand Down Expand Up @@ -295,7 +297,7 @@ public void testParseArrayTypeWithEnum() throws Exception {
public void testParseToArrayOfArrayMaps() throws Exception {
ArrayOfArrayMapsType xml = new ArrayOfArrayMapsType();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(COLLECTION_OF_ARRAY));
parser.setInput(new StringReader(COLLECTION_OF_ARRAY_STRING));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
Expand All @@ -313,17 +315,18 @@ public void testParseToArrayOfArrayMaps() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(COLLECTION_OF_ARRAY, out.toString());
assertEquals(COLLECTION_OF_ARRAY_STRING, out.toString());
}

/**
* The purpose is to have an Collection of {@link java.lang.reflect.ParameterizedType} elements.
* The purpose is to have an Collection of {@link java.lang.reflect.ParameterizedType} elements,
* whereas the Value Type is a {@link String}
*/
@Test
public void testParseToCollectionOfArrayMaps() throws Exception {
CollectionOfArrayMapsType xml = new CollectionOfArrayMapsType();
public void testParseToCollectionOfArrayMapStringValue() throws Exception {
CollectionOfArrayMapStringType xml = new CollectionOfArrayMapStringType();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(COLLECTION_OF_ARRAY));
parser.setInput(new StringReader(COLLECTION_OF_ARRAY_STRING));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
Expand All @@ -341,14 +344,48 @@ public void testParseToCollectionOfArrayMaps() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(COLLECTION_OF_ARRAY, out.toString());
assertEquals(COLLECTION_OF_ARRAY_STRING, out.toString());
}

private static class CollectionOfArrayMapsType {
/**
* The purpose is to have an Collection of {@link java.lang.reflect.ParameterizedType} elements,
* whereas the Value Type is a {@link Integer}
*/
@Test
public void testParseToCollectionOfArrayMapIntegerValues() throws Exception {
CollectionOfArrayMapIntegerType xml = new CollectionOfArrayMapIntegerType();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(COLLECTION_OF_ARRAY_INTEGER));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals(2, xml.rep.size());
assertEquals(1, xml.rep.toArray(new ArrayMap[]{})[0].getValue(0));
assertEquals("a", xml.rep.toArray(new ArrayMap[]{})[0].getKey(0));
assertEquals(2, xml.rep.toArray(new ArrayMap[]{})[0].getValue(1));
assertEquals("b", xml.rep.toArray(new ArrayMap[]{})[0].getKey(1));
assertEquals(3, xml.rep.toArray(new ArrayMap[]{})[1].getValue(0));
assertEquals("c", xml.rep.toArray(new ArrayMap[]{})[1].getKey(0));
assertEquals(4, xml.rep.toArray(new ArrayMap[]{})[1].getValue(1));
assertEquals("d", xml.rep.toArray(new ArrayMap[]{})[1].getKey(1));
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(COLLECTION_OF_ARRAY_INTEGER, out.toString());
}

private static class CollectionOfArrayMapStringType {
@Key
public Collection<ArrayMap<String, String>> rep;
}

private static class CollectionOfArrayMapIntegerType {
@Key
public Collection<ArrayMap<String, Integer>> rep;
}

private static class ArrayOfArrayMapsType {
@Key
public ArrayMap<String, String>[] rep;
Expand Down

0 comments on commit edfafab

Please sign in to comment.