Skip to content

Commit 5bd5c8e

Browse files
author
eugenp
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents 551ee20 + 0d1476c commit 5bd5c8e

11 files changed

Lines changed: 427 additions & 1 deletion

File tree

‎rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
4545
}
4646

4747
@Test
48-
public void givenUrl_whenSuccessOnGetsResponse_andJsonHasRequiredKV_thenCorrect() {
48+
public void givenUrl_whenSuccessOnGetsResponseAndJsonHasRequiredKV_thenCorrect() {
4949

5050
get("/events?id=390").then().statusCode(200).assertThat()
5151
.body("id", equalTo("390"));

‎xmlunit2-tutorial/README.md‎

Whitespace-only changes.

‎xmlunit2-tutorial/pom.xml‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>xmlunit2-tutorial</artifactId>
6+
<version>1.0</version>
7+
<name>XMLUnit-2</name>
8+
<build>
9+
<plugins>
10+
<plugin>
11+
<groupId>org.apache.maven.plugins</groupId>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<version>3.3</version>
14+
<configuration>
15+
<source>7</source>
16+
<target>7</target>
17+
</configuration>
18+
</plugin>
19+
</plugins>
20+
</build>
21+
<dependencies>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>4.3</version>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.hamcrest</groupId>
30+
<artifactId>hamcrest-all</artifactId>
31+
<version>1.3</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.xmlunit</groupId>
35+
<artifactId>xmlunit-matchers</artifactId>
36+
<version>2.2.1</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.xmlunit</groupId>
41+
<artifactId>xmlunit-core</artifactId>
42+
<version>2.2.1</version>
43+
</dependency>
44+
45+
</dependencies>
46+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.xmlunit;
2+
3+
import org.w3c.dom.Attr;
4+
import org.w3c.dom.Node;
5+
import org.xmlunit.diff.Comparison;
6+
import org.xmlunit.diff.ComparisonResult;
7+
import org.xmlunit.diff.DifferenceEvaluator;
8+
9+
public class IgnoreAttributeDifferenceEvaluator implements DifferenceEvaluator {
10+
11+
private String attributeName;
12+
13+
public IgnoreAttributeDifferenceEvaluator(String attributeName) {
14+
this.attributeName = attributeName;
15+
}
16+
17+
@Override
18+
public ComparisonResult evaluate(Comparison comparison,
19+
ComparisonResult outcome) {
20+
if (outcome == ComparisonResult.EQUAL)
21+
return outcome;
22+
final Node controlNode = comparison.getControlDetails().getTarget();
23+
if (controlNode instanceof Attr) {
24+
Attr attr = (Attr) controlNode;
25+
if (attr.getName().equals(attributeName)) {
26+
return ComparisonResult.SIMILAR;
27+
}
28+
}
29+
return outcome;
30+
}
31+
}
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<struct>
2+
<int>3</int>
3+
<boolean>false</boolean>
4+
</struct>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version = "1.0"?>
2+
<class>
3+
<student id="393">
4+
<name>Rajiv</name>
5+
<age>18</age>
6+
</student>
7+
<student id="493">
8+
<name>Candie</name>
9+
<age>19</age>
10+
</student>
11+
</class>

0 commit comments

Comments
 (0)