View Javadoc

1   package org.codehaus.xfire.test;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.File;
5   import java.io.InputStream;
6   import java.io.InputStreamReader;
7   import java.io.Reader;
8   import java.io.StringReader;
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  
13  import javax.xml.stream.XMLInputFactory;
14  import javax.xml.stream.XMLStreamException;
15  
16  import junit.framework.TestCase;
17  
18  import org.codehaus.xfire.DefaultXFire;
19  import org.codehaus.xfire.MessageContext;
20  import org.codehaus.xfire.XFire;
21  import org.codehaus.xfire.exchange.InMessage;
22  import org.codehaus.xfire.service.Service;
23  import org.codehaus.xfire.service.ServiceFactory;
24  import org.codehaus.xfire.service.ServiceRegistry;
25  import org.codehaus.xfire.service.binding.MessageBindingProvider;
26  import org.codehaus.xfire.service.binding.ObjectServiceFactory;
27  import org.codehaus.xfire.soap.Soap11;
28  import org.codehaus.xfire.soap.Soap12;
29  import org.codehaus.xfire.soap.SoapConstants;
30  import org.codehaus.xfire.soap.SoapTransport;
31  import org.codehaus.xfire.transport.Channel;
32  import org.codehaus.xfire.transport.Transport;
33  import org.codehaus.xfire.transport.TransportManager;
34  import org.codehaus.xfire.transport.http.SoapHttpTransport;
35  import org.codehaus.xfire.transport.local.LocalTransport;
36  import org.codehaus.xfire.util.STAXUtils;
37  import org.codehaus.xfire.wsdl.WSDLWriter;
38  import org.codehaus.yom.Document;
39  import org.codehaus.yom.Node;
40  import org.codehaus.yom.Serializer;
41  import org.codehaus.yom.stax.StaxBuilder;
42  
43  import com.ctc.wstx.stax.WstxInputFactory;
44  
45  /***
46   * Contains helpful methods to test SOAP services.
47   *
48   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
49   */
50  public abstract class AbstractXFireTest
51          extends TestCase
52  {
53      private XFire xfire;
54  
55      private ServiceFactory factory;
56  
57      private static String basedirPath;
58  
59      private XMLInputFactory defaultInputFactory = XMLInputFactory.newInstance(WstxInputFactory.class.getName(),
60                                                                                getClass().getClassLoader());
61      /***
62       * Namespaces for the XPath expressions.
63       */
64      private Map namespaces = new HashMap();
65  
66      protected void printNode(Node node)
67              throws Exception
68      {
69          Serializer writer = new Serializer(System.out);
70          writer.setOutputStream(System.out);
71  
72          if (node instanceof Document)
73              writer.write((Document) node);
74          else
75          {
76              writer.flush();
77              writer.writeChild(node);
78          }
79      }
80  
81      /***
82       * Invoke a service with the specified document.
83       *
84       * @param service  The name of the service.
85       * @param document The request as an xml document in the classpath.
86       */
87      protected Document invokeService(String service, String document)
88              throws Exception
89      {
90          ByteArrayOutputStream out = new ByteArrayOutputStream();
91          MessageContext context = new MessageContext();
92          context.setXFire(getXFire());
93          context.setProperty(Channel.BACKCHANNEL_URI, out);
94  
95          if (service != null)
96              context.setService(getServiceRegistry().getService(service));
97          
98          InputStream stream = getResourceAsStream(document); 
99          InMessage msg = new InMessage(STAXUtils.createXMLStreamReader(stream, "UTF-8"));
100 
101         Transport t = getXFire().getTransportManager().getTransport(LocalTransport.NAME);
102         Channel c = t.createChannel();
103         
104         c.receive(context, msg);
105         
106         String response = out.toString();
107         if (response == null || response.length() == 0)
108             return null;
109 
110         return readDocument(response);
111     }
112 
113     protected Document readDocument(String text)
114             throws XMLStreamException
115     {
116         return readDocument(text, defaultInputFactory);
117     }
118 
119     protected Document readDocument(String text, XMLInputFactory ifactory)
120             throws XMLStreamException
121     {
122         try
123         {
124             StaxBuilder builder = new StaxBuilder(ifactory);
125             return builder.build(new StringReader(text));
126         }
127         catch (XMLStreamException e)
128         {
129             System.err.println("Could not read the document!");
130             System.err.println(text);
131             throw e;
132         }
133     }
134     
135     protected Document getWSDLDocument(String service)
136             throws Exception
137     {
138         ByteArrayOutputStream out = new ByteArrayOutputStream();
139 
140         getXFire().generateWSDL(service, out);
141 
142         return readDocument(out.toString());
143     }
144 
145     /***
146      * @see junit.framework.TestCase#setUp()
147      */
148     protected void setUp()
149             throws Exception
150     {
151         super.setUp();
152 
153         if (xfire == null)
154             xfire = new DefaultXFire();
155 
156         addNamespace("s", Soap11.getInstance().getNamespace());
157         addNamespace("soap12", Soap12.getInstance().getNamespace());
158 
159         TransportManager trans = getXFire().getTransportManager();
160         trans.register(SoapTransport.createSoapTransport(new SoapHttpTransport()));
161     }
162 
163     /***
164      * Assert that the following XPath query selects one or more nodes.
165      *
166      * @param xpath
167      */
168     public List assertValid(String xpath, Node node)
169             throws Exception
170     {
171         return XPathAssert.assertValid(xpath, node, namespaces);
172     }
173 
174     /***
175      * Assert that the following XPath query selects no nodes.
176      *
177      * @param xpath
178      */
179     public List assertInvalid(String xpath, Node node)
180             throws Exception
181     {
182         return XPathAssert.assertInvalid(xpath, node, namespaces);
183     }
184 
185     /***
186      * Asser that the text of the xpath node retrieved is equal to the value specified.
187      *
188      * @param xpath
189      * @param value
190      * @param node
191      */
192     public void assertXPathEquals(String xpath, String value, Node node)
193             throws Exception
194     {
195         XPathAssert.assertXPathEquals(xpath, value, node, namespaces);
196     }
197 
198     public void assertNoFault(Node node)
199             throws Exception
200     {
201         XPathAssert.assertNoFault(node);
202     }
203 
204     /***
205      * Add a namespace that will be used for XPath expressions.
206      *
207      * @param ns  Namespace name.
208      * @param uri The namespace uri.
209      */
210     public void addNamespace(String ns, String uri)
211     {
212         namespaces.put(ns, uri);
213     }
214 
215     /***
216      * Get the WSDL for a service.
217      *
218      * @param service The name of the service.
219      */
220     protected WSDLWriter getWSDL(String service)
221             throws Exception
222     {
223         ServiceRegistry reg = getServiceRegistry();
224         Service hello = reg.getService(service);
225 
226         return hello.getWSDLWriter();
227     }
228 
229     protected XFire getXFire()
230     {
231         return xfire;
232     }
233 
234     protected ServiceRegistry getServiceRegistry()
235     {
236         return getXFire().getServiceRegistry();
237     }
238 
239     public ServiceFactory getServiceFactory()
240     {
241         if (factory == null)
242         {
243             ObjectServiceFactory ofactory = 
244                 new ObjectServiceFactory(getXFire().getTransportManager(),
245                                          new MessageBindingProvider());
246             
247             ofactory.setStyle(SoapConstants.STYLE_MESSAGE);
248             
249             factory = ofactory;
250         }
251 
252         return factory;
253     }
254 
255     public void setServiceFactory(ServiceFactory factory)
256     {
257         this.factory = factory;
258     }
259 
260     protected InputStream getResourceAsStream(String resource)
261     {
262         return getClass().getResourceAsStream(resource);
263     }
264 
265     protected Reader getResourceAsReader(String resource)
266     {
267         return new InputStreamReader(getResourceAsStream(resource));
268     }
269 
270     public File getTestFile(String relativePath)
271     {
272         return new File(getBasedir(), relativePath);
273     }
274 
275     public static String getBasedir()
276     {
277         if (basedirPath != null)
278         {
279             return basedirPath;
280         }
281 
282         basedirPath = System.getProperty("basedir");
283 
284         if (basedirPath == null)
285         {
286             basedirPath = new File("").getAbsolutePath();
287         }
288 
289         return basedirPath;
290     }
291 }