Skip to content

Instantly share code, notes, and snippets.

@olithin
Last active December 11, 2017 17:13
Show Gist options
  • Save olithin/5261a0eae7419d805870811d639bedb4 to your computer and use it in GitHub Desktop.
Save olithin/5261a0eae7419d805870811d639bedb4 to your computer and use it in GitHub Desktop.
XmlUnit_DifferentEvaluator
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Diff;
import org.xmlunit.diff.Difference;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
public class SolDifferenceEvaluatorChain {
public static void main(String[] args) {
String control = "<Parent>\n" +
" <Costum>\n" +
" <Work>\n" +
" <Person>\n" +
" <FirstName>John</FirstName>\n" +
" <LastName>Doe</LastName>\n" +
" <Email>[email protected]</Email>\n" +
" </Person>\n" +
" <Person>\n" +
" <FirstName>Mickey</FirstName>\n" +
" <LastName>Mouse</LastName>\n" +
" <Email>[email protected]</Email>\n" +
" </Person>\n" +
" <Person>\n" +
" <FirstName>HO</FirstName>\n" +
" <LastName>FRT</LastName>\n" +
" <Email>[email protected]</Email>\n" +
" </Person>\n" +
" </Work>\n" +
" </Costum>\n" +
"</Parent>";
String test = "<Parent>\n" +
" <Costum>\n" +
" <Work>\n" +
" <Person>\n" +
" <FirstName>John</FirstName>\n" +
" <LastName>Doe</LastName>\n" +
" <Email>[email protected]</Email>\n" +
" </Person>\n" +
" <Person>\n" +
" <FirstName>HO</FirstName>\n" +
" <LastName>FRT</LastName>\n" +
" <Email>[email protected]</Email>\n" +
" </Person>\n" +
" <Person>\n" +
" <FirstName>Mickey</FirstName>\n" +
" <LastName>Mouse</LastName>\n" +
" <Email>[email protected]</Email>\n" +
" </Person>\n" +
" </Work>\n" +
" </Costum>\n" +
"</Parent>";
Diff myDiff = DiffBuilder.compare(Input.fromString(control))
.withTest(Input.fromString(test))
.ignoreWhitespace()
.checkForSimilar()
.build();
ArrayList<String> arrayOfDiff = new ArrayList<String>();
for (Difference di : myDiff.getDifferences()) {
System.out.println(di);
//Micky
String valueControl = di.getComparison().getControlDetails().getTarget().getNodeValue();
String valueTest = di.getComparison().getTestDetails().getTarget().getNodeValue();
//Firstname
String nodeControl = di.getComparison().getControlDetails().getTarget().getParentNode().getLocalName();
String nodeTest = di.getComparison().getTestDetails().getTarget().getParentNode().getLocalName();
//Person
String ownerNode = di.getComparison().getControlDetails().getTarget().getParentNode().getParentNode().getLocalName();
String ownerTest = di.getComparison().getTestDetails().getTarget().getParentNode().getParentNode().getLocalName();
String pathControl = ownerNode + "/" + nodeControl + "/" + valueControl;
String pathTest = ownerTest + "/" + nodeTest + "/" + valueTest;
arrayOfDiff.add(pathControl);
arrayOfDiff.add(pathTest);
System.out.println(valueControl + " " + nodeControl + " " + ownerNode);
}
ArrayList<String> duplicates = new ArrayList<>();
for (int i = 0; i < arrayOfDiff.size(); i++) {
if (duplicates.contains(arrayOfDiff.get(i))) {
continue;
}
for (int j = i + 1; j < arrayOfDiff.size(); j++) {
if (arrayOfDiff.get(i).equals(arrayOfDiff.get(j))) {
duplicates.add(arrayOfDiff.get(i));
break;
}
}
}
List<String> distinctElementsControl = arrayOfDiff.stream().distinct().collect(Collectors.toList());
System.out.println(distinctElementsControl);
System.out.println(duplicates);
assertEquals("В документе есть различия", distinctElementsControl, duplicates);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment