Skip to content

Instantly share code, notes, and snippets.

@php-coder
Forked from olithin/SolDifferenceEvaluatorChain
Last active December 11, 2017 17:13
Show Gist options
  • Save php-coder/3f07da358420d827d95edadaf74c1b1e to your computer and use it in GitHub Desktop.
Save php-coder/3f07da358420d827d95edadaf74c1b1e to your computer and use it in GitHub Desktop.
XmlUnit_DifferentEvaluator
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Comparison;
import org.xmlunit.diff.Diff;
import org.xmlunit.diff.Difference;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class XmlUnitTest {
@Test
public void testForDifferences() {
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();
List<String> controlElements = new ArrayList<>();
List<String> testElements = new ArrayList<>();
for (Difference di : myDiff.getDifferences()) {
controlElements.add(describeElement(di.getComparison().getControlDetails()));
testElements.add(describeElement(di.getComparison().getTestDetails()));
}
Collections.sort(controlElements);
Collections.sort(testElements);
assertEquals("В документе есть различия", controlElements, testElements);
}
private static String describeElement(Comparison.Detail detail) {
String value = detail.getTarget().getNodeValue();
String name = detail.getTarget().getParentNode().getLocalName();
String parentName = detail.getTarget().getParentNode().getParentNode().getLocalName();
return new StringBuilder()
.append(parentName)
.append('/')
.append(name)
.append('/')
.append(value)
.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment