You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HtmlParagraphAssert.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.test.html;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.stream.Stream;
  26. import org.assertj.core.api.Assertions;
  27. import org.jsoup.nodes.Element;
  28. import org.jsoup.nodes.Node;
  29. import org.jsoup.nodes.TextNode;
  30. import static java.util.Collections.emptyList;
  31. public class HtmlParagraphAssert extends HtmlBlockAssert<HtmlParagraphAssert> {
  32. private final Iterator<Element> nextBlocks;
  33. public HtmlParagraphAssert(Element paragraph, Iterator<Element> nextBlocks) {
  34. super(paragraph, HtmlParagraphAssert.class);
  35. this.nextBlocks = nextBlocks;
  36. }
  37. static void verifyIsParagraph(Element element) {
  38. Assertions.assertThat(element.tagName())
  39. .describedAs(
  40. "next block is not a <%s> (got <%s>):" + PRINT_FRAGMENT_TEMPLATE,
  41. "p", element.tagName(), element.toString())
  42. .isEqualTo("p");
  43. }
  44. /**
  45. * Verify the next block exists, is a paragraph and returns an Assert on this block.
  46. */
  47. public HtmlParagraphAssert hasParagraph() {
  48. return new HtmlParagraphAssert(hasParagraphImpl(), nextBlocks);
  49. }
  50. private Element hasParagraphImpl() {
  51. isNotNull();
  52. Assertions.assertThat(nextBlocks.hasNext())
  53. .describedAs("no more bloc")
  54. .isTrue();
  55. Element element = nextBlocks.next();
  56. verifyIsParagraph(element);
  57. return element;
  58. }
  59. /**
  60. * Convenience method.
  61. * Sames as {@code hasParagraph().withText(text)}.
  62. */
  63. public HtmlParagraphAssert hasParagraph(String text) {
  64. return hasParagraph()
  65. .withText(text);
  66. }
  67. /**
  68. * Verifies next paragraph is empty or contains only "&nbsp;"
  69. */
  70. public HtmlParagraphAssert hasEmptyParagraph() {
  71. Element paragraph = hasParagraphImpl();
  72. Assertions.assertThat(paragraph.text())
  73. .describedAs(PRINT_FRAGMENT_TEMPLATE, paragraph)
  74. .isIn("", "\u00A0");
  75. return new HtmlParagraphAssert(paragraph, nextBlocks);
  76. }
  77. /**
  78. * Verifies there is no more block.
  79. */
  80. public void noMoreBlock() {
  81. isNotNull();
  82. Assertions.assertThat(nextBlocks.hasNext())
  83. .describedAs("there are still some paragraph. Next one:" + PRINT_FRAGMENT_TEMPLATE,
  84. new Object() {
  85. @Override
  86. public String toString() {
  87. return nextBlocks.next().toString();
  88. }
  89. })
  90. .isFalse();
  91. }
  92. /**
  93. * Verifies the current block as the specified text, ignoring lines.
  94. */
  95. public HtmlParagraphAssert withText(String text) {
  96. isNotNull();
  97. Assertions.assertThat(actual.text())
  98. .describedAs(PRINT_FRAGMENT_TEMPLATE, actual)
  99. .isEqualTo(text);
  100. return this;
  101. }
  102. /**
  103. * Verifies the current block has all and only the specified lines, in order.
  104. */
  105. public HtmlParagraphAssert withLines(String firstLine, String... otherLines) {
  106. isNotNull();
  107. List<String> actualLines = toLines(actual);
  108. String[] expectedLines = Stream.concat(
  109. Stream.of(firstLine),
  110. Arrays.stream(otherLines))
  111. .toArray(String[]::new);
  112. Assertions.assertThat(actualLines)
  113. .describedAs(PRINT_FRAGMENT_TEMPLATE, actual)
  114. .containsExactly(expectedLines);
  115. return this;
  116. }
  117. private static List<String> toLines(Element parent) {
  118. Iterator<Node> iterator = parent.childNodes().iterator();
  119. if (!iterator.hasNext()) {
  120. return emptyList();
  121. }
  122. List<String> actualLines = new ArrayList<>(parent.childNodeSize());
  123. StringBuilder currentLine = null;
  124. while (iterator.hasNext()) {
  125. Node node = iterator.next();
  126. if (node instanceof TextNode) {
  127. if (currentLine == null) {
  128. currentLine = new StringBuilder(node.toString());
  129. } else {
  130. currentLine.append(node.toString());
  131. }
  132. } else if (node instanceof Element) {
  133. Element element = (Element) node;
  134. if (element.tagName().equals("br")) {
  135. actualLines.add(currentLine == null ? "" : currentLine.toString());
  136. currentLine = null;
  137. } else {
  138. if (currentLine == null) {
  139. currentLine = new StringBuilder(element.text());
  140. } else {
  141. currentLine.append(element.text());
  142. }
  143. }
  144. } else {
  145. throw new IllegalStateException("unsupported node " + node.getClass());
  146. }
  147. if (!iterator.hasNext()) {
  148. actualLines.add(currentLine == null ? "" : currentLine.toString());
  149. currentLine = null;
  150. }
  151. }
  152. return actualLines;
  153. }
  154. /**
  155. * Convenience method.
  156. * Same as {@code hasList().withItemTexts("foo", "bar")}.
  157. */
  158. public HtmlListAssert hasList(String firstItemText, String... otherItemsText) {
  159. return hasList()
  160. .withItemTexts(firstItemText, otherItemsText);
  161. }
  162. public HtmlListAssert hasList() {
  163. isNotNull();
  164. Assertions.assertThat(nextBlocks.hasNext())
  165. .describedAs("no more block")
  166. .isTrue();
  167. Element element = nextBlocks.next();
  168. HtmlListAssert.verifyIsList(element);
  169. return new HtmlListAssert(element, nextBlocks);
  170. }
  171. }