Skip to content

Commit

Permalink
Add test, add test docu, restructure test (googleapis#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
geri-m committed Oct 26, 2018
1 parent ed6fc22 commit 31aab8e
Show file tree
Hide file tree
Showing 3 changed files with 666 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,83 @@ public void testParseArrayOfGenerics() throws Exception {
assertEquals(COLLECTION_TYPE_WITH_ENUM, out.toString());
}



public static class ArrayType extends GenericXml {
@Key
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>";

@Test
public void testParse_arrayType() throws Exception {
ArrayType xml = new ArrayType();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(ARRAY_TYPE));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
Map<String, String>[] rep = xml.rep;
assertEquals(2, rep.length);
ArrayMap<String, String> map0 = (ArrayMap<String, String>) rep[0];
assertEquals(1, map0.size());
assertEquals("rep1", map0.get("text()"));
ArrayMap<String, String> map1 = (ArrayMap<String, String>) rep[1];
assertEquals(1, map1.size());
assertEquals("rep2", map1.get("text()"));
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
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(xmlString));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
int[] rep = xml.rep;
assertNotNull(rep);
assertEquals(2, rep.length);
assertEquals(1, rep[0]);
assertEquals(2, rep[1]);
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
return out.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
package com.google.api.client.xml;

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 java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import com.google.api.client.util.ArrayMap;
import com.google.api.client.util.Key;

public class XmlListTest {


public static class ArrayWithClassType {
@Key
public XmlTest.AnyType[] rep;
}

public static class CollectionWithClassType {
@Key
public Collection<XmlTest.AnyType> rep;
}

public static class ListnWithClassType {
@Key
public List<XmlTest.AnyType> rep;
}

private static final String ARRAY_TYPE_WITH_CLASS_TYPE =
"<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\">" +
"<rep><elem>content1</elem><rep>rep10</rep><rep>rep11</rep><value>value1</value></rep>" +
"<rep><elem>content2</elem><rep>rep20</rep><rep>rep21</rep><value>value2</value></rep>" +
"<rep><elem>content3</elem><rep>rep30</rep><rep>rep31</rep><value>value3</value></rep>" +
"</any>";

/**
* The purpose of this test is to map an XML with an array of Objects correctly. The parse will
* use a {@link DedicatedObjectParser} to map the {@link ArrayWithClassType} at first, then
* another {@link DedicatedObjectParser} for the {@link XmlTest.AnyType} Objects in the Array, but the
* will switch to the {@link MapParser} as the members of {@link XmlTest.AnyType} are of type {@link
* Object}, with the exception of the {@link XmlTest.ValueType}.
*/
@Test
public void testParseArrayTypeWithClassType() throws Exception {
ArrayWithClassType xml = new ArrayWithClassType();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(ARRAY_TYPE_WITH_CLASS_TYPE));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertTrue(xml.rep instanceof XmlTest.AnyType[]);
XmlTest.AnyType[] rep = xml.rep;
assertNotNull(rep);
assertEquals(3, rep.length);
ArrayList<ArrayMap<String, String>> elem0 = (ArrayList<ArrayMap<String, String>>) rep[0].elem;
assertEquals(1, elem0.size());
assertEquals("content1", elem0.get(0).get("text()"));
ArrayList<ArrayMap<String, String>> elem1 = (ArrayList<ArrayMap<String, String>>) rep[1].elem;
assertEquals(1, elem1.size());
assertEquals("content2", elem1.get(0).get("text()"));
ArrayList<ArrayMap<String, String>> elem2 = (ArrayList<ArrayMap<String, String>>) rep[2].elem;
assertEquals(1, elem2.size());
assertEquals("content3", elem2.get(0).get("text()"));

// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(ARRAY_TYPE_WITH_CLASS_TYPE, out.toString());
}

@Test
public void testParseCollectionWithClassType() throws Exception {
CollectionWithClassType xml = new CollectionWithClassType();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(ARRAY_TYPE_WITH_CLASS_TYPE));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertTrue(xml.rep instanceof Collection);
Collection<XmlTest.AnyType> rep = xml.rep;
assertNotNull(rep);
assertEquals(3, rep.size());


// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(ARRAY_TYPE_WITH_CLASS_TYPE, out.toString());
}

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

public static class CollectionTypeString {
@Key
public Collection<String> rep;
}

public static class ArrayTypeString {
@Key
public String[] rep;
}

public static class ListTypeString {
@Key
public List<String> rep;
}

