Skip to content

Commit

Permalink
extract Mapper into a standalone class
Browse files Browse the repository at this point in the history
Conflicts:
	xmlunit-core/src/main/java/org/xmlunit/diff/ElementSelectors.java
  • Loading branch information
bodewig committed May 10, 2015
1 parent 118a095 commit c9edd8f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.xmlunit.util.Convert;
import org.xmlunit.util.IterableNodeList;
import org.xmlunit.util.Linqy;
import org.xmlunit.util.Mapper;
import org.xmlunit.util.Nodes;
import org.xmlunit.util.Predicate;
import org.w3c.dom.Attr;
Expand Down Expand Up @@ -188,9 +189,9 @@ private DeferredComparison compareChildren(final Node control,
@Override
public ComparisonState apply() {
controlContext
.setChildren(Linqy.map(controlChildren, TO_NODE_INFO));
.setChildren(Linqy.map(controlChildren, ElementSelectors.TO_NODE_INFO));
testContext
.setChildren(Linqy.map(testChildren, TO_NODE_INFO));
.setChildren(Linqy.map(testChildren, ElementSelectors.TO_NODE_INFO));
return compareNodeLists(controlChildren, controlContext,
testChildren, testContext);
}
Expand Down Expand Up @@ -790,23 +791,12 @@ private static Attr findMatchingAttr(final List<Attr> attrs,
/**
* Maps Nodes to their QNames.
*/
private static final Linqy.Mapper<Node, QName> QNAME_MAPPER =
new Linqy.Mapper<Node, QName>() {
private static final Mapper<Node, QName> QNAME_MAPPER =
new Mapper<Node, QName>() {
@Override
public QName apply(Node n) { return Nodes.getQName(n); }
};

/**
* Maps Nodes to their NodeInfo equivalent.
*/
private static final Linqy.Mapper<Node, XPathContext.NodeInfo> TO_NODE_INFO =
new Linqy.Mapper<Node, XPathContext.NodeInfo>() {
@Override
public XPathContext.NodeInfo apply(Node n) {
return new XPathContext.DOMNodeInfo(n);
}
};

/**
* Suppresses document-type nodes.
*/
Expand Down
94 changes: 94 additions & 0 deletions xmlunit-core/src/main/java/org/xmlunit/diff/ElementSelectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.xmlunit.util.Linqy.all;
import static org.xmlunit.util.Linqy.any;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -28,6 +29,7 @@
import javax.xml.transform.dom.DOMSource;
import org.xmlunit.util.IsNullPredicate;
import org.xmlunit.util.Linqy;
import org.xmlunit.util.Mapper;
import org.xmlunit.util.Nodes;
import org.xmlunit.util.Predicate;
import org.xmlunit.xpath.JAXPXPathEngine;
Expand Down Expand Up @@ -626,4 +628,96 @@ public boolean test(ElementSelector es) {
}
}

private static class ByNameAndTextRecSelector implements ElementSelector {
@Override
public boolean canBeCompared(Element controlElement,
Element testElement) {
if (!byNameAndText.canBeCompared(controlElement,
testElement)) {
return false;
}
NodeList controlChildren = controlElement.getChildNodes();
NodeList testChildren = testElement.getChildNodes();
final int controlLen = controlChildren.getLength();
final int testLen = testChildren.getLength();
int controlIndex, testIndex;
for (controlIndex = testIndex = 0;
controlIndex < controlLen && testIndex < testLen;
) {
// find next non-text child nodes
Map.Entry<Integer, Node> control = findNonText(controlChildren,
controlIndex,
controlLen);
controlIndex = control.getKey();
Node c = control.getValue();
if (isText(c)) {
break;
}
Map.Entry<Integer, Node> test = findNonText(testChildren,
testIndex,
testLen);
testIndex = test.getKey();
Node t = test.getValue();
if (isText(t)) {
break;
}

// different types of children make elements
// non-comparable
if (c.getNodeType() != t.getNodeType()) {
return false;
}
// recurse for child elements
if (c instanceof Element && !byNameAndTextRec.canBeCompared((Element) c,
(Element) t)) {
return false;
}
controlIndex++;
testIndex++;
}

// child lists exhausted?
if (controlIndex < controlLen) {
Map.Entry<Integer, Node> p = findNonText(controlChildren,
controlIndex,
controlLen);
controlIndex = p.getKey();
// some non-Text children remained
if (controlIndex < controlLen) {
return false;
}
}
if (testIndex < testLen) {
Map.Entry<Integer, Node> p = findNonText(testChildren,
testIndex,
testLen);
testIndex = p.getKey();
// some non-Text children remained
if (testIndex < testLen) {
return false;
}
}
return true;
}

private Map.Entry<Integer, Node> findNonText(NodeList nl, int current, int len) {
Node n = nl.item(current);
while (isText(n) && ++current < len) {
n = nl.item(current);
}
return new AbstractMap.SimpleImmutableEntry<Integer, Node>(current, n);
}
}

/**
* Maps Nodes to their NodeInfo equivalent.
*/
static final Mapper<Node, XPathContext.NodeInfo> TO_NODE_INFO =
new Mapper<Node, XPathContext.NodeInfo>() {
@Override
public XPathContext.NodeInfo apply(Node n) {
return new XPathContext.DOMNodeInfo(n);
}
};

}
7 changes: 0 additions & 7 deletions xmlunit-core/src/main/java/org/xmlunit/util/Linqy.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ public Iterator<T> iterator() {
};
}

/**
* A function mapping from one type to another.
*/
public interface Mapper<F, T> {
T apply(F from);
}

/**
* Exclude all elements from an iterable that don't match a given
* predicate.
Expand Down
22 changes: 22 additions & 0 deletions xmlunit-core/src/main/java/org/xmlunit/util/Mapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
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.util;

/**
* A function mapping from one type to another.
*/
public interface Mapper<F, T> {
T apply(F from);
}

2 changes: 1 addition & 1 deletion xmlunit-core/src/test/java/org/xmlunit/util/LinqyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public boolean test(Boolean b) {
}
}

private static class IdentityMapper implements Linqy.Mapper<Object, Object> {
private static class IdentityMapper implements Mapper<Object, Object> {
@Override
public Object apply(Object s) {
return s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.xmlunit.builder.Input;
import org.xmlunit.util.IsNullPredicate;
import org.xmlunit.util.Linqy.Mapper;
import org.xmlunit.util.Mapper;
import org.xmlunit.validation.JAXPValidator;
import org.xmlunit.validation.Languages;
import org.xmlunit.validation.ValidationProblem;
Expand Down

0 comments on commit c9edd8f

Please sign in to comment.