|
| 1 | +package com.baeldung.xmlunit; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
| 5 | +import static org.hamcrest.Matchers.greaterThan; |
| 6 | +import static org.hamcrest.core.IsNot.not; |
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | +import static org.junit.Assert.assertFalse; |
| 9 | +import static org.junit.Assert.assertNotNull; |
| 10 | +import static org.junit.Assert.assertTrue; |
| 11 | +import static org.xmlunit.matchers.CompareMatcher.isIdenticalTo; |
| 12 | +import static org.xmlunit.matchers.CompareMatcher.isSimilarTo; |
| 13 | +import static org.xmlunit.matchers.HasXPathMatcher.hasXPath; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.util.Iterator; |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | +import org.w3c.dom.NamedNodeMap; |
| 20 | +import org.w3c.dom.Node; |
| 21 | +import org.xmlunit.builder.DiffBuilder; |
| 22 | +import org.xmlunit.builder.Input; |
| 23 | +import org.xmlunit.diff.ComparisonControllers; |
| 24 | +import org.xmlunit.diff.DefaultNodeMatcher; |
| 25 | +import org.xmlunit.diff.Diff; |
| 26 | +import org.xmlunit.diff.Difference; |
| 27 | +import org.xmlunit.diff.ElementSelectors; |
| 28 | +import org.xmlunit.validation.Languages; |
| 29 | +import org.xmlunit.validation.ValidationProblem; |
| 30 | +import org.xmlunit.validation.ValidationResult; |
| 31 | +import org.xmlunit.validation.Validator; |
| 32 | +import org.xmlunit.xpath.JAXPXPathEngine; |
| 33 | + |
| 34 | +public class XMLUnitTest { |
| 35 | + @Test |
| 36 | + public void givenWrongXml_whenValidateFailsAgainstXsd_thenCorrect() { |
| 37 | + Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI); |
| 38 | + v.setSchemaSource(Input.fromStream( |
| 39 | + new XMLUnitTest().getClass().getResourceAsStream( |
| 40 | + "/students.xsd")).build()); |
| 41 | + ValidationResult r = v.validateInstance(Input.fromStream( |
| 42 | + new XMLUnitTest().getClass().getResourceAsStream( |
| 43 | + "/students_with_error.xml")).build()); |
| 44 | + assertFalse(r.isValid()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenXmlWithErrors_whenReturnsValidationProblems_thenCorrect() { |
| 49 | + Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI); |
| 50 | + v.setSchemaSource(Input.fromStream( |
| 51 | + new XMLUnitTest().getClass().getResourceAsStream( |
| 52 | + "/students.xsd")).build()); |
| 53 | + ValidationResult r = v.validateInstance(Input.fromStream( |
| 54 | + new XMLUnitTest().getClass().getResourceAsStream( |
| 55 | + "/students_with_error.xml")).build()); |
| 56 | + Iterator<ValidationProblem> probs = r.getProblems().iterator(); |
| 57 | + int count = 0; |
| 58 | + while (probs.hasNext()) { |
| 59 | + count++; |
| 60 | + probs.next().toString(); |
| 61 | + } |
| 62 | + assertTrue(count > 0); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void givenXml_whenValidatesAgainstXsd_thenCorrect() { |
| 67 | + Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI); |
| 68 | + v.setSchemaSource(Input.fromStream( |
| 69 | + new XMLUnitTest().getClass().getResourceAsStream( |
| 70 | + "/students.xsd")).build()); |
| 71 | + ValidationResult r = v.validateInstance(Input.fromStream( |
| 72 | + new XMLUnitTest().getClass().getResourceAsStream( |
| 73 | + "/students.xml")).build()); |
| 74 | + Iterator<ValidationProblem> probs = r.getProblems().iterator(); |
| 75 | + while (probs.hasNext()) { |
| 76 | + System.out.println(probs.next().toString()); |
| 77 | + } |
| 78 | + assertTrue(r.isValid()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenXPath_whenAbleToRetrieveNodes_thenCorrect() { |
| 83 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 84 | + |
| 85 | + Iterable<Node> i = new JAXPXPathEngine().selectNodes( |
| 86 | + "//teacher", |
| 87 | + Input.fromFile( |
| 88 | + new File(classLoader.getResource("teachers.xml") |
| 89 | + .getFile())).build()); |
| 90 | + assertNotNull(i); |
| 91 | + int count = 0; |
| 92 | + for (Iterator<Node> it = i.iterator(); it.hasNext();) { |
| 93 | + count++; |
| 94 | + Node node = it.next(); |
| 95 | + assertEquals("teacher", node.getNodeName()); |
| 96 | + NamedNodeMap map = node.getAttributes(); |
| 97 | + assertEquals("department", map.item(0).getNodeName()); |
| 98 | + assertEquals("id", map.item(1).getNodeName()); |
| 99 | + assertEquals("teacher", node.getNodeName()); |
| 100 | + } |
| 101 | + assertEquals(2, count); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void givenXmlSource_whenAbleToValidateExistingXPath_thenCorrect() { |
| 106 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 107 | + assertThat(Input.fromFile(new File(classLoader.getResource( |
| 108 | + "teachers.xml").getFile())), hasXPath("//teachers")); |
| 109 | + assertThat(Input.fromFile(new File(classLoader.getResource( |
| 110 | + "teachers.xml").getFile())), hasXPath("//teacher")); |
| 111 | + assertThat(Input.fromFile(new File(classLoader.getResource( |
| 112 | + "teachers.xml").getFile())), hasXPath("//subject")); |
| 113 | + assertThat(Input.fromFile(new File(classLoader.getResource( |
| 114 | + "teachers.xml").getFile())), hasXPath("//@department")); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void givenXmlSource_whenFailsToValidateInExistentXPath_thenCorrect() { |
| 119 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 120 | + |
| 121 | + assertThat(Input.fromFile(new File(classLoader.getResource( |
| 122 | + "teachers.xml").getFile())), not(hasXPath("//sujet"))); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void given2XMLs_whenSimilarWithDiff_thenCorrect() throws Exception { |
| 127 | + String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 128 | + String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>"; |
| 129 | + |
| 130 | + Diff myDiffSimilar = DiffBuilder |
| 131 | + .compare(myControlXML) |
| 132 | + .withTest(myTestXML) |
| 133 | + .withNodeMatcher( |
| 134 | + new DefaultNodeMatcher(ElementSelectors.byName)) |
| 135 | + .checkForSimilar().build(); |
| 136 | + assertFalse("XML similar " + myDiffSimilar.toString(), |
| 137 | + myDiffSimilar.hasDifferences()); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void given2XMLsWithDifferences_whenTestsSimilarWithDifferenceEvaluator_thenCorrect() { |
| 143 | + final String control = "<a><b attr=\"abc\"></b></a>"; |
| 144 | + final String test = "<a><b attr=\"xyz\"></b></a>"; |
| 145 | + Diff myDiff = DiffBuilder |
| 146 | + .compare(control) |
| 147 | + .withTest(test) |
| 148 | + .withDifferenceEvaluator( |
| 149 | + new IgnoreAttributeDifferenceEvaluator("attr")) |
| 150 | + .checkForSimilar().build(); |
| 151 | + |
| 152 | + assertFalse(myDiff.toString(), myDiff.hasDifferences()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void given2XMLsWithDifferences_whenTestsDifferentWithoutDifferenceEvaluator_thenCorrect() { |
| 157 | + final String control = "<a><b attr=\"abc\"></b></a>"; |
| 158 | + final String test = "<a><b attr=\"xyz\"></b></a>"; |
| 159 | + |
| 160 | + Diff myDiff = DiffBuilder.compare(control).withTest(test) |
| 161 | + .checkForSimilar().build(); |
| 162 | + |
| 163 | + assertTrue(myDiff.toString(), myDiff.hasDifferences()); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void given2XMLS_whenSimilarWithCustomElementSelector_thenCorrect() { |
| 168 | + String controlXml = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 169 | + String testXml = "<struct><boolean>false</boolean><int>3</int></struct>"; |
| 170 | + assertThat( |
| 171 | + testXml, |
| 172 | + isSimilarTo(controlXml).withNodeMatcher( |
| 173 | + new DefaultNodeMatcher(ElementSelectors.byName))); |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void givenFileSourceAsObject_whenAbleToInput_thenCorrect() { |
| 179 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 180 | + assertThat(Input.from(new File(classLoader.getResource("test.xml") |
| 181 | + .getFile())), isSimilarTo(Input.from(new File(classLoader |
| 182 | + .getResource("control.xml").getFile())))); |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void givenStreamAsSource_whenAbleToInput_thenCorrect() { |
| 188 | + assertThat(Input.fromStream(new XMLUnitTest().getClass() |
| 189 | + .getResourceAsStream("/test.xml")), |
| 190 | + isSimilarTo(Input.fromStream(new XMLUnitTest().getClass() |
| 191 | + .getResourceAsStream("/control.xml")))); |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void givenStreamAsObject_whenAbleToInput_thenCorrect() { |
| 197 | + assertThat(Input.from(new XMLUnitTest().getClass().getResourceAsStream( |
| 198 | + "/test.xml")), isSimilarTo(Input.from(new XMLUnitTest() |
| 199 | + .getClass().getResourceAsStream("/control.xml")))); |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + public void givenStringSourceAsObject_whenAbleToInput_thenCorrect() { |
| 205 | + assertThat( |
| 206 | + Input.from("<struct><int>3</int><boolean>false</boolean></struct>"), |
| 207 | + isSimilarTo(Input |
| 208 | + .from("<struct><int>3</int><boolean>false</boolean></struct>"))); |
| 209 | + |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void givenFileSource_whenAbleToInput_thenCorrect() { |
| 214 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 215 | + String testPath = classLoader.getResource("test.xml").getPath(); |
| 216 | + String controlPath = classLoader.getResource("control.xml").getPath(); |
| 217 | + assertThat(Input.fromFile(testPath), |
| 218 | + isSimilarTo(Input.fromFile(controlPath))); |
| 219 | + |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void givenStringSource_whenAbleToInput_thenCorrect() { |
| 224 | + String controlXml = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 225 | + String testXml = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 226 | + assertThat(Input.fromString(testXml), |
| 227 | + isSimilarTo(Input.fromString(controlXml))); |
| 228 | + |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + public void givenSource_whenAbleToInput_thenCorrect() { |
| 233 | + String controlXml = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 234 | + String testXml = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 235 | + assertThat(Input.fromString(testXml), |
| 236 | + isSimilarTo(Input.fromString(controlXml))); |
| 237 | + |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + public void given2XMLS_whenIdentical_thenCorrect() { |
| 242 | + String controlXml = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 243 | + String testXml = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 244 | + assertThat(testXml, isIdenticalTo(controlXml)); |
| 245 | + |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void given2XMLSWithSimilarNodesButDifferentSequence_whenNotIdentical_thenCorrect() { |
| 250 | + String controlXml = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 251 | + String testXml = "<struct><boolean>false</boolean><int>3</int></struct>"; |
| 252 | + assertThat(testXml, not(isIdenticalTo(controlXml))); |
| 253 | + |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + public void given2XMLS_whenGeneratesDifferences_thenCorrect() |
| 258 | + throws Exception { |
| 259 | + String controlXml = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 260 | + String testXml = "<struct><boolean>false</boolean><int>3</int></struct>"; |
| 261 | + Diff myDiff = DiffBuilder.compare(controlXml).withTest(testXml).build(); |
| 262 | + Iterator<Difference> iter = myDiff.getDifferences().iterator(); |
| 263 | + int size = 0; |
| 264 | + while (iter.hasNext()) { |
| 265 | + iter.next().toString(); |
| 266 | + size++; |
| 267 | + } |
| 268 | + assertThat(size, greaterThan(1)); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + public void given2XMLS_whenGeneratesOneDifference_thenCorrect() |
| 273 | + throws Exception { |
| 274 | + String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>"; |
| 275 | + String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>"; |
| 276 | + Diff myDiff = DiffBuilder |
| 277 | + .compare(myControlXML) |
| 278 | + .withTest(myTestXML) |
| 279 | + .withComparisonController( |
| 280 | + ComparisonControllers.StopWhenDifferent).build(); |
| 281 | + Iterator<Difference> iter = myDiff.getDifferences().iterator(); |
| 282 | + int size = 0; |
| 283 | + while (iter.hasNext()) { |
| 284 | + iter.next().toString(); |
| 285 | + size++; |
| 286 | + } |
| 287 | + assertThat(size, equalTo(1)); |
| 288 | + } |
| 289 | + |
| 290 | +} |
0 commit comments