|
| 1 | +package com.google.api.client.xml; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.StringReader; |
| 5 | +import java.util.ArrayList; |
| 6 | +import org.xmlpull.v1.XmlPullParser; |
| 7 | +import org.xmlpull.v1.XmlSerializer; |
| 8 | +import com.google.api.client.util.Key; |
| 9 | +import com.google.api.client.util.Value; |
| 10 | +import junit.framework.TestCase; |
| 11 | + |
| 12 | +public class XmlEnumTest extends TestCase { |
| 13 | + |
| 14 | + public enum AnyEnum { |
| 15 | + @Value ENUM_1, |
| 16 | + @Value ENUM_2 |
| 17 | + } |
| 18 | + |
| 19 | + public static class AnyType { |
| 20 | + @Key("@attr") |
| 21 | + public Object attr; |
| 22 | + @Key |
| 23 | + public Object elem; |
| 24 | + @Key |
| 25 | + public Object rep; |
| 26 | + @Key("@anyEnum") |
| 27 | + public XmlEnumTest.AnyEnum anyEnum; |
| 28 | + @Key |
| 29 | + public XmlEnumTest.AnyEnum anotherEnum; |
| 30 | + @Key |
| 31 | + public ValueType value; |
| 32 | + } |
| 33 | + |
| 34 | + public static class AnyTypeEnumElementOnly { |
| 35 | + @Key |
| 36 | + public XmlEnumTest.AnyEnum elementEnum; |
| 37 | + } |
| 38 | + |
| 39 | + public static class AnyTypeEnumAttributeOnly { |
| 40 | + @Key("@attributeEnum") |
| 41 | + public XmlEnumTest.AnyEnum attributeEnum; |
| 42 | + } |
| 43 | + |
| 44 | + public static class ValueType { |
| 45 | + @Key("text()") |
| 46 | + public XmlEnumTest.AnyEnum content; |
| 47 | + } |
| 48 | + |
| 49 | + private static final String XML = |
| 50 | + "<?xml version=\"1.0\"?><any anyEnum=\"ENUM_1\" attr=\"value\" xmlns=\"http://www.w3.org/2005/Atom\">" |
| 51 | + + "<anotherEnum>ENUM_2</anotherEnum><elem>content</elem><rep>rep1</rep><rep>rep2</rep><value>ENUM_1</value></any>"; |
| 52 | + |
| 53 | + 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>"; |
| 54 | + |
| 55 | + private static final String XML_ENUM_ATTRIBUTE_ONLY = "<?xml version=\"1.0\"?><any attributeEnum=\"ENUM_1\" xmlns=\"http://www.w3.org/2005/Atom\" />"; |
| 56 | + |
| 57 | + private static final String XML_ENUM_INCORRECT = "<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\"><elementEnum>ENUM_3</elementEnum></any>"; |
| 58 | + |
| 59 | + |
| 60 | + @SuppressWarnings("cast") |
| 61 | + public void testParse_anyType() throws Exception { |
| 62 | + AnyType xml = new AnyType(); |
| 63 | + XmlPullParser parser = Xml.createParser(); |
| 64 | + parser.setInput(new StringReader(XML)); |
| 65 | + XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary(); |
| 66 | + Xml.parseElement(parser, xml, namespaceDictionary, null); |
| 67 | + assertTrue(xml.attr instanceof String); |
| 68 | + assertTrue(xml.elem.toString(), xml.elem instanceof ArrayList<?>); |
| 69 | + assertTrue(xml.rep.toString(), xml.rep instanceof ArrayList<?>); |
| 70 | + assertTrue(xml.value instanceof ValueType); |
| 71 | + assertTrue(xml.value.content instanceof XmlEnumTest.AnyEnum); |
| 72 | + assertTrue(xml.anyEnum instanceof XmlEnumTest.AnyEnum); |
| 73 | + assertTrue(xml.anotherEnum instanceof XmlEnumTest.AnyEnum); |
| 74 | + assertTrue(xml.anyEnum.equals(AnyEnum.ENUM_1)); |
| 75 | + assertTrue(xml.anotherEnum.equals(AnyEnum.ENUM_2)); |
| 76 | + assertTrue(xml.value.content.equals(AnyEnum.ENUM_1)); |
| 77 | + // serialize |
| 78 | + XmlSerializer serializer = Xml.createSerializer(); |
| 79 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 80 | + serializer.setOutput(out, "UTF-8"); |
| 81 | + namespaceDictionary.serialize(serializer, "any", xml); |
| 82 | + assertEquals(XML, out.toString()); |
| 83 | + } |
| 84 | + |
| 85 | + public void testParse_enumElementType() throws Exception { |
| 86 | + XmlEnumTest.AnyTypeEnumElementOnly xml = new XmlEnumTest.AnyTypeEnumElementOnly(); |
| 87 | + XmlPullParser parser = Xml.createParser(); |
| 88 | + parser.setInput(new StringReader(XML_ENUM_ELEMENT_ONLY)); |
| 89 | + XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary(); |
| 90 | + Xml.parseElement(parser, xml, namespaceDictionary, null); |
| 91 | + assertTrue(xml.elementEnum instanceof XmlEnumTest.AnyEnum); |
| 92 | + assertTrue(xml.elementEnum.equals(AnyEnum.ENUM_2)); |
| 93 | + // serialize |
| 94 | + XmlSerializer serializer = Xml.createSerializer(); |
| 95 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 96 | + serializer.setOutput(out, "UTF-8"); |
| 97 | + namespaceDictionary.serialize(serializer, "any", xml); |
| 98 | + assertEquals(XML_ENUM_ELEMENT_ONLY, out.toString()); |
| 99 | + } |
| 100 | + |
| 101 | + public void testParse_enumAttributeType() throws Exception { |
| 102 | + XmlEnumTest.AnyTypeEnumAttributeOnly xml = new XmlEnumTest.AnyTypeEnumAttributeOnly(); |
| 103 | + XmlPullParser parser = Xml.createParser(); |
| 104 | + parser.setInput(new StringReader(XML_ENUM_ATTRIBUTE_ONLY)); |
| 105 | + XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary(); |
| 106 | + Xml.parseElement(parser, xml, namespaceDictionary, null); |
| 107 | + assertTrue(xml.attributeEnum instanceof XmlEnumTest.AnyEnum); |
| 108 | + assertTrue(xml.attributeEnum.equals(AnyEnum.ENUM_1)); |
| 109 | + // serialize |
| 110 | + XmlSerializer serializer = Xml.createSerializer(); |
| 111 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 112 | + serializer.setOutput(out, "UTF-8"); |
| 113 | + namespaceDictionary.serialize(serializer, "any", xml); |
| 114 | + assertEquals(XML_ENUM_ATTRIBUTE_ONLY, out.toString()); |
| 115 | + } |
| 116 | + |
| 117 | + public void testParse_enumElementTypeIncorrect() throws Exception { |
| 118 | + XmlEnumTest.AnyTypeEnumElementOnly xml = new XmlEnumTest.AnyTypeEnumElementOnly(); |
| 119 | + XmlPullParser parser = Xml.createParser(); |
| 120 | + parser.setInput(new StringReader(XML_ENUM_INCORRECT)); |
| 121 | + XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary(); |
| 122 | + try{ |
| 123 | + Xml.parseElement(parser, xml, namespaceDictionary, null); |
| 124 | + // fail test, if there is no exception |
| 125 | + fail(); |
| 126 | + } catch (final IllegalArgumentException e){ |
| 127 | + assertEquals("given enum name ENUM_3 not part of enumeration", e.getMessage()); |
| 128 | + } |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments