Search in sources :

Example 1 with Traverser

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);
    }
}
Also used : XML(org.apache.axiom.truth.xml.spi.XML) QName(javax.xml.namespace.QName) Traverser(org.apache.axiom.truth.xml.spi.Traverser) Event(org.apache.axiom.truth.xml.spi.Event) TraverserException(org.apache.axiom.truth.xml.spi.TraverserException)

Example 2 with Traverser

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();
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) Traverser(org.apache.axiom.truth.xml.spi.Traverser) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 3 with Traverser

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;
}
Also used : Traverser(org.apache.axiom.truth.xml.spi.Traverser) Event(org.apache.axiom.truth.xml.spi.Event) TraverserException(org.apache.axiom.truth.xml.spi.TraverserException) HashSet(java.util.HashSet)

Example 4 with 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();
}
Also used : Traverser(org.apache.axiom.truth.xml.spi.Traverser) Test(org.junit.Test)

Aggregations

Traverser (org.apache.axiom.truth.xml.spi.Traverser)4 QName (javax.xml.namespace.QName)2 Event (org.apache.axiom.truth.xml.spi.Event)2 TraverserException (org.apache.axiom.truth.xml.spi.TraverserException)2 Test (org.junit.Test)2 StringReader (java.io.StringReader)1 HashSet (java.util.HashSet)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 XML (org.apache.axiom.truth.xml.spi.XML)1