1 package org.codehaus.xfire.plexus;
2
3 import java.io.ByteArrayOutputStream;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import org.codehaus.plexus.PlexusTestCase;
8 import org.codehaus.plexus.util.StringInputStream;
9 import org.codehaus.xfire.MessageContext;
10 import org.codehaus.xfire.SOAPConstants;
11 import org.codehaus.xfire.XFire;
12 import org.codehaus.xfire.service.Service;
13 import org.codehaus.xfire.service.ServiceRegistry;
14 import org.codehaus.xfire.wsdl.WSDL;
15 import org.dom4j.Document;
16 import org.dom4j.DocumentHelper;
17 import org.dom4j.Node;
18 import org.dom4j.XPath;
19 import org.dom4j.io.OutputFormat;
20 import org.dom4j.io.SAXReader;
21 import org.dom4j.io.XMLWriter;
22
23 /***
24 * Contains helpful methods to test SOAP services.
25 *
26 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
27 */
28 public class PlexusXFireTest
29 extends PlexusTestCase
30 {
31 /*** Namespaces for the XPath expressions. */
32 private Map namespaces = new HashMap();
33
34 protected void printNode( Node node )
35 throws Exception
36 {
37 XMLWriter writer = new XMLWriter( OutputFormat.createPrettyPrint() );
38 writer.setOutputStream( System.out );
39 writer.write( node );
40 }
41
42 /***
43 * Invoke a service with the specified document.
44 *
45 * @param service The name of the service.
46 * @param document The request as an xml document in the classpath.
47 * @return
48 * @throws Exception
49 */
50 protected Document invokeService( String service, String document )
51 throws Exception
52 {
53 ByteArrayOutputStream out = new ByteArrayOutputStream();
54 MessageContext context =
55 new MessageContext( service,
56 null,
57 out,
58 null,
59 null );
60
61 getXFire().invoke( getResourceAsStream( document ), context );
62
63 SAXReader reader = new SAXReader();
64 return reader.read( new StringInputStream(out.toString()) );
65 }
66
67 protected Document getWSDLDocument( String service )
68 throws Exception
69 {
70 ByteArrayOutputStream out = new ByteArrayOutputStream();
71
72 getXFire().generateWSDL( service, out );
73
74 SAXReader reader = new SAXReader();
75 return reader.read( new StringInputStream(out.toString()) );
76 }
77
78 /***
79 * @see junit.framework.TestCase#setUp()
80 */
81 protected void setUp() throws Exception
82 {
83 super.setUp();
84
85 addNamespace("s", SOAPConstants.SOAP11_ENVELOPE_NS );
86 addNamespace("soap12", SOAPConstants.SOAP12_ENVELOPE_NS);
87 }
88
89 /***
90 * Assert that the following XPath query selects one or more nodes.
91 *
92 * @param xpath
93 * @throws Exception
94 */
95 public void assertValid( String xpath, Node node )
96 throws Exception
97 {
98 List nodes = createXPath( xpath ).selectNodes( node );
99
100 if ( nodes.size() == 0 )
101 {
102 throw new Exception( "Failed to select any nodes for expression:.\n" +
103 xpath + "\n" +
104 node.asXML() );
105 }
106 }
107
108 /***
109 * Assert that the following XPath query selects no nodes.
110 *
111 * @param xpath
112 * @throws Exception
113 */
114 public void assertInvalid( String xpath, Node node )
115 throws Exception
116 {
117 List nodes = createXPath( xpath ).selectNodes( node );
118
119 if ( nodes.size() > 0 )
120 {
121 throw new Exception( "Found multiple nodes for expression:\n" +
122 xpath + "\n" +
123 node.asXML() );
124 }
125 }
126
127 /***
128 * Asser that the text of the xpath node retrieved is equal to the
129 * value specified.
130 *
131 * @param xpath
132 * @param value
133 * @param node
134 * @throws Exception
135 */
136 public void assertXPathEquals( String xpath, String value, Node node )
137 throws Exception
138 {
139 String value2 = createXPath( xpath ).selectSingleNode( node ).getText().trim();
140
141 assertEquals( value, value2 );
142 }
143
144 public void assertNoFault( Node node )
145 throws Exception
146 {
147 assertInvalid("/s:Envelope/s:Body/s:Fault", node);
148 }
149
150 /***
151 * Create the specified XPath expression with the namespaces added
152 * via addNamespace().
153 */
154 protected XPath createXPath( String xpathString )
155 {
156 XPath xpath = DocumentHelper.createXPath( xpathString );
157 xpath.setNamespaceURIs(namespaces);
158
159 return xpath;
160 }
161
162 /***
163 * Add a namespace that will be used for XPath expressions.
164 * @param ns Namespace name.
165 * @param uri The namespace uri.
166 */
167 public void addNamespace( String ns, String uri )
168 {
169 namespaces.put(ns, uri);
170 }
171
172 /***
173 * Get the WSDL for a service.
174 *
175 * @param string The name of the service.
176 * @return
177 * @throws Exception
178 */
179 protected WSDL getWSDL(String service)
180 throws Exception
181 {
182 ServiceRegistry reg = getServiceRegistry();
183 Service hello = reg.getService(service);
184
185 return hello.getWSDL();
186 }
187
188 protected XFire getXFire() throws Exception
189 {
190 return (XFire) lookup( XFire.ROLE );
191 }
192
193 protected ServiceRegistry getServiceRegistry() throws Exception
194 {
195 return getXFire().getServiceRegistry();
196 }
197 }