/** * This test was inspired by an actual bug in one of implementations: initial state was not * properly set if nextTag() was called (instead of nextEvent()), and subsequent peek() failed. */ public void testPeek() throws XMLStreamException { String XML = "<root>text</root>"; for (int i = 0; i < 4; ++i) { boolean ns = (i & 1) != 0; boolean coal = (i & 2) != 0; XMLEventReader er = getReader(XML, ns, coal); assertTokenType(START_DOCUMENT, er.nextEvent().getEventType()); XMLEvent tag = er.nextTag(); assertTokenType(START_ELEMENT, tag.getEventType()); // Now, peek() should produce text.. XMLEvent text = er.peek(); assertTokenType(CHARACTERS, text.getEventType()); Characters chars = text.asCharacters(); assertNotNull(chars); assertEquals("text", chars.getData()); // and need nextEvent() to get rid of it, too: text = er.nextEvent(); // Let's verify it again: assertTokenType(CHARACTERS, text.getEventType()); chars = text.asCharacters(); assertNotNull(chars); assertEquals("text", chars.getData()); assertTokenType(END_ELEMENT, er.nextTag().getEventType()); assertTokenType(END_DOCUMENT, er.nextEvent().getEventType()); // And at the end, peek() should return null assertNull(er.peek()); } }
public void testNextTagOk() throws XMLStreamException { String XML = "<root>\n" + "<branch> <leaf> </leaf></branch>" + "</root>"; for (int i = 0; i < 4; ++i) { boolean ns = (i & 1) != 0; boolean coal = (i & 2) != 0; XMLEventReader er = getReader(XML, ns, coal); assertTokenType(START_DOCUMENT, er.nextEvent().getEventType()); assertTokenType(START_ELEMENT, er.nextTag().getEventType()); assertTokenType(START_ELEMENT, er.nextTag().getEventType()); /* Ok, let's mix in bit of peeking to ensure reader won't * be confused too badly... */ // This should be space between <branch> and <leaf>... assertTokenType(CHARACTERS, er.peek().getEventType()); // And then the leaf assertTokenType(START_ELEMENT, er.nextTag().getEventType()); assertTokenType(END_ELEMENT, er.nextTag().getEventType()); assertTokenType(END_ELEMENT, er.nextTag().getEventType()); assertTokenType(END_ELEMENT, er.nextTag().getEventType()); assertTokenType(END_DOCUMENT, er.nextEvent().getEventType()); assertFalse(er.hasNext()); } }
public void testNextTagInvalid() throws XMLStreamException { String XML = "<root> non-empty<leaf /></root>"; String XML2 = "<root><leaf></leaf>text </root>"; for (int i = 0; i < 4; ++i) { boolean ns = (i & 1) != 0; boolean coal = (i & 2) != 0; XMLEventReader er = getReader(XML, ns, coal); assertTokenType(START_DOCUMENT, er.nextEvent().getEventType()); assertTokenType(START_ELEMENT, er.nextEvent().getEventType()); String msg = null; try { XMLEvent evt = er.nextTag(); msg = "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on non-empty CHARACTERS"; } catch (XMLStreamException sex) { // fine! } catch (Throwable t) { msg = "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on non-empty CHARACTERS; got (" + t.getClass() + "): " + t; } if (msg != null) { fail(msg); } er.close(); /* any other easily failing cases? Maybe if we are on top of * END_ELEMENT, and will hit another one? */ er = getReader(XML2, ns, coal); assertTokenType(START_DOCUMENT, er.nextEvent().getEventType()); assertTokenType(START_ELEMENT, er.nextEvent().getEventType()); assertTokenType(START_ELEMENT, er.nextEvent().getEventType()); assertTokenType(END_ELEMENT, er.nextEvent().getEventType()); try { XMLEvent evt = er.nextTag(); msg = "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on END_ELEMENT and hitting non-ws text; got event " + tokenTypeDesc(evt); } catch (XMLStreamException sex) { msg = null; // fine! } catch (Throwable t) { msg = "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on END_ELEMENT and hitting non-ws text; got: " + t; } if (msg != null) { fail(msg); } er.close(); } }