private void parseUntilNext(int event) throws XmlPullParserException, IOException { while (parser.next() != event) { if (parser.getEventType() == XmlResourceParser.END_DOCUMENT) { throw new RuntimeException("Impossible to find: " + event + ". End of document reached."); } } }
@Test public void testSetFeature() throws XmlPullParserException { for (String feature : ResourceParser.AVAILABLE_FEATURES) { parser.setFeature(feature, true); try { parser.setFeature(feature, false); fail(feature + " should be true."); } catch (XmlPullParserException ex) { // pass } } for (String feature : ResourceParser.UNAVAILABLE_FEATURES) { try { parser.setFeature(feature, false); fail(feature + " should not be true."); } catch (XmlPullParserException ex) { // pass } try { parser.setFeature(feature, true); fail(feature + " should not be true."); } catch (XmlPullParserException ex) { // pass } } }
@Test public void testRequire() throws XmlPullParserException, IOException { parseUntilNext(XmlResourceParser.START_TAG); parser.require(XmlResourceParser.START_TAG, parser.getNamespace(), parser.getName()); try { parser.require(XmlResourceParser.END_TAG, parser.getNamespace(), parser.getName()); fail("Require with wrong event should have failed"); } catch (XmlPullParserException ex) { // pass } try { parser.require(XmlResourceParser.START_TAG, "foo", parser.getName()); fail("Require with wrong namespace should have failed"); } catch (XmlPullParserException ex) { // pass } try { parser.require(XmlResourceParser.START_TAG, parser.getNamespace(), "foo"); fail("Require with wrong tag name should have failed"); } catch (XmlPullParserException ex) { // pass } }
@Test public void testGetEventType() throws XmlPullParserException, IOException { int evt; while ((evt = parser.next()) != XmlResourceParser.END_DOCUMENT) { assertThat(parser.getEventType()).isEqualTo(evt); } }
@Test public void testGetAttributeName() throws XmlPullParserException, IOException { assertThat(parser.getAttributeName(0)).isNull(); forgeAndOpenDocument("<foo bar=\"bar\"/>"); assertThat(parser.getAttributeName(0)).isEqualTo("bar"); assertThat(parser.getAttributeName(attributeIndexOutOfIndex())).isNull(); }
@Test public void testGetText() throws XmlPullParserException, IOException { forgeAndOpenDocument("<foo/>"); assertThat(parser.getText()).isEqualTo(""); forgeAndOpenDocument("<foo>bar</foo>"); assertThat(parser.getText()).isEqualTo("bar"); }
@Test public void testGetClassAttribute() throws XmlPullParserException, IOException { forgeAndOpenDocument("<foo/>"); assertThat(parser.getClassAttribute()).isEqualTo(null); forgeAndOpenDocument("<foo class=\"bar\"/>"); assertThat(parser.getClassAttribute()).isEqualTo("bar"); }
@Test public void testGetAttributeCount() throws XmlPullParserException, IOException { assertThat(parser.getAttributeCount()) .isEqualTo(-1) .as("When no node is being explored the number of attributes should be -1."); forgeAndOpenDocument("<foo bar=\"bar\"/>"); assertThat(parser.getAttributeCount()).isEqualTo(1); }
@Test public void testGetAttributeListValue_StringStringStringArrayInt() throws XmlPullParserException, IOException { String[] options = {"foo", "bar"}; forgeAndOpenDocument("<foo xmlns:bar=\"bar\"/>"); assertThat(parser.getAttributeListValue(XMLNS_NS, "bar", options, 0)).isEqualTo(1); forgeAndOpenDocument("<foo xmlns:bar=\"unexpected\"/>"); assertThat(parser.getAttributeListValue(XMLNS_NS, "bar", options, 0)).isEqualTo(0); }
@Test public void testNextText_noText() throws XmlPullParserException, IOException { forgeAndOpenDocument("<foo><bar/></foo>"); try { assertThat(parser.nextText()).isEqualTo(parser.getText()); fail("nextText on a document with no text should have failed"); } catch (XmlPullParserException ex) { assertThat(parser.getEventType()) .isIn(XmlResourceParser.START_TAG, XmlResourceParser.END_DOCUMENT); } }
@Test public void testGetAttributeIntValue_IntInt() throws XmlPullParserException { forgeAndOpenDocument("<foo bar=\"-12\"/>"); assertThat(parser.getAttributeIntValue(0, 0)).isEqualTo(-12); assertThat(parser.getAttributeIntValue(attributeIndexOutOfIndex(), 0)).isEqualTo(0); forgeAndOpenDocument("<foo bar=\"unexpected\"/>"); assertThat(parser.getAttributeIntValue(0, 0)).isEqualTo(0); }
@Test public void testGetAttributeFloatValue_StringStringFloat() throws XmlPullParserException { forgeAndOpenDocument("<foo xmlns:bar=\"12.01\"/>"); assertThat(parser.getAttributeFloatValue(XMLNS_NS, "bar", 0.0f)).isEqualTo(12.01f); assertThat(parser.getAttributeFloatValue(XMLNS_NS, "foo", 0.0f)).isEqualTo(0.0f); forgeAndOpenDocument("<foo bar=\"unexpected\"/>"); assertThat(parser.getAttributeFloatValue(XMLNS_NS, "bar", 0.0f)).isEqualTo(0.0f); }
@Test public void testGetAttributeFloatValue_IntFloat() throws XmlPullParserException, IOException { forgeAndOpenDocument("<foo bar=\"12.01\"/>"); assertThat(parser.getAttributeFloatValue(0, 0.0f)).isEqualTo(12.01f); assertThat(parser.getAttributeFloatValue(attributeIndexOutOfIndex(), 0.0f)).isEqualTo(0.0f); forgeAndOpenDocument("<foo bar=\"unexpected\"/>"); assertThat(parser.getAttributeFloatValue(0, 0.0f)).isEqualTo(0.0f); }
@Test public void testGetAttributeValue_Int() throws XmlPullParserException { forgeAndOpenDocument("<foo bar=\"bar\"/>"); assertThat(parser.getAttributeValue(0)).isEqualTo("bar"); try { parser.getAttributeValue(attributeIndexOutOfIndex()); fail(); } catch (IndexOutOfBoundsException ex) { // pass } }
@Test public void testGetFeature() { for (String feature : ResourceParser.AVAILABLE_FEATURES) { assertThat(parser.getFeature(feature)).isTrue(); } for (String feature : ResourceParser.UNAVAILABLE_FEATURES) { assertThat(parser.getFeature(feature)).isFalse(); } assertThat(parser.getFeature(null)).isFalse(); }
/** Test that next tag will only return tag events. */ @Test public void testNextTag() throws XmlPullParserException, IOException { Set<Integer> acceptableTags = new HashSet<>(); acceptableTags.add(XmlResourceParser.START_TAG); acceptableTags.add(XmlResourceParser.END_TAG); forgeAndOpenDocument("<foo><bar/><text>message</text></foo>"); int evt; do { evt = parser.next(); assertTrue(acceptableTags.contains(evt)); } while (evt == XmlResourceParser.END_TAG && "foo".equals(parser.getName())); }
@Test public void testGetAttributeUnsignedIntValue_IntInt() throws XmlPullParserException { forgeAndOpenDocument("<foo bar=\"12\"/>"); assertThat(parser.getAttributeUnsignedIntValue(0, 0)).isEqualTo(12); assertThat(parser.getAttributeUnsignedIntValue(attributeIndexOutOfIndex(), 0)).isEqualTo(0); // Negative unsigned int must be forgeAndOpenDocument("<foo bar=\"-12\"/>"); assertThat(parser.getAttributeUnsignedIntValue(0, 0)) .isEqualTo(0) .as("Getting a negative number as unsigned should return the default value."); }
@Test public void testGetAttributeUnsignedIntValue_StringStringInt() throws XmlPullParserException { forgeAndOpenDocument("<foo xmlns:bar=\"12\"/>"); assertThat(parser.getAttributeUnsignedIntValue(XMLNS_NS, "bar", 0)).isEqualTo(12); assertThat(parser.getAttributeUnsignedIntValue(XMLNS_NS, "foo", 0)).isEqualTo(0); // Negative unsigned int must be forgeAndOpenDocument("<foo xmlns:bar=\"-12\"/>"); assertThat(parser.getAttributeUnsignedIntValue(XMLNS_NS, "bar", 0)) .isEqualTo(0) .as("Getting a negative number as unsigned should return the default value."); }
@Test public void testGetAttributeIntValue_StringStringInt() throws XmlPullParserException { forgeAndOpenDocument("<foo xmlns:bar=\"-12\"/>"); assertThat(parser.getAttributeIntValue(XMLNS_NS, "bar", 0)).isEqualTo(-12); assertThat(parser.getAttributeIntValue(XMLNS_NS, "foo", 0)).isEqualTo(0); }
@Test public void testGetAttributeBooleanValue_StringStringBoolean() throws XmlPullParserException, IOException { forgeAndOpenDocument("<foo xmlns:bar=\"true\"/>"); assertThat(parser.getAttributeBooleanValue(XMLNS_NS, "bar", false)).isTrue(); assertThat(parser.getAttributeBooleanValue(XMLNS_NS, "foo", false)).isFalse(); }
@Test public void testIsEmptyElementTag() throws XmlPullParserException, IOException { assertThat(parser.isEmptyElementTag()) .isEqualTo(false) .as("Before START_DOCUMENT should return false."); forgeAndOpenDocument("<foo><bar/></foo>"); assertThat(parser.isEmptyElementTag()) .isEqualTo(false) .as("Not empty tag should return false."); forgeAndOpenDocument("<foo/>"); assertThat(parser.isEmptyElementTag()) .isEqualTo(false) .as("In the Android implementation this method always return false."); }
@Test public void testGetDepth() throws XmlPullParserException, IOException { // Recorded depths from preference file elements List<Integer> expectedDepths = asList(1, 2, 3, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3); List<Integer> actualDepths = new ArrayList<>(); int evt; while ((evt = parser.next()) != XmlResourceParser.END_DOCUMENT) { switch (evt) { case (XmlResourceParser.START_TAG): { actualDepths.add(parser.getDepth()); break; } } } assertThat(actualDepths).isEqualTo(expectedDepths); }
@Test public void testSetProperty() { try { parser.setProperty("foo", "bar"); fail("Properties should not be supported"); } catch (XmlPullParserException ex) { // pass } }
@Test public void testDefineEntityReplacementText() { try { parser.defineEntityReplacementText("foo", "bar"); fail("This method should not be supported"); } catch (XmlPullParserException ex) { // pass } }
@Test public void testGetNamespace_String() { try { parser.getNamespace("bar"); fail("This method should not be supported"); } catch (RuntimeException ex) { // pass } }
@Test public void testGetNamespaceUri() { try { parser.getNamespaceUri(0); fail("This method should not be supported"); } catch (XmlPullParserException ex) { // pass } }
@Test public void testGetPrefix() { try { parser.getPrefix(); fail("This method should not be supported"); } catch (RuntimeException ex) { // pass } }
@Test public void testSetInput_Reader() { try { parser.setInput(new StringReader("")); fail("This method should not be supported"); } catch (XmlPullParserException ex) { // pass } }
@Test public void testSetInput_InputStreamString() throws IOException { try (InputStream inputStream = getClass().getResourceAsStream("src/test/resources/res/xml/preferences.xml")) { parser.setInput(inputStream, "UTF-8"); fail("This method should not be supported"); } catch (XmlPullParserException ex) { // pass } }
@Test public void testGetAttributePrefix() throws XmlPullParserException, IOException { parseUntilNext(XmlResourceParser.START_TAG); try { parser.getAttributePrefix(0); fail("This method should not be supported"); } catch (RuntimeException ex) { // pass } }