Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HtmlBlockAssert.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.stream.Collectors;
  22. import org.assertj.core.api.AbstractAssert;
  23. import org.assertj.core.api.Assertions;
  24. import org.jsoup.nodes.Element;
  25. import org.jsoup.select.Elements;
  26. import static org.assertj.core.util.Preconditions.checkArgument;
  27. public abstract class HtmlBlockAssert<T extends HtmlBlockAssert<T>> extends AbstractAssert<T, Element> {
  28. static final String PRINT_FRAGMENT_TEMPLATE = "\n---fragment---\n%s\n---fragment---";
  29. private static final String NO_LINK_IN_BLOC = "no link in bloc";
  30. public HtmlBlockAssert(Element v, Class<T> selfType) {
  31. super(v, selfType);
  32. }
  33. /**
  34. * Verifies the current block contains a single link with the specified piece of text.
  35. */
  36. public T withLinkOn(String linkText) {
  37. return withLinkOn(linkText, 1);
  38. }
  39. /**
  40. * Verifies the current block contains {@code times} links with the specified piece of text.
  41. */
  42. public T withLinkOn(String linkText, int times) {
  43. checkArgument(times >= 1, "times must be >= 1");
  44. isNotNull();
  45. Elements as = actual.select("a");
  46. Assertions.assertThat(as)
  47. .describedAs(NO_LINK_IN_BLOC + PRINT_FRAGMENT_TEMPLATE, actual)
  48. .isNotEmpty();
  49. long count = as.stream().filter(t -> linkText.equals(t.text())).count();
  50. if (count != times) {
  51. failWithMessage("link on text \"%s\" found %s times in bloc (expected %s). \n Got: %s", linkText, count, times, asyncLinksToString(as));
  52. }
  53. return myself;
  54. }
  55. /**
  56. * Verifies the current block contains a link with the specified text and href.
  57. */
  58. public T withLink(String linkText, String href) {
  59. isNotNull();
  60. Elements as = actual.select("a");
  61. Assertions.assertThat(as)
  62. .describedAs(NO_LINK_IN_BLOC + PRINT_FRAGMENT_TEMPLATE, actual)
  63. .isNotEmpty();
  64. if (as.stream().noneMatch(t -> linkText.equals(t.text()) && href.equals(t.attr("href")))) {
  65. failWithMessage(
  66. "link with text \"%s\" and href \"%s\" not found in block. \n Got: %s" + PRINT_FRAGMENT_TEMPLATE,
  67. linkText, href, asyncLinksToString(as), actual);
  68. }
  69. return myself;
  70. }
  71. public T withoutLink() {
  72. isNotNull();
  73. Assertions.assertThat(actual.select("a")).isEmpty();
  74. return myself;
  75. }
  76. private static Object asyncLinksToString(Elements linkElements) {
  77. return new Object() {
  78. @Override
  79. public String toString() {
  80. return linkElements.stream()
  81. .map(a -> "<a href=\"" + a.attr("href") + "\">" + a.text() + "<a>")
  82. .collect(Collectors.joining("\n"));
  83. }
  84. };
  85. }
  86. public T withEmphasisOn(String emphasisText) {
  87. isNotNull();
  88. Elements emphases = actual.select("em");
  89. Assertions.assertThat(emphases)
  90. .describedAs("no <em> in block")
  91. .isNotEmpty();
  92. Assertions.assertThat(emphases.stream().map(Element::text))
  93. .contains(emphasisText);
  94. return myself;
  95. }
  96. public T withSmallOn(String emphasisText) {
  97. isNotNull();
  98. Elements smalls = actual.select("small");
  99. Assertions.assertThat(smalls)
  100. .describedAs("no <small> in block")
  101. .isNotEmpty();
  102. Assertions.assertThat(smalls.stream().map(Element::text))
  103. .contains(emphasisText);
  104. return myself;
  105. }
  106. }