001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.test;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import javax.xml.namespace.NamespaceContext;
026: import javax.xml.transform.TransformerException;
027: import javax.xml.xpath.XPath;
028: import javax.xml.xpath.XPathConstants;
029: import javax.xml.xpath.XPathFactory;
030:
031: import org.w3c.dom.Node;
032: import org.w3c.dom.NodeList;
033:
034: import junit.framework.Assert;
035: import junit.framework.AssertionFailedError;
036:
037: import org.apache.cxf.helpers.DOMUtils;
038:
039: /**
040: * XPath test assertions.
041: *
042: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
043: */
044: public final class XPathAssert {
045: private XPathAssert() {
046: }
047:
048: /**
049: * Assert that the following XPath query selects one or more nodes.
050: *
051: * @param xpath
052: */
053: public static NodeList assertValid(String xpath, Node node,
054: Map<String, String> namespaces) throws Exception {
055: if (node == null) {
056: throw new NullPointerException("Node cannot be null.");
057: }
058:
059: NodeList nodes = (NodeList) createXPath(namespaces).evaluate(
060: xpath, node, XPathConstants.NODESET);
061:
062: if (nodes.getLength() == 0) {
063: throw new AssertionFailedError(
064: "Failed to select any nodes for expression:\n"
065: + xpath + " from document:\n"
066: + writeNodeToString(node));
067: }
068:
069: return nodes;
070: }
071:
072: private static String writeNodeToString(Node node) {
073: ByteArrayOutputStream bos = new ByteArrayOutputStream();
074: try {
075: DOMUtils.writeXml(node, bos);
076: } catch (TransformerException e) {
077: throw new RuntimeException(e);
078: }
079: return bos.toString();
080: }
081:
082: /**
083: * Assert that the following XPath query selects no nodes.
084: *
085: * @param xpath
086: */
087: public static NodeList assertInvalid(String xpath, Node node,
088: Map<String, String> namespaces) throws Exception {
089: if (node == null) {
090: throw new NullPointerException("Node cannot be null.");
091: }
092:
093: NodeList nodes = (NodeList) createXPath(namespaces).evaluate(
094: xpath, node, XPathConstants.NODESET);
095:
096: if (nodes.getLength() > 0) {
097: String value = writeNodeToString(node);
098:
099: throw new AssertionFailedError(
100: "Found multiple nodes for expression:\n" + xpath
101: + "\n" + value);
102: }
103:
104: return nodes;
105: }
106:
107: /**
108: * Asser that the text of the xpath node retrieved is equal to the value
109: * specified.
110: *
111: * @param xpath
112: * @param value
113: * @param node
114: */
115: public static void assertXPathEquals(String xpath, String value,
116: Node node, Map<String, String> namespaces) throws Exception {
117: Node result = (Node) createXPath(namespaces).evaluate(xpath,
118: node, XPathConstants.NODE);
119: if (result == null) {
120: throw new AssertionFailedError(
121: "No nodes were found for expression: " + xpath);
122: }
123:
124: String value2 = DOMUtils.getContent(result).trim();
125:
126: Assert.assertEquals(value, value2);
127: }
128:
129: public static void assertNoFault(Node node) throws Exception {
130: Map<String, String> namespaces = new HashMap<String, String>();
131: namespaces
132: .put("s", "http://schemas.xmlsoap.org/soap/envelope/");
133: namespaces
134: .put("s12", "http://www.w3.org/2003/05/soap-envelope");
135:
136: assertInvalid("/s:Envelope/s:Body/s:Fault", node, namespaces);
137: assertInvalid("/s12:Envelope/s12:Body/s12:Fault", node,
138: namespaces);
139: }
140:
141: public static void assertFault(Node node) throws Exception {
142: Map<String, String> namespaces = new HashMap<String, String>();
143: namespaces
144: .put("s", "http://schemas.xmlsoap.org/soap/envelope/");
145: namespaces
146: .put("s12", "http://www.w3.org/2003/05/soap-envelope");
147:
148: assertValid("/s:Envelope/s:Body/s:Fault", node, namespaces);
149: assertValid("/s12:Envelope/s12:Body/s12:Fault", node,
150: namespaces);
151: }
152:
153: /**
154: * Create the specified XPath expression with the namespaces added via
155: * addNamespace().
156: */
157: public static XPath createXPath(Map<String, String> namespaces)
158: throws Exception {
159: XPath xpath = XPathFactory.newInstance().newXPath();
160:
161: if (namespaces != null) {
162: xpath.setNamespaceContext(new MapNamespaceContext(
163: namespaces));
164: }
165:
166: return xpath;
167: }
168:
169: static class MapNamespaceContext implements NamespaceContext {
170: private Map<String, String> namespaces;
171:
172: public MapNamespaceContext(Map<String, String> namespaces) {
173: super ();
174: this .namespaces = namespaces;
175: }
176:
177: public String getNamespaceURI(String prefix) {
178: return namespaces.get(prefix);
179: }
180:
181: public String getPrefix(String namespaceURI) {
182: for (Map.Entry<String, String> e : namespaces.entrySet()) {
183: if (e.getValue().equals(namespaceURI)) {
184: return e.getKey();
185: }
186: }
187: return null;
188: }
189:
190: public Iterator getPrefixes(String namespaceURI) {
191: return null;
192: }
193:
194: }
195: }
|