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.provider;
020:
021: import org.apache.axis2.jaxws.TestLogger;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.InputStream;
027: import java.io.StringWriter;
028:
029: import javax.xml.namespace.QName;
030: import javax.xml.soap.SOAPFault;
031: import javax.xml.transform.Result;
032: import javax.xml.transform.Source;
033: import javax.xml.transform.Transformer;
034: import javax.xml.transform.TransformerFactory;
035: import javax.xml.transform.stream.StreamResult;
036: import javax.xml.transform.stream.StreamSource;
037: import javax.xml.ws.BindingProvider;
038: import javax.xml.ws.Dispatch;
039: import javax.xml.ws.Service;
040: import javax.xml.ws.soap.SOAPFaultException;
041:
042: public class SourceProviderTests extends ProviderTestCase {
043:
044: private String endpointUrl = "http://localhost:8080/axis2/services/SourceProviderService";
045: private QName serviceName = new QName("http://ws.apache.org/axis2",
046: "SourceProviderService");
047: private String xmlDir = "xml";
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: }
052:
053: protected void tearDown() throws Exception {
054: super .tearDown();
055: }
056:
057: public SourceProviderTests(String name) {
058: super (name);
059: }
060:
061: private Dispatch<Source> getDispatch() {
062: Service svc = Service.create(serviceName);
063: svc.addPort(portName, null, endpointUrl);
064:
065: Dispatch<Source> dispatch = svc.createDispatch(portName,
066: Source.class, Service.Mode.PAYLOAD);
067:
068: // Force soap action because we are passing junk over the wire
069: dispatch.getRequestContext().put(
070: BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
071: dispatch.getRequestContext().put(
072: BindingProvider.SOAPACTION_URI_PROPERTY, "test");
073:
074: return dispatch;
075:
076: }
077:
078: private Source getSource(String text) {
079: if (text == null) {
080: return null;
081: } else {
082: ByteArrayInputStream stream = new ByteArrayInputStream(text
083: .getBytes());
084: return new StreamSource((InputStream) stream);
085: }
086:
087: }
088:
089: private String getString(Source source) throws Exception {
090: if (source == null) {
091: return null;
092: }
093: StringWriter writer = new StringWriter();
094: Transformer t = TransformerFactory.newInstance()
095: .newTransformer();
096: Result result = new StreamResult(writer);
097: t.transform(source, result);
098: return writer.getBuffer().toString();
099:
100: }
101:
102: public void testNormal() throws Exception {
103: TestLogger.logger
104: .debug("---------------------------------------");
105: TestLogger.logger.debug("test: " + getName());
106:
107: Dispatch<Source> dispatch = getDispatch();
108:
109: String request = "<test>hello world</test>";
110: Source requestSource = getSource(request);
111: Source responseSource = dispatch.invoke(requestSource);
112: String response = getString(responseSource);
113:
114: assertTrue(response.contains(request));
115: }
116:
117: public void testEmptyString() throws Exception {
118: TestLogger.logger
119: .debug("---------------------------------------");
120: TestLogger.logger.debug("test: " + getName());
121:
122: Dispatch<Source> dispatch = getDispatch();
123:
124: String request = "";
125: Source requestSource = getSource(request);
126: Source responseSource = dispatch.invoke(requestSource);
127: String response = getString(responseSource);
128:
129: // The current belief is that this should return a null indicating
130: // the nothing is echo'ed
131: assertTrue(response == null);
132:
133: //assertTrue(request.equals(response));
134: }
135:
136: public void testNullSource() throws Exception {
137: TestLogger.logger
138: .debug("---------------------------------------");
139: TestLogger.logger.debug("test: " + getName());
140:
141: Dispatch<Source> dispatch = getDispatch();
142:
143: Source responseSource = dispatch.invoke(null);
144: String response = getString(responseSource);
145:
146: // The current belief is that this should return a null indicating
147: // the nothing is echo'ed
148: assertTrue(response == null);
149: }
150:
151: public void testEmptySource() throws Exception {
152: TestLogger.logger
153: .debug("---------------------------------------");
154: TestLogger.logger.debug("test: " + getName());
155:
156: Dispatch<Source> dispatch = getDispatch();
157:
158: Source responseSource = dispatch.invoke(new StreamSource());
159: String response = getString(responseSource);
160:
161: // The current belief is that this should return a null indicating
162: // the nothing is echo'ed
163: assertTrue(response == null);
164: }
165:
166: public void testNonNullString() throws Exception {
167: TestLogger.logger
168: .debug("---------------------------------------");
169: TestLogger.logger.debug("test: " + getName());
170:
171: Dispatch<Source> dispatch = getDispatch();
172:
173: String request = "mixedContent";
174: Source requestSource = getSource(request);
175: Source responseSource = dispatch.invoke(requestSource);
176: String response = getString(responseSource);
177:
178: // The current implementation does not send the mixedContent over the wire, so the
179: // expectation is that the echo'd response is null
180: assertTrue(response == null);
181: }
182:
183: public void testCommentString() throws Exception {
184: TestLogger.logger
185: .debug("---------------------------------------");
186: TestLogger.logger.debug("test: " + getName());
187:
188: Dispatch<Source> dispatch = getDispatch();
189:
190: String request = "<!--comment-->";
191: Source requestSource = getSource(request);
192: Source responseSource = dispatch.invoke(requestSource);
193: String response = getString(responseSource);
194: // The current implementation does not send the comment over the wire, so the
195: // expectation is that the echo'd response is null
196: assertTrue(response == null);
197: }
198:
199: public void testTwoElementsString() throws Exception {
200: TestLogger.logger
201: .debug("---------------------------------------");
202: TestLogger.logger.debug("test: " + getName());
203:
204: Dispatch<Source> dispatch = getDispatch();
205:
206: String request = "<a>hello</a><b>world</b>";
207: Source requestSource = getSource(request);
208: Source responseSource = dispatch.invoke(requestSource);
209: String response = getString(responseSource);
210:
211: // The current implementatin only sends the first element
212: // So the echo'd response is just the first one.
213: assertTrue(response.contains("<a>hello</a>"));
214: }
215:
216: public void testTwoElementsAndMixedContentString() throws Exception {
217: TestLogger.logger
218: .debug("---------------------------------------");
219: TestLogger.logger.debug("test: " + getName());
220:
221: Dispatch<Source> dispatch = getDispatch();
222:
223: String request = "mixed1<a>hello</a>mixed2<b>world</b>mixed3";
224: Source requestSource = getSource(request);
225: Source responseSource = dispatch.invoke(requestSource);
226: String response = getString(responseSource);
227: // The current implementation only sends the first element.
228: // The mixed content (mixed1) interferes and thus nothing is sent.
229: assertTrue(response == null);
230: }
231:
232: public void testException() throws Exception {
233: TestLogger.logger
234: .debug("---------------------------------------");
235: TestLogger.logger.debug("test: " + getName());
236:
237: Dispatch<Source> dispatch = getDispatch();
238:
239: String request = "<test>throwWebServiceException</test>";
240: try {
241: Source requestSource = getSource(request);
242: Source responseSource = dispatch.invoke(requestSource);
243: String response = getString(responseSource);
244: fail("Expected Exception");
245: } catch (SOAPFaultException e) {
246: SOAPFault sf = e.getFault();
247: assertTrue(sf.getFaultString().equals("provider"));
248: }
249: }
250:
251: public void testProviderSource() {
252: try {
253: String resourceDir = new File(providerResourceDir, xmlDir)
254: .getAbsolutePath();
255: String fileName = resourceDir + File.separator + "web.xml";
256:
257: File file = new File(fileName);
258: InputStream inputStream = new FileInputStream(file);
259: StreamSource xmlStreamSource = new StreamSource(inputStream);
260:
261: Service svc = Service.create(serviceName);
262: svc.addPort(portName, null, endpointUrl);
263: Dispatch<Source> dispatch = svc.createDispatch(portName,
264: Source.class, null);
265: TestLogger.logger
266: .debug(">> Invoking Source Provider Dispatch");
267: Source response = dispatch.invoke(xmlStreamSource);
268:
269: TestLogger.logger.debug(">> Response ["
270: + response.toString() + "]");
271:
272: } catch (Exception e) {
273: e.printStackTrace();
274: fail("Caught exception " + e);
275: }
276:
277: }
278: }
|