Skip to content

Commit

Permalink
Add Generic Simple Tests (googleapis#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
geri-m committed Oct 20, 2018
1 parent a73c9f1 commit 2f1efe9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,45 @@ public void testParse_arrayTypeWithClassTypeAsGenericXml() throws Exception {
assertEquals(ARRAY_TYPE_WITH_CLASS_TYPE, out.toString());
}

private static final String SIMPLE_XML_NUMERIC = "<any xmlns=\"\">1</any>";

@Test
public void testParseSimpleInteger() throws Exception {
GenericXml xml = new GenericXml();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(SIMPLE_XML_NUMERIC));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals ("text()", ((Map.Entry<String, String>)xml.entrySet().toArray()[0]).getKey());
assertEquals ("1", ((Map.Entry<String, String>)xml.entrySet().toArray()[0]).getValue());
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals("<?xml version=\"1.0\"?><any xmlns=\"\">1</any>", out.toString());
}


private static final String SIMPLE_XML = "<any xmlns=\"\">test</any>";

@Test
public void testParseSimpleString() throws Exception {
GenericXml xml = new GenericXml();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(SIMPLE_XML));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertEquals ("text()", ((Map.Entry<String, String>)xml.entrySet().toArray()[0]).getKey());
assertEquals ("test", ((Map.Entry<String, String>)xml.entrySet().toArray()[0]).getValue());
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals("<?xml version=\"1.0\"?><any xmlns=\"\">test</any>", out.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,13 @@ public static class SimpleTypeNumeric {
public int value;
}

private static final String SIMPLE_XML_NUMBERIC = "<any>1</any>";

private static final String SIMPLE_XML_NUMERIC = "<any>1</any>";

@Test
public void testParseSimpleTypeAsValueInteger() throws Exception {
SimpleTypeNumeric xml = new SimpleTypeNumeric();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(SIMPLE_XML_NUMBERIC));
parser.setInput(new StringReader(SIMPLE_XML_NUMERIC));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary().set("","");
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
Expand Down

0 comments on commit 2f1efe9

Please sign in to comment.