001// Generated by delombok at Sun Jul 30 17:21:56 UTC 2023
002package de.cuioss.test.jsf.renderer.util;
003
004import static org.junit.jupiter.api.Assertions.assertEquals;
005import static org.junit.jupiter.api.Assertions.assertNotNull;
006import static org.junit.jupiter.api.Assertions.fail;
007import org.jdom2.Attribute;
008import org.jdom2.Document;
009import org.jdom2.Element;
010
011/**
012 * Provides asserts for {@link Document} and Jdom elements.
013 *
014 * @author Oliver Wolff
015 */
016public final class HtmlTreeAsserts {
017    private static final String THE_VALUES_ARE_NOT_EQUAL_EXPECTED = "%s: The values for attribute \'%s\' are not equal, expected=%s, actual=%s";
018    private static final String ACTUAL_MUST_NOT_BE_NULL = "Actual must not be null";
019    private static final String EXPECTED_MUST_NOT_BE_NULL = "Expected must not be null";
020    private static final AttributeComparator ATTRIBUTE_COMPARATOR = new AttributeComparator();
021
022    /**
023     * Compares two instances of {@link Document} for equality. Attribute order is
024     * not relevant, but element order is.
025     *
026     * @param expected expected value
027     * @param actual   the value to check against <code>expected</code>
028     */
029    public static void assertHtmlTreeEquals(final Document expected, final Document actual) {
030        assertNotNull(expected, EXPECTED_MUST_NOT_BE_NULL);
031        assertNotNull(actual, ACTUAL_MUST_NOT_BE_NULL);
032        var expectedNode = expected.getRootElement();
033        var actualNode = actual.getRootElement();
034        assertElementWithChildrenEquals(expectedNode, actualNode, "");
035    }
036
037    /**
038     * Compares two instances of {@link Element} for equality. Attribute order is
039     * not relevant.It checks the children recursively as well.
040     *
041     * @param expected expected value
042     * @param actual   the value to check against <code>expected</code>
043     * @param pointer  String based path identifier
044     */
045    public static void assertElementWithChildrenEquals(final Element expected, final Element actual, final String pointer) {
046        var currentPointer = pointer + ">" + expected.getName();
047        assertElementEquals(expected, actual, currentPointer);
048        var expectedChildren = expected.getChildren();
049        var actualChildren = actual.getChildren();
050        if (null != expected.getAttribute("id")) {
051            currentPointer = currentPointer + "[" + expected.getAttribute("id").getValue() + "]";
052        }
053        var expectedTextChild = expected.getTextNormalize();
054        var actualTextChild = actual.getTextNormalize();
055        assertEquals(expectedTextChild, actualTextChild, String.format("%s: The text content of the elements are not equal, expected=%s, actual=%s", currentPointer, expectedTextChild, actualTextChild));
056        if (expectedChildren.isEmpty() && actualChildren.isEmpty()) {
057            return;
058        }
059        if (expectedChildren.size() != actualChildren.size()) {
060            fail(String.format("%s: The number of children is not equal, expected=%s, actual=%s", currentPointer, expectedChildren, actualChildren));
061        }
062        for (var i = 0; i < expectedChildren.size(); i++) {
063            assertElementWithChildrenEquals(expectedChildren.get(i), actualChildren.get(i), currentPointer);
064        }
065    }
066
067    /**
068     * Compares two instances of {@link Element} for equality. Attribute order is
069     * not relevant. It checks the name of the elements and the attributes,
070     * <em>not</em> the children
071     *
072     * @param expected expected value
073     * @param actual   actual the value to check against <code>expected</code>
074     * @param pointer  String based path identifier
075     */
076    public static void assertElementEquals(final Element expected, final Element actual, final String pointer) {
077        assertNotNull(expected, EXPECTED_MUST_NOT_BE_NULL);
078        assertNotNull(actual, ACTUAL_MUST_NOT_BE_NULL);
079        assertEquals(expected.getName(), actual.getName(), String.format("%s: The names are not equal, expected=%s, actual=%s", pointer, expected, actual));
080        var expectedAttributes = expected.getAttributes();
081        var actualAttributes = actual.getAttributes();
082        if (expectedAttributes.isEmpty() && actualAttributes.isEmpty()) {
083            return;
084        }
085        expectedAttributes.sort(ATTRIBUTE_COMPARATOR);
086        actualAttributes.sort(ATTRIBUTE_COMPARATOR);
087        if (expectedAttributes.size() != actualAttributes.size()) {
088            fail(String.format("%s: The number of the attributes are not equal, expected=%s, actual=%s", pointer, expectedAttributes, actualAttributes));
089        }
090        for (var i = 0; i < expectedAttributes.size(); i++) {
091            assertAttributeEquals(expectedAttributes.get(i), actualAttributes.get(i), pointer);
092        }
093    }
094
095    /**
096     * Compares two instances of {@link Attribute} for equality.
097     *
098     * @param expected expected value
099     * @param actual   the value to check against <code>expected</code>
100     * @param pointer  String based path identifier
101     */
102    public static void assertAttributeEquals(final Attribute expected, final Attribute actual, final String pointer) {
103        assertNotNull(expected, EXPECTED_MUST_NOT_BE_NULL);
104        assertNotNull(actual, ACTUAL_MUST_NOT_BE_NULL);
105        assertEquals(expected.getName(), actual.getName(), String.format("%s: The name of the attributes are not equal, expected=%s, actual=%s", pointer, expected, actual));
106        assertEquals(expected.getValue(), actual.getValue(), String.format(THE_VALUES_ARE_NOT_EQUAL_EXPECTED, pointer, expected.getName(), expected.getValue(), actual.getValue()));
107    }
108
109    @java.lang.SuppressWarnings("all")
110    @lombok.Generated
111    private HtmlTreeAsserts() {
112        throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated");
113    }
114}