Search in sources :

Example 1 with XML

use of org.apache.axiom.truth.xml.spi.XML 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)

Aggregations

QName (javax.xml.namespace.QName)1 Event (org.apache.axiom.truth.xml.spi.Event)1 Traverser (org.apache.axiom.truth.xml.spi.Traverser)1 TraverserException (org.apache.axiom.truth.xml.spi.TraverserException)1 XML (org.apache.axiom.truth.xml.spi.XML)1