-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementDifferenceEvaluator.java
More file actions
47 lines (39 loc) · 1.42 KB
/
Copy pathElementDifferenceEvaluator.java
File metadata and controls
47 lines (39 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xmlunit.diff.Comparison;
import org.xmlunit.diff.ComparisonResult;
import org.xmlunit.diff.DifferenceEvaluator;
public class ElementDifferenceEvaluator implements DifferenceEvaluator {
private String elementName;
private List<String> elementNames;
public ElementDifferenceEvaluator(String elementName) {
this.elementName = elementName;
}
public ElementDifferenceEvaluator(List<String> elementNames) {
this.elementNames = elementNames;
}
@Override
public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) {
if (outcome == ComparisonResult.EQUAL)
return outcome; // only evaluate differences.
final Node controlNode = comparison.getControlDetails().getTarget();
final Node testNode = comparison.getTestDetails().getTarget();
if (controlNode.getParentNode() instanceof Element && testNode.getParentNode() instanceof Element) {
Element controlElement = (Element) controlNode.getParentNode();
Element testElement = (Element) testNode.getParentNode();
if (elementName != null) {
if (controlElement.getNodeName().equals(elementName)) {
return ComparisonResult.SIMILAR;
}
} else if (elementNames != null) {
for (String elString : elementNames) {
if (controlElement.getNodeName().equals(elString)) {
return ComparisonResult.SIMILAR;
}
}
}
}
return outcome;
}
}