@Test
public final void encoding() throws Exception
{
final XmlPullParser parser = new XmlPullParser();
parser.parse(
new StringResourceStream("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>").getInputStream(),
null);
assertEquals("iso-8859-1", parser.getEncoding());
XmlTag tag = parser.nextTag();
assertNull(tag);
parser.parse(new StringResourceStream(
"<?xml version=\"1.0\" encoding='iso-8859-1' ?> test test").getInputStream(), null);
assertEquals("iso-8859-1", parser.getEncoding());
tag = parser.nextTag();
assertNull(tag);
// re-order and move close (remove whitespaces
parser.parse(new StringResourceStream(
" <?xml encoding='iso-8859-1'version=\"1.0\"?> test test").getInputStream(), null);
assertEquals("iso-8859-1", parser.getEncoding());
tag = parser.nextTag();
assertNull(tag);
// attribute value must be enclosed by ""
parser.parse(
new StringResourceStream("<?xml encoding=iso-8859-1 ?> test test").getInputStream(),
null);
assertEquals("iso-8859-1", parser.getEncoding());
// Invaluid encoding
Exception ex = null;
try
{
parser.parse(new StringResourceStream("<?xml encoding='XXX' ?>").getInputStream(), null);
}
catch (UnsupportedEncodingException e)
{
ex = e;
}
assertNotNull(ex);
// no extra characters allowed before <?xml>
// TODO General: I'd certainly prefer an exception
parser.parse(
new StringResourceStream("xxxx <?xml encoding='iso-8859-1' ?>").getInputStream(), null);
assertNull(parser.getEncoding());
tag = parser.nextTag();
assertNull(tag);
// no extra characters allowed before <?xml>
// Are comments allowed preceding the encoding string?
parser.parse(
new StringResourceStream("<!-- Comment --> <?xml encoding='iso-8859-1' ?>").getInputStream(),
null);
assertNull(parser.getEncoding());
tag = parser.nextTag();
assertNull(tag);
// 'test' is not a valid attribut. But we currently don't test it.
parser.parse(new StringResourceStream("<?xml test='123' >").getInputStream(), null);
assertNull(parser.getEncoding());
tag = parser.nextTag();
assertNull(tag);
}