1 package org.codehaus.xfire.plexus; 2 3 import java.io.ByteArrayOutputStream; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.codehaus.plexus.PlexusTestCase; 9 import org.codehaus.plexus.util.StringInputStream; 10 import org.codehaus.xfire.MessageContext; 11 import org.codehaus.xfire.XFire; 12 import org.codehaus.xfire.service.Service; 13 import org.codehaus.xfire.service.ServiceRegistry; 14 import org.codehaus.xfire.soap.Soap11; 15 import org.codehaus.xfire.soap.Soap12; 16 import org.codehaus.xfire.wsdl.WSDLWriter; 17 import org.dom4j.Document; 18 import org.dom4j.DocumentHelper; 19 import org.dom4j.Node; 20 import org.dom4j.XPath; 21 import org.dom4j.io.OutputFormat; 22 import org.dom4j.io.SAXReader; 23 import org.dom4j.io.XMLWriter; 24 25 /*** 26 * Contains helpful methods to test SOAP services. 27 * 28 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> 29 */ 30 public class PlexusXFireTest 31 extends PlexusTestCase 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 StringInputStream(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 StringInputStream(out.toString()) ); 78 } 79 80 /*** 81 * @see junit.framework.TestCase#setUp() 82 */ 83 protected void setUp() throws Exception 84 { 85 super.setUp(); 86 87 addNamespace("s", Soap11.getInstance().getNamespace()); 88 addNamespace("soap12", Soap12.getInstance().getNamespace()); 89 } 90 91 /*** 92 * Assert that the following XPath query selects one or more nodes. 93 * 94 * @param xpath 95 * @throws Exception 96 */ 97 public void assertValid( String xpath, Node node ) 98 throws Exception 99 { 100 List nodes = createXPath( xpath ).selectNodes( node ); 101 102 if ( nodes.size() == 0 ) 103 { 104 throw new Exception( "Failed to select any nodes for expression:.\n" + 105 xpath + "\n" + 106 node.asXML() ); 107 } 108 } 109 110 /*** 111 * Assert that the following XPath query selects no nodes. 112 * 113 * @param xpath 114 * @throws Exception 115 */ 116 public void assertInvalid( String xpath, Node node ) 117 throws Exception 118 { 119 List nodes = createXPath( xpath ).selectNodes( node ); 120 121 if ( nodes.size() > 0 ) 122 { 123 throw new Exception( "Found multiple nodes for expression:\n" + 124 xpath + "\n" + 125 node.asXML() ); 126 } 127 } 128 129 /*** 130 * Asser that the text of the xpath node retrieved is equal to the 131 * value specified. 132 * 133 * @param xpath 134 * @param value 135 * @param node 136 * @throws Exception 137 */ 138 public void assertXPathEquals( String xpath, String value, Node node ) 139 throws Exception 140 { 141 String value2 = createXPath( xpath ).selectSingleNode( node ).getText().trim(); 142 143 assertEquals( value, value2 ); 144 } 145 146 public void assertNoFault( Node node ) 147 throws Exception 148 { 149 assertInvalid("/s:Envelope/s:Body/s:Fault", node); 150 } 151 152 /*** 153 * Create the specified XPath expression with the namespaces added 154 * via addNamespace(). 155 */ 156 protected XPath createXPath( String xpathString ) 157 { 158 XPath xpath = DocumentHelper.createXPath( xpathString ); 159 xpath.setNamespaceURIs(namespaces); 160 161 return xpath; 162 } 163 164 /*** 165 * Add a namespace that will be used for XPath expressions. 166 * @param ns Namespace name. 167 * @param uri The namespace uri. 168 */ 169 public void addNamespace( String ns, String uri ) 170 { 171 namespaces.put(ns, uri); 172 } 173 174 /*** 175 * Get the WSDL for a service. 176 * 177 * @param string The name of the service. 178 * @return 179 * @throws Exception 180 */ 181 protected WSDLWriter getWSDL(String service) 182 throws Exception 183 { 184 ServiceRegistry reg = getServiceRegistry(); 185 Service hello = reg.getService(service); 186 187 return hello.getWSDLWriter(); 188 } 189 190 protected XFire getXFire() throws Exception 191 { 192 return (XFire) lookup( XFire.ROLE ); 193 } 194 195 protected ServiceRegistry getServiceRegistry() throws Exception 196 { 197 return getXFire().getServiceRegistry(); 198 } 199 }