Skip to content

Commit

Permalink
port MultiLevelElementNameAndTextQualifier to XMLUnit 2.x
Browse files Browse the repository at this point in the history
closes #29
  • Loading branch information
bodewig committed May 30, 2015
1 parent b5e6426 commit f137ec3
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 66 deletions.
@@ -0,0 +1,114 @@
/*
This file is licensed to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.xmlunit.diff;

import org.w3c.dom.CDATASection;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

/**
* {@link ElementSelector} that allows two elements to be compared if
* their name (including namespace URI, if any) and textual content is
* the same at a certain level of nesting.
*
* <p>This means {@link ElementSelectors#byNameAndText} and {@code
* MultiLevelByNameAndTextSelector(1)} should lead to the same
* results.</p>
*
* <p>Any attribute values are completely ignored. Only works on
* elements with exactly one child element at each level.</p>
*
* <p>This class mostly exists as an example for custom
* ElementSelectors and may need to be combined inside a {@link
* ElementSelectors#conditionalSelector conditionalSelector} in order
* to be useful for the document as a whole.</p>
*/
public class MultiLevelByNameAndTextSelector implements ElementSelector {

private final int levels;
private final boolean ignoreEmptyTexts;

/**
* Uses element names and the text nested {@code levels} child
* elements deeper into the element to compare elements.
*
* <p>Does not ignore empty text nodes.
*/
public MultiLevelByNameAndTextSelector(int levels) {
this(levels, false);
}

/**
* Uses element names and the text nested {@code levels} child
* elements deeper into the element to compare elements.
*
* @param ignoreEmptyTexts whether whitespace-only textnodes
* should be ignored.
*/
public MultiLevelByNameAndTextSelector(int levels, boolean ignoreEmptyTexts) {
if (levels < 1) {
throw new IllegalArgumentException("levels must be equal or"
+ " greater than one");
}
this.levels = levels;
this.ignoreEmptyTexts = ignoreEmptyTexts;
}

@Override
public boolean canBeCompared(Element controlElement,
Element testElement) {
boolean stillSimilar = true;
Element currentControl = controlElement;
Element currentTest = testElement;

// match on element names only for leading levels
for (int currentLevel = 0; currentLevel <= levels - 2; currentLevel++) {
if (!ElementSelectors.byName.canBeCompared(currentControl, currentTest)
|| !currentControl.hasChildNodes()
|| !currentTest.hasChildNodes()) {
return false;
}
Node n1 = getFirstEligibleChild(currentControl);
Node n2 = getFirstEligibleChild(currentTest);
if (n1.getNodeType() == Node.ELEMENT_NODE
&& n2.getNodeType() == Node.ELEMENT_NODE) {
currentControl = (Element) n1;
currentTest = (Element) n2;
} else {
return false;
}
}

// finally compare the level containing the text child node
return ElementSelectors.byNameAndText.canBeCompared(currentControl,
currentTest);
}

private Node getFirstEligibleChild(Node parent) {
Node n1 = parent.getFirstChild();
if (ignoreEmptyTexts) {
while (isText(n1) && n1.getNodeValue().trim().length() == 0) {
Node n2 = n1.getNextSibling();
if (n2 == null) break;
n1 = n2;
}
}
return n1;
}

private static boolean isText(Node n) {
return n instanceof Text || n instanceof CDATASection;
}
}
@@ -0,0 +1,95 @@
/*
This file is licensed to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.xmlunit.diff;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import static org.junit.Assert.*;
import static org.xmlunit.diff.ElementSelectorsTest.*;

public class MultiLevelByNameAndTextSelectorTest {

private Document doc;

@Before
public void createDoc() throws Exception {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument();
}

@Test
public void singleLevel() {
byNameAndText_SingleLevel(new MultiLevelByNameAndTextSelector(1), doc);
}

@Test
public void byNameAndTextRec() {
Element control = doc.createElement(FOO);
Element child = doc.createElement(BAR);
control.appendChild(child);
child.appendChild(doc.createTextNode(BAR));
Element equal = doc.createElement(FOO);
Element child2 = doc.createElement(BAR);
equal.appendChild(child2);
child2.appendChild(doc.createTextNode(BAR));
Element equalC = doc.createElement(FOO);
Element child3 = doc.createElement(BAR);
equalC.appendChild(child3);
child3.appendChild(doc.createCDATASection(BAR));
Element noText = doc.createElement(FOO);
Element differentLevel = doc.createElement(FOO);
differentLevel.appendChild(doc.createTextNode(BAR));
Element differentElement = doc.createElement(FOO);
Element child4 = doc.createElement(FOO);
differentElement.appendChild(child4);
child4.appendChild(doc.createTextNode(BAR));
Element differentText = doc.createElement(FOO);
Element child5 = doc.createElement(BAR);
differentText.appendChild(child5);
child5.appendChild(doc.createTextNode(FOO));

ElementSelector s = new MultiLevelByNameAndTextSelector(2);
assertTrue(s.canBeCompared(control, equal));
assertTrue(s.canBeCompared(control, equalC));
assertFalse(s.canBeCompared(control, noText));
assertFalse(s.canBeCompared(control, differentLevel));
assertFalse(s.canBeCompared(control, differentElement));
assertFalse(s.canBeCompared(control, differentText));
}

@Test
public void emptyTexts() {
Element control = doc.createElement(FOO);
Element child = doc.createElement(BAR);
control.appendChild(doc.createTextNode(""));
control.appendChild(child);
child.appendChild(doc.createTextNode(BAR));
Element test = doc.createElement(FOO);
Element child2 = doc.createElement(BAR);
test.appendChild(child2);
child2.appendChild(doc.createTextNode(BAR));

ElementSelector s = new MultiLevelByNameAndTextSelector(2);
assertFalse(new MultiLevelByNameAndTextSelector(2)
.canBeCompared(control, test));
assertTrue(new MultiLevelByNameAndTextSelector(2, true)
.canBeCompared(control, test));
}

}
Expand Up @@ -36,12 +36,11 @@ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN

package org.custommonkey.xmlunit.examples;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.custommonkey.xmlunit.ElementNameAndTextQualifier;
import org.custommonkey.xmlunit.ElementNameQualifier;
import org.custommonkey.xmlunit.ElementQualifier;
import org.xmlunit.diff.MultiLevelByNameAndTextSelector;
import org.xmlunit.diff.ElementSelector;

import org.w3c.dom.Element;

/**
* Per popular request an interface implementation that uses element
Expand All @@ -60,13 +59,7 @@ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
public class MultiLevelElementNameAndTextQualifier
implements ElementQualifier {

private final int levels;
private final boolean ignoreEmptyTexts;

private static final ElementNameQualifier NAME_QUALIFIER =
new ElementNameQualifier();
private static final ElementNameAndTextQualifier NAME_AND_TEXT_QUALIFIER =
new ElementNameAndTextQualifier();
private final ElementSelector es;

/**
* Uses element names and the text nested <code>levels</code>
Expand All @@ -87,62 +80,10 @@ public MultiLevelElementNameAndTextQualifier(int levels) {
*/
public MultiLevelElementNameAndTextQualifier(int levels,
boolean ignoreEmptyTexts) {
if (levels < 1) {
throw new IllegalArgumentException("levels must be equal or"
+ " greater than one");
}
this.levels = levels;
this.ignoreEmptyTexts = ignoreEmptyTexts;
es = new MultiLevelByNameAndTextSelector(levels, ignoreEmptyTexts);
}

