001: package org.objectweb.celtix.bus.bindings.soap;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.InputStreamReader;
007: import java.lang.reflect.Method;
008: import java.net.URL;
009: import java.util.concurrent.Executor;
010: import java.util.concurrent.Future;
011:
012: import javax.wsdl.Port;
013: import javax.wsdl.WSDLException;
014: import javax.xml.namespace.QName;
015: import javax.xml.soap.MessageFactory;
016: import javax.xml.soap.SOAPMessage;
017: import javax.xml.ws.handler.MessageContext;
018: import javax.xml.ws.handler.soap.SOAPMessageContext;
019:
020: import junit.framework.TestCase;
021:
022: import org.objectweb.celtix.Bus;
023: import org.objectweb.celtix.bindings.DataBindingCallback;
024: import org.objectweb.celtix.bindings.ResponseCallback;
025: import org.objectweb.celtix.bus.bindings.TestInputStreamContext;
026: import org.objectweb.celtix.bus.bindings.TestOutputStreamContext;
027: import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback;
028: import org.objectweb.celtix.context.GenericMessageContext;
029: import org.objectweb.celtix.context.InputStreamMessageContext;
030: import org.objectweb.celtix.context.ObjectMessageContext;
031: import org.objectweb.celtix.context.OutputStreamMessageContext;
032: import org.objectweb.celtix.transports.ClientTransport;
033: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
034: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
035: import org.objectweb.hello_world_soap_http.Greeter;
036:
037: public class SOAPClientBindingTest extends TestCase {
038: Bus bus;
039: EndpointReferenceType epr;
040:
041: public SOAPClientBindingTest(String arg0) {
042: super (arg0);
043: }
044:
045: public static void main(String[] args) {
046: junit.textui.TestRunner.run(SOAPClientBindingTest.class);
047: }
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: bus = Bus.init();
052: epr = new EndpointReferenceType();
053:
054: URL wsdlUrl = getClass().getResource("/wsdl/hello_world.wsdl");
055: QName serviceName = new QName(
056: "http://objectweb.org/hello_world_soap_http",
057: "SOAPService");
058: epr = EndpointReferenceUtils.getEndpointReference(wsdlUrl,
059: serviceName, "SoapPort");
060: }
061:
062: public void testGetBinding() throws Exception {
063: SOAPClientBinding clientBinding = new SOAPClientBinding(bus,
064: epr);
065: assertNotNull(clientBinding.getBinding());
066: }
067:
068: public void testCreateObjectContext() throws Exception {
069: SOAPClientBinding clientBinding = new SOAPClientBinding(bus,
070: epr);
071: assertNotNull(clientBinding.createObjectContext());
072: }
073:
074: public void testInvokeOneWay() throws Exception {
075: TestClientBinding clientBinding = new TestClientBinding(bus,
076: epr);
077: ObjectMessageContext objContext = clientBinding
078: .createObjectContext();
079: assertNotNull(objContext);
080: Method method = SOAPMessageUtil.getMethod(Greeter.class,
081: "greetMe");
082:
083: String arg0 = new String("TestSOAPInputPMessage");
084: objContext.setMessageObjects(arg0);
085:
086: clientBinding.invokeOneWay(objContext,
087: new JAXBDataBindingCallback(method,
088: DataBindingCallback.Mode.PARTS, null));
089: }
090:
091: public void testhasFault() throws Exception {
092: TestClientBinding clientBinding = new TestClientBinding(bus,
093: epr);
094: SOAPMessageContext soapCtx = new SOAPMessageContextImpl(
095: new GenericMessageContext());
096:
097: InputStream is = getClass().getResourceAsStream(
098: "resources/NoSuchCodeDocLiteral.xml");
099: MessageFactory msgFactory = MessageFactory.newInstance();
100: SOAPMessage faultMsg = msgFactory.createMessage(null, is);
101: soapCtx.setMessage(faultMsg);
102: assertTrue(clientBinding.getBindingImpl().hasFault(soapCtx));
103:
104: is = getClass().getResourceAsStream(
105: "resources/GreetMeDocLiteralReq.xml");
106: faultMsg = msgFactory.createMessage(null, is);
107: soapCtx.setMessage(faultMsg);
108: assertFalse(clientBinding.getBindingImpl().hasFault(soapCtx));
109: }
110:
111: public void testRead() throws Exception {
112: TestClientBinding clientBinding = new TestClientBinding(bus,
113: epr);
114: InputStream is = getClass().getResourceAsStream(
115: "resources/GreetMeDocLiteralResp.xml");
116: TestInputStreamContext tisc = new TestInputStreamContext(null);
117: tisc.setInputStream(is);
118:
119: SOAPMessageContext soapCtx = new SOAPMessageContextImpl(
120: new GenericMessageContext());
121: clientBinding.getBindingImpl().read(tisc, soapCtx);
122: assertNotNull(soapCtx.getMessage());
123: }
124:
125: public void testWrite() throws Exception {
126: TestClientBinding clientBinding = new TestClientBinding(bus,
127: epr);
128:
129: InputStream is = getClass().getResourceAsStream(
130: "resources/GreetMeDocLiteralReq.xml");
131:
132: MessageFactory msgFactory = MessageFactory.newInstance();
133: SOAPMessage greetMeMsg = msgFactory.createMessage(null, is);
134: is.close();
135:
136: BufferedReader br = new BufferedReader(new InputStreamReader(
137: getClass().getResourceAsStream(
138: "resources/GreetMeDocLiteralReq.xml")));
139:
140: SOAPMessageContext soapCtx = new SOAPMessageContextImpl(
141: new GenericMessageContext());
142: soapCtx.setMessage(greetMeMsg);
143:
144: TestOutputStreamContext tosc = new TestOutputStreamContext(
145: null, soapCtx);
146: clientBinding.getBindingImpl().write(soapCtx, tosc);
147:
148: byte[] bArray = tosc.getOutputStreamBytes();
149: assertEquals(br.readLine(), (new String(bArray)).trim());
150: }
151:
152: class TestClientBinding extends SOAPClientBinding {
153:
154: public TestClientBinding(Bus b, EndpointReferenceType ref)
155: throws WSDLException, IOException {
156: super (b, ref);
157: }
158:
159: protected ClientTransport createTransport(
160: EndpointReferenceType ref) throws WSDLException,
161: IOException {
162: // REVISIT: non-null response callback
163: return new TestClientTransport(bus, ref);
164: }
165:
166: }
167:
168: class TestClientTransport implements ClientTransport {
169:
170: public TestClientTransport(Bus b, EndpointReferenceType ref) {
171: }
172:
173: public EndpointReferenceType getTargetEndpoint() {
174: return null;
175: }
176:
177: public EndpointReferenceType getDecoupledEndpoint()
178: throws IOException {
179: return null;
180: }
181:
182: public Port getPort() {
183: return null;
184: }
185:
186: public OutputStreamMessageContext createOutputStreamContext(
187: MessageContext context) throws IOException {
188: return new TestOutputStreamContext(null, context);
189: }
190:
191: public void finalPrepareOutputStreamContext(
192: OutputStreamMessageContext context) throws IOException {
193: }
194:
195: public void invokeOneway(OutputStreamMessageContext context)
196: throws IOException {
197: //nothing to do
198: }
199:
200: public InputStreamMessageContext invoke(
201: OutputStreamMessageContext context) throws IOException {
202: return context.getCorrespondingInputStreamContext();
203: }
204:
205: public Future<InputStreamMessageContext> invokeAsync(
206: OutputStreamMessageContext context, Executor ex)
207: throws IOException {
208: return null;
209: }
210:
211: public ResponseCallback getResponseCallback() {
212: return null;
213: }
214:
215: public void shutdown() {
216: //nothing to do
217: }
218: }
219:
220: }
|