use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class JAXBEncoderDecoderTest method testUnMarshall.
@Test
public void testUnMarshall() throws Exception {
// Hello World Wsdl generated namespace
QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
part.setTypeClass(Class.forName(wrapperAnnotation.className()));
Document doc = DOMUtils.getEmptyDocument();
Element elNode = doc.createElementNS(elName.getNamespaceURI(), elName.getLocalPart());
Element rtEl = doc.createElementNS(elName.getNamespaceURI(), "requestType");
elNode.appendChild(rtEl);
rtEl.appendChild(doc.createTextNode("Hello Test"));
Object obj = JAXBEncoderDecoder.unmarshall(context.createUnmarshaller(), elNode, part, true);
assertNotNull(obj);
// Add a Node and then test
assertEquals(GreetMe.class, obj.getClass());
assertEquals("Hello Test", ((GreetMe) obj).getRequestType());
part.setTypeClass(String.class);
Node n = null;
try {
JAXBEncoderDecoder.unmarshall(context.createUnmarshaller(), n, part, true);
fail("Should have received a Fault");
} catch (Fault pe) {
// Expected Exception
} catch (Exception ex) {
fail("Should have received a Fault, not: " + ex);
}
// Now test schema validation during unmarshaling
elName = new QName(wrapperAnnotation.targetNamespace(), "stringStruct");
// Create an XML Tree of
// <StringStruct><arg1>World</arg1></StringStruct>
// elNode = soapElFactory.createElement(elName);
// elNode.addNamespaceDeclaration("", elName.getNamespaceURI());
part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
part.setTypeClass(Class.forName("org.apache.hello_world_soap_http.types.StringStruct"));
doc = DOMUtils.getEmptyDocument();
elNode = doc.createElementNS(elName.getNamespaceURI(), elName.getLocalPart());
rtEl = doc.createElementNS(elName.getNamespaceURI(), "arg1");
elNode.appendChild(rtEl);
rtEl.appendChild(doc.createTextNode("World"));
// Should unmarshal without problems when no schema used.
obj = JAXBEncoderDecoder.unmarshall(context.createUnmarshaller(), elNode, part, true);
assertNotNull(obj);
assertEquals(StringStruct.class, obj.getClass());
assertEquals("World", ((StringStruct) obj).getArg1());
try {
// unmarshal with schema should raise exception.
Unmarshaller m = context.createUnmarshaller();
m.setSchema(schema);
JAXBEncoderDecoder.unmarshall(m, elNode, part, true);
fail("Should have thrown a Fault");
} catch (Fault ex) {
// expected - schema validation should fail.
}
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshalRPCLit.
@Test
public void testMarshalRPCLit() throws Exception {
QName elName = new QName("http://test_jaxb_marshall", "in");
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
Document doc = DOMUtils.createDocument();
Element elNode = doc.createElementNS(elName.getNamespaceURI(), elName.getLocalPart());
JAXBEncoderDecoder.marshall(context.createMarshaller(), new String("TestSOAPMessage"), part, elNode);
assertEquals("TestSOAPMessage", elNode.getFirstChild().getFirstChild().getNodeValue());
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshallExceptionWithOrder.
@Test
public void testMarshallExceptionWithOrder() throws Exception {
Document doc = DOMUtils.getEmptyDocument();
Element elNode = doc.createElementNS("http://cxf.apache.org", "ExceptionRoot");
OrderException exception = new OrderException("Mymessage");
exception.setAValue("avalue");
exception.setDetail("detail");
exception.setInfo1("info1");
exception.setInfo2("info2");
exception.setIntVal(10000);
QName elName = new QName("http://cxf.apache.org", "OrderException");
ServiceInfo serviceInfo = new ServiceInfo();
InterfaceInfo interfaceInfo = new InterfaceInfo(serviceInfo, null);
OperationInfo op = interfaceInfo.addOperation(new QName("http://cxf.apache.org", "operation"));
MessageInfo message = new MessageInfo(op, null, null);
MessagePartInfo part = new MessagePartInfo(elName, message);
part.setElement(true);
part.setElementQName(elName);
part.setTypeClass(OrderException.class);
// just need a simple generic context to handle the exceptions internal primitives
JAXBContext exceptionContext = JAXBContext.newInstance(new Class[] { String.class });
JAXBEncoderDecoder.marshallException(exceptionContext.createMarshaller(), exception, part, elNode);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
StaxUtils.writeTo(elNode, bout);
int a = bout.toString().lastIndexOf("aValue");
int b = bout.toString().lastIndexOf("detail");
int c = bout.toString().lastIndexOf("info1");
int d = bout.toString().lastIndexOf("info2");
int e = bout.toString().lastIndexOf("intVal");
assertTrue(a < b);
assertTrue(b < c);
assertTrue(c < d);
assertTrue(d < e);
assertTrue(bout.toString().indexOf("transientValue") < 0);
assertTrue(bout.toString(), bout.toString().indexOf("mappedField=\"MappedField\"") > 0);
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class XMLStreamDataWriterTest method testWriteRPCLit1.
@Test
public void testWriteRPCLit1() throws Exception {
JAXBDataBinding db = getTestWriterFactory();
DataWriter<XMLStreamWriter> dw = db.createWriter(XMLStreamWriter.class);
assertNotNull(dw);
String val = new String("TESTOUTPUTMESSAGE");
QName elName = new QName("http://apache.org/hello_world_rpclit/types", "in");
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
dw.write(val, part, streamWriter);
streamWriter.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
StaxUtils.toNextElement(reader);
assertEquals(new QName("http://apache.org/hello_world_rpclit/types", "in"), reader.getName());
StaxUtils.nextEvent(reader);
StaxUtils.toNextText(reader);
assertEquals("TESTOUTPUTMESSAGE", reader.getText());
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class XMLStreamDataWriterTest method testSetProperty.
@Test
public void testSetProperty() throws Exception {
MyCustomHandler handler = new MyCustomHandler();
DataWriterImpl<XMLStreamWriter> dw = newDataWriter(handler);
// Write Stuff
TradePriceData val = new TradePriceData();
val.setTickerSymbol("This is a symbol");
val.setTickerPrice(1.0f);
QName elName = new QName("http://apache.org/hello_world_doc_lit_bare/types", "inout");
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
dw.write(val, part, streamWriter);
streamWriter.flush();
// Test MyCustomHandler
assertTrue(handler.getUsed());
}
Aggregations