001: /*
002: ******************************************************************
003: Copyright (c) 2001-2007, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit;
038:
039: import java.io.File;
040: import java.io.FileReader;
041: import java.io.FileWriter;
042: import java.io.Reader;
043: import java.util.HashSet;
044: import java.util.Set;
045:
046: import junit.framework.TestCase;
047:
048: import org.w3c.dom.Document;
049: import org.w3c.dom.Element;
050: import org.w3c.dom.Node;
051: import org.w3c.dom.Text;
052:
053: /**
054: * Test a Diff
055: */
056: public class test_Diff extends TestCase {
057: private static final String[] control = new String[] {
058: "<test/>",
059: "<test></test>",
060: "<test>test</test>",
061: "<test test=\"test\">test</test>",
062: "<test/>",
063: "<test>test</test>",
064: "<test test=\"test\"/>",
065: "<test><test><test></test></test></test>",
066: "<test test=\"test\"><test>test<test>test</test></test></test>",
067: "<test test=\"test\"><test>test<test>test</test></test></test>",
068: "<html>Yo this is a test!</html>", "<java></java>" };
069: private static final String[] test = new String[] {
070: "<fail/>",
071: "<fail/>",
072: "<fail>test</fail>",
073: "<test>test</test>",
074: "<fail/>",
075: "<test>fail</test>",
076: "<test test=\"fail\"/>",
077: "<test><test><test>test</test></test></test>",
078: "<test test=\"test\"><test>fail<test>test</test></test></test>",
079: "<test test=\"fail\"><test>test<test>test</test></test></test>",
080: "<html>Yo this isn't a test!</html>",
081: "<java><package-def><ident>org</ident><dot/><ident>apache</ident><dot/><ident>test</ident></package-def></java>" };
082: private Document aDocument;
083:
084: public void setUp() throws Exception {
085: aDocument = XMLUnit.newControlParser().newDocument();
086: }
087:
088: public void testToString() {
089: Diff diff = buildDiff(aDocument, aDocument);
090: String[] animals = { "Monkey", "Chicken" };
091: String tag = "tag";
092: Element elemA = aDocument.createElement(tag);
093: Text textA = aDocument.createTextNode(animals[0]);
094: elemA.appendChild(textA);
095:
096: Element elemB = aDocument.createElement(tag);
097: Difference difference = new Difference(
098: DifferenceConstants.HAS_CHILD_NODES, new NodeDetail(
099: Boolean.TRUE.toString(), elemA, "/tag"),
100: new NodeDetail(Boolean.FALSE.toString(), elemB, "/tag"));
101: diff.differenceFound(difference);
102:
103: assertEquals(
104: diff.getClass().getName()
105: + "\n[different] Expected "
106: + DifferenceConstants.HAS_CHILD_NODES
107: .getDescription()
108: + " 'true' but was 'false' - comparing <tag...> at /tag to <tag...> at /tag\n",
109: diff.toString());
110:
111: diff = buildDiff(aDocument, aDocument);
112: Text textB = aDocument.createTextNode(animals[1]);
113: elemB.appendChild(textB);
114: difference = new Difference(DifferenceConstants.TEXT_VALUE,
115: new NodeDetail(animals[0], textA, "/tag/text()"),
116: new NodeDetail(animals[1], textB, "/tag/text()"));
117: diff.differenceFound(difference);
118:
119: assertEquals(
120: diff.getClass().getName()
121: + "\n[different] Expected "
122: + DifferenceConstants.TEXT_VALUE
123: .getDescription()
124: + " 'Monkey' but was 'Chicken' - comparing <tag ...>Monkey</tag> "
125: + "at /tag/text() to <tag ...>Chicken</tag> at /tag/text()\n",
126: diff.toString());
127:
128: }
129:
130: /**
131: * Tests the compare method
132: */
133: public void testSimilar() throws Exception {
134: for (int i = 0; i < control.length; i++) {
135: assertEquals("XMLUnit.compare().similar() test case " + i
136: + " failed", true,
137: buildDiff(control[i], control[i]).similar());
138: assertEquals("!XMLUnit.compare().similar() test case " + i
139: + " failed", false,
140: (buildDiff(control[i], test[i])).similar());
141: }
142: }
143:
144: public void testIdentical() throws Exception {
145: String control = "<control><test>test1</test><test>test2</test></control>";
146: String test = "<control><test>test2</test><test>test1</test></control>";
147:
148: assertEquals("Documents are identical, when they are not",
149: false, buildDiff(control, test).identical());
150: }
151:
152: public void testFiles() throws Exception {
153: FileReader control = new FileReader(test_Constants.BASEDIR
154: + "/tests/etc/test.blame.html");
155: FileReader test = new FileReader(test_Constants.BASEDIR
156: + "/tests/etc/test.blame.html");
157: Diff diff = buildDiff(control, test);
158: assertEquals(diff.toString(), true, diff.identical());
159: }
160:
161: public void testSameTwoStrings() throws Exception {
162: Diff diff = buildDiff("<same>pass</same>", "<same>pass</same>");
163: assertEquals("same should be identical", true, diff.identical());
164: assertEquals("same should be similar", true, diff.similar());
165: }
166:
167: public void testMissingElement() throws Exception {
168: Diff diff = buildDiff("<root></root>", "<root><node/></root>");
169: assertEquals("should not be identical", false, diff.identical());
170: assertEquals("and should not be similar", false, diff.similar());
171: }
172:
173: public void testExtraElement() throws Exception {
174: Diff diff = buildDiff("<root><node/></root>", "<root></root>");
175: assertEquals("should not be identical", false, diff.identical());
176: assertEquals("and should not be similar", false, diff.similar());
177: }
178:
179: public void testElementsInReverseOrder() throws Exception {
180: Diff diff = buildDiff("<root><same/><pass/></root>",
181: "<root><pass/><same/></root>");
182: assertEquals("should not be identical", false, diff.identical());
183: assertEquals("but should be similar", true, diff.similar());
184: }
185:
186: public void testMissingAttribute() throws Exception {
187: Diff diff = buildDiff("<same>pass</same>",
188: "<same except=\"this\">pass</same>");
189: assertEquals("should not be identical", false, diff.identical());
190: assertEquals("and should not be similar", false, diff.similar());
191: }
192:
193: public void testExtraAttribute() throws Exception {
194: Diff diff = buildDiff("<same except=\"this\">pass</same>",
195: "<same>pass</same>");
196: assertEquals("should not be identical", false, diff.identical());
197: assertEquals("and should not be similar", false, diff.similar());
198: }
199:
200: public void testAttributesInReverseOrder() throws Exception {
201: Diff diff = buildDiff(
202: "<same zzz=\"qwerty\" aaa=\"uiop\">pass</same>",
203: "<same aaa=\"uiop\" zzz=\"qwerty\">pass</same>");
204: if (diff.identical()) {
205: System.out
206: .println(getName()
207: + " - should not ideally be identical "
208: + "but JAXP implementations can reorder attributes inside NamedNodeMap");
209: }
210: assertEquals(diff.toString() + ": but should be similar", true,
211: diff.similar());
212: }
213:
214: public void testDiffStringWithAttributes() throws Exception {
215: final String fruitBat = "<bat type=\"fruit\"/>", longEaredBat = "<bat type=\"longeared\"/>";
216: Diff diff = buildDiff(fruitBat, longEaredBat);
217: assertEquals(
218: diff.getClass().getName()
219: + "\n[different] Expected "
220: + DifferenceConstants.ATTR_VALUE
221: .getDescription()
222: + " 'fruit' but was 'longeared' - comparing "
223: + "<bat type=\"fruit\"...> at /bat[1]/@type to <bat type=\"longeared\"...> at /bat[1]/@type\n",
224: diff.toString());
225: }
226:
227: public void testXMLWithDTD() throws Exception {
228: String aDTDpart = "<!DOCTYPE test ["
229: + "<!ELEMENT assertion EMPTY>"
230: + "<!ATTLIST assertion result (pass|fail) \"fail\">"
231: + "<!ELEMENT test (assertion)*>";
232: String aDTD = aDTDpart + "]>";
233: String xmlWithoutDTD = "<test>"
234: + "<assertion result=\"pass\"/>"
235: + "<assertion result=\"fail\"/>" + "</test>";
236: String xmlWithDTD = aDTD + xmlWithoutDTD;
237: Diff diff = buildDiff(xmlWithDTD, xmlWithoutDTD);
238: assertTrue("similar. " + diff.toString(), diff.similar());
239: assertTrue("not identical. " + diff.toString(), !diff
240: .identical());
241:
242: File tempDtdFile = File.createTempFile(getName(), "dtd");
243: tempDtdFile.deleteOnExit();
244: FileWriter dtdWriter = new FileWriter(tempDtdFile);
245: dtdWriter.write(aDTD);
246: try {
247: String xmlWithExternalDTD = "<!DOCTYPE test SYSTEM \""
248: + tempDtdFile.toURL().toExternalForm() + "\">"
249: + xmlWithoutDTD;
250: diff = buildDiff(xmlWithDTD, xmlWithExternalDTD);
251: assertTrue("similar again. " + diff.toString(), diff
252: .similar());
253: assertTrue("not identical again. " + diff.toString(), !diff
254: .identical());
255: } finally {
256: tempDtdFile.delete();
257: }
258:
259: String anotherDTD = aDTDpart + "<!ELEMENT comment (ANY)>"
260: + "]>";
261: String xmlWithAnotherDTD = anotherDTD + xmlWithoutDTD;
262: diff = buildDiff(xmlWithDTD, xmlWithAnotherDTD);
263: assertTrue("similar. " + diff.toString(), diff.similar());
264: assertTrue("amd identical as DTD content is not compared. "
265: + diff.toString(), diff.identical());
266: }
267:
268: /**
269: * Raised by aakture 25.04.2002
270: * Despite the name under which this defect was raised the issue is really
271: * about managing redundant whitespace
272: */
273: public void testXMLUnitDoesNotWorkWellWithFiles() throws Exception {
274: // to avoid test sequencing issues we need to restore whitespace setting
275: boolean startValueIgnoreWhitespace = XMLUnit
276: .getIgnoreWhitespace();
277: try {
278: XMLUnit.setIgnoreWhitespace(false);
279: Diff whitespaceAwareDiff = buildDiff(
280: test_Constants.XML_WITHOUT_WHITESPACE,
281: test_Constants.XML_WITH_WHITESPACE);
282: assertTrue(whitespaceAwareDiff.toString(),
283: !whitespaceAwareDiff.similar());
284:
285: XMLUnit.setIgnoreWhitespace(true);
286: Diff whitespaceIgnoredDiff = buildDiff(
287: test_Constants.XML_WITHOUT_WHITESPACE,
288: test_Constants.XML_WITH_WHITESPACE);
289: assertTrue(whitespaceIgnoredDiff.toString(),
290: whitespaceIgnoredDiff.similar());
291: } finally {
292: XMLUnit.setIgnoreWhitespace(startValueIgnoreWhitespace);
293: }
294: }
295:
296: public void testCommentHandlingDoesntAffectWhitespaceHandling()
297: throws Exception {
298: try {
299: XMLUnit.setIgnoreComments(true);
300: testXMLUnitDoesNotWorkWellWithFiles();
301: } finally {
302: XMLUnit.setIgnoreComments(false);
303: }
304: }
305:
306: public void testNormalizationDoesntAffectWhitespaceHandling()
307: throws Exception {
308: try {
309: XMLUnit.setNormalize(true);
310: testXMLUnitDoesNotWorkWellWithFiles();
311: } finally {
312: XMLUnit.setNormalize(false);
313: }
314: }
315:
316: /**
317: * Raised 15.05.2002
318: */
319: public void testNamespaceIssues() throws Exception {
320: String control = "<control:abc xmlns:control=\"http://yada.com\">"
321: + "<control:xyz>text</control:xyz></control:abc>";
322: Replacement replace = new Replacement("control", "test");
323: String test = replace.replace(control);
324:
325: Diff diff = buildDiff(control, test);
326: assertTrue("a-" + diff.toString(), diff.similar());
327: assertTrue("b-" + diff.toString(), !diff.identical());
328:
329: Diff reverseDiff = buildDiff(test, control);
330: assertTrue("c-" + reverseDiff.toString(), reverseDiff.similar());
331: assertTrue("d-" + reverseDiff.toString(), !reverseDiff
332: .identical());
333: }
334:
335: /**
336: * Raised 16.05.2002
337: */
338: public void testDefaultNamespace() throws Exception {
339: String control = "<control:abc xmlns:control=\"http://yada.com\">"
340: + "<control:xyz>text</control:xyz></control:abc>";
341: Replacement replace = new Replacement("control:", "");
342: Replacement trim = new Replacement("xmlns:control", "xmlns");
343: String test = trim.replace(replace.replace(control));
344:
345: Diff diff = buildDiff(control, test);
346: assertTrue("a-" + diff.toString(), diff.similar());
347: assertTrue("b-" + diff.toString(), !diff.identical());
348:
349: Diff reverseDiff = buildDiff(test, control);
350: assertTrue("c-" + reverseDiff.toString(), reverseDiff.similar());
351: assertTrue("d-" + reverseDiff.toString(), !reverseDiff
352: .identical());
353: }
354:
355: public void testSameNameDifferentQName() throws Exception {
356: String control = "<ns1:root xmlns:ns1=\"http://example.org/ns1\" xmlns:ns2=\"http://example.org/ns2\">"
357: + "<ns1:branch>In namespace 1</ns1:branch>"
358: + "<ns2:branch>In namespace 2</ns2:branch>"
359: + "</ns1:root>";
360:
361: String test = "<ns1:root xmlns:ns1=\"http://example.org/ns1\" xmlns:ns2=\"http://example.org/ns2\">"
362: + "<ns2:branch>In namespace 2</ns2:branch>"
363: + "<ns1:branch>In namespace 1</ns1:branch>"
364: + "</ns1:root>";
365:
366: Diff diff = buildDiff(control, test);
367: assertTrue("a-" + diff.toString(), diff.similar());
368: assertTrue("b-" + diff.toString(), !diff.identical());
369:
370: Diff reverseDiff = buildDiff(test, control);
371: assertTrue("c-" + reverseDiff.toString(), reverseDiff.similar());
372: assertTrue("d-" + reverseDiff.toString(), !reverseDiff
373: .identical());
374: }
375:
376: public void testOverrideDifferenceListener() throws Exception {
377: String control = "<vehicles><car colour=\"white\">ford fiesta</car>"
378: + "<car colour=\"red\">citroen xsara</car></vehicles>";
379: String test = "<vehicles><car colour=\"white\">nissan primera</car>"
380: + "<car colour=\"blue\">peugot 206</car></vehicles>";
381: Diff diff = buildDiff(control, test);
382: assertTrue("initially " + diff.toString(), !diff.similar());
383:
384: Diff diffWithIdenticalOverride = buildDiff(control, test);
385: diffWithIdenticalOverride
386: .overrideDifferenceListener(new OverrideDifferenceListener(
387: DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL));
388: assertTrue("now identical"
389: + diffWithIdenticalOverride.toString(),
390: diffWithIdenticalOverride.identical());
391:
392: Diff diffWithSimilarOverride = buildDiff(control, test);
393: diffWithSimilarOverride
394: .overrideDifferenceListener(new OverrideDifferenceListener(
395: DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR));
396: assertTrue("no longer identical"
397: + diffWithSimilarOverride.toString(),
398: !diffWithSimilarOverride.identical());
399: assertTrue("but still similar"
400: + diffWithSimilarOverride.toString(),
401: diffWithSimilarOverride.similar());
402:
403: Diff diffWithOverride = buildDiff(control, test);
404: diffWithOverride
405: .overrideDifferenceListener(new OverrideDifferenceListener(
406: DifferenceListener.RETURN_ACCEPT_DIFFERENCE));
407: assertTrue("default behaviour" + diffWithOverride.toString(),
408: !diffWithOverride.similar());
409: }
410:
411: public void testNamespacedAttributes() throws Exception {
412: FileReader control = new FileReader(test_Constants.BASEDIR
413: + "/tests/etc/controlNamespaces.xml");
414: FileReader test = new FileReader(test_Constants.BASEDIR
415: + "/tests/etc/testNamespaces.xml");
416: Diff diff = buildDiff(control, test);
417: diff.overrideDifferenceListener(new ExpectedDifferenceListener(
418: DifferenceConstants.NAMESPACE_PREFIX_ID));
419: assertEquals(diff.toString(), false, diff.identical());
420: assertEquals(diff.toString(), true, diff.similar());
421: }
422:
423: public void testDifferentStructure() throws Exception {
424: String control = "<root><node>text</node></root>";
425: String test = "<root><node><inner-node>text</inner-node></node></root>";
426: Diff myDiff = buildDiff(control, test);
427: assertEquals(myDiff.toString(), false, myDiff.similar());
428: }
429:
430: public void testRepeatedElementNamesWithAttributeQualification1()
431: throws Exception {
432: Diff diff = buildDiff(
433: "<root><node id=\"1\"/><node id=\"2\"/></root>",
434: "<root><node id=\"2\"/><node id=\"1\"/></root>");
435: diff
436: .overrideElementQualifier(new ElementNameAndAttributeQualifier(
437: "id"));
438: assertFalse("should not be identical: " + diff.toString(), diff
439: .identical());
440: assertTrue("should be similar: " + diff.toString(), diff
441: .similar());
442: }
443:
444: public void testRepeatedElementNamesWithAttributeQualification2()
445: throws Exception {
446: Diff diff = buildDiff(
447: "<root><node id=\"1\" val=\"4\"/><node id=\"2\" val=\"3\"/></root>",
448: "<root><node id=\"2\" val=\"4\"/><node id=\"1\" val=\"3\"/></root>");
449: diff
450: .overrideElementQualifier(new ElementNameAndAttributeQualifier(
451: "id"));
452: assertFalse("should not be identical: " + diff.toString(), diff
453: .identical());
454: assertFalse("should not be similar: " + diff.toString(), diff
455: .similar());
456: }
457:
458: public void testRepeatedElementNamesWithAttributeQualification3()
459: throws Exception {
460: Diff diff = buildDiff(
461: "<root><node id=\"1\" val=\"4\"/><node id=\"2\" val=\"3\"/></root>",
462: "<root><node id=\"2\" val=\"3\"/><node id=\"1\" val=\"4\"/></root>");
463: diff
464: .overrideElementQualifier(new ElementNameAndAttributeQualifier());
465: assertFalse("should not be identical: " + diff.toString(), diff
466: .identical());
467: assertTrue("should be similar: " + diff.toString(), diff
468: .similar());
469: }
470:
471: public void testRepeatedElementNamesWithAttributeQualification4()
472: throws Exception {
473: Diff diff = buildDiff(
474: "<root><node id=\"1\" val=\"4\"/><node id=\"2\" val=\"3\"/></root>",
475: "<root><node id=\"2\" val=\"4\"/><node id=\"1\" val=\"3\"/></root>");
476: diff
477: .overrideElementQualifier(new ElementNameAndAttributeQualifier());
478: assertFalse("should not be identical: " + diff.toString(), diff
479: .identical());
480: assertFalse("should not be similar: " + diff.toString(), diff
481: .similar());
482: }
483:
484: public void testRepeatedElementNamesWithNamespacedAttributeQualification()
485: throws Exception {
486: Diff diff = buildDiff(
487: "<root xmlns:a=\"http://a.com\" xmlns:b=\"http://b.com\">"
488: + "<node id=\"1\" a:val=\"a\" b:val=\"b\"/><node id=\"2\" a:val=\"a2\" b:val=\"b2\"/></root>",
489: "<root xmlns:c=\"http://a.com\" xmlns:d=\"http://b.com\">"
490: + "<node id=\"2\" c:val=\"a2\" d:val=\"b2\"/><node id=\"1\" c:val=\"a\" d:val=\"b\"/></root>");
491: diff
492: .overrideElementQualifier(new ElementNameAndAttributeQualifier());
493: diff
494: .overrideDifferenceListener(new ExpectedDifferenceListener(
495: new int[] {
496: DifferenceConstants.NAMESPACE_PREFIX_ID,
497: DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID }));
498: assertFalse("should not be identical: " + diff.toString(), diff
499: .identical());
500: assertTrue("should be similar: " + diff.toString(), diff
501: .similar());
502: }
503:
504: public void testRepeatedElementNamesWithTextQualification()
505: throws Exception {
506: Diff diff = buildDiff(
507: "<root><node>1</node><node>2</node></root>",
508: "<root><node>2</node><node>1</node></root>");
509: diff
510: .overrideElementQualifier(new ElementNameAndTextQualifier());
511: diff
512: .overrideDifferenceListener(new ExaminingExpectedDifferenceListener(
513: DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID) {
514: private int i = 0;
515:
516: protected void examineDifferenceContents(
517: Difference difference) {
518: ++i;
519: assertEquals("/root[1]/node[" + i + "]",
520: difference.getControlNodeDetail()
521: .getXpathLocation());
522: }
523: });
524: assertFalse("should not be identical: " + diff.toString(), diff
525: .identical());
526: assertTrue("should be similar: " + diff.toString(), diff
527: .similar());
528: }
529:
530: // defect raised by Kevin Krouse Jan 2003
531: public void testXMLNSNumberOfAttributes() throws Exception {
532: Diff diff = buildDiff("<root xmlns=\"qwerty\"><node/></root>",
533: "<root xmlns=\"qwerty\" xmlns:qwerty=\"qwerty\"><qwerty:node/></root>");
534: assertTrue(diff.toString(), diff.similar());
535: assertFalse(diff.toString(), diff.identical());
536: }
537:
538: protected Diff buildDiff(Document control, Document test) {
539: return new Diff(control, test);
540: }
541:
542: protected Diff buildDiff(String control, String test)
543: throws Exception {
544: return new Diff(control, test);
545: }
546:
547: protected Diff buildDiff(Reader control, Reader test)
548: throws Exception {
549: return new Diff(control, test);
550: }
551:
552: /**
553: * Construct a test
554: * @param name Test name
555: */
556: public test_Diff(String name) {
557: super (name);
558: }
559:
560: private class OverrideDifferenceListener implements
561: DifferenceListener {
562: private final int overrideValue;
563:
564: private OverrideDifferenceListener(int overrideValue) {
565: this .overrideValue = overrideValue;
566: }
567:
568: public int differenceFound(Difference difference) {
569: return overrideValue;
570: }
571:
572: public void skippedComparison(Node control, Node test) {
573: }
574: }
575:
576: private class ExpectedDifferenceListener implements
577: DifferenceListener {
578: private final Set expectedIds;
579:
580: private ExpectedDifferenceListener(int expectedIdValue) {
581: this (new int[] { expectedIdValue });
582: }
583:
584: private ExpectedDifferenceListener(int[] expectedIdValues) {
585: this .expectedIds = new HashSet(expectedIdValues.length);
586: for (int i = 0; i < expectedIdValues.length; ++i) {
587: expectedIds.add(new Integer(expectedIdValues[i]));
588: }
589: }
590:
591: public int differenceFound(Difference difference) {
592: assertTrue(difference.toString(), expectedIds
593: .contains(new Integer(difference.getId())));
594: examineDifferenceContents(difference);
595: return RETURN_ACCEPT_DIFFERENCE;
596: }
597:
598: public void skippedComparison(Node control, Node test) {
599: }
600:
601: protected void examineDifferenceContents(Difference difference) {
602: }
603: }
604:
605: private abstract class ExaminingExpectedDifferenceListener extends
606: ExpectedDifferenceListener {
607: private ExaminingExpectedDifferenceListener(int expectedIdValue) {
608: super (expectedIdValue);
609: }
610:
611: protected abstract void examineDifferenceContents(
612: Difference difference);
613: }
614:
615: public void testIssue1189681() throws Exception {
616: String left = "" + "<farm>\n" + "<size>100</size>\n"
617: + " <animal>\n" + "<name>Cow</name>\n" + " </animal>\n"
618: + " <animal>\n" + "<name>Sheep</name>\n"
619: + " </animal>\n" + "</farm>";
620: String right = "" + "<farm>\n" + " <animal>\n"
621: + "<name>Sheep</name>\n" + " </animal>\n"
622: + " <size>100</size>\n" + " <animal>\n"
623: + " <name>Cow</name>\n" + " </animal>\n" + "</farm>";
624: assertFalse(buildDiff(left, right).similar());
625: }
626:
627: public void testCDATANoIgnore() throws Exception {
628: String expected = "<a>Hello</a>";
629: String actual = "<a><![CDATA[Hello]]></a>";
630: assertFalse(buildDiff(expected, actual).similar());
631: assertFalse(buildDiff(expected, actual).identical());
632: }
633:
634: public void testCDATAIgnore() throws Exception {
635: try {
636: XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
637: String expected = "<a>Hello</a>";
638: String actual = "<a><![CDATA[Hello]]></a>";
639: assertTrue(buildDiff(expected, actual).similar());
640: assertTrue(buildDiff(expected, actual).identical());
641: } finally {
642: XMLUnit.setIgnoreDiffBetweenTextAndCDATA(false);
643: }
644: }
645:
646: public void testCommentHandling() throws Exception {
647: String xml1 = "<foo><!-- test --><bar a=\"b\"/> </foo>";
648: String xml2 = "<foo><bar a=\"b\"><!-- test --></bar> </foo>";
649: try {
650: assertFalse(buildDiff(xml1, xml2).identical());
651: assertFalse(buildDiff(xml1, xml2).similar());
652: XMLUnit.setIgnoreComments(true);
653: assertTrue(buildDiff(xml1, xml2).identical());
654: assertTrue(buildDiff(xml1, xml2).similar());
655: } finally {
656: XMLUnit.setIgnoreComments(false);
657: }
658: }
659:
660: public void testWhitespaceHandlingDoesntAffectCommentHandling()
661: throws Exception {
662: try {
663: XMLUnit.setIgnoreWhitespace(true);
664: testCommentHandling();
665: } finally {
666: XMLUnit.setIgnoreWhitespace(false);
667: }
668: }
669:
670: public void testNormalizationDoesntAffectCommentHandling()
671: throws Exception {
672: try {
673: XMLUnit.setNormalize(true);
674: testCommentHandling();
675: } finally {
676: XMLUnit.setNormalize(false);
677: }
678: }
679:
680: public void testNormalization() throws Exception {
681: Document control = XMLUnit.newControlParser().newDocument();
682: Element root = control.createElement("root");
683: control.appendChild(root);
684: root.appendChild(control.createTextNode("Text 1"));
685: root.appendChild(control.createTextNode(" and 2"));
686: Element inner = control.createElement("inner");
687: root.appendChild(inner);
688: inner.appendChild(control.createTextNode("Text 3 and 4"));
689:
690: Document test = XMLUnit.newTestParser().newDocument();
691: root = test.createElement("root");
692: test.appendChild(root);
693: root.appendChild(test.createTextNode("Text 1 and 2"));
694: inner = test.createElement("inner");
695: root.appendChild(inner);
696: inner.appendChild(test.createTextNode("Text 3"));
697: inner.appendChild(test.createTextNode(" and 4"));
698:
699: assertFalse(buildDiff(control, test).identical());
700: try {
701: XMLUnit.setNormalize(true);
702: assertTrue(buildDiff(control, test).identical());
703: assertTrue(buildDiff(control, test).similar());
704: } finally {
705: XMLUnit.setNormalize(false);
706: }
707: assertFalse(buildDiff(control, test).similar());
708: }
709:
710: // fails with Java 5 and later
711: public void XtestWhitespaceHandlingDoesntAffectNormalization()
712: throws Exception {
713: try {
714: XMLUnit.setIgnoreWhitespace(true);
715: testNormalization();
716: } finally {
717: XMLUnit.setIgnoreWhitespace(false);
718: }
719: }
720:
721: // fails with Java 5 and later
722: public void XtestCommentHandlingDoesntAffectNormalization()
723: throws Exception {
724: try {
725: XMLUnit.setIgnoreComments(true);
726: testNormalization();
727: } finally {
728: XMLUnit.setIgnoreComments(false);
729: }
730: }
731:
732: public void testNormalizedWhitespace() throws Exception {
733: String xml1 = "<foo>a = b;</foo>";
734: String xml2 = "<foo>\r\n\ta = b; \r\n</foo>";
735: try {
736: assertFalse(buildDiff(xml1, xml2).identical());
737: assertFalse(buildDiff(xml1, xml2).similar());
738: XMLUnit.setNormalizeWhitespace(true);
739: assertTrue(buildDiff(xml1, xml2).identical());
740: assertTrue(buildDiff(xml1, xml2).similar());
741: } finally {
742: XMLUnit.setNormalizeWhitespace(false);
743: }
744: }
745:
746: /**
747: * inspired by {@link
748: * http://day-to-day-stuff.blogspot.com/2007/05/comparing-xml-in-junit-test.html
749: * Erik von Oosten's Weblog}, made us implement special handling
750: * of schemaLocation.
751: */
752: public void testNamespacePrefixDiff() throws Exception {
753: String xml1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
754: + "<Message xmlns=\"http://www.a.nl/a10.xsd\""
755: + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
756: + " xsi:schemaLocation=\"C:/longpath/a10.xsd\"" + ">"
757: + "<MessageHeader/>" + "</Message>";
758: String xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
759: + "<a:Message xmlns:a=\"http://www.a.nl/a10.xsd\">"
760: + "<a:MessageHeader/>" + "</a:Message>";
761: Diff d = buildDiff(xml1, xml2);
762: assertFalse(d.toString(), d.identical());
763: assertTrue(d.toString(), d.similar());
764: }
765:
766: }
|