Search in sources :

Example 1 with StringStruct

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
    }
}
Also used : GreetMe(org.apache.hello_world_soap_http.types.GreetMe) Marshaller(javax.xml.bind.Marshaller) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) ObjectWithQualifiedElementElement(org.apache.cxf.jaxb_form.ObjectWithQualifiedElementElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Fault(org.apache.cxf.interceptor.Fault) StringStruct(org.apache.hello_world_soap_http.types.StringStruct) Document(org.w3c.dom.Document) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Test(org.junit.Test)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)1 Marshaller (javax.xml.bind.Marshaller)1 QName (javax.xml.namespace.QName)1 Fault (org.apache.cxf.interceptor.Fault)1 ObjectWithQualifiedElementElement (org.apache.cxf.jaxb_form.ObjectWithQualifiedElementElement)1 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)1 GreetMe (org.apache.hello_world_soap_http.types.GreetMe)1 StringStruct (org.apache.hello_world_soap_http.types.StringStruct)1 Test (org.junit.Test)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1