1 package org.codehaus.xfire.test;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Map;
7
8 import junit.framework.Assert;
9 import junit.framework.AssertionFailedError;
10
11 import org.codehaus.xfire.soap.Soap11;
12 import org.codehaus.xfire.soap.Soap12;
13 import org.codehaus.yom.Node;
14 import org.codehaus.yom.xpath.YOMXPath;
15 import org.jaxen.XPath;
16
17 /***
18 * WebService assertions.
19 *
20 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
21 */
22 public class XPathAssert
23 {
24 /***
25 * Assert that the following XPath query selects one or more nodes.
26 *
27 * @param xpath
28 */
29 public static List assertValid(String xpath, Node node, Map namespaces)
30 throws Exception
31 {
32 List nodes = createXPath(xpath, namespaces).selectNodes(node);
33
34 if (nodes.size() == 0)
35 {
36 throw new AssertionFailedError("Failed to select any nodes for expression:.\n" + xpath + "\n"
37 + node.toXML());
38 }
39
40 return nodes;
41 }
42
43 /***
44 * Assert that the following XPath query selects no nodes.
45 *
46 * @param xpath
47 */
48 public static List assertInvalid(String xpath, Node node, Map namespaces)
49 throws Exception
50 {
51 List nodes = createXPath(xpath, namespaces).selectNodes(node);
52
53 if (nodes.size() > 0)
54 {
55 throw new AssertionFailedError("Found multiple nodes for expression:\n" + xpath + "\n"
56 + node.toXML());
57 }
58
59 return nodes;
60 }
61
62 /***
63 * Asser that the text of the xpath node retrieved is equal to the value
64 * specified.
65 *
66 * @param xpath
67 * @param value
68 * @param node
69 */
70 public static void assertXPathEquals(String xpath, String value, Node node, Map namespaces)
71 throws Exception
72 {
73 String value2 = ((Node) createXPath( xpath, namespaces ).selectSingleNode( node )).getValue().trim();
74
75 Assert.assertEquals( value, value2 );
76 }
77
78 public static void assertNoFault(Node node)
79 throws Exception
80 {
81 Map namespaces = new HashMap();
82 namespaces.put("s", Soap11.getInstance().getNamespace());
83 namespaces.put("s12", Soap12.getInstance().getNamespace());
84
85 assertInvalid("/s:Envelope/s:Body/s:Fault", node, namespaces);
86 assertInvalid("/s12:Envelope/s12:Body/s12:Fault", node, namespaces);
87 }
88
89 public static void assertFault(Node node)
90 throws Exception
91 {
92 Map namespaces = new HashMap();
93 namespaces.put("s", Soap11.getInstance().getNamespace());
94 namespaces.put("s12", Soap12.getInstance().getNamespace());
95
96 assertValid("/s:Envelope/s:Body/s:Fault", node, namespaces);
97 assertValid("/s12:Envelope/s12:Body/s12:Fault", node, namespaces);
98 }
99
100 /***
101 * Create the specified XPath expression with the namespaces added via
102 * addNamespace().
103 */
104 public static XPath createXPath( String xpathString, Map namespaces )
105 throws Exception
106 {
107 XPath xpath = new YOMXPath( xpathString );
108
109 for ( Iterator itr = namespaces.keySet().iterator(); itr.hasNext(); )
110 {
111 String ns = (String) itr.next();
112 xpath.addNamespace(ns, (String) namespaces.get(ns));
113 }
114
115 return xpath;
116 }
117 }