Source Code Cross Referenced for XMLAssert.java in  » Testing » XMLUnit » org » custommonkey » xmlunit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » XMLUnit » org.custommonkey.xmlunit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         ******************************************************************
0003:        Copyright (c) 2001-2007, Jeff Martin, Tim Bacon
0004:        All rights reserved.
0005:
0006:        Redistribution and use in source and binary forms, with or without
0007:        modification, are permitted provided that the following conditions
0008:        are met:
0009:
0010:         * Redistributions of source code must retain the above copyright
0011:              notice, this list of conditions and the following disclaimer.
0012:         * Redistributions in binary form must reproduce the above
0013:              copyright notice, this list of conditions and the following
0014:              disclaimer in the documentation and/or other materials provided
0015:              with the distribution.
0016:         * Neither the name of the xmlunit.sourceforge.net nor the names
0017:              of its contributors may be used to endorse or promote products
0018:              derived from this software without specific prior written
0019:              permission.
0020:
0021:        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0022:        "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0023:        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0024:        FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0025:        COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0026:        INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0027:        BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0028:        LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
0029:        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0030:        LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
0031:        ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032:        POSSIBILITY OF SUCH DAMAGE.
0033:
0034:         ******************************************************************
0035:         */
0036:
0037:        package org.custommonkey.xmlunit;
0038:
0039:        import org.custommonkey.xmlunit.exceptions.ConfigurationException;
0040:        import org.custommonkey.xmlunit.exceptions.XpathException;
0041:
0042:        import java.io.IOException;
0043:        import java.io.Reader;
0044:        import java.io.StringReader;
0045:
0046:        import javax.xml.parsers.DocumentBuilder;
0047:
0048:        import junit.framework.Assert;
0049:        import org.w3c.dom.Document;
0050:        import org.w3c.dom.Element;
0051:        import org.w3c.dom.NodeList;
0052:        import org.xml.sax.InputSource;
0053:        import org.xml.sax.SAXException;
0054:
0055:        /**
0056:         * Collection of static methods so that XML assertion facilities are available
0057:         * in any class, not just test suites. Thanks to Andrew McCormick and others for
0058:         * suggesting this refactoring.<br/>
0059:         * Available assertion methods are:
0060:         * <ul>
0061:         * <li><strong><code>assertXMLEqual</code></strong><br/>
0062:         *  assert that two pieces of XML markup are <i>similar</i></li>
0063:         * <li><strong><code>assertXMLNotEqual</code></strong><br/>
0064:         *  assert that two pieces of XML markup are <i>different</i></li>
0065:         * <li><strong><code>assertXMLIdentical</code></strong><br/>
0066:         *  assert that two pieces of XML markup are <i>identical</i>. In most cases
0067:         *  this assertion is too strong and <code>assertXMLEqual</code> is sufficient</li>
0068:         * <li><strong><code>assertXpathExists</code></strong><br/> 
0069:         * assert that an XPath expression matches at least one node</li>
0070:         * <li><strong><code>assertXpathNotExists</code></strong><br/> 
0071:         * assert that an XPath expression does not match any nodes</li>
0072:         * <li><strong><code>assertXpathsEqual</code></strong><br/>
0073:         *  assert that the nodes obtained by executing two Xpaths
0074:         *  are <i>similar</i></li>
0075:         * <li><strong><code>assertXpathsNotEqual</code></strong><br/>
0076:         *  assert that the nodes obtained by executing two Xpaths
0077:         *  are <i>different</i></li>
0078:         * <li><strong><code>assertXpathValuesEqual</code></strong><br/>
0079:         *  assert that the flattened String obtained by executing two Xpaths
0080:         *  are <i>similar</i></li>
0081:         * <li><strong><code>assertXpathValuesNotEqual</code></strong><br/>
0082:         *  assert that the flattened String obtained by executing two Xpaths
0083:         *  are <i>different</i></li>
0084:         * <li><strong><code>assertXpathEvaluatesTo</code></strong><br/>
0085:         *  assert that the flattened String obtained by executing an Xpath
0086:         *  is a particular value</li>
0087:         * <li><strong><code>assertXMLValid</code></strong><br/>
0088:         *  assert that a piece of XML markup is valid with respect to a DTD: either
0089:         *  by using the markup's own DTD or a different DTD</li>
0090:         * <li><strong><code>assertNodeTestPasses</code></strong><br/>
0091:         *  assert that a piece of XML markup passes a {@link NodeTest NodeTest}</li>
0092:         * </ul>
0093:         * All underlying similarity and difference testing is done using 
0094:         * {@link Diff Diff} instances which can be instantiated and evaluated
0095:         * independently of this class.
0096:         * @see Diff#similar()
0097:         * @see Diff#identical()
0098:         * <br />Examples and more at <a href="http://xmlunit.sourceforge.net"/>xmlunit.sourceforge.net</a>
0099:         */
0100:        public class XMLAssert extends Assert implements  XSLTConstants {
0101:
0102:            protected XMLAssert() {
0103:                super ();
0104:            }
0105:
0106:            /**
0107:             * Assert that the result of an XML comparison is or is not similar.
0108:             * @param diff the result of an XML comparison
0109:             * @param assertion true if asserting that result is similar
0110:             */
0111:            public static void assertXMLEqual(Diff diff, boolean assertion) {
0112:                assertXMLEqual(null, diff, assertion);
0113:            }
0114:
0115:            /**
0116:             * Assert that the result of an XML comparison is or is not similar.
0117:             * @param msg additional message to display if assertion fails
0118:             * @param diff the result of an XML comparison
0119:             * @param assertion true if asserting that result is similar
0120:             */
0121:            public static void assertXMLEqual(String msg, Diff diff,
0122:                    boolean assertion) {
0123:                if (assertion != diff.similar()) {
0124:                    fail(getFailMessage(msg, diff));
0125:                }
0126:            }
0127:
0128:            private static String getFailMessage(String msg, Diff diff) {
0129:                StringBuffer sb = new StringBuffer();
0130:                if (msg != null && msg.length() > 0) {
0131:                    sb.append(msg).append(", ");
0132:                }
0133:                return sb.append(diff.toString()).toString();
0134:            }
0135:
0136:            /**
0137:             * Assert that the result of an XML comparison is or is not identical
0138:             * @param diff the result of an XML comparison
0139:             * @param assertion true if asserting that result is identical
0140:             */
0141:            public static void assertXMLIdentical(Diff diff, boolean assertion) {
0142:                assertXMLIdentical(null, diff, assertion);
0143:            }
0144:
0145:            /**
0146:             * Assert that the result of an XML comparison is or is not identical
0147:             * @param msg Message to display if assertion fails
0148:             * @param diff the result of an XML comparison
0149:             * @param assertion true if asserting that result is identical
0150:             */
0151:            public static void assertXMLIdentical(String msg, Diff diff,
0152:                    boolean assertion) {
0153:                if (assertion != diff.identical()) {
0154:                    fail(getFailMessage(msg, diff));
0155:                }
0156:            }
0157:
0158:            /**
0159:             * Assert that two XML documents are similar
0160:             * @param control XML to be compared against
0161:             * @param test XML to be tested
0162:             * @throws SAXException
0163:             * @throws IOException
0164:             */
0165:            public static void assertXMLEqual(InputSource control,
0166:                    InputSource test) throws SAXException, IOException {
0167:                assertXMLEqual(null, control, test);
0168:            }
0169:
0170:            /**
0171:             * Assert that two XML documents are similar
0172:             * @param control XML to be compared against
0173:             * @param test XML to be tested
0174:             * @throws SAXException
0175:             * @throws IOException
0176:             */
0177:            public static void assertXMLEqual(String control, String test)
0178:                    throws SAXException, IOException {
0179:                assertXMLEqual(null, control, test);
0180:            }
0181:
0182:            /**
0183:             * Assert that two XML documents are similar
0184:             * @param control XML to be compared against
0185:             * @param test XML to be tested
0186:             */
0187:            public static void assertXMLEqual(Document control, Document test) {
0188:                assertXMLEqual(null, control, test);
0189:            }
0190:
0191:            /**
0192:             * Assert that two XML documents are similar
0193:             * @param control XML to be compared against
0194:             * @param test XML to be tested
0195:             * @throws SAXException
0196:             * @throws IOException
0197:             */
0198:            public static void assertXMLEqual(Reader control, Reader test)
0199:                    throws SAXException, IOException {
0200:                assertXMLEqual(null, control, test);
0201:            }
0202:
0203:            /**
0204:             * Assert that two XML documents are similar
0205:             * @param err Message to be displayed on assertion failure
0206:             * @param control XML to be compared against
0207:             * @param test XML to be tested
0208:             * @throws SAXException
0209:             * @throws IOException
0210:             */
0211:            public static void assertXMLEqual(String err, InputSource control,
0212:                    InputSource test) throws SAXException, IOException {
0213:                Diff diff = new Diff(control, test);
0214:                assertXMLEqual(err, diff, true);
0215:            }
0216:
0217:            /**
0218:             * Assert that two XML documents are similar
0219:             * @param err Message to be displayed on assertion failure
0220:             * @param control XML to be compared against
0221:             * @param test XML to be tested
0222:             * @throws SAXException
0223:             * @throws IOException
0224:             */
0225:            public static void assertXMLEqual(String err, String control,
0226:                    String test) throws SAXException, IOException {
0227:                Diff diff = new Diff(control, test);
0228:                assertXMLEqual(err, diff, true);
0229:            }
0230:
0231:            /**
0232:             * Assert that two XML documents are similar
0233:             * @param err Message to be displayed on assertion failure
0234:             * @param control XML to be compared against
0235:             * @param test XML to be tested
0236:             */
0237:            public static void assertXMLEqual(String err, Document control,
0238:                    Document test) {
0239:                Diff diff = new Diff(control, test);
0240:                assertXMLEqual(err, diff, true);
0241:            }
0242:
0243:            /**
0244:             * Assert that two XML documents are similar
0245:             * @param err Message to be displayed on assertion failure
0246:             * @param control XML to be compared against
0247:             * @param test XML to be tested
0248:             * @throws SAXException
0249:             * @throws IOException
0250:             */
0251:            public static void assertXMLEqual(String err, Reader control,
0252:                    Reader test) throws SAXException, IOException {
0253:                Diff diff = new Diff(control, test);
0254:                assertXMLEqual(err, diff, true);
0255:            }
0256:
0257:            /**
0258:             * Assert that two XML documents are NOT similar
0259:             * @param control XML to be compared against
0260:             * @param test XML to be tested
0261:             * @throws SAXException
0262:             * @throws IOException
0263:             */
0264:            public static void assertXMLNotEqual(InputSource control,
0265:                    InputSource test) throws SAXException, IOException {
0266:                assertXMLNotEqual(null, control, test);
0267:            }
0268:
0269:            /**
0270:             * Assert that two XML documents are NOT similar
0271:             * @param control XML to be compared against
0272:             * @param test XML to be tested
0273:             * @throws SAXException
0274:             * @throws IOException
0275:             */
0276:            public static void assertXMLNotEqual(String control, String test)
0277:                    throws SAXException, IOException {
0278:                assertXMLNotEqual(null, control, test);
0279:            }
0280:
0281:            /**
0282:             * Assert that two XML documents are NOT similar
0283:             * @param control XML to be compared against
0284:             * @param test XML to be tested
0285:             */
0286:            public static void assertXMLNotEqual(Document control, Document test) {
0287:                assertXMLNotEqual(null, control, test);
0288:            }
0289:
0290:            /**
0291:             * Assert that two XML documents are NOT similar
0292:             * @param control XML to be compared against
0293:             * @param test XML to be tested
0294:             * @throws SAXException
0295:             * @throws IOException
0296:             */
0297:            public static void assertXMLNotEqual(Reader control, Reader test)
0298:                    throws SAXException, IOException {
0299:                assertXMLNotEqual(null, control, test);
0300:            }
0301:
0302:            /**
0303:             * Assert that two XML documents are NOT similar
0304:             * @param err Message to be displayed on assertion failure
0305:             * @param control XML to be compared against
0306:             * @param test XML to be tested
0307:             * @throws SAXException
0308:             * @throws IOException
0309:             */
0310:            public static void assertXMLNotEqual(String err,
0311:                    InputSource control, InputSource test) throws SAXException,
0312:                    IOException {
0313:                Diff diff = new Diff(control, test);
0314:                assertXMLEqual(err, diff, false);
0315:            }
0316:
0317:            /**
0318:             * Assert that two XML documents are NOT similar
0319:             * @param err Message to be displayed on assertion failure
0320:             * @param control XML to be compared against
0321:             * @param test XML to be tested
0322:             * @throws SAXException
0323:             * @throws IOException
0324:             */
0325:            public static void assertXMLNotEqual(String err, String control,
0326:                    String test) throws SAXException, IOException {
0327:                Diff diff = new Diff(control, test);
0328:                assertXMLEqual(err, diff, false);
0329:            }
0330:
0331:            /**
0332:             * Assert that two XML documents are NOT similar
0333:             * @param err Message to be displayed on assertion failure
0334:             * @param control XML to be compared against
0335:             * @param test XML to be tested
0336:             */
0337:            public static void assertXMLNotEqual(String err, Document control,
0338:                    Document test) {
0339:                Diff diff = new Diff(control, test);
0340:                assertXMLEqual(err, diff, false);
0341:            }
0342:
0343:            /**
0344:             * Assert that two XML documents are NOT similar
0345:             * @param err Message to be displayed on assertion failure
0346:             * @param control XML to be compared against
0347:             * @param test XML to be tested
0348:             * @throws SAXException
0349:             * @throws IOException
0350:             */
0351:            public static void assertXMLNotEqual(String err, Reader control,
0352:                    Reader test) throws SAXException, IOException {
0353:                Diff diff = new Diff(control, test);
0354:                assertXMLEqual(err, diff, false);
0355:            }
0356:
0357:            /**
0358:             * Assert that the node lists of two Xpaths in the same document are equal
0359:             * @param xpathOne
0360:             * @param xpathTwo
0361:             * @param document
0362:             * @see XpathEngine
0363:             */
0364:            public static void assertXpathsEqual(String controlXpath,
0365:                    String testXpath, Document document) throws XpathException {
0366:                assertXpathsEqual(controlXpath, document, testXpath, document);
0367:            }
0368:
0369:            /**
0370:             * Assert that the node lists of two Xpaths in the same document are equal
0371:             * @param xpathOne
0372:             * @param xpathTwo
0373:             * @param document
0374:             * @see XpathEngine
0375:             */
0376:            public static void assertXpathsEqual(String controlXpath,
0377:                    String testXpath, InputSource document)
0378:                    throws SAXException, IOException, XpathException {
0379:                assertXpathsEqual(controlXpath, testXpath, XMLUnit
0380:                        .buildControlDocument(document));
0381:            }
0382:
0383:            /**
0384:             * Assert that the node lists of two Xpaths in the same XML string are
0385:             * equal
0386:             * @param xpathOne
0387:             * @param xpathTwo
0388:             * @param inXMLString
0389:             * @throws SAXException
0390:             * @throws IOException
0391:             */
0392:            public static void assertXpathsEqual(String controlXpath,
0393:                    String testXpath, String inXMLString) throws SAXException,
0394:                    IOException, XpathException {
0395:                assertXpathsEqual(controlXpath, testXpath, XMLUnit
0396:                        .buildControlDocument(inXMLString));
0397:            }
0398:
0399:            /**
0400:             * Assert that the node lists of two Xpaths in two documents are equal
0401:             * @param xpathOne
0402:             * @param xpathTwo
0403:             * @param controlDocument
0404:             * @param testDocument
0405:             * @see XpathEngine
0406:             */
0407:            public static void assertXpathsEqual(String controlXpath,
0408:                    InputSource controlDocument, String testXpath,
0409:                    InputSource testDocument) throws SAXException, IOException,
0410:                    XpathException {
0411:                assertXpathsEqual(controlXpath, XMLUnit
0412:                        .buildControlDocument(controlDocument), testXpath,
0413:                        XMLUnit.buildTestDocument(testDocument));
0414:            }
0415:
0416:            /**
0417:             * Assert that the node lists of two Xpaths in two XML strings are equal
0418:             * @param xpathOne
0419:             * @param inControlXMLString
0420:             * @param xpathTwo
0421:             * @param inTestXMLString
0422:             * @throws SAXException
0423:             * @throws IOException
0424:             */
0425:            public static void assertXpathsEqual(String controlXpath,
0426:                    String inControlXMLString, String testXpath,
0427:                    String inTestXMLString) throws SAXException, IOException,
0428:                    XpathException {
0429:                assertXpathsEqual(controlXpath, XMLUnit
0430:                        .buildControlDocument(inControlXMLString), testXpath,
0431:                        XMLUnit.buildTestDocument(inTestXMLString));
0432:            }
0433:
0434:            /**
0435:             * Assert that the node lists of two Xpaths in two documents are equal
0436:             * @param xpathOne
0437:             * @param xpathTwo
0438:             * @param controlDocument
0439:             * @param testDocument
0440:             * @see XpathEngine
0441:             */
0442:            public static void assertXpathsEqual(String controlXpath,
0443:                    Document controlDocument, String testXpath,
0444:                    Document testDocument) throws XpathException {
0445:                assertXpathEquality(controlXpath, controlDocument, testXpath,
0446:                        testDocument, true);
0447:            }
0448:
0449:            /**
0450:             * Assert that the node lists of two Xpaths in the same document are NOT equal
0451:             * @param xpathOne
0452:             * @param xpathTwo
0453:             * @param document
0454:             * @see XpathEngine
0455:             */
0456:            public static void assertXpathsNotEqual(String controlXpath,
0457:                    String testXpath, Document document) throws XpathException {
0458:                assertXpathsNotEqual(controlXpath, document, testXpath,
0459:                        document);
0460:            }
0461:
0462:            /**
0463:             * Assert that the node lists of two Xpaths in the same document are NOT equal
0464:             * @param xpathOne
0465:             * @param xpathTwo
0466:             * @param document
0467:             * @see XpathEngine
0468:             */
0469:            public static void assertXpathsNotEqual(String controlXpath,
0470:                    String testXpath, InputSource document)
0471:                    throws SAXException, IOException, XpathException {
0472:                assertXpathsNotEqual(controlXpath, testXpath, XMLUnit
0473:                        .buildControlDocument(document));
0474:            }
0475:
0476:            /**
0477:             * Assert that the node lists of two Xpaths in the same XML string are NOT
0478:             * equal
0479:             * @param xpathOne
0480:             * @param xpathTwo
0481:             * @param inXMLString
0482:             * @throws SAXException
0483:             * @throws IOException
0484:             */
0485:            public static void assertXpathsNotEqual(String controlXpath,
0486:                    String testXpath, String inXMLString) throws SAXException,
0487:                    IOException, XpathException {
0488:                assertXpathsNotEqual(controlXpath, testXpath, XMLUnit
0489:                        .buildControlDocument(inXMLString));
0490:            }
0491:
0492:            /**
0493:             * Assert that the node lists of two Xpaths in two XML strings are NOT equal
0494:             * @param xpathOne
0495:             * @param inControlXMLString
0496:             * @param xpathTwo
0497:             * @param inTestXMLString
0498:             * @throws SAXException
0499:             * @throws IOException
0500:             */
0501:            public static void assertXpathsNotEqual(String controlXpath,
0502:                    String inControlXMLString, String testXpath,
0503:                    String inTestXMLString) throws SAXException, IOException,
0504:                    XpathException {
0505:                assertXpathsNotEqual(controlXpath, XMLUnit
0506:                        .buildControlDocument(inControlXMLString), testXpath,
0507:                        XMLUnit.buildTestDocument(inTestXMLString));
0508:            }
0509:
0510:            /**
0511:             * Assert that the node lists of two Xpaths in two XML strings are
0512:             * NOT equal
0513:             * @param xpathOne
0514:             * @param controlDocument
0515:             * @param xpathTwo
0516:             * @param testDocument
0517:             * @throws SAXException
0518:             * @throws IOException
0519:             */
0520:            public static void assertXpathsNotEqual(String controlXpath,
0521:                    InputSource controlDocument, String testXpath,
0522:                    InputSource testDocument) throws SAXException, IOException,
0523:                    XpathException {
0524:                assertXpathsNotEqual(controlXpath, XMLUnit
0525:                        .buildControlDocument(controlDocument), testXpath,
0526:                        XMLUnit.buildTestDocument(testDocument));
0527:            }
0528:
0529:            /**
0530:             * Assert that the node lists of two Xpaths in two documents are NOT equal
0531:             * @param xpathOne
0532:             * @param xpathTwo
0533:             * @param document
0534:             * @see XpathEngine
0535:             */
0536:            public static void assertXpathsNotEqual(String controlXpath,
0537:                    Document controlDocument, String testXpath,
0538:                    Document testDocument) throws XpathException {
0539:                assertXpathEquality(controlXpath, controlDocument, testXpath,
0540:                        testDocument, false);
0541:            }
0542:
0543:            /**
0544:             * Assert that the node lists of two Xpaths in two documents are
0545:             * equal or not.
0546:             * @param xpathOne
0547:             * @param xpathTwo
0548:             * @param document
0549:             * @param equality whether the values should be equal.
0550:             * @see XpathEngine
0551:             */
0552:            private static void assertXpathEquality(String controlXpath,
0553:                    Document controlDocument, String testXpath,
0554:                    Document testDocument, boolean equal) throws XpathException {
0555:                XpathEngine xpath = XMLUnit.newXpathEngine();
0556:                Diff diff = new Diff(asXpathResultDocument(XMLUnit
0557:                        .newControlParser(), xpath.getMatchingNodes(
0558:                        controlXpath, controlDocument)), asXpathResultDocument(
0559:                        XMLUnit.newTestParser(), xpath.getMatchingNodes(
0560:                                testXpath, testDocument)));
0561:                assertXMLEqual(diff, equal);
0562:            }
0563:
0564:            /**
0565:             * Assert that the evaluation of two Xpaths in the same document are equal
0566:             * @param xpathOne
0567:             * @param xpathTwo
0568:             * @param document
0569:             * @see XpathEngine
0570:             */
0571:            public static void assertXpathValuesEqual(String controlXpath,
0572:                    String testXpath, Document document) throws XpathException {
0573:                assertXpathValuesEqual(controlXpath, document, testXpath,
0574:                        document);
0575:            }
0576:
0577:            /**
0578:             * Assert that the evaluation of two Xpaths in the same XML string are
0579:             *  equal
0580:             * @param xpathOne
0581:             * @param xpathTwo
0582:             * @param document
0583:             * @throws SAXException
0584:             * @throws IOException
0585:             */
0586:            public static void assertXpathValuesEqual(String controlXpath,
0587:                    String testXpath, InputSource document)
0588:                    throws SAXException, IOException, XpathException {
0589:                assertXpathValuesEqual(controlXpath, testXpath, XMLUnit
0590:                        .buildControlDocument(document));
0591:            }
0592:
0593:            /**
0594:             * Assert that the evaluation of two Xpaths in the same XML string are
0595:             *  equal
0596:             * @param xpathOne
0597:             * @param xpathTwo
0598:             * @param inXMLString
0599:             * @throws SAXException
0600:             * @throws IOException
0601:             */
0602:            public static void assertXpathValuesEqual(String controlXpath,
0603:                    String testXpath, String inXMLString) throws SAXException,
0604:                    IOException, XpathException {
0605:                assertXpathValuesEqual(controlXpath, testXpath, XMLUnit
0606:                        .buildControlDocument(inXMLString));
0607:            }
0608:
0609:            /**
0610:             * Assert that the evaluation of two Xpaths in two XML strings are equal
0611:             * @param xpathOne
0612:             * @param control
0613:             * @param xpathTwo
0614:             * @param test
0615:             * @throws SAXException
0616:             * @throws IOException
0617:             */
0618:            public static void assertXpathValuesEqual(String controlXpath,
0619:                    InputSource control, String testXpath, InputSource test)
0620:                    throws SAXException, IOException, XpathException {
0621:                assertXpathValuesEqual(controlXpath, XMLUnit
0622:                        .buildControlDocument(control), testXpath, XMLUnit
0623:                        .buildTestDocument(test));
0624:            }
0625:
0626:            /**
0627:             * Assert that the evaluation of two Xpaths in two XML strings are equal
0628:             * @param xpathOne
0629:             * @param inControlXMLString
0630:             * @param xpathTwo
0631:             * @param inTestXMLString
0632:             * @throws SAXException
0633:             * @throws IOException
0634:             */
0635:            public static void assertXpathValuesEqual(String controlXpath,
0636:                    String inControlXMLString, String testXpath,
0637:                    String inTestXMLString) throws SAXException, IOException,
0638:                    XpathException {
0639:                assertXpathValuesEqual(controlXpath, XMLUnit
0640:                        .buildControlDocument(inControlXMLString), testXpath,
0641:                        XMLUnit.buildTestDocument(inTestXMLString));
0642:            }
0643:
0644:            /**
0645:             * Assert that the evaluation of two Xpaths in two documents are equal
0646:             * @param xpathOne
0647:             * @param xpathTwo
0648:             * @param document
0649:             * @see XpathEngine
0650:             */
0651:            public static void assertXpathValuesEqual(String controlXpath,
0652:                    Document controlDocument, String testXpath,
0653:                    Document testDocument) throws XpathException {
0654:                XpathEngine xpath = XMLUnit.newXpathEngine();
0655:                assertEquals(xpath.evaluate(controlXpath, controlDocument),
0656:                        xpath.evaluate(testXpath, testDocument));
0657:            }
0658:
0659:            /**
0660:             * Assert that the evaluation of two Xpaths in the same XML string are
0661:             * NOT equal
0662:             * @param xpathOne
0663:             * @param xpathTwo
0664:             * @param control
0665:             * @throws SAXException
0666:             * @throws IOException
0667:             */
0668:            public static void assertXpathValuesNotEqual(String controlXpath,
0669:                    String testXpath, InputSource control) throws SAXException,
0670:                    IOException, XpathException {
0671:                assertXpathValuesNotEqual(controlXpath, testXpath, XMLUnit
0672:                        .buildControlDocument(control));
0673:            }
0674:
0675:            /**
0676:             * Assert that the evaluation of two Xpaths in the same XML string are
0677:             * NOT equal
0678:             * @param xpathOne
0679:             * @param xpathTwo
0680:             * @param inXMLString
0681:             * @throws SAXException
0682:             * @throws IOException
0683:             */
0684:            public static void assertXpathValuesNotEqual(String controlXpath,
0685:                    String testXpath, String inXMLString) throws SAXException,
0686:                    IOException, XpathException {
0687:                assertXpathValuesNotEqual(controlXpath, testXpath, XMLUnit
0688:                        .buildControlDocument(inXMLString));
0689:            }
0690:
0691:            /**
0692:             * Assert that the evaluation of two Xpaths in the same document are
0693:             * NOT equal
0694:             * @param xpathOne
0695:             * @param xpathTwo
0696:             * @param document
0697:             */
0698:            public static void assertXpathValuesNotEqual(String controlXpath,
0699:                    String testXpath, Document document) throws XpathException {
0700:                assertXpathValuesNotEqual(controlXpath, document, testXpath,
0701:                        document);
0702:            }
0703:
0704:            /**
0705:             * Assert that the evaluation of two Xpaths in two XML strings are
0706:             * NOT equal
0707:             * @param xpathOne
0708:             * @param control
0709:             * @param xpathTwo
0710:             * @param test
0711:             * @throws SAXException
0712:             * @throws IOException
0713:             */
0714:            public static void assertXpathValuesNotEqual(String controlXpath,
0715:                    InputSource control, String testXpath, InputSource test)
0716:                    throws SAXException, IOException, XpathException {
0717:                assertXpathValuesNotEqual(controlXpath, XMLUnit
0718:                        .buildControlDocument(control), testXpath, XMLUnit
0719:                        .buildTestDocument(test));
0720:            }
0721:
0722:            /**
0723:             * Assert that the evaluation of two Xpaths in two XML strings are
0724:             * NOT equal
0725:             * @param xpathOne
0726:             * @param inControlXMLString
0727:             * @param xpathTwo
0728:             * @param inTestXMLString
0729:             * @throws SAXException
0730:             * @throws IOException
0731:             */
0732:            public static void assertXpathValuesNotEqual(String controlXpath,
0733:                    String inControlXMLString, String testXpath,
0734:                    String inTestXMLString) throws SAXException, IOException,
0735:                    XpathException {
0736:                assertXpathValuesNotEqual(controlXpath, XMLUnit
0737:                        .buildControlDocument(inControlXMLString), testXpath,
0738:                        XMLUnit.buildTestDocument(inTestXMLString));
0739:            }
0740:
0741:            /**
0742:             * Assert that the evaluation of two Xpaths in two documents are
0743:             * NOT equal
0744:             * @param xpathOne
0745:             * @param xpathTwo
0746:             * @param document
0747:             */
0748:            public static void assertXpathValuesNotEqual(String controlXpath,
0749:                    Document controlDocument, String testXpath,
0750:                    Document testDocument) throws XpathException {
0751:                XpathEngine xpath = XMLUnit.newXpathEngine();
0752:                String control = xpath.evaluate(controlXpath, controlDocument);
0753:                String test = xpath.evaluate(testXpath, testDocument);
0754:                if (control != null) {
0755:                    if (control.equals(test)) {
0756:                        fail("Expected test value NOT to be equal to control but both were "
0757:                                + test);
0758:                    }
0759:                } else if (test != null) {
0760:                    fail("control xPath evaluated to empty node set, "
0761:                            + "but test xPath evaluated to " + test);
0762:                }
0763:            }
0764:
0765:            /**
0766:             * Assert the value of an Xpath expression in an XML document.
0767:             * @param expectedValue
0768:             * @param xpathExpression
0769:             * @param control
0770:             * @throws SAXException
0771:             * @throws IOException
0772:             * @see XpathEngine which provides the underlying evaluation mechanism
0773:             */
0774:            public static void assertXpathEvaluatesTo(String expectedValue,
0775:                    String xpathExpression, InputSource control)
0776:                    throws SAXException, IOException, XpathException {
0777:                Document document = XMLUnit.buildControlDocument(control);
0778:                assertXpathEvaluatesTo(expectedValue, xpathExpression, document);
0779:            }
0780:
0781:            /**
0782:             * Assert the value of an Xpath expression in an XML String
0783:             * @param expectedValue
0784:             * @param xpathExpression
0785:             * @param inXMLString
0786:             * @throws SAXException
0787:             * @throws IOException
0788:             * @see XpathEngine which provides the underlying evaluation mechanism
0789:             */
0790:            public static void assertXpathEvaluatesTo(String expectedValue,
0791:                    String xpathExpression, String inXMLString)
0792:                    throws SAXException, IOException, XpathException {
0793:                Document document = XMLUnit.buildControlDocument(inXMLString);
0794:                assertXpathEvaluatesTo(expectedValue, xpathExpression, document);
0795:            }
0796:
0797:            /**
0798:             * Assert the value of an Xpath expression in an DOM Document
0799:             * @param expectedValue
0800:             * @param xpathExpression
0801:             * @param inDocument
0802:             * @see XpathEngine which provides the underlying evaluation mechanism
0803:             */
0804:            public static void assertXpathEvaluatesTo(String expectedValue,
0805:                    String xpathExpression, Document inDocument)
0806:                    throws XpathException {
0807:                XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
0808:                assertEquals(expectedValue, simpleXpathEngine.evaluate(
0809:                        xpathExpression, inDocument));
0810:            }
0811:
0812:            /**
0813:             * Assert that a specific XPath exists in some given XML
0814:             * @param inXpathExpression
0815:             * @param control
0816:             * @see XpathEngine which provides the underlying evaluation mechanism
0817:             */
0818:            public static void assertXpathExists(String xPathExpression,
0819:                    InputSource control) throws IOException, SAXException,
0820:                    XpathException {
0821:                Document inDocument = XMLUnit.buildControlDocument(control);
0822:                assertXpathExists(xPathExpression, inDocument);
0823:            }
0824:
0825:            /**
0826:             * Assert that a specific XPath exists in some given XML
0827:             * @param inXpathExpression
0828:             * @param inXMLString
0829:             * @see XpathEngine which provides the underlying evaluation mechanism
0830:             */
0831:            public static void assertXpathExists(String xPathExpression,
0832:                    String inXMLString) throws IOException, SAXException,
0833:                    XpathException {
0834:                Document inDocument = XMLUnit.buildControlDocument(inXMLString);
0835:                assertXpathExists(xPathExpression, inDocument);
0836:            }
0837:
0838:            /**
0839:             * Assert that a specific XPath exists in some given XML
0840:             * @param inXpathExpression
0841:             * @param inDocument
0842:             * @see XpathEngine which provides the underlying evaluation mechanism
0843:             */
0844:            public static void assertXpathExists(String xPathExpression,
0845:                    Document inDocument) throws XpathException {
0846:                XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
0847:                NodeList nodeList = simpleXpathEngine.getMatchingNodes(
0848:                        xPathExpression, inDocument);
0849:                int matches = nodeList.getLength();
0850:                assertTrue("Expecting to find matches for Xpath "
0851:                        + xPathExpression, matches > 0);
0852:            }
0853:
0854:            /**
0855:             * Assert that a specific XPath does NOT exist in some given XML
0856:             * @param inXpathExpression
0857:             * @param control
0858:             * @see XpathEngine which provides the underlying evaluation mechanism
0859:             */
0860:            public static void assertXpathNotExists(String xPathExpression,
0861:                    InputSource control) throws IOException, SAXException,
0862:                    XpathException {
0863:                Document inDocument = XMLUnit.buildControlDocument(control);
0864:                assertXpathNotExists(xPathExpression, inDocument);
0865:            }
0866:
0867:            /**
0868:             * Assert that a specific XPath does NOT exist in some given XML
0869:             * @param inXpathExpression
0870:             * @param inXMLString
0871:             * @see XpathEngine which provides the underlying evaluation mechanism
0872:             */
0873:            public static void assertXpathNotExists(String xPathExpression,
0874:                    String inXMLString) throws IOException, SAXException,
0875:                    XpathException {
0876:                Document inDocument = XMLUnit.buildControlDocument(inXMLString);
0877:                assertXpathNotExists(xPathExpression, inDocument);
0878:            }
0879:
0880:            /**
0881:             * Assert that a specific XPath does NOT exist in some given XML
0882:             * @param inXpathExpression
0883:             * @param inDocument
0884:             * @see XpathEngine which provides the underlying evaluation mechanism
0885:             */
0886:            public static void assertXpathNotExists(String xPathExpression,
0887:                    Document inDocument) throws XpathException {
0888:                XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
0889:                NodeList nodeList = simpleXpathEngine.getMatchingNodes(
0890:                        xPathExpression, inDocument);
0891:                int matches = nodeList.getLength();
0892:                assertEquals("Should be zero matches for Xpath "
0893:                        + xPathExpression, 0, matches);
0894:            }
0895:
0896:            /**
0897:             * Assert that an InputSource containing XML contains valid XML:
0898:             * the document must contain a DOCTYPE declaration to be validated
0899:             * @param xml
0900:             * @throws SAXException
0901:             * @throws ConfigurationException if validation could not be turned on
0902:             * @see Validator
0903:             */
0904:            public static void assertXMLValid(InputSource xml)
0905:                    throws SAXException, ConfigurationException {
0906:                assertXMLValid(new Validator(xml));
0907:            }
0908:
0909:            /**
0910:             * Assert that a String containing XML contains valid XML: the String must
0911:             * contain a DOCTYPE declaration to be validated
0912:             * @param xmlString
0913:             * @throws SAXException
0914:             * @throws ConfigurationException if validation could not be turned on
0915:             * @see Validator
0916:             */
0917:            public static void assertXMLValid(String xmlString)
0918:                    throws SAXException, ConfigurationException {
0919:                assertXMLValid(new Validator(xmlString));
0920:            }
0921:
0922:            /**
0923:             * Assert that an InputSource containing XML contains valid XML:
0924:             * the document must contain a DOCTYPE to be validated, but the
0925:             * validation will use the systemId to obtain the DTD
0926:             * @param xml
0927:             * @param systemId
0928:             * @throws SAXException
0929:             * @throws ConfigurationException if validation could not be turned on
0930:             * @see Validator
0931:             */
0932:            public static void assertXMLValid(InputSource xml, String systemId)
0933:                    throws SAXException, ConfigurationException {
0934:                assertXMLValid(new Validator(xml, systemId));
0935:            }
0936:
0937:            /**
0938:             * Assert that a String containing XML contains valid XML: the String must
0939:             * contain a DOCTYPE to be validated, but the validation will use the
0940:             * systemId to obtain the DTD
0941:             * @param xmlString
0942:             * @param systemId
0943:             * @throws SAXException
0944:             * @throws ConfigurationException if validation could not be turned on
0945:             * @see Validator
0946:             */
0947:            public static void assertXMLValid(String xmlString, String systemId)
0948:                    throws SAXException, ConfigurationException {
0949:                assertXMLValid(new Validator(xmlString, systemId));
0950:            }
0951:
0952:            /**
0953:             * Assert that a piece of XML contains valid XML: the document
0954:             * will be given a DOCTYPE to be validated with the name and
0955:             * systemId specified regardless of whether it already contains a
0956:             * doctype declaration.
0957:             * @param xml
0958:             * @param systemId
0959:             * @param doctype
0960:             * @throws SAXException
0961:             * @throws ConfigurationException if validation could not be turned on
0962:             * @see Validator
0963:             */
0964:            public static void assertXMLValid(InputSource xml, String systemId,
0965:                    String doctype) throws SAXException, ConfigurationException {
0966:                assertXMLValid(new Validator(xml, systemId, doctype));
0967:            }
0968:
0969:            /**
0970:             * Assert that a String containing XML contains valid XML: the String will
0971:             * be given a DOCTYPE to be validated with the name and systemId specified
0972:             * regardless of whether it already contains a doctype declaration.
0973:             * @param xmlString
0974:             * @param systemId
0975:             * @param doctype
0976:             * @throws SAXException
0977:             * @throws ConfigurationException if validation could not be turned on
0978:             * @see Validator
0979:             */
0980:            public static void assertXMLValid(String xmlString,
0981:                    String systemId, String doctype) throws SAXException,
0982:                    ConfigurationException {
0983:                assertXMLValid(new Validator(new StringReader(xmlString),
0984:                        systemId, doctype));
0985:            }
0986:
0987:            /**
0988:             * Assert that a Validator instance returns <code>isValid() == true</code>
0989:             * @param validator
0990:             */
0991:            public static void assertXMLValid(Validator validator) {
0992:                assertEquals(validator.toString(), true, validator.isValid());
0993:            }
0994:
0995:            /**
0996:             * Execute a <code>NodeTest<code> for a single node type
0997:             * and assert that it passes
0998:             * @param xml XML to be tested
0999:             * @param tester The test strategy
1000:             * @param nodeType The node type to be tested: constants defined
1001:             *  in {@link Node org.w3c.dom.Node} e.g. <code>Node.ELEMENT_NODE</code>
1002:             * @throws SAXException
1003:             * @throws IOException
1004:             * @see AbstractNodeTester
1005:             * @see CountingNodeTester
1006:             */
1007:            public static void assertNodeTestPasses(InputSource xml,
1008:                    NodeTester tester, short nodeType) throws SAXException,
1009:                    IOException {
1010:                NodeTest test = new NodeTest(xml);
1011:                assertNodeTestPasses(test, tester, new short[] { nodeType },
1012:                        true);
1013:            }
1014:
1015:            /**
1016:             * Execute a <code>NodeTest<code> for a single node type
1017:             * and assert that it passes
1018:             * @param xmlString XML to be tested
1019:             * @param tester The test strategy
1020:             * @param nodeType The node type to be tested: constants defined
1021:             *  in {@link Node org.w3c.dom.Node} e.g. <code>Node.ELEMENT_NODE</code>
1022:             * @throws SAXException
1023:             * @throws IOException
1024:             * @see AbstractNodeTester
1025:             * @see CountingNodeTester
1026:             */
1027:            public static void assertNodeTestPasses(String xmlString,
1028:                    NodeTester tester, short nodeType) throws SAXException,
1029:                    IOException {
1030:                NodeTest test = new NodeTest(xmlString);
1031:                assertNodeTestPasses(test, tester, new short[] { nodeType },
1032:                        true);
1033:            }
1034:
1035:            /**
1036:             * Execute a <code>NodeTest<code> for multiple node types and make an
1037:             * assertion about it whether it is expected to pass
1038:             * @param test a NodeTest instance containing the XML source to be tested
1039:             * @param tester The test strategy
1040:             * @param nodeTypes The node types to be tested: constants defined
1041:             *  in {@link Node org.w3c.dom.Node} e.g. <code>Node.ELEMENT_NODE</code>
1042:             * @param assertion true if the test is expected to pass, false otherwise
1043:             * @see AbstractNodeTester
1044:             * @see CountingNodeTester
1045:             */
1046:            public static void assertNodeTestPasses(NodeTest test,
1047:                    NodeTester tester, short[] nodeTypes, boolean assertion) {
1048:                try {
1049:                    test.performTest(tester, nodeTypes);
1050:                    if (!assertion) {
1051:                        fail("Expected node test to fail, but it passed!");
1052:                    }
1053:                } catch (NodeTestException e) {
1054:                    if (assertion) {
1055:                        fail("Expected node test to pass, but it failed! "
1056:                                + e.getMessage());
1057:                    }
1058:                }
1059:            }
1060:
1061:            private static Document asXpathResultDocument(
1062:                    final DocumentBuilder builder, final NodeList nodes) {
1063:                final Document d = builder.newDocument();
1064:                final Element root = d.createElement("xpathResult");
1065:                d.appendChild(root);
1066:                final int length = nodes.getLength();
1067:                for (int i = 0; i < length; i++) {
1068:                    root.appendChild(d.importNode(nodes.item(i), true));
1069:                }
1070:                return d;
1071:            }
1072:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.