1 package org.codehaus.ivory.plexus;
2
3 /*
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution, if
22 * any, must include the following acknowlegement:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowlegement may appear in the software itself,
26 * if and wherever such third-party acknowlegements normally appear.
27 *
28 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
29 * Foundation" must not be used to endorse or promote products derived
30 * from this software without prior written permission. For written
31 * permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache"
34 * nor may "Apache" appear in their names without prior written
35 * permission of the Apache Group.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.IOException;
58 import java.io.InputStream;
59 import java.io.StringWriter;
60 import java.net.MalformedURLException;
61
62 import javax.servlet.ServletContext;
63
64 import org.apache.avalon.framework.service.ServiceManager;
65 import org.apache.axis.MessageContext;
66 import org.apache.axis.server.AxisServer;
67 import org.apache.axis.soap.SOAPConstants;
68 import org.apache.axis.transport.local.LocalTransport;
69 import org.apache.axis.utils.XMLUtils;
70 import org.codehaus.plexus.PlexusTestCase;
71 import org.codehaus.plexus.lifecycle.avalon.AvalonLifecycleHandler;
72 import org.codehaus.plexus.lifecycle.avalon.AvalonServiceManager;
73 import org.codehaus.plexus.servlet.PlexusServlet;
74 import org.codehaus.ivory.AxisService;
75 import org.w3c.dom.Document;
76 import org.xml.sax.SAXException;
77
78 import com.meterware.httpunit.HttpException;
79 import com.meterware.httpunit.HttpUnitOptions;
80 import com.meterware.httpunit.WebConversation;
81 import com.meterware.httpunit.WebRequest;
82 import com.meterware.httpunit.WebResponse;
83 import com.meterware.servletunit.InvocationContext;
84 import com.meterware.servletunit.ServletRunner;
85 import com.meterware.servletunit.ServletUnitClient;
86
87 /***
88 * A generic test-case for testing Ivory and other SOAP services for Plexus.
89 *
90 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
91 * @since May 4, 2003
92 */
93 public class IvoryTestCase extends PlexusTestCase
94 {
95 private ServletRunner sr;
96
97 private ServiceManager manager;
98
99 private String services = "http://localhost/services/";
100
101 private boolean verbose = false;
102
103 public final static String VERBOSE_KEY =
104 "IvoryTestCase.verbose";
105
106 public IvoryTestCase(String name)
107 {
108 super(name);
109 }
110
111 public void setUp() throws Exception
112 {
113 super.setUp();
114 setVerbose( Boolean.getBoolean( VERBOSE_KEY ) );
115
116 HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
117
118 manager = (ServiceManager) lookup( AvalonLifecycleHandler.SERVICE_MANAGER );
119
120 InputStream is =
121 getClass().getResourceAsStream(
122 "/org/codehaus/ivory/plexus/web.xml");
123
124 sr = new ServletRunner(is);
125
126 ServletUnitClient client = newClient();
127
128 // There must be a better way to do this.
129 InvocationContext ic =
130 client.newInvocation("http://localhost/servlet/AxisServlet");
131 ServletContext context =
132 ic.getServlet().getServletConfig().getServletContext();
133 context.setAttribute(PlexusServlet.SERVICE_MANAGER_KEY, manager);
134 }
135
136 protected ServletUnitClient newClient() throws Exception
137 {
138 return sr.newClient();
139 }
140
141 /***
142 * @return
143 */
144 public boolean isVerbose()
145 {
146 return verbose;
147 }
148
149 /***
150 * @param b
151 */
152 public void setVerbose(boolean b)
153 {
154 verbose = b;
155 }
156
157 /***
158 * Assert that the response contains a string.
159 * @param response
160 * @param searchfor
161 * @throws IOException
162 */
163 public void assertStringInBody(
164 String body,
165 String searchfor)
166 throws IOException
167 {
168 boolean found = body.indexOf(searchfor) >= 0;
169 if (!found)
170 {
171 String message;
172 message = "failed to find [" + searchfor + "].\n";
173
174 if ( isVerbose() )
175 message += "Body:\n" + body;
176
177 fail(message);
178 }
179 }
180
181 /***
182 * Assert that the response contains a string.
183 * @param response
184 * @param searchfor
185 * @throws IOException
186 */
187 public void assertStringInBody(
188 WebResponse response,
189 String searchfor)
190 throws IOException
191 {
192 String body = response.getText();
193 boolean found = body.indexOf(searchfor) >= 0;
194 if (!found)
195 {
196 String message;
197 message = "failed to find [" + searchfor + "]";
198
199 if ( isVerbose() )
200 message += "Body:\n" + body;
201
202 fail(message);
203 }
204 }
205
206 /***
207 * Assert that the response contains a string.
208 * @param response
209 * @param searchfor
210 * @param url
211 * @throws IOException
212 */
213 public void assertStringInBody(
214 WebResponse response,
215 String searchfor,
216 String url)
217 throws IOException
218 {
219 String body = response.getText();
220 boolean found = body.indexOf(searchfor) >= 0;
221 if (!found)
222 {
223 String message;
224 message = "failed to find [" + searchfor + "] at " + url;
225
226 if ( isVerbose() )
227 message += "Body:\n" + body;
228
229 fail(message);
230 }
231 }
232
233 /***
234 * Assert that a named string is in the request body of the.
235 *
236 * response to a request
237 * @param request what we ask
238 * @param searchfor string to look for
239 * @throws IOException when the fetch fails
240 * @throws org.xml.sax.SAXException
241 */
242 protected void assertStringInBody(WebRequest request, String searchfor)
243 throws IOException, org.xml.sax.SAXException
244 {
245 WebResponse response = makeRequest(request);
246 assertStringInBody(response, searchfor, request.getURL().toString());
247 }
248
249 /***
250 * Make a request in a new session.
251 * @param request request to make
252 * @return the response
253 * @throws IOException
254 * @throws SAXException
255 */
256 protected WebResponse makeRequest(WebRequest request)
257 throws IOException, SAXException
258 {
259 WebConversation session = new WebConversation();
260 WebResponse response = session.getResponse(request);
261 return response;
262 }
263
264 /***
265 * Assert that a string is not in a response.
266 * @param response
267 * @param searchfor
268 * @param url
269 * @throws IOException
270 */
271 protected void assertStringNotInBody(
272 String body,
273 String searchfor)
274 throws IOException
275 {
276 boolean found = body.indexOf(searchfor) >= 0;
277 if (found)
278 {
279 String message;
280 message = "unexpectedly found [" + searchfor + "].";
281
282 if ( isVerbose() )
283 message += "Body:\n" + body;
284
285 fail(message);
286 }
287 }
288
289 /***
290 * Assert that a string is not in a response.
291 * @param response
292 * @param searchfor
293 * @param url
294 * @throws IOException
295 */
296 protected void assertStringNotInBody(
297 WebResponse response,
298 String searchfor)
299 throws IOException
300 {
301 String body = response.getText();
302 boolean found = body.indexOf(searchfor) >= 0;
303 if (found)
304 {
305 String message;
306 message = "unexpectedly found [" + searchfor + "].";
307
308 if ( isVerbose() )
309 message += "Body:\n" + body;
310
311 fail(message);
312 }
313 }
314
315 /***
316 * Assert that a string is not in a response.
317 * @param response
318 * @param searchfor
319 * @param url
320 * @throws IOException
321 */
322 protected void assertStringNotInBody(
323 WebResponse response,
324 String searchfor,
325 String url)
326 throws IOException
327 {
328 String body = response.getText();
329 boolean found = body.indexOf(searchfor) >= 0;
330 if (found)
331 {
332 String message;
333 message = "unexpectedly found [" + searchfor + "] at " + url;
334
335 if ( isVerbose() )
336 message += "Body:\n" + body;
337
338 fail(message);
339 }
340
341 }
342
343 /***
344 * Assert that a string is not in the response to a request.
345 * @param request
346 * @param searchfor
347 * @throws IOException
348 * @throws org.xml.sax.SAXException
349 */
350 protected void assertStringNotInBody(WebRequest request, String searchfor)
351 throws IOException, org.xml.sax.SAXException
352 {
353 WebConversation session = new WebConversation();
354 WebResponse response = session.getResponse(request);
355 assertStringNotInBody(response, searchfor, request.getURL().toString());
356 }
357
358 protected void assertIsXml(String response)
359 {
360 if( response.indexOf("<?xml") != 0 )
361 {
362 fail( "Invalid XML:\n" + response );
363 }
364 }
365
366 /***
367 * Here we expect an errorCode other than 200, and look for it
368 * checking for text is omitted as it doesnt work. It would never work on
369 * java1.3, but one may have expected java1.4+ to have access to the
370 * error stream in responses. Clearly not.
371 * @param request
372 * @param errorCode
373 * @param errorText optional text string to search for
374 * @throws MalformedURLException
375 * @throws IOException
376 * @throws SAXException
377 */
378 protected void expectErrorCode(
379 WebRequest request,
380 int errorCode,
381 String errorText)
382 throws MalformedURLException, IOException, SAXException
383 {
384 WebConversation session = new WebConversation();
385 String failureText =
386 "Expected error " + errorCode + " from " + request.getURL();
387
388 try
389 {
390 session.getResponse(request);
391 fail(errorText + " -got success instead");
392 }
393 catch (HttpException e)
394 {
395 assertEquals(failureText, errorCode, e.getResponseCode());
396 /* checking for text omitted as it doesnt work.
397 if(errorText!=null) {
398 assertTrue(
399 "Failed to find "+errorText+" in "+ e.getResponseMessage(),
400 e.getMessage().indexOf(errorText)>=0);
401 }
402 */
403 }
404 }
405
406 /***
407 * Verifies that the service generates WSDL.
408 *
409 * @param service
410 * @param method
411 */
412 public void assertValidWSDL( String serviceName, String method )
413 throws Exception
414 {
415 assertValidWSDL( serviceName, new String[]{ method } );
416 }
417
418 public String getWSDL( String serviceName ) throws Exception
419 {
420 AxisService service = ( AxisService ) lookup( AxisService.ROLE );
421 AxisServer server = service.getAxisServer();
422
423 LocalTransport transport = new LocalTransport(server);
424
425 MessageContext msgContext = new MessageContext(server);
426 msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
427 msgContext.setEncodingStyle(SOAPConstants.SOAP12_CONSTANTS.getEncodingURI());
428
429 msgContext.setTargetService( serviceName );
430
431 // During a real invocation this is set by the handler, however we
432 // need to set it hear to get the wsdl generation working.
433 msgContext.setProperty( MessageContext.TRANS_URL,
434 services + serviceName );
435 server.generateWSDL( msgContext );
436
437 // another one of those undocumented "features"
438 Document doc = (Document) msgContext.getProperty( "WSDL" );
439
440 StringWriter writer = new StringWriter();
441 XMLUtils.DocumentToWriter(doc, writer);
442
443 return writer.toString();
444 }
445
446 /***
447 * Verifies that the service generates WSDL.
448 *
449 * @param service
450 * @param methods
451 */
452 public void assertValidWSDL( String serviceName, String methods[] )
453 throws Exception
454 {
455 String response = getWSDL( serviceName );
456
457 if ( isVerbose() )
458 {
459 System.out.println( "WSDL for " + serviceName + ":" );
460 System.out.println( response );
461 }
462
463 assertIsXml( response );
464
465 for ( int i = 0; i < methods.length; i++ )
466 {
467 assertStringInBody( response, "<wsdl:operation name=\"" + methods[i] + "\">" );
468 assertStringInBody( response, "<wsdl:input name=\"" + methods[i] + "Request\">" );
469 assertStringInBody( response, "<wsdl:output name=\"" + methods[i] + "Response\">" );
470 }
471 }
472 }
This page was automatically generated by Maven