001: package org.objectweb.celtix.bus.bindings.xml;
002:
003: import java.io.*;
004: import java.net.*;
005: import java.util.*;
006: import javax.wsdl.Binding;
007: import javax.wsdl.BindingInput;
008: import javax.wsdl.BindingOperation;
009: import javax.wsdl.Definition;
010: import javax.wsdl.Port;
011: import javax.xml.namespace.QName;
012:
013: import org.w3c.dom.*;
014:
015: import junit.framework.TestCase;
016:
017: import org.objectweb.celtix.Bus;
018: import org.objectweb.celtix.bindings.DataBindingCallback;
019: import org.objectweb.celtix.bindings.xmlformat.TBody;
020: import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback;
021: import org.objectweb.celtix.context.GenericMessageContext;
022: import org.objectweb.celtix.context.ObjectMessageContext;
023: import org.objectweb.celtix.context.ObjectMessageContextImpl;
024: import org.objectweb.celtix.helpers.XMLUtils;
025: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
026: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
027: import org.objectweb.hello_world_xml_http.bare.Greeter;
028:
029: public class XMLBindingImplBareTest extends TestCase {
030: private XMLBindingImpl binding;
031: private ObjectMessageContextImpl objContext;
032: private XMLMessageContextImpl xmlContext;
033: private XMLUtils xmlUtils;
034: private TestUtils testUtils;
035:
036: public XMLBindingImplBareTest(String arg0) {
037: super (arg0);
038: }
039:
040: public static void main(String[] args) {
041: junit.textui.TestRunner.run(XMLBindingImplTest.class);
042: }
043:
044: protected void setUp() throws Exception {
045: super .setUp();
046:
047: xmlUtils = new XMLUtils();
048: testUtils = new TestUtils();
049: binding = new XMLBindingImpl(false);
050: objContext = new ObjectMessageContextImpl();
051: xmlContext = new XMLMessageContextImpl(
052: new GenericMessageContext());
053:
054: objContext.setMethod(ClassUtils.getMethod(Greeter.class,
055: "greetMe"));
056: }
057:
058: public void testBindingReference() throws Exception {
059: Bus bus = Bus.init();
060: binding = getBindingImpl(bus, testUtils.getEndpointReference());
061:
062: bus = binding.getBus();
063: assertNotNull(bus);
064: EndpointReferenceType reference = binding
065: .getEndpointReference();
066: assertNotNull(reference);
067:
068: // test wsdl definition from the endpoint reference
069: Definition wsdlDef = EndpointReferenceUtils.getWSDLDefinition(
070: bus.getWSDLManager(), reference);
071: assertNotNull(wsdlDef);
072: QName wsdlName = new QName(
073: "http://objectweb.org/hello_world_xml_http/bare",
074: "HelloWorld");
075: assertEquals(wsdlDef.getQName(), wsdlName);
076:
077: Port port = EndpointReferenceUtils.getPort(
078: bus.getWSDLManager(), reference);
079: assertNotNull(port);
080: Binding b = port.getBinding();
081: assertNotNull(b);
082: BindingOperation operation = b.getBindingOperation("sayHi",
083: "sayHiRequest", "sayHiResponse");
084: assertNotNull(operation);
085: BindingInput input = operation.getBindingInput();
086: assertNotNull(input);
087:
088: TBody xmlBinding = null;
089: Iterator ite = input.getExtensibilityElements().iterator();
090: while (ite.hasNext()) {
091: Object obj = ite.next();
092: if (obj instanceof TBody) {
093: xmlBinding = (TBody) obj;
094: }
095: }
096: assertNotNull(xmlBinding);
097: assertEquals(new QName(
098: "http://objectweb.org/hello_world_xml_http/bare",
099: "sayHi"), xmlBinding.getRootNode());
100: }
101:
102: // Test greetMe which has one part in message
103: public void testMarshalBareWithoutRootNode() throws Exception {
104: Bus bus = Bus.init();
105: binding = getBindingImpl(bus, testUtils.getEndpointReference());
106:
107: xmlContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
108: String arg0 = new String("TestXMLInputMessage");
109: objContext.setMessageObjects(arg0);
110:
111: binding.marshal(objContext, xmlContext,
112: new JAXBDataBindingCallback(objContext.getMethod(),
113: DataBindingCallback.Mode.PARTS, null));
114: XMLMessage msg = xmlContext.getMessage();
115: assertNotNull(msg);
116:
117: InputStream is = getClass().getResourceAsStream(
118: "resources/GreetMeBareReq.xml");
119: Document expectDOM = xmlUtils.parse(is);
120: Document resultDOM = msg.getRoot();
121:
122: is.close();
123: assertTrue(expectDOM.isEqualNode(resultDOM));
124: }
125:
126: // Test sayHi which has no parameter to passed in
127: public void testMarshalBareWithRootNode() throws Exception {
128: Bus bus = Bus.init();
129:
130: binding = getBindingImpl(bus, testUtils.getEndpointReference());
131:
132: objContext.setMethod(ClassUtils.getMethod(Greeter.class,
133: "sayHi"));
134: xmlContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
135: String arg0 = new String("TestXMLInputMessage");
136: objContext.setMessageObjects(arg0);
137:
138: binding.marshal(objContext, xmlContext,
139: new JAXBDataBindingCallback(objContext.getMethod(),
140: DataBindingCallback.Mode.PARTS, null));
141:
142: XMLMessage msg = xmlContext.getMessage();
143: assertNotNull(msg);
144:
145: InputStream is = getClass().getResourceAsStream(
146: "resources/SayHiBareReq.xml");
147: Document expectDOM = xmlUtils.parse(is);
148: Document resultDOM = msg.getRoot();
149: is.close();
150:
151: assertNotNull(expectDOM);
152: assertNotNull(resultDOM);
153:
154: String expect = xmlUtils.toString(expectDOM);
155: String result = xmlUtils.toString(resultDOM);
156: assertTrue(expect.equals(result));
157: }
158:
159: // Test the greetMe Operation in message
160: public void testUnmarshalBareInputMessage() throws Exception {
161: xmlContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
162: InputStream in = getClass().getResourceAsStream(
163: "resources/GreetMeBareReq.xml");
164: assertNotNull(binding.getMessageFactory());
165: XMLMessage xmlMessage = binding.getMessageFactory()
166: .createMessage(in);
167: xmlContext.setMessage(xmlMessage);
168:
169: String data = new String("TestXMLInputMessage");
170:
171: //testDocLitBare method has a IN parameter
172: objContext.setMethod(ClassUtils.getMethod(Greeter.class,
173: "greetMe"));
174: objContext.setMessageObjects(new Object[1]);
175: binding.unmarshal(xmlContext, objContext,
176: new JAXBDataBindingCallback(objContext.getMethod(),
177: DataBindingCallback.Mode.PARTS, null));
178:
179: Object[] params = objContext.getMessageObjects();
180: assertNotNull(params);
181: assertNull(objContext.getReturn());
182: assertEquals(1, params.length);
183: assertEquals(data, (String) params[0]);
184: }
185:
186: // Test the greetMe Operation out message
187: public void testUnmarshalBareOutputMessage() throws Exception {
188: xmlContext.put(ObjectMessageContext.MESSAGE_INPUT, true);
189: InputStream in = getClass().getResourceAsStream(
190: "resources/GreetMeBareResp.xml");
191: assertNotNull(binding.getMessageFactory());
192: XMLMessage xmlMessage = binding.getMessageFactory()
193: .createMessage(in);
194: xmlContext.setMessage(xmlMessage);
195:
196: //testDocLitBare method has a IN parameter
197: objContext.setMethod(ClassUtils.getMethod(Greeter.class,
198: "greetMe"));
199: objContext.setMessageObjects(new Object[1]);
200: binding.unmarshal(xmlContext, objContext,
201: new JAXBDataBindingCallback(objContext.getMethod(),
202: DataBindingCallback.Mode.PARTS, null));
203:
204: Object[] params = objContext.getMessageObjects();
205: assertNotNull(params);
206: assertNotNull(objContext.getReturn());
207: assertEquals("TestXMLOutputMessage", (String) objContext
208: .getReturn());
209: }
210:
211: private XMLBindingImpl getBindingImpl(Bus bus,
212: EndpointReferenceType ref) {
213: return new XMLBindingImpl(bus, ref, false);
214: }
215: }
|