AssertHtml.java

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

Mutations

40

1.1
Location : assertEquals
Killed by :
negated conditional → RUN_ERROR

41

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/html/AssertHtml::logDifferences → NO_COVERAGE

47

1.1
Location : assertEquals
Killed by :
removed call to org/junit/jupiter/api/AssertionFailureBuilder::buildAndThrow → NO_COVERAGE

69

1.1
Location : assertSimilar
Killed by :
negated conditional → NO_COVERAGE

70

1.1
Location : assertSimilar
Killed by :
removed call to fr/sii/ogham/testing/assertion/html/AssertHtml::logDifferences → NO_COVERAGE

76

1.1
Location : assertSimilar
Killed by :
removed call to org/junit/jupiter/api/AssertionFailureBuilder::buildAndThrow → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1