1 | package fr.sii.ogham.testing.assertion.util; | |
2 | ||
3 | import java.io.IOException; | |
4 | ||
5 | import javax.xml.parsers.ParserConfigurationException; | |
6 | ||
7 | import org.custommonkey.xmlunit.DetailedDiff; | |
8 | import org.custommonkey.xmlunit.HTMLDocumentBuilder; | |
9 | import org.custommonkey.xmlunit.TolerantSaxDocumentBuilder; | |
10 | import org.custommonkey.xmlunit.XMLUnit; | |
11 | import org.custommonkey.xmlunit.exceptions.ConfigurationException; | |
12 | import org.w3c.dom.Document; | |
13 | import org.xml.sax.SAXException; | |
14 | ||
15 | import fr.sii.ogham.testing.assertion.exception.ComparisonException; | |
16 | ||
17 | public final class HtmlUtils { | |
18 | /** | |
19 | * Compare two HTML documents. The HTML strings are parsed into | |
20 | * {@link Document}s. The HTML are compared by elements and attributes, not | |
21 | * using directly using string. | |
22 | * | |
23 | * <p> | |
24 | * A {@link DetailedDiff} is provided to know if the documents are | |
25 | * "identical" or "similar": | |
26 | * <ul> | |
27 | * <li>Two documents are considered to be "identical" if they contain the | |
28 | * same elements and attributes in the same order.</li> | |
29 | * <li>Two documents are considered to be "similar" if they contain the same | |
30 | * elements and attributes regardless of order.</li> | |
31 | * </ul> | |
32 | * | |
33 | * | |
34 | * @param expected | |
35 | * the expected HTML | |
36 | * @param actual | |
37 | * the HTML content to check | |
38 | * @return a report that let you know differences between the two HTML | |
39 | * strings | |
40 | */ | |
41 | public static DetailedDiff compare(String expected, String actual) { | |
42 |
5
1. compare : negated conditional → NO_COVERAGE 2. compare : negated conditional → TIMED_OUT 3. compare : negated conditional → KILLED 4. compare : negated conditional → KILLED 5. compare : negated conditional → KILLED |
if (expected == null) { |
43 | throw new IllegalArgumentException("expected html can't be null"); | |
44 | } | |
45 | try { | |
46 | HTMLDocumentBuilder builder = new HTMLDocumentBuilder(new TolerantSaxDocumentBuilder(XMLUnit.newTestParser())); | |
47 | Document expectedDoc = builder.parse(expected); | |
48 |
5
1. compare : negated conditional → NO_COVERAGE 2. compare : negated conditional → TIMED_OUT 3. compare : negated conditional → KILLED 4. compare : negated conditional → KILLED 5. compare : negated conditional → KILLED |
Document actualDoc = builder.parse(actual==null ? "" : actual); |
49 |
5
1. compare : replaced return value with null for fr/sii/ogham/testing/assertion/util/HtmlUtils::compare → NO_COVERAGE 2. compare : replaced return value with null for fr/sii/ogham/testing/assertion/util/HtmlUtils::compare → TIMED_OUT 3. compare : replaced return value with null for fr/sii/ogham/testing/assertion/util/HtmlUtils::compare → KILLED 4. compare : replaced return value with null for fr/sii/ogham/testing/assertion/util/HtmlUtils::compare → KILLED 5. compare : replaced return value with null for fr/sii/ogham/testing/assertion/util/HtmlUtils::compare → KILLED |
return new DetailedDiff(XMLUnit.compareXML(expectedDoc, actualDoc)); |
50 | } catch (SAXException | IOException | ConfigurationException | ParserConfigurationException e) { | |
51 | throw new ComparisonException("Failed to compare HTML", e); | |
52 | } | |
53 | } | |
54 | | |
55 | private HtmlUtils() { | |
56 | super(); | |
57 | } | |
58 | } | |
Mutations | ||
42 |
1.1 2.2 3.3 4.4 5.5 |
|
48 |
1.1 2.2 3.3 4.4 5.5 |
|
49 |
1.1 2.2 3.3 4.4 5.5 |