View Javadoc

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