public boolean qualifyForComparison(Element control, Element test) {
boolean stillSimilar = true;
Element currentControl = control;
Element currentTest = test;

// match on element names only for leading levels
for (int currentLevel = 0; stillSimilar && currentLevel <= levels - 2;
currentLevel++) {
stillSimilar = NAME_QUALIFIER.qualifyForComparison(currentControl,
currentTest);

if (stillSimilar) {
if (currentControl.hasChildNodes()
&& currentTest.hasChildNodes()) {
Node n1 = getFirstEligibleChild(currentControl);
Node n2 = getFirstEligibleChild(currentTest);
if (n1.getNodeType() == Node.ELEMENT_NODE
&& n2.getNodeType() == Node.ELEMENT_NODE) {
currentControl = (Element) n1;
currentTest = (Element) n2;
} else {
stillSimilar = false;
}
} else {
stillSimilar = false;
}
}
}

// finally compare the level containing the text child node
if (stillSimilar) {
stillSimilar = NAME_AND_TEXT_QUALIFIER
.qualifyForComparison(currentControl, currentTest);
}

return stillSimilar;
}

private Node getFirstEligibleChild(Node parent) {
Node n1 = parent.getFirstChild();
if (ignoreEmptyTexts) {
while (n1.getNodeType() == Node.TEXT_NODE
&& n1.getNodeValue().trim().length() == 0) {
Node n2 = n1.getNextSibling();
if (n2 == null) break;
n1 = n2;
}
}
return n1;
return es.canBeCompared(control, test);
}
}

0 comments on commit f137ec3

Please sign in to comment.