001: /*
002: ******************************************************************
003: Copyright (c) 200, 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.util.Iterator;
040: import java.util.List;
041:
042: import junit.framework.TestCase;
043:
044: /**
045: * @author TimBacon
046: */
047: public class test_IgnoreTextAndAttributeValuesDifferenceListener extends
048: TestCase {
049: private DifferenceListener listener;
050:
051: public void testDifferenceFound() {
052: assertCorrectInterpretation(
053: DifferenceConstants.ATTR_NAME_NOT_FOUND,
054: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
055: assertCorrectInterpretation(DifferenceConstants.ATTR_SEQUENCE,
056: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
057: assertCorrectInterpretation(
058: DifferenceConstants.ATTR_VALUE,
059: DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR);
060: assertCorrectInterpretation(
061: DifferenceConstants.ATTR_VALUE_EXPLICITLY_SPECIFIED,
062: DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR);
063: assertCorrectInterpretation(DifferenceConstants.CDATA_VALUE,
064: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
065: assertCorrectInterpretation(
066: DifferenceConstants.CHILD_NODELIST_LENGTH,
067: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
068: assertCorrectInterpretation(
069: DifferenceConstants.CHILD_NODELIST_SEQUENCE,
070: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
071: assertCorrectInterpretation(DifferenceConstants.COMMENT_VALUE,
072: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
073: assertCorrectInterpretation(DifferenceConstants.DOCTYPE_NAME,
074: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
075: assertCorrectInterpretation(
076: DifferenceConstants.DOCTYPE_PUBLIC_ID,
077: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
078: assertCorrectInterpretation(
079: DifferenceConstants.DOCTYPE_SYSTEM_ID,
080: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
081: assertCorrectInterpretation(
082: DifferenceConstants.ELEMENT_NUM_ATTRIBUTES,
083: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
084: assertCorrectInterpretation(
085: DifferenceConstants.ELEMENT_TAG_NAME,
086: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
087: assertCorrectInterpretation(
088: DifferenceConstants.HAS_CHILD_NODES,
089: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
090: assertCorrectInterpretation(
091: DifferenceConstants.HAS_DOCTYPE_DECLARATION,
092: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
093: assertCorrectInterpretation(
094: DifferenceConstants.NAMESPACE_PREFIX,
095: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
096: assertCorrectInterpretation(DifferenceConstants.NAMESPACE_URI,
097: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
098: assertCorrectInterpretation(DifferenceConstants.NODE_TYPE,
099: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
100: assertCorrectInterpretation(
101: DifferenceConstants.PROCESSING_INSTRUCTION_DATA,
102: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
103: assertCorrectInterpretation(
104: DifferenceConstants.PROCESSING_INSTRUCTION_TARGET,
105: DifferenceListener.RETURN_ACCEPT_DIFFERENCE);
106: assertCorrectInterpretation(
107: DifferenceConstants.TEXT_VALUE,
108: DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR);
109: }
110:
111: private void assertCorrectInterpretation(Difference difference,
112: int returnValue) {
113: assertEquals(difference.toString(), returnValue, listener
114: .differenceFound(difference));
115: }
116:
117: public void testClassInUse() throws Exception {
118: String control = "<clouds><cloud name=\"cumulus\" rain=\"maybe\">fluffy</cloud></clouds>";
119: String similarTest = "<clouds><cloud name=\"cirrus\" rain=\"no\">wispy</cloud></clouds>";
120:
121: Diff diff = new Diff(control, similarTest);
122: diff.overrideDifferenceListener(listener);
123: assertTrue("similar " + diff.toString(), diff.similar());
124: assertTrue("but not identical " + diff.toString(), !diff
125: .identical());
126:
127: DetailedDiff detailedDiff = new DetailedDiff(new Diff(control,
128: similarTest));
129: assertEquals("2 attribute and 1 text values", 3, detailedDiff
130: .getAllDifferences().size());
131:
132: String dissimilarTest = "<clouds><cloud name=\"nimbus\"/></clouds>";
133: Diff dissimilarDiff = new Diff(control, dissimilarTest);
134: dissimilarDiff.overrideDifferenceListener(listener);
135: assertTrue("not similar " + dissimilarDiff.toString(),
136: !dissimilarDiff.similar());
137:
138: DetailedDiff dissimilarDetailedDiff = new DetailedDiff(
139: new Diff(control, dissimilarTest));
140: dissimilarDetailedDiff.overrideDifferenceListener(listener);
141: List differences = dissimilarDetailedDiff.getAllDifferences();
142: assertEquals(
143: "wrong number of attributes, missing attribute, different attribute value, and missing text node. "
144: + dissimilarDetailedDiff.toString(), 4,
145: differences.size());
146: int recoverable = 0;
147: for (Iterator iter = differences.iterator(); iter.hasNext();) {
148: Difference aDifference = (Difference) iter.next();
149: if (aDifference.isRecoverable()) {
150: recoverable++;
151: }
152: }
153: assertEquals(
154: "attribute value difference has been overridden as similar",
155: 1, recoverable);
156: }
157:
158: public void testIssue771839() throws Exception {
159: String xmlString1 = "<location>"
160: + "<street-address>22 any street</street-address>"
161: + "<postcode id='3'>XY0099Z</postcode>" + "</location>";
162: String xmlString2 = "<location>"
163: + "<postcode1 id='1'>EC3M 1EB</postcode1>"
164: + "<street-address>20 east cheap</street-address>"
165: + "</location>";
166:
167: Diff d = new Diff(xmlString1, xmlString2);
168: d.overrideDifferenceListener(listener);
169: assertFalse(d.similar());
170: assertTrue("postcode was matched against postcode1", d
171: .toString().indexOf(
172: "Expected element tag name 'postcode'"
173: + " but was 'postcode1'") > -1);
174: }
175:
176: public void setUp() {
177: listener = new IgnoreTextAndAttributeValuesDifferenceListener();
178: }
179:
180: /**
181: * Constructor for test_IgnoreTextAndAttributeValuesDifferenceListener.
182: * @param name
183: */
184: public test_IgnoreTextAndAttributeValuesDifferenceListener(
185: String name) {
186: super(name);
187: }
188: }
|