/**
* The Purpose of this test is to map a given list of elements (Strings) to a {@link Collection}
* of Strings. This test uses the {@link DedicatedObjectParser} only, as all field/types are
* specified.
*/
@Test
public void testParseCollectionTypeString() throws Exception {
CollectionTypeString xml = new CollectionTypeString();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(MULTIPLE_STRING_ELEMENT));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals(2, xml.rep.size());
assertEquals("rep1", xml.rep.toArray(new String[]{})[0]);
assertEquals("rep2", xml.rep.toArray(new String[]{})[1]);
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(MULTIPLE_STRING_ELEMENT, out.toString());
}

/**
* The Purpose of this test is to map a given list of elements (Strings) to a String-Array.
* This test uses the {@link DedicatedObjectParser} only, as all field/types are specified.
*/
@Test
public void testParseArrayTypeString() throws Exception {
ArrayTypeString xml = new ArrayTypeString();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(MULTIPLE_STRING_ELEMENT));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals(2, xml.rep.length);
assertEquals("rep1", xml.rep[0]);
assertEquals("rep2", xml.rep[1]);
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(MULTIPLE_STRING_ELEMENT, out.toString());
}

/**
* The Purpose of this test is to map a given list of elements (Strings) to a {@link List}
* of Strings. This test uses the {@link DedicatedObjectParser} only, as all field/types are
* specified.
*/
@Test
public void testParseListTypeString() throws Exception {
ListTypeString xml = new ListTypeString();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(MULTIPLE_STRING_ELEMENT));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals(2, xml.rep.size());
assertEquals("rep1", xml.rep.get(0));
assertEquals("rep2", xml.rep.get(1));
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(MULTIPLE_STRING_ELEMENT, out.toString());
}


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

public static class CollectionTypeInteger {
@Key
public Collection<Integer> rep;
}

public static class ArrayTypeInteger {
@Key
public Integer[] rep;
}

public static class ArrayTypeInt {
@Key
public int[] rep;
}

public static class ListTypeInteger {
@Key
public List<Integer> rep;
}

/**
* The Purpose of this test is to map a given list of elements (Strings) to a {@link Collection}
* of Strings. This test uses the {@link DedicatedObjectParser} only, as all field/types are
* specified.
*/
@Test
public void testParseCollectionTypeInteger() throws Exception {
CollectionTypeInteger xml = new CollectionTypeInteger();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(MULTIPLE_INTEGER_ELEMENT));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals(2, xml.rep.size());
assertEquals(1, xml.rep.toArray(new Integer[]{})[0].intValue());
assertEquals(2, xml.rep.toArray(new Integer[]{})[1].intValue());
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(MULTIPLE_INTEGER_ELEMENT, out.toString());
}

/**
* The Purpose of this test is to map a given list of elements (Strings) to a String-Array.
* This test uses the {@link DedicatedObjectParser} only, as all field/types are specified.
*/
@Test
public void testParseArrayTypeInteger() throws Exception {
ArrayTypeInteger xml = new ArrayTypeInteger();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(MULTIPLE_INTEGER_ELEMENT));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals(2, xml.rep.length);
assertEquals(1, xml.rep[0].intValue());
assertEquals(2, xml.rep[1].intValue());
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(MULTIPLE_INTEGER_ELEMENT, out.toString());
}

/**
* The Purpose of this test is to map a given list of elements (Strings) to a {@link List}
* of Strings. This test uses the {@link DedicatedObjectParser} only, as all field/types are
* specified.
*/
@Test
public void testParseListTypeInteger() throws Exception {
ListTypeInteger xml = new ListTypeInteger();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(MULTIPLE_INTEGER_ELEMENT));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals(2, xml.rep.size());
assertEquals(1, xml.rep.get(0).intValue());
assertEquals(2, xml.rep.get(1).intValue());
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(MULTIPLE_INTEGER_ELEMENT, out.toString());
}


/**
* The Purpose of this test is to map a given list of elements (int) to a {@link List}
* of Strings. This test uses the {@link DedicatedObjectParser} only, as all field/types are
* specified.
*/
@Test
public void testParseArrayTypeInt() throws Exception {
ArrayTypeInt xml = new ArrayTypeInt();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(MULTIPLE_INTEGER_ELEMENT));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals(2, xml.rep.length);
assertEquals(1, xml.rep[0]);
assertEquals(2, xml.rep[1]);
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(MULTIPLE_INTEGER_ELEMENT, out.toString());
}


}
Loading

0 comments on commit 31aab8e

Please sign in to comment.