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: */package org.apache.cxf.test;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.io.OutputStream;
027: import java.io.Reader;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import javax.wsdl.Definition;
032: import javax.wsdl.WSDLException;
033: import javax.wsdl.factory.WSDLFactory;
034: import javax.wsdl.xml.WSDLWriter;
035:
036: import org.w3c.dom.Document;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039:
040: import org.xml.sax.SAXParseException;
041:
042: import org.apache.cxf.Bus;
043: import org.apache.cxf.BusException;
044: import org.apache.cxf.BusFactory;
045: import org.apache.cxf.endpoint.Server;
046: import org.apache.cxf.helpers.DOMUtils;
047: import org.apache.cxf.helpers.IOUtils;
048: import org.apache.cxf.message.Message;
049: import org.apache.cxf.message.MessageImpl;
050: import org.apache.cxf.service.Service;
051: import org.apache.cxf.service.model.EndpointInfo;
052: import org.apache.cxf.transport.Conduit;
053: import org.apache.cxf.transport.ConduitInitiator;
054: import org.apache.cxf.transport.ConduitInitiatorManager;
055: import org.apache.cxf.transport.MessageObserver;
056: import org.apache.cxf.wsdl11.ServiceWSDLBuilder;
057: import org.junit.After;
058: import org.junit.Assert;
059: import org.junit.Before;
060:
061: /**
062: * A basic test case meant for helping users unit test their services.
063: */
064: public class AbstractCXFTest extends Assert {
065:
066: private static String basedirPath;
067:
068: protected Bus bus;
069: /**
070: * Namespaces for the XPath expressions.
071: */
072: private Map<String, String> namespaces = new HashMap<String, String>();
073:
074: @Before
075: public void setUpBus() throws Exception {
076: if (bus == null) {
077: bus = createBus();
078:
079: addNamespace("s",
080: "http://schemas.xmlsoap.org/soap/envelope/");
081: addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
082: addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
083: addNamespace("wsdlsoap",
084: "http://schemas.xmlsoap.org/wsdl/soap/");
085: addNamespace("soap", "http://schemas.xmlsoap.org/soap/");
086: addNamespace("soap12env",
087: "http://www.w3.org/2003/05/soap-envelope");
088: addNamespace("xml", "http://www.w3.org/XML/1998/namespace");
089: }
090: }
091:
092: public Bus getBus() {
093: return bus;
094: }
095:
096: @After
097: public void shutdownBus() {
098: if (bus != null) {
099: bus.shutdown(false);
100: bus = null;
101: }
102: BusFactory.setDefaultBus(null);
103: }
104:
105: protected Bus createBus() throws BusException {
106: return BusFactory.newInstance().createBus();
107: }
108:
109: protected byte[] invokeBytes(String address, String transport,
110: String message) throws Exception {
111: EndpointInfo ei = new EndpointInfo(null,
112: "http://schemas.xmlsoap.org/soap/http");
113: ei.setAddress(address);
114:
115: ConduitInitiatorManager conduitMgr = getBus().getExtension(
116: ConduitInitiatorManager.class);
117: ConduitInitiator conduitInit = conduitMgr
118: .getConduitInitiator(transport);
119: Conduit conduit = conduitInit.getConduit(ei);
120:
121: TestMessageObserver obs = new TestMessageObserver();
122: conduit.setMessageObserver(obs);
123:
124: Message m = new MessageImpl();
125: conduit.prepare(m);
126:
127: OutputStream os = m.getContent(OutputStream.class);
128: InputStream is = getResourceAsStream(message);
129: if (is == null) {
130: throw new RuntimeException("Could not find resource "
131: + message);
132: }
133:
134: IOUtils.copy(is, os);
135:
136: // TODO: shouldn't have to do this. IO caching needs cleaning
137: // up or possibly removal...
138: os.flush();
139: is.close();
140: os.close();
141:
142: byte[] bs = obs.getResponseStream().toByteArray();
143:
144: return bs;
145: }
146:
147: protected Node invoke(String address, String transport,
148: String message) throws Exception {
149: byte[] bs = invokeBytes(address, transport, message);
150:
151: ByteArrayInputStream input = new ByteArrayInputStream(bs);
152: try {
153: return DOMUtils.readXml(input);
154: } catch (SAXParseException e) {
155: throw new IllegalStateException(
156: "Could not parse message:\n" + new String(bs));
157: }
158: }
159:
160: /**
161: * Assert that the following XPath query selects one or more nodes.
162: *
163: * @param xpath
164: * @throws Exception
165: */
166: public NodeList assertValid(String xpath, Node node)
167: throws Exception {
168: return XPathAssert.assertValid(xpath, node, namespaces);
169: }
170:
171: /**
172: * Assert that the following XPath query selects no nodes.
173: *
174: * @param xpath
175: */
176: public NodeList assertInvalid(String xpath, Node node)
177: throws Exception {
178: return XPathAssert.assertInvalid(xpath, node, namespaces);
179: }
180:
181: /**
182: * Asser that the text of the xpath node retrieved is equal to the value
183: * specified.
184: *
185: * @param xpath
186: * @param value
187: * @param node
188: */
189: public void assertXPathEquals(String xpath, String value, Node node)
190: throws Exception {
191: XPathAssert.assertXPathEquals(xpath, value, node, namespaces);
192: }
193:
194: public void assertNoFault(Node node) throws Exception {
195: XPathAssert.assertNoFault(node);
196: }
197:
198: /**
199: * Add a namespace that will be used for XPath expressions.
200: *
201: * @param ns Namespace name.
202: * @param uri The namespace uri.
203: */
204: public void addNamespace(String ns, String uri) {
205: namespaces.put(ns, uri);
206: }
207:
208: public Map<String, String> getNamespaces() {
209: return namespaces;
210: }
211:
212: protected InputStream getResourceAsStream(String resource) {
213: return getClass().getResourceAsStream(resource);
214: }
215:
216: protected Reader getResourceAsReader(String resource) {
217: return new InputStreamReader(getResourceAsStream(resource));
218: }
219:
220: public File getTestFile(String relativePath) {
221: return new File(getBasedir(), relativePath);
222: }
223:
224: public static String getBasedir() {
225: if (basedirPath != null) {
226: return basedirPath;
227: }
228:
229: basedirPath = System.getProperty("basedir");
230:
231: if (basedirPath == null) {
232: basedirPath = new File("").getAbsolutePath();
233: }
234:
235: return basedirPath;
236: }
237:
238: protected Document getWSDLDocument(Server server)
239: throws WSDLException {
240: Service service = server.getEndpoint().getService();
241:
242: ServiceWSDLBuilder wsdlBuilder = new ServiceWSDLBuilder(bus,
243: service.getServiceInfos().get(0));
244: wsdlBuilder.setUseSchemaImports(false);
245: Definition definition = wsdlBuilder.build();
246: WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
247:
248: return writer.getDocument(definition);
249: }
250:
251: public static class TestMessageObserver implements MessageObserver {
252: ByteArrayOutputStream response = new ByteArrayOutputStream();
253: boolean written;
254: String contentType;
255:
256: public ByteArrayOutputStream getResponseStream()
257: throws Exception {
258: synchronized (this ) {
259: if (!written) {
260: wait(1000000000);
261: }
262: }
263: return response;
264: }
265:
266: public String getResponseContentType() {
267: return contentType;
268: }
269:
270: public void onMessage(Message message) {
271: try {
272: contentType = (String) message
273: .get(Message.CONTENT_TYPE);
274: InputStream is = message.getContent(InputStream.class);
275: IOUtils.copy(is, response);
276:
277: is.close();
278: response.close();
279: } catch (IOException e) {
280: e.printStackTrace();
281: fail();
282: } finally {
283: synchronized (this ) {
284: written = true;
285: notifyAll();
286: }
287: }
288: }
289: }
290: }
|