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;
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: /**
046: * JUnit testcase for ElementNameAndAttributeQualifier
047: * @see test_Diff#testRepeatedElementNamesWithAttributeQualification()
048: */
049: public class test_ElementNameAndAttributeQualifier extends TestCase {
050: private Document document;
051: private ElementNameAndAttributeQualifier elementNameAndAttributeQualifier;
052: private static final String TAG_NAME = "qwerty";
053:
054: public void testSingleQualifyingAttribute() throws Exception {
055: final String attrName = "id";
056:
057: elementNameAndAttributeQualifier = new ElementNameAndAttributeQualifier();
058: testAssertionsFor(attrName, new boolean[] { false, false });
059:
060: elementNameAndAttributeQualifier = new ElementNameAndAttributeQualifier(
061: attrName);
062: testAssertionsFor(attrName, new boolean[] { true, true });
063: }
064:
065: private void testAssertionsFor(String attrName,
066: boolean[] expectedValues) throws Exception {
067: Element control = document.createElement(TAG_NAME);
068: control.setAttribute(attrName, "1");
069:
070: Element test = document.createElement(TAG_NAME);
071: assertFalse(
072: "qwerty id 1 not comparable to qwerty with no attributes",
073: elementNameAndAttributeQualifier.qualifyForComparison(
074: control, test));
075:
076: test.setAttribute(attrName, "1");
077: assertTrue("qwerty id 1 comparable to qwerty id 1",
078: elementNameAndAttributeQualifier.qualifyForComparison(
079: control, test));
080:
081: control.setAttribute("uiop", "true");
082: assertEquals("qwerty id 1 && uiop comparable to qwerty id 1",
083: expectedValues[0], elementNameAndAttributeQualifier
084: .qualifyForComparison(control, test));
085:
086: test.setAttribute("uiop", "false");
087: assertEquals(
088: "qwerty id 1 && uiop comparable to qwerty id 1 && !uiop",
089: expectedValues[1], elementNameAndAttributeQualifier
090: .qualifyForComparison(control, test));
091:
092: test.setAttribute(attrName, "2");
093: assertFalse(
094: "qwerty id 1 && uiop NOT comparable to qwerty id 2 && !uiop",
095: elementNameAndAttributeQualifier.qualifyForComparison(
096: control, test));
097: }
098:
099: public void testMultipleQualifyingAttributes() throws Exception {
100: final String[] attrNames = { "id", "uid" };
101:
102: elementNameAndAttributeQualifier = new ElementNameAndAttributeQualifier();
103: testAssertionsFor(attrNames, new boolean[] { false, false });
104:
105: elementNameAndAttributeQualifier = new ElementNameAndAttributeQualifier(
106: attrNames);
107: testAssertionsFor(attrNames, new boolean[] { true, true });
108: }
109:
110: private void testAssertionsFor(String[] attrNames,
111: boolean[] expectedValues) throws Exception {
112: Element control = document.createElement(TAG_NAME);
113: for (int i = 0; i < attrNames.length; ++i) {
114: control.setAttribute(attrNames[i], "1");
115: }
116:
117: Element test = document.createElement(TAG_NAME);
118: assertFalse(
119: "qwerty id/uid 1 not comparable to qwerty with no attributes",
120: elementNameAndAttributeQualifier.qualifyForComparison(
121: control, test));
122:
123: for (int i = 0; i < attrNames.length; ++i) {
124: test.setAttribute(attrNames[i], "1");
125: }
126:
127: assertTrue("qwerty id/uid 1 comparable to qwerty id/uid 1",
128: elementNameAndAttributeQualifier.qualifyForComparison(
129: control, test));
130:
131: control.setAttribute("oid", "0x2394b3456df");
132: assertEquals(
133: "qwerty id/uid 1 with oid comparable to qwerty id/uid 1",
134: expectedValues[0], elementNameAndAttributeQualifier
135: .qualifyForComparison(control, test));
136:
137: test.setAttribute("oid", "0xfd6543b4932");
138: assertEquals(
139: "qwerty id/uid 1 with oid comparable to qwerty id/uid 1 with different oid",
140: expectedValues[1], elementNameAndAttributeQualifier
141: .qualifyForComparison(control, test));
142:
143: test.setAttribute(attrNames[0], "2");
144: assertFalse(
145: "qwerty id/uid 1 not comparable to qwerty id 2 /uid 1",
146: elementNameAndAttributeQualifier.qualifyForComparison(
147: control, test));
148:
149: test.setAttribute(attrNames[0], "1");
150: test.setAttribute(attrNames[1], "2");
151: assertFalse(
152: "qwerty id/uid 1 not comparable to qwerty id 1 /uid 2",
153: elementNameAndAttributeQualifier.qualifyForComparison(
154: control, test));
155:
156: test.setAttribute(attrNames[0], "2");
157: assertFalse(
158: "qwerty id/uid 1 not comparable to qwerty id/uid 2",
159: elementNameAndAttributeQualifier.qualifyForComparison(
160: control, test));
161: }
162:
163: public void testNamespacedQualifyingAttribute() throws Exception {
164: final String attrName = "id";
165: final String nsURI = "http://xmlunit.sourceforge.net/tests";
166:
167: elementNameAndAttributeQualifier = new ElementNameAndAttributeQualifier();
168: testAssertionsFor(attrName, nsURI,
169: new boolean[] { false, false });
170:
171: elementNameAndAttributeQualifier = new ElementNameAndAttributeQualifier(
172: attrName);
173: testAssertionsFor(attrName, nsURI, new boolean[] { true, true });
174: }
175:
176: private void testAssertionsFor(String attrName, String nsURI,
177: boolean[] expectedValues) throws Exception {
178: Element control = document.createElement(TAG_NAME);
179: control.setAttributeNS(nsURI, attrName, "1");
180:
181: Element test = document.createElement(TAG_NAME);
182: assertFalse(
183: "qwerty id 1 not comparable to qwerty with no attributes",
184: elementNameAndAttributeQualifier.qualifyForComparison(
185: control, test));
186:
187: test.setAttributeNS(nsURI, attrName, "1");
188: assertTrue("qwerty id 1 comparable to qwerty id 1",
189: elementNameAndAttributeQualifier.qualifyForComparison(
190: control, test));
191:
192: String otherNsURI = nsURI + "/2";
193: test.setAttributeNS(otherNsURI, attrName, "2");
194: assertTrue(
195: "qwerty id 1 comparable to qwerty id 1 and other-NS id 2",
196: elementNameAndAttributeQualifier.qualifyForComparison(
197: control, test));
198:
199: control.setAttributeNS(nsURI, "uiop", "true");
200: assertEquals("qwerty id 1 && uiop comparable to qwerty id 1",
201: expectedValues[0], elementNameAndAttributeQualifier
202: .qualifyForComparison(control, test));
203:
204: test.setAttributeNS(nsURI, "uiop", "false");
205: assertEquals(
206: "qwerty id 1 && uiop comparable to qwerty id 1 && !uiop",
207: expectedValues[1], elementNameAndAttributeQualifier
208: .qualifyForComparison(control, test));
209:
210: test.setAttributeNS(nsURI, attrName, "2");
211: assertFalse(
212: "qwerty id 1 && uiop NOT comparable to qwerty id 2 && !uiop",
213: elementNameAndAttributeQualifier.qualifyForComparison(
214: control, test));
215: }
216:
217: // Bug 952920
218: public void testQualifyingAttributeMissingInControl()
219: throws Exception {
220: elementNameAndAttributeQualifier = new ElementNameAndAttributeQualifier(
221: "foo");
222: assertQualifyingAttributeMissingInControl();
223: elementNameAndAttributeQualifier = new ElementNameAndAttributeQualifier(
224: new String[] { "foo", "bar" });
225: assertQualifyingAttributeMissingInControl();
226: }
227:
228: private void assertQualifyingAttributeMissingInControl()
229: throws Exception {
230: Element control = document.createElement(TAG_NAME);
231:
232: Element test = document.createElement(TAG_NAME);
233: assertTrue("empty elements match",
234: elementNameAndAttributeQualifier.qualifyForComparison(
235: control, test));
236:
237: test.setAttribute("id", "1");
238: assertTrue("extra attribute on test matches",
239: elementNameAndAttributeQualifier.qualifyForComparison(
240: control, test));
241:
242: control.setAttribute("id", "2");
243: assertTrue(
244: "differerent values for extra attribute still matches",
245: elementNameAndAttributeQualifier.qualifyForComparison(
246: control, test));
247:
248: control.setAttribute("uid", "1");
249: assertTrue("extra attribute on control matches",
250: elementNameAndAttributeQualifier.qualifyForComparison(
251: control, test));
252:
253: test.setAttribute("foo", "1");
254: assertFalse("no match if attribute is present in test",
255: elementNameAndAttributeQualifier.qualifyForComparison(
256: control, test));
257: }
258:
259: /**
260: * @see https://sourceforge.net/forum/forum.php?thread_id=1135716&forum_id=73274l
261: */
262: public void testHelpForumThread1135716() throws Exception {
263: String control = "<class id=\"c0\"> "
264: + "<method id=\"c0_m0\"> "
265: + "<dependency_info stmtId=\"c0_m0_s4\"> "
266: + "<dependent tid=\"c3_m1_s18\"/> "
267: + "<dependent tid=\"c3_m1_s32\"/> "
268: + "<dependent tid=\"c3_m1_s26\"/> "
269: + "</dependency_info> " + "</method> " + "</class>";
270: String test = "<class id=\"c0\"> " + "<method id=\"c0_m0\"> "
271: + "<dependency_info stmtId=\"c0_m0_s4\"> "
272: + "<dependent tid=\"c3_m1_s32\"/> "
273: + "<dependent tid=\"c3_m1_s18\"/> "
274: + "<dependent tid=\"c3_m1_s26\"/> "
275: + "</dependency_info> " + "</method> " + "</class>";
276: Diff d = new Diff(control, test);
277: assertFalse(d.similar());
278:
279: // reset
280: d = new Diff(control, test);
281: d
282: .overrideElementQualifier(new ElementNameAndAttributeQualifier());
283: assertTrue(d.similar());
284: }
285:
286: public void setUp() throws Exception {
287: document = XMLUnit.newControlParser().newDocument();
288: }
289:
290: public static TestSuite suite() {
291: return new TestSuite(
292: test_ElementNameAndAttributeQualifier.class);
293: }
294:
295: /**
296: * Constructor for test_ElementNameAndAttributeQualifier.
297: * @param name
298: */
299: public test_ElementNameAndAttributeQualifier(String name) {
300: super(name);
301: }
302:
303: }
|