001: /*
002: ******************************************************************
003: Copyright (c) 2001, 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.examples;
038:
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041:
042: import junit.framework.TestCase;
043: import junit.framework.TestSuite;
044:
045: import org.custommonkey.xmlunit.Diff;
046: import org.custommonkey.xmlunit.ElementNameAndTextQualifier;
047: import org.custommonkey.xmlunit.ElementQualifier;
048: import org.custommonkey.xmlunit.XMLUnit;
049:
050: /**
051: * JUnit testcase for MultiLevelElementNameAndTextQualifier
052: * @see test_Diff#testRepeatedElementNamesWithTextQualification()
053: */
054: public class test_MultiLevelElementNameAndTextQualifier extends
055: TestCase {
056: private static final String TAG_NAME = "tagYoureIt";
057: private static final String TAG_NAME2 = "tagYoureIt2";
058: private static final String TEXT_A = "textA";
059: private static final String TEXT_B = "textB";
060: private Document document;
061:
062: // copy of ElementNameAndTextQualifier test
063: public void testSingleTextValue() throws Exception {
064: ElementQualifier qualifier = new MultiLevelElementNameAndTextQualifier(
065: 1);
066:
067: Element control = document.createElement(TAG_NAME);
068: control.appendChild(document.createTextNode(TEXT_A));
069:
070: Element test = document.createElement(TAG_NAME);
071:
072: assertFalse("control text not comparable to empty text",
073: qualifier.qualifyForComparison(control, test));
074:
075: test.appendChild(document.createTextNode(TEXT_A));
076: assertTrue("control textA comparable to test textA", qualifier
077: .qualifyForComparison(control, test));
078:
079: test = document.createElement(TAG_NAME);
080:
081: test.appendChild(document.createTextNode(TEXT_B));
082: assertFalse("control textA not comparable to test textB",
083: qualifier.qualifyForComparison(control, test));
084: }
085:
086: // copy of ElementNameAndTextQualifier test
087: public void testMultipleTextValues() throws Exception {
088: ElementQualifier qualifier = new MultiLevelElementNameAndTextQualifier(
089: 1);
090:
091: Element control = document.createElement(TAG_NAME);
092: control.appendChild(document.createTextNode(TEXT_A));
093: control.appendChild(document.createTextNode(TEXT_B));
094:
095: Element test = document.createElement(TAG_NAME);
096: test.appendChild(document.createTextNode(TEXT_A + TEXT_B));
097: assertTrue(
098: "denormalised control text comparable to normalised test text",
099: qualifier.qualifyForComparison(control, test));
100: }
101:
102: // three levels
103: public void testThreeLevels() throws Exception {
104: ElementQualifier qualifier = new MultiLevelElementNameAndTextQualifier(
105: 3);
106:
107: Element control = document.createElement(TAG_NAME);
108: Element child = document.createElement(TAG_NAME2);
109: control.appendChild(child);
110: Element child2 = document.createElement(TAG_NAME);
111: child.appendChild(child2);
112: child2.appendChild(document.createTextNode(TEXT_B));
113:
114: Element test = document.createElement(TAG_NAME);
115: child = document.createElement(TAG_NAME2);
116: test.appendChild(child);
117: child2 = document.createElement(TAG_NAME);
118: child.appendChild(child2);
119: child2.appendChild(document.createTextNode(TEXT_B));
120:
121: assertTrue(qualifier.qualifyForComparison(control, test));
122: }
123:
124: /**
125: * @see https://sourceforge.net/forum/forum.php?thread_id=1440169&forum_id=73274
126: */
127: public void testThread1440169() throws Exception {
128: String s1 = "<a><b><c>foo</c></b><b><c>bar</c></b></a>";
129: String s2 = "<a><b><c>bar</c></b><b><c>foo</c></b></a>";
130: Diff d = new Diff(s1, s2);
131: assertFalse(d.similar());
132:
133: // reset
134: d = new Diff(s1, s2);
135: d.overrideElementQualifier(new ElementNameAndTextQualifier());
136: assertFalse(d.similar());
137:
138: // reset once again
139: d = new Diff(s1, s2);
140: d
141: .overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(
142: 2));
143: assertTrue(d.similar());
144:
145: }
146:
147: public void setUp() throws Exception {
148: document = XMLUnit.newControlParser().newDocument();
149: }
150:
151: }
|