01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.jaxws.dispatch;
20:
21: import java.io.StringWriter;
22:
23: import javax.xml.bind.JAXBContext;
24: import javax.xml.bind.util.JAXBSource;
25: import javax.xml.namespace.QName;
26: import javax.xml.transform.Result;
27: import javax.xml.transform.Source;
28: import javax.xml.transform.Transformer;
29: import javax.xml.transform.TransformerFactory;
30: import javax.xml.transform.stream.StreamResult;
31: import javax.xml.ws.Dispatch;
32: import javax.xml.ws.Service;
33:
34: import junit.framework.TestCase;
35: import org.test.dispatch.jaxbsource.Invoke;
36: import org.test.dispatch.jaxbsource.ObjectFactory;
37: import org.apache.axis2.jaxws.TestLogger;
38:
39: /*
40: * This is a test case for Invoking Dispatch with a JAXBSource.
41: * test uses JAXB Objects from org.test.dispatch.jaxbsource package, create a request of JAXBSource type
42: * and invokes the service endpoint and reads the response of type Source. Assert failure if response not received.
43: */
44:
45: public class JAXBSourceDispatch extends TestCase {
46: /**
47: * Invoke a sync Dispatch<JAXBSource> in PAYLOAD mode
48: */
49:
50: private String url = "http://localhost:8080/axis2/services/SourceProviderService";
51: private QName serviceName = new QName("http://ws.apache.org/axis2",
52: "SourceProviderService");
53: private QName portName = new QName("http://ws.apache.org/axis2",
54: "SimpleProviderServiceSOAP11port0");
55:
56: public void testJAXBSourceSyncPayloadMode() throws Exception {
57: TestLogger.logger
58: .debug("---------------------------------------");
59: TestLogger.logger.debug("test: " + getName());
60: try {
61: // Initialize the JAX-WS client artifacts
62: Service svc = Service.create(serviceName);
63: svc.addPort(portName, null, url);
64: Dispatch<Source> dispatch = svc.createDispatch(portName,
65: Source.class, Service.Mode.PAYLOAD);
66:
67: //Create JAXBContext and JAXBSource here.
68: ObjectFactory factory = new ObjectFactory();
69: Invoke invokeObj = factory.createInvoke();
70: invokeObj.setInvokeStr("Some Request");
71: JAXBContext ctx = JAXBContext
72: .newInstance("org.test.dispatch.jaxbsource");
73:
74: JAXBSource jbSrc = new JAXBSource(ctx.createMarshaller(),
75: invokeObj);
76: // Invoke the Dispatch
77: TestLogger.logger.debug(">> Invoking sync Dispatch");
78: //Invoke Server endpoint and read response
79: Source response = dispatch.invoke(jbSrc);
80:
81: assertNotNull("dispatch invoke returned null", response);
82: //Print the response as string.
83: StringWriter writer = new StringWriter();
84: Transformer t = TransformerFactory.newInstance()
85: .newTransformer();
86: Result result = new StreamResult(writer);
87: t.transform(response, result);
88:
89: TestLogger.logger.debug("Response On Client: \n"
90: + writer.getBuffer().toString());
91: TestLogger.logger
92: .debug("---------------------------------------");
93: } catch (Exception e) {
94: e.printStackTrace();
95: }
96:
97: }
98:
99: }
|