use of org.apache.axiom.truth.xml.spi.Traverser in project webservices-axiom by apache.
the class XMLSubject method hasSameContentAs.
/**
* Fails unless the subject represents the same XML as the given object.
*
* @param other
* the object to compare with
*/
public void hasSameContentAs(Object other) {
try {
Traverser actual = createTraverser(xml);
XML expectedXML = XMLTruth.xml(other);
Traverser expected = createTraverser(expectedXML);
while (true) {
Event actualEvent = actual.next();
Event expectedEvent = expected.next();
if (expectedEvent == Event.WHITESPACE || expectedEvent == Event.TEXT) {
if (!xml.isReportingElementContentWhitespace()) {
assertThat(actualEvent).isEqualTo(Event.TEXT);
} else if (treatWhitespaceAsText || !expectedXML.isReportingElementContentWhitespace()) {
assertThat(actualEvent).isAnyOf(Event.WHITESPACE, Event.TEXT);
} else {
assertThat(actualEvent).isEqualTo(expectedEvent);
}
} else {
assertThat(actualEvent).isEqualTo(expectedEvent);
}
if (expectedEvent == null) {
break;
}
switch(expectedEvent) {
case DOCUMENT_TYPE:
assertThat(actual.getRootName()).isEqualTo(expected.getRootName());
assertThat(actual.getPublicId()).isEqualTo(expected.getPublicId());
assertThat(actual.getSystemId()).isEqualTo(expected.getSystemId());
break;
case START_ELEMENT:
QName actualQName = actual.getQName();
Map<QName, String> actualAttributes = actual.getAttributes();
QName expectedQName = expected.getQName();
Map<QName, String> expectedAttributes = expected.getAttributes();
assertThat(actualQName).isEqualTo(expectedQName);
assertThat(actualAttributes).isEqualTo(expectedAttributes);
if (!ignoreNamespacePrefixes) {
assertThat(actualQName.getPrefix()).isEqualTo(expectedQName.getPrefix());
if (expectedAttributes != null) {
assertThat(extractPrefixes(actualAttributes.keySet())).isEqualTo(extractPrefixes(expectedAttributes.keySet()));
}
}
if (!ignoreNamespaceDeclarations) {
assertThat(actual.getNamespaces()).isEqualTo(expected.getNamespaces());
}
break;
case END_ELEMENT:
break;
case TEXT:
case WHITESPACE:
case COMMENT:
case CDATA_SECTION:
assertThat(actual.getText()).isEqualTo(expected.getText());
break;
case ENTITY_REFERENCE:
if (expandEntityReferences) {
throw new IllegalStateException();
}
assertThat(actual.getEntityName()).isEqualTo(expected.getEntityName());
break;
case PROCESSING_INSTRUCTION:
assertThat(actual.getPITarget()).isEqualTo(expected.getPITarget());
assertThat(actual.getPIData()).isEqualTo(expected.getPIData());
break;
default:
throw new IllegalStateException();
}
}
} catch (TraverserException ex) {
// TODO: check how to fail properly
throw new RuntimeException(ex);
}
}
use of org.apache.axiom.truth.xml.spi.Traverser in project webservices-axiom by apache.
the class StAXTraverserTest method testFragment.
@Test
public void testFragment() throws Exception {
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader("<root><a><b/></a></root>"));
reader.next();
reader.next();
Traverser t = new StAXTraverser(reader);
assertThat(t.next()).isEqualTo(Event.START_ELEMENT);
assertThat(t.getQName()).isEqualTo(new QName("a"));
assertThat(t.next()).isEqualTo(Event.START_ELEMENT);
assertThat(t.getQName()).isEqualTo(new QName("b"));
assertThat(t.next()).isEqualTo(Event.END_ELEMENT);
assertThat(t.next()).isEqualTo(Event.END_ELEMENT);
assertThat(t.next()).isNull();
}
use of org.apache.axiom.truth.xml.spi.Traverser in project webservices-axiom by apache.
the class XMLSubject method createTraverser.
private Traverser createTraverser(XML xml) throws TraverserException {
Traverser traverser = xml.createTraverser(expandEntityReferences);
if (ignoreWhitespaceInPrologAndEpilog || ignorePrologAndEpilog) {
final boolean onlyWhitespace = !ignorePrologAndEpilog;
traverser = new Filter(traverser) {
private int depth;
@Override
public Event next() throws TraverserException {
Event event;
while ((event = super.next()) != null) {
switch(event) {
case START_ELEMENT:
depth++;
break;
case END_ELEMENT:
depth--;
break;
default:
if (onlyWhitespace) {
break;
}
// Fall through
case WHITESPACE:
if (depth == 0) {
continue;
}
}
break;
}
return event;
}
};
}
final Set<Event> ignoredEvents = new HashSet<>();
if (ignoreComments) {
ignoredEvents.add(Event.COMMENT);
}
if (ignoreWhitespace || ignoreElementContentWhitespace) {
ignoredEvents.add(Event.WHITESPACE);
}
if (!ignoredEvents.isEmpty()) {
traverser = new Filter(traverser) {
@Override
public Event next() throws TraverserException {
Event event;
while (ignoredEvents.contains(event = super.next())) {
// loop
}
return event;
}
};
}
traverser = new CoalescingFilter(traverser);
if (ignoreWhitespace) {
traverser = new Filter(traverser) {
@Override
public Event next() throws TraverserException {
Event event = super.next();
if (event == Event.TEXT) {
String text = getText();
for (int i = 0; i < text.length(); i++) {
if (" \r\n\t".indexOf(text.charAt(i)) == -1) {
return Event.TEXT;
}
}
return super.next();
} else {
return event;
}
}
};
}
if (ignoreRedundantNamespaceDeclarations && !ignoreNamespaceDeclarations) {
traverser = new RedundantNamespaceDeclarationFilter(traverser);
}
return traverser;
}
use of org.apache.axiom.truth.xml.spi.Traverser in project webservices-axiom by apache.
the class DOMTraverserTest method testEntityReferenceExpansion.
@Test
public void testEntityReferenceExpansion() throws Exception {
Traverser t = new CoalescingFilter(new DOMXML(XMLSample.ENTITY_REFERENCE_NESTED.getDocument()).createTraverser(true));
assertThat(t.next()).isEqualTo(Event.DOCUMENT_TYPE);
assertThat(t.next()).isEqualTo(Event.START_ELEMENT);
assertThat(t.next()).isEqualTo(Event.TEXT);
assertThat(t.getText().trim()).isEqualTo("A B C");
assertThat(t.next()).isEqualTo(Event.END_ELEMENT);
assertThat(t.next()).isNull();
}
Aggregations