use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestSetLocalName method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMSourcedElement element = factory.createOMElement(new PullOMDataSource("<p:root xmlns:p='urn:test'><child/></p:root>"), "root", factory.createOMNamespace("urn:test", "p"));
if (expand) {
element.getFirstOMChild();
}
element.setLocalName("newroot");
assertAbout(xml()).that(element.toString()).hasSameContentAs("<p:newroot xmlns:p='urn:test'><child/></p:newroot>");
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestDataHandlerSerializationWithoutMTOM method runTest.
@Override
protected void runTest() throws Throwable {
SOAPFactory factory = metaFactory.getSOAP11Factory();
JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
// Construct the original message
DocumentBean orgObject = new DocumentBean();
orgObject.setId("123456");
orgObject.setContent(new DataHandler("some content", "text/plain; charset=utf-8"));
SOAPEnvelope orgEnvelope = factory.getDefaultEnvelope();
OMSourcedElement element = factory.createOMElement(new JAXBOMDataSource(context, orgObject));
orgEnvelope.getBody().addChild(element);
// Serialize the message
ByteArrayOutputStream out = new ByteArrayOutputStream();
orgEnvelope.serialize(out);
assertFalse(element.isExpanded());
SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(new ByteArrayInputStream(out.toByteArray()), null).getSOAPEnvelope();
DocumentBean object = (DocumentBean) context.createUnmarshaller().unmarshal(envelope.getBody().getFirstElement().getXMLStreamReader(false));
assertEquals("some content", IOUtils.toString(object.getContent().getInputStream(), "utf-8"));
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestAddAttribute method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMSourcedElement element = factory.createOMElement(new PullOMDataSource("<root attr='orgvalue'><child/></root>"), "root", null);
// Add an attribute before expansion
OMAttribute attr = strategy.addAttribute(element, "attr", null, "newvalue");
// Force expansion; this should not overwrite the attribute we just added
assertThat(element.getFirstOMChild()).isNotNull();
OMAttribute attr2 = element.getAttribute(new QName("attr"));
assertThat(attr2).isSameAs(attr);
assertThat(attr2.getAttributeValue()).isEqualTo("newvalue");
Iterator<OMAttribute> it = element.getAllAttributes();
assertThat(it.hasNext()).isTrue();
assertThat(it.next()).isSameAs(attr);
assertThat(it.hasNext()).isFalse();
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestBlobOMDataSource method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
String localName = "myPayload";
String encoding = "utf-8";
String payload = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
OMNamespace ns = factory.createOMNamespace("urn://test", "tns");
BlobOMDataSource ds = new BlobOMDataSource(Blobs.createBlob(payload.getBytes(encoding)), encoding);
OMElement parent = factory.createOMElement("root", null);
OMSourcedElement omse = factory.createOMElement(ds, 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()).isSameAs(ds);
// A BlobOMDataSource does not consume the backing object when read.
// Thus getting the XMLStreamReader of the BlobOMDataSource should not
// cause expansion of the OMSourcedElement.
XMLStreamReader reader = child.getXMLStreamReader();
reader.next();
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
// Likewise, a BlobOMDataSource does not consume the backing object when
// written. Thus serializing the OMSourcedElement should not cause the expansion
// of the OMSourcedElement.
assertTrue("The payload was not present in the output", parent.toString().indexOf(payload) > 0);
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
// If a consumer calls build or buildWithAttachments on the tree, the
// tree should not be expanded.
parent.build();
assertTrue("OMSourcedElement is expanded after build(). This is unexpected", !child.isExpanded());
parent.buildWithAttachments();
assertTrue("OMSourcedElement is expanded after buildWithAttachments(). This is unexpected", !child.isExpanded());
// Test getting the raw bytes from the BlobOMDataSource.
assertThat(child.getDataSource()).isSameAs(ds);
}
use of org.apache.axiom.om.OMSourcedElement in project webservices-axiom by apache.
the class TestCloneUnknownName method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMDataSource ds = new StringOMDataSource("<p:element xmlns:p='urn:ns'>test</p:element>");
OMSourcedElement element = factory.createOMElement(ds);
OMCloneOptions options = new OMCloneOptions();
options.setCopyOMDataSources(true);
OMElement clone = (OMElement) element.clone(options);
assertTrue(clone instanceof OMSourcedElement);
assertFalse(element.isExpanded());
OMNamespace expectedNS = factory.createOMNamespace("urn:ns", "p");
assertEquals("element", element.getLocalName());
assertEquals("element", clone.getLocalName());
assertEquals(expectedNS, element.getNamespace());
assertEquals(expectedNS, clone.getNamespace());
}
Aggregations