Skip to content

Commit

Permalink
Fixes bug in googleapis#475, add relevant Tests (googleapis#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
geri-m committed Oct 28, 2018
1 parent 24a0f5e commit 5e75e4f
Show file tree
Hide file tree
Showing 3 changed files with 410 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.google.api.client.xml;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import com.google.api.client.util.Key;
import com.google.api.client.util.Value;
import junit.framework.TestCase;

public class XmlEnumTest extends TestCase {
/**
* Tests {@link Xml}.
*
* @author Gerald Madlmayr
*/
public class XmlEnumTest {

public enum AnyEnum {
@Value ENUM_1,
Expand Down Expand Up @@ -50,14 +58,21 @@ 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 {
AnyType xml = new AnyType();
XmlPullParser parser = Xml.createParser();
Expand All @@ -82,10 +97,34 @@ public void testParse_anyType() throws Exception {
assertEquals(XML, out.toString());
}

public void testParse_enumElementType() throws Exception {
XmlEnumTest.AnyTypeEnumElementOnly xml = new XmlEnumTest.AnyTypeEnumElementOnly();
/**
* The purpose of this test is to parse an XML element to an objects's member variable
*/
@Test
public void testParseToEnumElementType() throws Exception {
assertEquals(XML_ENUM_ELEMENT_ONLY, testStandardXml(XML_ENUM_ELEMENT_ONLY));
}


/**
* The purpose of this test is to parse an XML element to an objects's member variable, whereas
* there are additional nested elements in the tag.
*/
@Test
public void testParseToEnumElementTypeWithNestedElement() throws Exception {
assertEquals(XML_ENUM_ELEMENT_ONLY, testStandardXml(XML_ENUM_ELEMENT_ONLY_NESTED));
}

/**
* Private Method to handle standard parsing and mapping to {@link AnyTypeEnumElementOnly}
* @param xmlString XML String that needs to be mapped to {@link AnyTypeEnumElementOnly}
* @return Returns the serialized string of the XML Objects
* @throws Exception
*/
private String testStandardXml(final String xmlString) throws Exception {
AnyTypeEnumElementOnly xml = new 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 @@ -95,9 +134,14 @@ 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();

}

/**
* The purpose of this test is to parse an XML attribute to an objects's member variable
*/
@Test
public void testParse_enumAttributeType() throws Exception {
XmlEnumTest.AnyTypeEnumAttributeOnly xml = new XmlEnumTest.AnyTypeEnumAttributeOnly();
XmlPullParser parser = Xml.createParser();
Expand All @@ -114,6 +158,11 @@ public void testParse_enumAttributeType() throws Exception {
assertEquals(XML_ENUM_ATTRIBUTE_ONLY, out.toString());
}

/**
* The purpose of this test is to parse an XML element to an objects's member variable, whereas
* the enumeration element does not exist.
*/
@Test
public void testParse_enumElementTypeIncorrect() throws Exception {
XmlEnumTest.AnyTypeEnumElementOnly xml = new XmlEnumTest.AnyTypeEnumElementOnly();
XmlPullParser parser = Xml.createParser();
Expand All @@ -126,7 +175,5 @@ public void testParse_enumElementTypeIncorrect() throws Exception {
} catch (final IllegalArgumentException e){
assertEquals("given enum name ENUM_3 not part of enumeration", e.getMessage());
}

}

}
Loading

0 comments on commit 5e75e4f

Please sign in to comment.