Skip to content

Commit

Permalink
Add Test for various corner cases (googleapis#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
geri-m committed Oct 12, 2018
1 parent 5619a49 commit 28ffefe
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private static boolean parseElementInternal(XmlPullParser parser,
GenericXml genericXml = destination instanceof GenericXml ? (GenericXml) destination : null;
@SuppressWarnings("unchecked")

// if the desitnation is GenericXML and the destination is a Map, create a desination Map.
// if the destination is GenericXML and the destination is a Map, create a desination Map.
Map<String, Object> destinationMap =
genericXml == null && destination instanceof Map<?, ?> ? Map.class.cast(destination) : null;

Expand Down Expand Up @@ -314,6 +314,7 @@ private static boolean parseElementInternal(XmlPullParser parser,
isStopped = true;
break main;
}
// not sure how the case looks like, when this happens.
if (destination == null) {
// we ignore the result, as we can't map it to anything. we parse for sanity?
parseTextContentForElement(parser, context, true, null);
Expand Down Expand Up @@ -458,7 +459,7 @@ private static boolean parseElementInternal(XmlPullParser parser,
subValueType = Data.resolveWildcardTypeOrTypeVariable(context, subValueType);
isStopped = parseElementInternal(parser,
context,
elementValue, // destination, only null if parseTextContentForElement returns null
elementValue, // destination, never null!
subValueType,
namespaceDictionary,
customizeParser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,19 @@ public static class ValueType {
"<?xml version=\"1.0\"?><any anyEnum=\"ENUM_1\" attr=\"value\" xmlns=\"http://www.w3.org/2005/Atom\">"
+ "<anotherEnum>ENUM_2</anotherEnum><elem>content</elem><rep>rep1</rep><rep>rep2</rep><value>ENUM_1</value></any>";

private static final String XML_ENUM_ELEMENT_ONLY = "<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\"><elementEnum>ENUM_2</elementEnum></any>";
private static final String XML_ENUM_ELEMENT_ONLY =
"<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\"><elementEnum>ENUM_2</elementEnum></any>";

private static final String XML_ENUM_ATTRIBUTE_ONLY = "<?xml version=\"1.0\"?><any attributeEnum=\"ENUM_1\" xmlns=\"http://www.w3.org/2005/Atom\" />";
private static final String XML_ENUM_ATTRIBUTE_ONLY =
"<?xml version=\"1.0\"?><any attributeEnum=\"ENUM_1\" xmlns=\"http://www.w3.org/2005/Atom\" />";

private static final String XML_ENUM_INCORRECT = "<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\"><elementEnum>ENUM_3</elementEnum></any>";
private static final String XML_ENUM_INCORRECT =
"<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\"><elementEnum>ENUM_3</elementEnum></any>";


private static final String XML_ENUM_ELEMENT_ONLY_NESTED =
"<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\"><elementEnum>ENUM_2<nested>something</nested></elementEnum></any>";

@SuppressWarnings("cast")
@Test
public void testParse_anyType() throws Exception {
Expand Down Expand Up @@ -89,9 +95,18 @@ public void testParse_anyType() throws Exception {

@Test
public void testParse_enumElementType() throws Exception {
assertEquals(XML_ENUM_ELEMENT_ONLY, testStandardXml(XML_ENUM_ELEMENT_ONLY));
}

@Test
public void testParse_enumElementTypeWithNestedElement() throws Exception {
assertEquals(XML_ENUM_ELEMENT_ONLY, testStandardXml(XML_ENUM_ELEMENT_ONLY_NESTED));
}

private String testStandardXml(final String xmlString) throws Exception {
XmlEnumTest.AnyTypeEnumElementOnly xml = new XmlEnumTest.AnyTypeEnumElementOnly();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(XML_ENUM_ELEMENT_ONLY));
parser.setInput(new StringReader(xmlString));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertTrue(xml.elementEnum instanceof XmlEnumTest.AnyEnum);
Expand All @@ -101,7 +116,8 @@ public void testParse_enumElementType() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(XML_ENUM_ELEMENT_ONLY, out.toString());
return out.toString();

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.google.api.client.util.ArrayMap;
import com.google.api.client.util.Key;
Expand Down Expand Up @@ -76,24 +75,44 @@ public void testParse_anyType() throws Exception {
assertEquals(ANY_TYPE_XML, out.toString());
}

public static class ArrayType extends GenericXml {
@Key
public Map<String, String>[] rep;
// this test only ensures, that there is no exception during paring with a NULL destination
@Test
public void testParse_anyTypeWithNullDestination() throws Exception {
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(ANY_TYPE_XML));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, null, namespaceDictionary, null);
}

public static class ArrayTypeWithPrimitive extends GenericXml {
@Test
public void testParse_anyTypWithCustomParser() throws Exception {
AnyType xml = new AnyType();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(ANY_TYPE_XML));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, new Xml.CustomizeParser());
assertTrue(xml.attr instanceof String);
assertTrue(xml.elem.toString(), xml.elem instanceof ArrayList<?>);
assertTrue(xml.rep.toString(), xml.rep instanceof ArrayList<?>);
assertTrue(xml.value instanceof ValueType);
assertTrue(xml.value.content instanceof String);
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(ANY_TYPE_XML, out.toString());
}

public static class ArrayType extends GenericXml {
@Key
public int[] rep;
public Map<String, String>[] rep;
}

private static final String ARRAY_TYPE =
"<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\">"
+ "<rep>rep1</rep><rep>rep2</rep></any>";

private static final String ARRAY_TYPE_WITH_PRIMITIVE =
"<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\">"
+ "<rep>1</rep><rep>2</rep></any>";

@Test
public void testParse_arrayType() throws Exception {
ArrayType xml = new ArrayType();
Expand All @@ -118,11 +137,33 @@ public void testParse_arrayType() throws Exception {
assertEquals(ARRAY_TYPE, out.toString());
}

public static class ArrayTypeWithPrimitive extends GenericXml {
@Key
public int[] rep;
}

private static final String ARRAY_TYPE_WITH_PRIMITIVE =
"<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\">"
+ "<rep>1</rep><rep>2</rep></any>";

@Test
public void testParse_arrayTypeWithPrimitive() throws Exception {
assertEquals(ARRAY_TYPE_WITH_PRIMITIVE, testStandardXml(ARRAY_TYPE_WITH_PRIMITIVE));
}

private static final String ARRAY_TYPE_WITH_PRIMITIVE_ADDED_NESTED =
"<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\">"
+ "<rep>1<nested>something</nested></rep><rep>2</rep></any>";

@Test
public void testParse_arrayTypeWithPrimitiveWithNestedElement() throws Exception {
assertEquals(ARRAY_TYPE_WITH_PRIMITIVE, testStandardXml(ARRAY_TYPE_WITH_PRIMITIVE_ADDED_NESTED));
}

private String testStandardXml(final String xmlString) throws Exception {
ArrayTypeWithPrimitive xml = new ArrayTypeWithPrimitive();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(ARRAY_TYPE_WITH_PRIMITIVE));
parser.setInput(new StringReader(xmlString));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
Expand All @@ -136,7 +177,7 @@ public void testParse_arrayTypeWithPrimitive() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(ARRAY_TYPE_WITH_PRIMITIVE, out.toString());
return out.toString();
}

private static final String NESTED_NS =
Expand Down

0 comments on commit 28ffefe

Please sign in to comment.