| 1 | package fr.sii.ogham.testing.assertion.html; | |
| 2 | ||
| 3 | import java.util.List; | |
| 4 | ||
| 5 | import org.custommonkey.xmlunit.DetailedDiff; | |
| 6 | import org.custommonkey.xmlunit.Difference; | |
| 7 | import org.junit.ComparisonFailure; | |
| 8 | import org.slf4j.Logger; | |
| 9 | import org.slf4j.LoggerFactory; | |
| 10 | import org.w3c.dom.Document; | |
| 11 | ||
| 12 | import fr.sii.ogham.testing.assertion.util.HtmlUtils; | |
| 13 | ||
| 14 | /** | |
| 15 | * Utility class for checking HTML content. | |
| 16 | * | |
| 17 | * @author Aurélien Baudet | |
| 18 | * | |
| 19 | */ | |
| 20 | public final class AssertHtml { | |
| 21 | private static final Logger LOG = LoggerFactory.getLogger(AssertHtml.class); | |
| 22 | ||
| 23 | /** | |
| 24 | * Check if the HTML is identical to the expected. The HTML strings are | |
| 25 | * parsed into {@link Document}s. Two documents are considered to be | |
| 26 | * "identical" if they contain the same elements and attributes in the same | |
| 27 | * order. | |
| 28 | * <p> | |
| 29 | * For each difference, the difference will be logged with error level. It | |
| 30 | * will generate a {@link ComparisonFailure} with the expected string and | |
| 31 | * actual string in order to be able to visualize the differences on sources | |
| 32 | * directly in the IDE. | |
| 33 | * </p> | |
| 34 | * | |
| 35 | * @param expected | |
| 36 | * the expected HTML | |
| 37 | * @param actual | |
| 38 | * the HTML content to check | |
| 39 | */ | |
| 40 | public static void assertEquals(String expected, String actual) { | |
| 41 | DetailedDiff diff = HtmlUtils.compare(expected, actual); | |
| 42 |
4
1. assertEquals : negated conditional → NO_COVERAGE 2. assertEquals : negated conditional → KILLED 3. assertEquals : negated conditional → KILLED 4. assertEquals : negated conditional → KILLED |
if (!diff.identical()) { |
| 43 |
2
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/html/AssertHtml::logDifferences → SURVIVED 2. assertEquals : removed call to fr/sii/ogham/testing/assertion/html/AssertHtml::logDifferences → NO_COVERAGE |
logDifferences(diff); |
| 44 | throw new ComparisonFailure("HTML element different to expected one. See logs for details about found differences.\n", expected, actual); | |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * Check if the HTML is similar to the expected. The HTML strings are parsed | |
| 50 | * into {@link Document}s. Two documents are considered to be "similar" if | |
| 51 | * they contain the same elements and attributes regardless of order. | |
| 52 | * <p> | |
| 53 | * For each difference, the difference will be logged with error level. It | |
| 54 | * will generate a {@link ComparisonFailure} with the expected string and | |
| 55 | * actual string in order to be able to visualize the differences on sources | |
| 56 | * directly in the IDE. | |
| 57 | * </p> | |
| 58 | * | |
| 59 | * @param expected | |
| 60 | * the expected HTML | |
| 61 | * @param actual | |
| 62 | * the HTML content to check | |
| 63 | */ | |
| 64 | public static void assertSimilar(String expected, String actual) { | |
| 65 | DetailedDiff diff = HtmlUtils.compare(expected, actual); | |
| 66 |
2
1. assertSimilar : negated conditional → NO_COVERAGE 2. assertSimilar : negated conditional → KILLED |
if (!diff.similar()) { |
| 67 |
2
1. assertSimilar : removed call to fr/sii/ogham/testing/assertion/html/AssertHtml::logUnrecoverableDifferences → NO_COVERAGE 2. assertSimilar : removed call to fr/sii/ogham/testing/assertion/html/AssertHtml::logUnrecoverableDifferences → SURVIVED |
logUnrecoverableDifferences(diff); |
| 68 | throw new ComparisonFailure("HTML element different to expected one. See logs for details about found differences.\n", expected, actual); | |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | private static void logDifferences(DetailedDiff diff) { | |
| 73 | for (Difference difference : (List<Difference>) diff.getAllDifferences()) { | |
| 74 | LOG.error(difference.toString()); // NOSONAR | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | private static void logUnrecoverableDifferences(DetailedDiff diff) { | |
| 79 | for (Difference difference : (List<Difference>) diff.getAllDifferences()) { | |
| 80 |
2
1. logUnrecoverableDifferences : negated conditional → NO_COVERAGE 2. logUnrecoverableDifferences : negated conditional → SURVIVED |
if (!difference.isRecoverable()) { |
| 81 | LOG.error(difference.toString()); // NOSONAR | |
| 82 | } | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | private AssertHtml() { | |
| 87 | super(); | |
| 88 | } | |
| 89 | } | |
Mutations | ||
| 42 |
1.1 2.2 3.3 4.4 |
|
| 43 |
1.1 2.2 |
|
| 66 |
1.1 2.2 |
|
| 67 |
1.1 2.2 |
|
| 80 |
1.1 2.2 |