001: // -*- Mode: JDE -*-
002: /*
003: ******************************************************************
004: Copyright (c) 2006-2007, Jeff Martin, Tim Bacon
005: All rights reserved.
006:
007: Redistribution and use in source and binary forms, with or without
008: modification, are permitted provided that the following conditions
009: are met:
010:
011: * Redistributions of source code must retain the above copyright
012: notice, this list of conditions and the following disclaimer.
013: * Redistributions in binary form must reproduce the above
014: copyright notice, this list of conditions and the following
015: disclaimer in the documentation and/or other materials provided
016: with the distribution.
017: * Neither the name of the xmlunit.sourceforge.net nor the names
018: of its contributors may be used to endorse or promote products
019: derived from this software without specific prior written
020: permission.
021:
022: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
023: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
024: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
025: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
026: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
027: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
028: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
030: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
031: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
032: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
033: POSSIBILITY OF SUCH DAMAGE.
034:
035: ******************************************************************
036: */
037: package org.custommonkey.xmlunit.examples;
038:
039: import java.io.IOException;
040: import java.io.Reader;
041:
042: import org.custommonkey.xmlunit.XpathEngine;
043: import org.custommonkey.xmlunit.XMLUnit;
044: import org.custommonkey.xmlunit.exceptions.XpathException;
045:
046: import org.w3c.dom.Document;
047: import org.xml.sax.InputSource;
048: import org.xml.sax.SAXException;
049:
050: import junit.framework.Assert;
051:
052: /**
053: * Example demonstrating how to use the XPath API of XMLUnit in
054: * conjunction with regular expressions (as provided by the
055: * java.util.regex package of JDK 1.4+).
056: */
057:
058: public class XPathRegexAssert {
059: // no instances
060: private XPathRegexAssert() {
061: }
062:
063: public static void assertXPathMatches(String message, String regex,
064: String xpath, Document doc) throws XpathException {
065: XpathEngine engine = XMLUnit.newXpathEngine();
066: String value = engine.evaluate(xpath, doc);
067: Assert.assertTrue(message, value.matches(regex));
068: }
069:
070: public static void assertXPathMatches(String message, String regex,
071: String xpath, String xml) throws XpathException,
072: SAXException, IOException {
073: Document doc = XMLUnit.buildControlDocument(xml);
074: assertXPathMatches(message, regex, xpath, doc);
075: }
076:
077: public static void assertXPathMatches(String message, String regex,
078: String xpath, Reader reader) throws XpathException,
079: SAXException, IOException {
080: Document doc = XMLUnit.buildControlDocument(new InputSource(
081: reader));
082: assertXPathMatches(message, regex, xpath, doc);
083: }
084:
085: public static void assertXPathMatches(String regex, String xpath,
086: Document doc) throws XpathException {
087: assertXPathMatches("expected value to match " + regex, regex,
088: xpath, doc);
089: }
090:
091: public static void assertXPathMatches(String regex, String xpath,
092: String xml) throws XpathException, SAXException,
093: IOException {
094: assertXPathMatches("expected value to match " + regex, regex,
095: xpath, xml);
096: }
097:
098: public static void assertXPathMatches(String regex, String xpath,
099: Reader reader) throws XpathException, SAXException,
100: IOException {
101: assertXPathMatches("expected value to match " + regex, regex,
102: xpath, reader);
103: }
104: }
|