use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestRegisterCustomBuilderForPayload method runTest.
@Override
protected void runTest() throws Throwable {
SOAPModelBuilder builder = message.getAdapter(SOAPSampleAdapter.class).getBuilder(metaFactory);
((CustomBuilderSupport) builder).registerCustomBuilder(CustomBuilder.Selector.PAYLOAD, new BlobOMDataSourceCustomBuilder(MemoryBlob.FACTORY, "utf-8"));
SOAPEnvelope envelope = builder.getSOAPEnvelope();
OMElement payload = envelope.getBody().getFirstElement();
if (message.getPayload() == null) {
assertThat(payload).isNull();
} else if (message.getPayload().getLocalName().equals("Fault")) {
assertThat(payload).isInstanceOf(SOAPFault.class);
} else {
assertThat(payload).isInstanceOf(OMSourcedElement.class);
BlobOMDataSource.Data data = (BlobOMDataSource.Data) ((OMSourcedElement) payload).getObject(BlobOMDataSource.class);
assertThat(data).isNotNull();
InputSource is = new InputSource(data.getBlob().getInputStream());
is.setEncoding(data.getEncoding());
assertAbout(xml()).that(is).ignoringNamespaceDeclarations().hasSameContentAs(message.getPayloadInputSource());
}
// We need to ignore redundant namespace declarations because the custom builder needs
// to preserve the namespace context when serializing to the blob.
assertAbout(xml()).that(envelope.getXMLStreamReader(false)).ignoringPrologAndEpilog().ignoringRedundantNamespaceDeclarations().hasSameContentAs(message.getInputStream());
if (payload instanceof OMSourcedElement) {
assertThat(((OMSourcedElement) payload).isExpanded()).isFalse();
}
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestCloneWithSourcedElement1 method runTest.
@Override
protected void runTest() throws Throwable {
SOAPEnvelope sourceEnv = soapFactory.getDefaultEnvelope();
SOAPBody body = sourceEnv.getBody();
// Create a payload
OMDataSource bads = new StringOMDataSource("<tns:payload xmlns:tns=\"urn://test\">Hello World</tns:payload>");
OMNamespace ns = body.getOMFactory().createOMNamespace("urn://test", "tns");
OMSourcedElement omse = body.getOMFactory().createOMElement(bads, "payload", ns);
body.addChild(omse);
copyAndCheck(sourceEnv);
assertFalse(omse.isExpanded());
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestHasFaultWithOMSEUnknownName method runTest.
@Override
protected void runTest() throws Throwable {
SOAPEnvelope envelope = soapFactory.getDefaultEnvelope();
SOAPBody body = envelope.getBody();
OMSourcedElement element = soapFactory.createOMElement(new StringOMDataSource("<ns:root xmlns:ns='urn:ns'/>"));
body.addChild(element);
assertFalse(body.hasFault());
assertFalse(element.isExpanded());
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestSerialize method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMSourcedElement element = TestDocument.DOCUMENT1.createOMSourcedElement(factory, push, destructive);
OMDataSource ds = element.getDataSource();
OMContainer parent = elementContext.wrap(element);
boolean parentComplete = parent != null && parent.isComplete();
expansionStrategy.apply(element);
boolean consuming = expansionStrategy.isConsumedAfterSerialization(push, destructive, serializationStrategy);
for (int iteration = 0; iteration < count; iteration++) {
boolean expectException = iteration != 0 && (consuming || serializeParent && !serializationStrategy.isCaching() && !parentComplete);
XML result;
try {
result = serializationStrategy.serialize(serializeParent ? parent : element);
if (expectException) {
fail("Expected exception");
}
} catch (Exception ex) {
if (!expectException) {
throw ex;
} else {
continue;
}
}
InputSource expectedXML = new InputSource(new StringReader(TestDocument.DOCUMENT1.getContent()));
if (serializeParent) {
expectedXML = elementContext.getControl(expectedXML);
}
assertAbout(xml()).that(result.getInputSource()).hasSameContentAs(expectedXML);
// the sourced element should be expanded.
if (expansionStrategy.isExpandedAfterSerialization(push, destructive, serializationStrategy)) {
assertTrue(element.isExpanded());
assertEquals("OMSourcedElement completion status", !consuming, element.isComplete());
} else {
assertFalse(element.isExpanded());
}
if (parent != null && !serializeParent) {
// Operations on the OMSourcedElement should have no impact on the parent
assertEquals("Parent completion status", parentComplete, parent.isComplete());
}
}
if (ds instanceof PullOMDataSource) {
assertFalse(((PullOMDataSource) ds).hasUnclosedReaders());
}
}
use of org.apache.axiom.om.OMSourcedElement 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);
}
Aggregations