aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-testing-harness/src/main/java/org/sonar/test/html/HtmlParagraphAssert.java
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-testing-harness/src/main/java/org/sonar/test/html/HtmlParagraphAssert.java')
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/html/HtmlParagraphAssert.java220
1 files changed, 220 insertions, 0 deletions
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/html/HtmlParagraphAssert.java b/sonar-testing-harness/src/main/java/org/sonar/test/html/HtmlParagraphAssert.java
new file mode 100644
index 00000000000..bf7210afa33
--- /dev/null
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/html/HtmlParagraphAssert.java
@@ -0,0 +1,220 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.test.html;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Stream;
+import org.assertj.core.api.Assertions;
+import org.jsoup.nodes.Element;
+import org.jsoup.nodes.Node;
+import org.jsoup.nodes.TextNode;
+
+import static java.util.Collections.emptyList;
+
+public class HtmlParagraphAssert extends HtmlBlockAssert<HtmlParagraphAssert> {
+ private final Iterator<Element> nextBlocks;
+
+ public HtmlParagraphAssert(Element paragraph, Iterator<Element> nextBlocks) {
+ super(paragraph, HtmlParagraphAssert.class);
+ this.nextBlocks = nextBlocks;
+ }
+
+ static void verifyIsParagraph(Element element) {
+ Assertions.assertThat(element.tagName())
+ .describedAs(
+ "next block is not a <%s> (got <%s>):" + PRINT_FRAGMENT_TEMPLATE,
+ "p", element.tagName(), element.toString())
+ .isEqualTo("p");
+ }
+
+ /**
+ * Verify the next block exists, is a paragraph and returns an Assert on this block.
+ */
+ public HtmlParagraphAssert hasParagraph() {
+ isNotNull();
+
+ Assertions.assertThat(nextBlocks.hasNext())
+ .describedAs("no more bloc")
+ .isTrue();
+
+ Element element = nextBlocks.next();
+ verifyIsParagraph(element);
+
+ return new HtmlParagraphAssert(element, nextBlocks);
+ }
+
+ /**
+ * Convenience method.
+ * Sames as {@code hasParagraph().withText(text)}.
+ */
+ public HtmlParagraphAssert hasParagraph(String text) {
+ return hasParagraph()
+ .withText(text);
+ }
+
+ /**
+ * Convenience method.
+ * Sames as {@code hasParagraph().withText("")}.
+ */
+ public HtmlParagraphAssert hasEmptyParagraph() {
+ return hasParagraph()
+ .withText("");
+ }
+
+ /**
+ * Convenience method.
+ * Sames as {@code hasParagraph().withLines(line1, line2, ...)}.
+ */
+ public HtmlParagraphAssert hasParagraph(String firstLine, String... otherLines) {
+ return hasParagraph()
+ .withLines(firstLine, otherLines);
+ }
+
+ /**
+ * Verifies there is no more block.
+ */
+ public void noMoreBlock() {
+ isNotNull();
+
+ Assertions.assertThat(nextBlocks.hasNext())
+ .describedAs("there are still some paragraph. Next one:" + PRINT_FRAGMENT_TEMPLATE,
+ new Object() {
+ @Override
+ public String toString() {
+ return nextBlocks.next().toString();
+ }
+ })
+ .isFalse();
+ }
+
+ /**
+ * Verifies the current block as the specified text, ignoring lines.
+ */
+ public HtmlParagraphAssert withText(String text) {
+ isNotNull();
+
+ Assertions.assertThat(actual.text())
+ .describedAs(PRINT_FRAGMENT_TEMPLATE, actual)
+ .isEqualTo(text);
+
+ return this;
+ }
+
+ /**
+ * Verifies the current block has all and only the specified lines, in order.
+ */
+ public HtmlParagraphAssert withLines(String firstLine, String... otherLines) {
+ isNotNull();
+
+ List<String> actualLines = toLines(actual);
+ String[] expectedLines = Stream.concat(
+ Stream.of(firstLine),
+ Arrays.stream(otherLines))
+ .toArray(String[]::new);
+
+ Assertions.assertThat(actualLines)
+ .describedAs(PRINT_FRAGMENT_TEMPLATE, actual)
+ .containsExactly(expectedLines);
+
+ return this;
+ }
+
+ /**
+ * Verifies the current block has all and only the specified lines, in any order.
+ */
+ public HtmlParagraphAssert withLines(Set<String> lines) {
+ isNotNull();
+
+ List<String> actualLines = toLines(actual);
+ String[] expectedLines = lines.toArray(new String[0]);
+
+ Assertions.assertThat(actualLines)
+ .describedAs(PRINT_FRAGMENT_TEMPLATE, actual)
+ .containsOnly(expectedLines);
+
+ return this;
+ }
+
+ private static List<String> toLines(Element parent) {
+ Iterator<Node> iterator = parent.childNodes().iterator();
+ if (!iterator.hasNext()) {
+ return emptyList();
+ }
+
+ List<String> actualLines = new ArrayList<>(parent.childNodeSize());
+ StringBuilder currentLine = null;
+ while (iterator.hasNext()) {
+ Node node = iterator.next();
+ if (node instanceof TextNode) {
+ if (currentLine == null) {
+ currentLine = new StringBuilder(node.toString());
+ } else {
+ currentLine.append(node.toString());
+ }
+ } else if (node instanceof Element) {
+ Element element = (Element) node;
+ if (element.tagName().equals("br")) {
+ actualLines.add(currentLine == null ? "" : currentLine.toString());
+ currentLine = null;
+ } else {
+ if (currentLine == null) {
+ currentLine = new StringBuilder(element.text());
+ } else {
+ currentLine.append(element.text());
+ }
+ }
+ } else {
+ throw new IllegalStateException("unsupported node " + node.getClass());
+ }
+
+ if (!iterator.hasNext()) {
+ actualLines.add(currentLine == null ? "" : currentLine.toString());
+ currentLine = null;
+ }
+ }
+ return actualLines;
+ }
+
+ /**
+ * Convenience method.
+ * Same as {@code hasList().withItemTexts("foo", "bar")}.
+ */
+ public HtmlListAssert hasList(String firstItemText, String... otherItemsText) {
+ return hasList()
+ .withItemTexts(firstItemText, otherItemsText);
+ }
+
+ public HtmlListAssert hasList() {
+ isNotNull();
+
+ Assertions.assertThat(nextBlocks.hasNext())
+ .describedAs("no more block")
+ .isTrue();
+
+ Element element = nextBlocks.next();
+ HtmlListAssert.verifyIsList(element);
+
+ return new HtmlListAssert(element, nextBlocks);
+ }
+}