From edfafab04bdff7b172b5a1a6bcaf731d53d5da8a Mon Sep 17 00:00:00 2001 From: Gerald Madlmayr Date: Sun, 4 Nov 2018 19:55:10 +0100 Subject: [PATCH] Add Additional Test Cases (#490) --- .../java/com/google/api/client/xml/Xml.java | 37 ++++++++++--- .../api/client/xml/GenericXmlListTest.java | 55 ++++++++++++++++--- .../com/google/api/client/xml/SanityTest.java | 19 +++++++ .../google/api/client/xml/XmlListTest.java | 55 ++++++++++++++++--- 4 files changed, 140 insertions(+), 26 deletions(-) diff --git a/google-http-client-xml/src/main/java/com/google/api/client/xml/Xml.java b/google-http-client-xml/src/main/java/com/google/api/client/xml/Xml.java index 259d63463..970cc5feb 100644 --- a/google-http-client-xml/src/main/java/com/google/api/client/xml/Xml.java +++ b/google-http-client-xml/src/main/java/com/google/api/client/xml/Xml.java @@ -229,7 +229,7 @@ public static void parseElement(XmlPullParser parser, Object destination, private static boolean parseElementInternal(XmlPullParser parser, ArrayList 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 @@ -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 @@ -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, @@ -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; @@ -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, @@ -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) { diff --git a/google-http-client-xml/src/test/java/com/google/api/client/xml/GenericXmlListTest.java b/google-http-client-xml/src/test/java/com/google/api/client/xml/GenericXmlListTest.java index e6d607c27..21d0ec07b 100644 --- a/google-http-client-xml/src/test/java/com/google/api/client/xml/GenericXmlListTest.java +++ b/google-http-client-xml/src/test/java/com/google/api/client/xml/GenericXmlListTest.java @@ -54,8 +54,10 @@ public class GenericXmlListTest { ">2"; private static final String MULTIPLE_ENUM_ELEMENT = "ENUM_1ENUM_2"; - private static final String COLLECTION_OF_ARRAY = "abcd"; + private static final String COLLECTION_OF_ARRAY_INTEGER = "1234"; /** * The purpose of this test is to map an XML with an Array of {@link XmlTest.AnyType} objects. @@ -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 @@ -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 @@ -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> rep; } + private static class CollectionOfArrayMapIntegerTypeGeneric extends GenericXml { + @Key + public Collection> rep; + } + private static class ArrayOfArrayMapsTypeGeneric extends GenericXml { @Key public ArrayMap[] rep; diff --git a/google-http-client-xml/src/test/java/com/google/api/client/xml/SanityTest.java b/google-http-client-xml/src/test/java/com/google/api/client/xml/SanityTest.java index cacc6545a..38ebefcc9 100644 --- a/google-http-client-xml/src/test/java/com/google/api/client/xml/SanityTest.java +++ b/google-http-client-xml/src/test/java/com/google/api/client/xml/SanityTest.java @@ -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; /** @@ -24,6 +30,19 @@ public void testInstanceOf(){ Object destination = new Object(); assertTrue(destination instanceof Object); assertFalse(destination instanceof Void); + + Object map = new ArrayMap(); + Object list = new ArrayList(); + Collection listCollection = new ArrayList(); + // need to better understand this. + // assertFalse(map instanceof Class>); + assertFalse(map instanceof ParameterizedType); + assertFalse(list instanceof ParameterizedType); + assertFalse(listCollection instanceof ParameterizedType); + + + Object arr = new String[]{}; + assertFalse(arr instanceof ParameterizedType); } } diff --git a/google-http-client-xml/src/test/java/com/google/api/client/xml/XmlListTest.java b/google-http-client-xml/src/test/java/com/google/api/client/xml/XmlListTest.java index fc45cbb6b..a00507b91 100644 --- a/google-http-client-xml/src/test/java/com/google/api/client/xml/XmlListTest.java +++ b/google-http-client-xml/src/test/java/com/google/api/client/xml/XmlListTest.java @@ -49,8 +49,10 @@ public class XmlListTest { "=\"http://www.w3.org/2005/Atom\">12"; private static final String MULTIPLE_ENUM_ELEMENT = "ENUM_1ENUM_2"; - private static final String COLLECTION_OF_ARRAY = "abcd"; + private static final String COLLECTION_OF_ARRAY_INTEGER = "1234"; /** * The purpose of this test is to map an XML with an Array of {@link XmlTest.AnyType} objects. @@ -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 @@ -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 @@ -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> rep; } + private static class CollectionOfArrayMapIntegerType { + @Key + public Collection> rep; + } + private static class ArrayOfArrayMapsType { @Key public ArrayMap[] rep;