View Javadoc

    1   package org.codehaus.xfire.plexus;
    2
    3   import java.io.ByteArrayOutputStream;
    4   import java.io.InputStream;
    5   import java.io.StringReader;
    6   import java.util.HashMap;
    7   import java.util.List;
    8   import java.util.Map;
    9
    10  import javax.xml.stream.XMLStreamException;
    11
    12  import org.codehaus.plexus.PlexusTestCase;
    13  import org.codehaus.xfire.MessageContext;
    14  import org.codehaus.xfire.XFire;
    15  import org.codehaus.xfire.exchange.InMessage;
    16  import org.codehaus.xfire.service.Service;
    17  import org.codehaus.xfire.service.ServiceRegistry;
    18  import org.codehaus.xfire.soap.Soap11;
    19  import org.codehaus.xfire.soap.Soap12;
    20  import org.codehaus.xfire.test.XPathAssert;
    21  import org.codehaus.xfire.transport.Channel;
    22  import org.codehaus.xfire.transport.Transport;
    23  import org.codehaus.xfire.transport.local.LocalTransport;
    24  import org.codehaus.xfire.util.STAXUtils;
    25  import org.codehaus.xfire.wsdl.WSDLWriter;
    26  import org.codehaus.yom.Document;
    27  import org.codehaus.yom.Node;
    28  import org.codehaus.yom.Serializer;
    29  import org.codehaus.yom.stax.StaxBuilder;
    30
    31  /***
    32   * Contains helpful methods to test SOAP services.
    33   *
    34   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    35   */
    36  public class PlexusXFireTest
    37          extends PlexusTestCase
    38  {
    39      /***
    40       * Namespaces for the XPath expressions.
    41       */
    42      private Map namespaces = new HashMap();
    43
    44      protected void printNode(Node node)
    45              throws Exception
    46      {
    47          Serializer writer = new Serializer(System.out);
    48          writer.setOutputStream(System.out);
    49
    50          if (node instanceof Document)
    51              writer.write((Document) node);
    52          else
    53          {
    54              writer.flush();
    55              writer.writeChild(node);
    56          }
    57      }
    58
    59      /***
    60       * Invoke a service with the specified document.
    61       *
    62       * @param service  The name of the service.
    63       * @param document The request as an xml document in the classpath.
    64       */
    65      protected Document invokeService(String service, String document)
    66              throws Exception
    67      {
    68          ByteArrayOutputStream out = new ByteArrayOutputStream();
    69          MessageContext context = new MessageContext();
    70          context.setXFire(getXFire());
    71          context.setProperty(Channel.BACKCHANNEL_URI, out);
    72
    73          if (service != null)
    74              context.setService(getServiceRegistry().getService(service));
    75
    76          InputStream stream = getResourceAsStream(document);
    77          InMessage msg = new InMessage(STAXUtils.createXMLStreamReader(stream, "UTF-8"));
    78
    79          Transport t = getXFire().getTransportManager().getTransport(LocalTransport.NAME);
    80          Channel c = t.createChannel();
    81
    82          c.receive(context, msg);
    83
    84          String response = out.toString();
    85          if (response == null || response.length() == 0)
    86              return null;
    87
    88          return readDocument(response);
    89      }
    90
    91      protected Document readDocument(String text)
    92              throws XMLStreamException
    93      {
    94          try
    95          {
    96              StaxBuilder builder = new StaxBuilder();
    97              return builder.build(new StringReader(text));
    98          }
    99          catch (XMLStreamException e)
    100         {
    101             System.err.println("Could not read the document!");
    102             System.out.println(text);
    103             throw e;
    104         }
    105     }
    106
    107     protected Document getWSDLDocument(String service)
    108             throws Exception
    109     {
    110         ByteArrayOutputStream out = new ByteArrayOutputStream();
    111
    112         getXFire().generateWSDL(service, out);
    113
    114         return readDocument(out.toString());
    115     }
    116
    117     /***
    118      * @see junit.framework.TestCase#setUp()
    119      */
    120     protected void setUp()
    121             throws Exception
    122     {
    123         super.setUp();
    124
    125         addNamespace("s", Soap11.getInstance().getNamespace());
    126         addNamespace("soap12", Soap12.getInstance().getNamespace());
    127     }
    128
    129     /***
    130      * Assert that the following XPath query selects one or more nodes.
    131      *
    132      * @param xpath
    133      * @return
    134      */
    135     public List assertValid(String xpath, Node node)
    136             throws Exception
    137     {
    138         return XPathAssert.assertValid(xpath, node, namespaces);
    139     }
    140
    141     /***
    142      * Assert that the following XPath query selects no nodes.
    143      *
    144      * @param xpath
    145      * @return
    146      */
    147     public List assertInvalid(String xpath, Node node)
    148             throws Exception
    149     {
    150         return XPathAssert.assertInvalid(xpath, node, namespaces);
    151     }
    152
    153     /***
    154      * Asser that the text of the xpath node retrieved is equal to the value specified.
    155      *
    156      * @param xpath
    157      * @param value
    158      * @param node
    159      */
    160     public void assertXPathEquals(String xpath, String value, Node node)
    161             throws Exception
    162     {
    163         XPathAssert.assertXPathEquals(xpath, value, node, namespaces);
    164     }
    165
    166     public void assertNoFault(Node node)
    167             throws Exception
    168     {
    169         XPathAssert.assertNoFault(node);
    170     }
    171
    172     /***
    173      * Add a namespace that will be used for XPath expressions.
    174      *
    175      * @param ns  Namespace name.
    176      * @param uri The namespace uri.
    177      */
    178     public void addNamespace(String ns, String uri)
    179     {
    180         namespaces.put(ns, uri);
    181     }
    182
    183     /***
    184      * Get the WSDL for a service.
    185      *
    186      * @param string The name of the service.
    187      * @return
    188      * @throws Exception
    189      */
    190     protected WSDLWriter getWSDL(String service)
    191             throws Exception
    192     {
    193         ServiceRegistry reg = getServiceRegistry();
    194         Service hello = reg.getService(service);
    195
    196         return hello.getWSDLWriter();
    197     }
    198
    199     protected XFire getXFire()
    200             throws Exception
    201     {
    202         return (XFire) lookup(XFire.ROLE);
    203     }
    204
    205     protected ServiceRegistry getServiceRegistry()
    206             throws Exception
    207     {
    208         return getXFire().getServiceRegistry();
    209     }
    210 }