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  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 AbstractXFireTest
29      extends TestCase
30  {
31      private XFire xfire;
32      
33      /*** Namespaces for the XPath expressions. */
34      private Map namespaces = new HashMap();
35      
36      protected void printNode( Node node ) 
37          throws Exception
38      {
39          XMLWriter writer = new XMLWriter( OutputFormat.createPrettyPrint() );
40          writer.setOutputStream( System.out );
41          writer.write( node );
42      }
43      
44      /***
45       * Invoke a service with the specified document.
46       * 
47       * @param service The name of the service.
48       * @param document The request as an xml document in the classpath.
49       * @return
50       * @throws Exception
51       */
52      protected Document invokeService( String service, String document ) 
53          throws Exception
54      {
55          ByteArrayOutputStream out = new ByteArrayOutputStream();
56          MessageContext context = 
57              new MessageContext( service,
58                                  null,
59                                  out,
60                                  null,
61                                  null );
62          
63          getXFire().invoke( getResourceAsStream( document ), context );
64          
65          SAXReader reader = new SAXReader();
66          return reader.read( new StringReader(out.toString()) );
67      }
68  
69      protected Document getWSDLDocument( String service ) 
70          throws Exception
71      {
72          ByteArrayOutputStream out = new ByteArrayOutputStream();
73  
74          getXFire().generateWSDL( service, out );
75          
76          SAXReader reader = new SAXReader();
77          return reader.read( new StringReader(out.toString()) );
78      }
79      
80      /***
81       * @see junit.framework.TestCase#setUp()
82       */
83      protected void setUp() throws Exception
84      {
85          super.setUp();
86          
87          xfire = new DefaultXFire();
88          
89          addNamespace("s", SOAPConstants.SOAP11_ENVELOPE_NS );
90          addNamespace("soap12", SOAPConstants.SOAP12_ENVELOPE_NS);
91      }
92      
93      /***
94       * Assert that the following XPath query selects one or more nodes.
95       * 
96       * @param xpath
97       * @throws Exception
98       */
99      public void assertValid( String xpath, Node node )
100         throws Exception
101     {
102         List nodes = createXPath( xpath ).selectNodes( node );
103         
104         if ( nodes.size() == 0 )
105         {
106             throw new Exception( "Failed to select any nodes for expression:.\n" +
107                                  xpath + "\n" +
108                                  node.asXML() );
109         }
110     }
111     
112     /***
113      * Assert that the following XPath query selects no nodes.
114      * 
115      * @param xpath
116      * @throws Exception
117      */
118     public void assertInvalid( String xpath, Node node )
119         throws Exception
120     {
121         List nodes = createXPath( xpath ).selectNodes( node );
122         
123         if ( nodes.size() > 0 )
124         {
125             throw new Exception( "Found multiple nodes for expression:\n" +
126                                  xpath + "\n" +
127                                  node.asXML() );
128         }
129     }
130 
131     /***
132      * Asser that the text of the xpath node retrieved is equal to the
133      * value specified.
134      * 
135      * @param xpath
136      * @param value
137      * @param node
138      * @throws Exception
139      */
140     public void assertXPathEquals( String xpath, String value, Node node )
141         throws Exception
142     {
143         String value2 = createXPath( xpath ).selectSingleNode( node ).getText().trim();
144         
145         assertEquals( value, value2 );
146     }
147     
148     public void assertNoFault( Node node )
149         throws Exception
150     {
151         assertInvalid("/s:Envelope/s:Body/s:Fault", node);
152     }
153     
154     /***
155      * Create the specified XPath expression with the namespaces added
156      * via addNamespace().
157      */
158     protected XPath createXPath( String xpathString )
159     {
160         XPath xpath = DocumentHelper.createXPath( xpathString );
161         xpath.setNamespaceURIs(namespaces);
162         
163         return xpath;
164     }
165     
166     /***
167      * Add a namespace that will be used for XPath expressions.
168      * @param ns Namespace name.
169      * @param uri The namespace uri.
170      */
171     public void addNamespace( String ns, String uri )
172     {
173         namespaces.put(ns, uri);
174     }
175 
176     /***
177      * Get the WSDL for a service.
178      * 
179      * @param string The name of the service.
180      * @return
181      * @throws Exception
182      */
183     protected WSDL getWSDL(String service) 
184         throws Exception
185     {
186         ServiceRegistry reg = getServiceRegistry();
187         Service hello = reg.getService(service);
188         
189         return hello.getWSDL();
190     }
191     
192     protected XFire getXFire()
193     {
194         return xfire;
195     }
196     
197     protected ServiceRegistry getServiceRegistry()
198     {
199         return getXFire().getServiceRegistry();
200     }
201     
202     protected InputStream getResourceAsStream( String resource )
203     {
204         return getClass().getResourceAsStream(resource);
205     }
206 
207     protected Reader getResourceAsReader( String resource )
208     {
209         return new InputStreamReader( getResourceAsStream(resource) );
210     }
211 }