001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.sample;
020:
021: import javax.xml.namespace.QName;
022: import javax.xml.ws.BindingProvider;
023: import javax.xml.ws.Dispatch;
024: import javax.xml.ws.Service;
025: import javax.xml.ws.WebServiceException;
026:
027: import org.apache.axis2.jaxws.dispatch.DispatchTestConstants;
028: import org.apache.axis2.jaxws.sample.dlwmin.sei.Greeter;
029: import org.apache.axis2.jaxws.sample.dlwmin.sei.TestException;
030: import org.apache.axis2.jaxws.sample.dlwmin.sei.TestException2;
031: import org.apache.axis2.jaxws.sample.dlwmin.sei.TestException3;
032: import org.apache.axis2.jaxws.sample.dlwmin.types.TestBean;
033: import org.apache.axis2.jaxws.TestLogger;
034:
035: import junit.framework.TestCase;
036:
037: public class DLWMinTests extends TestCase {
038:
039: private static final String NAMESPACE = "http://apache.org/axis2/jaxws/sample/dlwmin";
040: private static final QName QNAME_SERVICE = new QName(NAMESPACE,
041: "GreeterService");
042: private static final QName QNAME_PORT = new QName(NAMESPACE,
043: "GreeterPort");
044: private static final String URL_ENDPOINT = "http://localhost:8080/axis2/services/GreeterService";
045:
046: private Greeter getProxy(String action) {
047: Service service = Service.create(QNAME_SERVICE);
048: Greeter proxy = service.getPort(QNAME_PORT, Greeter.class);
049: BindingProvider p = (BindingProvider) proxy;
050: p.getRequestContext().put(
051: BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
052: p.getRequestContext().put(
053: BindingProvider.SOAPACTION_URI_PROPERTY, action);
054: p.getRequestContext()
055: .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
056: URL_ENDPOINT);
057: return proxy;
058: }
059:
060: private Dispatch<String> getDispatch(String action) {
061: // Get a dispatch
062: Service svc = Service.create(QNAME_SERVICE);
063: svc.addPort(QNAME_PORT, null, URL_ENDPOINT);
064: Dispatch<String> dispatch = svc.createDispatch(QNAME_PORT,
065: String.class, Service.Mode.PAYLOAD);
066: BindingProvider p = (BindingProvider) dispatch;
067: p.getRequestContext().put(
068: BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
069: p.getRequestContext().put(
070: BindingProvider.SOAPACTION_URI_PROPERTY, action);
071: return dispatch;
072: }
073:
074: /**
075: * Test simple greetMe method
076: * with style doc/lit wrapped without the presence of wrapper classes.
077: */
078: public void testGreetMe() {
079:
080: Greeter proxy = getProxy("greetMe");
081:
082: String me = "Scheu";
083: String response = proxy.greetMe(me);
084: assertTrue("Hello Scheu".equals(response));
085: }
086:
087: /**
088: * Test simple greetMe method with dispatch
089: * with style doc/lit wrapped without the presence of wrapper classes.
090: */
091: public void testGreetMe_Dispatch() {
092:
093: Dispatch<String> dispatch = getDispatch("greetMe");
094:
095: String request = "<pre:greetMe xmlns:pre='http://apache.org/axis2/jaxws/sample/dlwmin'>"
096: + "<pre:requestType>Scheu</pre:requestType>"
097: + "</pre:greetMe>";
098: TestLogger.logger.debug("Doc/Lit Wrapped Minimal Request ="
099: + request);
100: String response = dispatch.invoke(request);
101: TestLogger.logger.debug("Doc/Lit Wrapped Minimal Response ="
102: + response);
103:
104: assertTrue(response.contains("Hello Scheu"));
105: assertTrue(response.contains("dlwmin:greetMeResponse"));
106: assertTrue(response.contains(":responseType")
107: || response.contains("responseType xmlns=")); // assert that response type is a qualified element
108: assertTrue(!response.contains("xsi:type")); // xsi:type should not be used
109: }
110:
111: /**
112: * Test simple greetMe method
113: * with style doc/lit wrapped without the presence of wrapper classes.
114: */
115: public void testUnqualified() {
116:
117: Greeter proxy = getProxy("testUnqualified");
118:
119: String request = "hello world";
120: String response = proxy.testUnqualified(request);
121: assertTrue("hello world".equals(response));
122: }
123:
124: /**
125: * Test simple greetMe method with dispatch
126: * with style doc/lit wrapped without the presence of wrapper classes.
127: */
128: public void testUnqualified_Dispatch() {
129:
130: Dispatch<String> dispatch = getDispatch("testUnqualified");
131:
132: String request = "<pre:unqualifiedTestResponse xmlns:pre='http://apache.org/axis2/jaxws/sample/dlwmin'>"
133: + "<unqualifiedRequest>hello world</unqualifiedRequest>"
134: + "</pre:unqualifiedTestResponse>";
135: TestLogger.logger.debug("Doc/Lit Wrapped Minimal Request ="
136: + request);
137: String response = dispatch.invoke(request);
138: TestLogger.logger.debug("Doc/Lit Wrapped Minimal Response ="
139: + response);
140:
141: assertTrue(response.contains("hello world"));
142: assertTrue(response.contains("dlwmin:testUnqualifiedResponse"));
143: assertTrue(response.contains("<unqualifiedResponse")); // assert that the child element is an uqualified element
144: assertTrue(!response.contains("xsi:type")); // xsi:type should not be used
145: }
146:
147: /**
148: * Test echo with complexType
149: */
150: public void testProcess_Echo() throws Exception {
151:
152: Greeter proxy = getProxy("process");
153:
154: TestBean request = new TestBean();
155: request.setData1("hello world");
156: request.setData2(10);
157: TestBean response = proxy.process(0, request);
158: assertTrue(response != null);
159: assertTrue(response.getData1().equals("hello world"));
160: assertTrue(response.getData2() == 10);
161: }
162:
163: /**
164: * Test throwing checked exception w/o a JAXB Bean
165: */
166: public void testProcess_CheckException() throws Exception {
167:
168: Greeter proxy = getProxy("process");
169:
170: TestBean request = new TestBean();
171: request.setData1("hello world");
172: request.setData2(10);
173: try {
174: TestBean response = proxy.process(1, request);
175: fail("Expected TestException thrown");
176: } catch (WebServiceException wse) {
177: // Currently there is no support if the fault bean is missing
178: assertTrue(wse.getMessage().contains(
179: "User fault processing is not supported"));
180: } catch (TestException te) {
181: assertTrue(te.getMessage().equals("TestException thrown"));
182: assertTrue(te.getFlag() == 123);
183: } catch (Exception e) {
184: fail("Expected TestException thrown but found "
185: + e.getClass());
186: }
187: }
188:
189: /**
190: * Test throwing checked exception that has a JAXB Bean
191: */
192: public void testProcess_CheckException2() throws Exception {
193:
194: Greeter proxy = getProxy("process");
195:
196: TestBean request = new TestBean();
197: request.setData1("hello world");
198: request.setData2(10);
199: try {
200: TestBean response = proxy.process(4, request);
201: fail("Expected TestException2 thrown");
202: } catch (TestException2 te) {
203: assertTrue(te.getMessage().equals("TestException2 thrown"));
204: assertTrue(te.getFlag() == 456);
205: } catch (Exception e) {
206: fail("Expected TestException2 thrown but found "
207: + e.getClass());
208: }
209: }
210:
211: /**
212: * Test throwing checked exception that is a compliant JAXWS exception
213: */
214: public void testProcess_CheckException3() throws Exception {
215:
216: Greeter proxy = getProxy("process");
217:
218: TestBean request = new TestBean();
219: request.setData1("hello world");
220: request.setData2(10);
221: try {
222: TestBean response = proxy.process(5, request);
223: fail("Expected TestException3 thrown");
224: } catch (TestException3 te) {
225: assertTrue(te.getMessage().equals("TestException3 thrown"));
226: assertTrue(te.getFaultInfo().getFlag() == 789);
227: } catch (Exception e) {
228: fail("Expected TestException3 thrown but found "
229: + e.getClass());
230: }
231: }
232:
233: /**
234: * Test throwing WebServiceException
235: */
236: public void testProcess_WebServiceException() throws Exception {
237:
238: Greeter proxy = getProxy("process");
239:
240: TestBean request = new TestBean();
241: request.setData1("hello world");
242: request.setData2(10);
243: try {
244: TestBean response = proxy.process(2, request);
245: fail("Expected WebServiceException thrown");
246: } catch (WebServiceException wse) {
247: assertTrue(wse.getMessage().equals(
248: "WebServiceException thrown"));
249: } catch (Exception e) {
250: fail("Expected WebServiceException thrown but found "
251: + e.getClass());
252: }
253: }
254:
255: /**
256: * Test throwing NPE
257: */
258: public void testProcess_NPE() throws Exception {
259:
260: Greeter proxy = getProxy("process");
261:
262: TestBean request = new TestBean();
263: request.setData1("hello world");
264: request.setData2(10);
265: try {
266: TestBean response = proxy.process(3, request);
267: fail("Expected NullPointerException thrown");
268: } catch (WebServiceException wse) {
269: assertTrue(wse.getMessage().equals("NPE thrown"));
270: } catch (Exception e) {
271: fail("Expected NullPointerException thrown but found "
272: + e.getClass());
273: }
274: }
275:
276: }
|