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