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