use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestSerializeOMDataSourceWritingToOutputStream method runTest.
@Override
protected void runTest() throws Throwable {
OMDataSourceImpl ds = new OMDataSourceImpl();
OMFactory factory = metaFactory.getOMFactory();
OMSourcedElement element = factory.createOMElement(ds);
OMElement elementToSerialize;
if (serializeParent) {
OMElement parent = factory.createOMElement("root", null);
parent.addChild(element);
elementToSerialize = parent;
} else {
elementToSerialize = element;
}
assertAbout(xml()).that(serializationStrategy.serialize(elementToSerialize).getInputSource()).hasSameContentAs(serializeParent ? "<root><test xmlns='urn:test'/></root>" : "<test xmlns='urn:test'/>");
assertThat(ds.isOutputStreamUsed()).isEqualTo(serializationStrategy instanceof SerializeToOutputStream);
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestGetObject method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
DataSource ds = new BlobDataSource(Blobs.createBlob("test".getBytes("utf-8")), "text/plain; charset=utf-8");
OMSourcedElement element = factory.createOMElement(new WrappedTextNodeOMDataSourceFromDataSource(new QName("wrapper"), ds, Charset.forName("utf-8")));
// getObject returns null if the data source is not of the expected type
assertNull(element.getObject(StringOMDataSource.class));
// Test with the right data source type
assertSame(ds, element.getObject(WrappedTextNodeOMDataSourceFromDataSource.class));
assertSame(ds, element.getObject(WrappedTextNodeOMDataSource.class));
// Now modify the content of the element
factory.createOMComment(element, "comment");
assertNull(element.getObject(WrappedTextNodeOMDataSourceFromDataSource.class));
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestGetReaderException method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMSourcedElement element = factory.createOMElement(new AbstractPullOMDataSource() {
@Override
public XMLStreamReader getReader() throws XMLStreamException {
throw new XMLStreamException("Test exception");
}
@Override
public boolean isDestructiveRead() {
return true;
}
});
try {
element.getLocalName();
fail("Expected OMException");
} catch (OMException ex) {
Throwable cause = ex.getCause();
assertTrue(cause instanceof XMLStreamException);
assertEquals("Test exception", cause.getMessage());
}
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestGetSAXSourceWithPushOMDataSource method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMSourcedElement sourcedElement = factory.createOMElement(new AbstractPushOMDataSource() {
@Override
public void serialize(XMLStreamWriter writer) throws XMLStreamException {
scenario.serialize(writer);
}
@Override
public boolean isDestructiveWrite() {
return false;
}
});
Iterator<Map.Entry<String, String>> it = scenario.getNamespaceContext().entrySet().iterator();
OMElement parent;
if (it.hasNext()) {
Map.Entry<String, String> binding = it.next();
parent = factory.createOMElement("parent", factory.createOMNamespace(binding.getValue(), binding.getKey()));
while (it.hasNext()) {
binding = it.next();
parent.declareNamespace(factory.createOMNamespace(binding.getValue(), binding.getKey()));
}
} else {
parent = factory.createOMElement("parent", null);
}
parent.addChild(sourcedElement);
SAXSource saxSource = (serializeParent ? parent : sourcedElement).getSAXSource(true);
OMElement element = OMXMLBuilderFactory.createOMBuilder(factory, saxSource, false).getDocumentElement();
if (serializeParent) {
element = element.getFirstElement();
}
scenario.validate(element, false);
}
use of org.apache.axiom.om.OMSourcedElement 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