use of org.apache.axiom.om.OMNode in project webservices-axiom by apache.
the class TestSetTextQNameWithExistingChildren method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMElement element = factory.createOMElement("TestElement", null);
// Add some children of various types
factory.createOMText(element, "some text");
factory.createOMText(element, "cdata section", OMNode.CDATA_SECTION_NODE);
factory.createOMComment(element, "comment");
factory.createOMProcessingInstruction(element, "piTarget", "piData");
factory.createOMElement("child", null, element);
QName qname = new QName("urn:ns1", "test", "ns");
element.setText(qname);
assertEquals("ns:test", element.getText());
// Check that OMElement#setText() has created the expected nodes
OMNode child = element.getFirstOMChild();
assertTrue(child instanceof OMText);
assertSame(element, child.getParent());
assertEquals("ns:test", ((OMText) child).getText());
assertNull(child.getNextOMSibling());
}
use of org.apache.axiom.om.OMNode in project webservices-axiom by apache.
the class TestCreateOMDocument method runTest.
@Override
protected void runTest() throws Throwable {
OMDocument document = metaFactory.getOMFactory().createOMDocument();
assertNotNull(document);
assertNull(document.getFirstOMChild());
// OMDocument doesn't extend OMNode. Therefore, the OMDocument implementation
// should not implement OMNode either. This is a regression test for AXIOM-385.
assertFalse(document instanceof OMNode);
}
use of org.apache.axiom.om.OMNode in project webservices-axiom by apache.
the class TestClose method runTest.
@Override
protected void runTest() throws Throwable {
OMElement rootElement = XMLSample.SIMPLE.getAdapter(XMLSampleAdapter.class).getDocumentElement(metaFactory);
// get the first OMElement child
OMNode omnode = rootElement.getFirstOMChild();
while (!(omnode instanceof OMElement)) {
omnode = omnode.getNextOMSibling();
}
// Close the element after building the element
OMElement omElement = (OMElement) omnode;
omElement.close(true);
Iterator<OMNode> children = ((OMElement) omnode).getChildren();
int childrenCount = 0;
while (children.hasNext()) {
if (children.next() instanceof OMElement) {
childrenCount++;
}
}
assertThat(childrenCount).isEqualTo(2);
}
use of org.apache.axiom.om.OMNode in project webservices-axiom by apache.
the class TestSetDataSource method runTest.
@Override
protected void runTest() throws Throwable {
String payload1 = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
String payload2 = "<tns:myPayload xmlns:tns=\"urn://test\">Payload Two</tns:myPayload>";
OMDataSource nonDestructiveOMDataSource1 = new PullOMDataSource(payload1, false);
OMDataSource nonDestructiveOMDataSource2 = new PullOMDataSource(payload2, false);
OMDataSource destructiveOMDataSource1 = new PullOMDataSource(payload1, true);
OMDataSource destructiveOMDataSource2 = new PullOMDataSource(payload2, true);
OMFactory factory = metaFactory.getOMFactory();
OMElement parent = factory.createOMElement("parent", null);
OMSourcedElement omse = factory.createOMElement(nonDestructiveOMDataSource1, "myPayload", factory.createOMNamespace("urn://test", "tns"));
parent.addChild(omse);
OMNode firstChild = parent.getFirstOMChild();
assertTrue("Expected OMSourcedElement child", firstChild instanceof OMSourcedElement);
OMSourcedElement child = (OMSourcedElement) firstChild;
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
assertThat(child.getDataSource()).isSameAs(nonDestructiveOMDataSource1);
// Write out the body
StringWriter sw = new StringWriter();
parent.serialize(sw);
String output = sw.toString();
// System.out.println(output);
assertTrue("The payload was not present in the output", output.indexOf(payload1) > 0);
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
// Replace with payload2.
// Important note, it is legal to replace the OMDataSource, but
// the namespace and local name of the OMSourcedElement cannot be changed.
child.setDataSource(nonDestructiveOMDataSource2);
// Write out the body
sw = new StringWriter();
parent.serialize(sw);
output = sw.toString();
// System.out.println(output);
assertTrue("The payload was not present in the output", output.indexOf(payload2) > 0);
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
// Now Replace with payload1 from an destructiveOMDataSource1
child.setDataSource(destructiveOMDataSource1);
sw = new StringWriter();
parent.serialize(sw);
output = sw.toString();
// System.out.println(output);
assertTrue("The payload was not present in the output", output.indexOf(payload1) > 0);
// Now Replace with payload2 from an destructiveOMDataSource2.
// Note at this point, the child's tree is expanded.
child.setDataSource(destructiveOMDataSource2);
sw = new StringWriter();
parent.serialize(sw);
output = sw.toString();
// System.out.println(output);
assertTrue("The payload was not present in the output", output.indexOf(payload2) > 0);
}
use of org.apache.axiom.om.OMNode in project webservices-axiom by apache.
the class TestStringOMDataSource method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
String localName = "myPayload";
String payload1 = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
OMNamespace ns = factory.createOMNamespace("urn://test", "tns");
StringOMDataSource somds = new StringOMDataSource(payload1);
OMElement parent = factory.createOMElement("root", null);
OMSourcedElement omse = factory.createOMElement(somds, localName, ns);
parent.addChild(omse);
OMNode firstChild = parent.getFirstOMChild();
assertTrue("Expected OMSourcedElement child", firstChild instanceof OMSourcedElement);
OMSourcedElement child = (OMSourcedElement) firstChild;
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
assertThat(child.getDataSource()).isInstanceOf(StringOMDataSource.class);
// A StringOMDataSource does not consume the backing object when read.
// Thus getting the XMLStreamReader of the StringOMDataSource should not
// cause expansion of the OMSourcedElement.
XMLStreamReader reader = child.getXMLStreamReader();
reader.next();
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
// Likewise, a StringOMDataSource does not consume the backing object when
// written. Thus serializing the OMSourcedElement should not cause the expansion
// of the OMSourcedElement.
StringWriter out = new StringWriter();
parent.serialize(out);
// System.out.println(output);
assertTrue("The payload was not present in the output", out.toString().indexOf(payload1) > 0);
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
// Test getting the raw content from the StringOMDataSource.
StringOMDataSource ds = (StringOMDataSource) child.getDataSource();
assertThat(ds.getObject()).isEqualTo(payload1);
// Validate close
ds.close();
assertTrue("Close should free the resource", ds.getObject() == null);
}
Aggregations