use of org.apache.hello_world_soap_http.types.StringStruct in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshallIntoDOM.
@Test
public void testMarshallIntoDOM() throws Exception {
String str = new String("Hello");
QName inCorrectElName = new QName("http://test_jaxb_marshall", "requestType");
MessagePartInfo part = new MessagePartInfo(inCorrectElName, null);
part.setElement(true);
part.setElementQName(inCorrectElName);
Document doc = DOMUtils.createDocument();
Element elNode = doc.createElementNS(inCorrectElName.getNamespaceURI(), inCorrectElName.getLocalPart());
assertNotNull(elNode);
Node node;
try {
JAXBEncoderDecoder.marshall(context.createMarshaller(), null, part, elNode);
fail("Should have thrown a Fault");
} catch (Fault ex) {
// expected - not a valid object
}
GreetMe obj = new GreetMe();
obj.setRequestType("Hello");
QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
part.setElementQName(elName);
JAXBEncoderDecoder.marshall(context.createMarshaller(), obj, part, elNode);
node = elNode.getLastChild();
// The XML Tree Looks like
// <GreetMe><requestType>Hello</requestType></GreetMe>
assertEquals(Node.ELEMENT_NODE, node.getNodeType());
Node childNode = node.getFirstChild();
assertEquals(Node.ELEMENT_NODE, childNode.getNodeType());
childNode = childNode.getFirstChild();
assertEquals(Node.TEXT_NODE, childNode.getNodeType());
assertEquals(str, childNode.getNodeValue());
// Now test schema validation during marshaling
StringStruct stringStruct = new StringStruct();
// Don't initialize one of the structure members.
// stringStruct.setArg0("hello");
stringStruct.setArg1("world");
// Marshal without the schema should work.
JAXBEncoderDecoder.marshall(context.createMarshaller(), stringStruct, part, elNode);
try {
Marshaller m = context.createMarshaller();
m.setSchema(schema);
// Marshal with the schema should get an exception.
JAXBEncoderDecoder.marshall(m, stringStruct, part, elNode);
fail("Marshal with schema should have thrown a Fault");
} catch (Fault ex) {
// expected - not a valid object
}
}
Aggregations