Search in sources :

Example 1 with OMNamedInformationItem

use of org.apache.axiom.om.OMNamedInformationItem in project webservices-axiom by apache.

the class SetNamespaceTestCase method runTest.

@Override
protected final void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element;
    OMNamedInformationItem node;
    if (context()) {
        // To avoid collisions if prefixInScope is the empty string
        OMNamespace dummyNS = factory.createOMNamespace("__dummy__", "__dummy__");
        OMElement parent = factory.createOMElement("parent", dummyNS);
        element = factory.createOMElement("test", dummyNS, parent);
        if (prefixInScope != null) {
            if (prefixInScope.length() == 0) {
                parent.declareDefaultNamespace(namespaceURI);
            } else {
                parent.declareNamespace(namespaceURI, prefixInScope);
            }
        }
    } else {
        element = null;
    }
    node = node(factory, element);
    OMNamespace ns = namespaceURI == null ? null : factory.createOMNamespace(namespaceURI, prefix);
    try {
        setNamespace(node, ns);
        if (invalid) {
            fail("Expected IllegalArgumentException");
        }
    } catch (IllegalArgumentException ex) {
        if (invalid) {
            return;
        } else {
            throw ex;
        }
    }
    String expectedPrefix;
    if (this.expectedPrefix == null) {
        expectedPrefix = node.getPrefix();
        assertNotNull(expectedPrefix);
        assertFalse(expectedPrefix.length() == 0);
    } else {
        expectedPrefix = this.expectedPrefix;
        if (expectedPrefix.length() == 0) {
            assertNull(node.getPrefix());
        } else {
            assertEquals(expectedPrefix, node.getPrefix());
        }
    }
    if (namespaceURI == null || namespaceURI.length() == 0) {
        assertNull(node.getNamespace());
    } else {
        OMNamespace actualNS = node.getNamespace();
        assertEquals(expectedPrefix, actualNS.getPrefix());
        assertEquals(namespaceURI, actualNS.getNamespaceURI());
    }
    if (namespaceURI == null || namespaceURI.length() == 0) {
        assertNull(node.getNamespaceURI());
    } else {
        assertEquals(namespaceURI, node.getNamespaceURI());
    }
    QName qname = node.getQName();
    assertEquals(expectedPrefix, qname.getPrefix());
    assertEquals(namespaceURI == null ? "" : namespaceURI, qname.getNamespaceURI());
    if (element != null) {
        Iterator<OMNamespace> it = element.getAllDeclaredNamespaces();
        if (expectNSDecl) {
            assertTrue(it.hasNext());
            OMNamespace decl = it.next();
            assertEquals(expectedPrefix, decl.getPrefix());
            assertEquals(namespaceURI == null ? "" : namespaceURI, decl.getNamespaceURI());
        }
        assertFalse(it.hasNext());
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamedInformationItem(org.apache.axiom.om.OMNamedInformationItem) OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement)

Aggregations

QName (javax.xml.namespace.QName)1 OMElement (org.apache.axiom.om.OMElement)1 OMFactory (org.apache.axiom.om.OMFactory)1 OMNamedInformationItem (org.apache.axiom.om.OMNamedInformationItem)1 OMNamespace (org.apache.axiom.om.OMNamespace)1