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.

HtmlDecoratorTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.colorizer;
  21. import org.junit.Test;
  22. import org.sonar.channel.CodeReader;
  23. import static org.assertj.core.api.Assertions.assertThat;
  24. public class HtmlDecoratorTest {
  25. @Test
  26. public void beginOfFileTag() {
  27. HtmlDecorator decorator = new HtmlDecorator(HtmlOptions.DEFAULT);
  28. String tag = decorator.getTagBeginOfFile();
  29. assertContains(tag, "<html", "<table");
  30. }
  31. @Test
  32. public void beginOfFileTagWithTableId() {
  33. HtmlOptions options = new HtmlOptions().setGenerateTable(true).setTableId("foo");
  34. HtmlDecorator decorator = new HtmlDecorator(options);
  35. String tag = decorator.getTagBeginOfFile();
  36. assertContains(tag, "<table class=\"code\" id=\"foo\">");
  37. }
  38. @Test
  39. public void beginOfFileTagWithoutHeader() {
  40. HtmlOptions options = new HtmlOptions().setGenerateTable(true).setGenerateHtmlHeader(false);
  41. HtmlDecorator decorator = new HtmlDecorator(options);
  42. String tag = decorator.getTagBeginOfFile();
  43. assertNotContains(tag, "<html", "<body");
  44. assertContains(tag, "<table");
  45. }
  46. @Test
  47. public void endOfFileTag() {
  48. HtmlDecorator decorator = new HtmlDecorator(HtmlOptions.DEFAULT);
  49. String tag = decorator.getTagEndOfFile();
  50. assertContains(tag, "</table>", "</body>", "</html>");
  51. }
  52. @Test
  53. public void endOfFileTagWithoutHeader() {
  54. HtmlOptions options = new HtmlOptions().setGenerateTable(true).setGenerateHtmlHeader(false);
  55. HtmlDecorator decorator = new HtmlDecorator(options);
  56. String tag = decorator.getTagEndOfFile();
  57. assertContains(tag, "</table>");
  58. assertNotContains(tag, "</body>", "</html>");
  59. }
  60. @Test
  61. public void shouldAddTagsBetweenEachLine() {
  62. HtmlOptions options = new HtmlOptions().setGenerateTable(true).setGenerateHtmlHeader(false);
  63. HtmlDecorator decorator = new HtmlDecorator(options);
  64. CodeReader code = new CodeReader("\r\n\r");
  65. HtmlCodeBuilder output = new HtmlCodeBuilder();
  66. output.appendWithoutTransforming(decorator.getTagBeginOfFile());
  67. assertThat(decorator.consume(code, output)).isTrue();
  68. assertThat(decorator.consume(code, output)).isTrue();
  69. assertThat(decorator.consume(code, output)).isTrue();
  70. output.appendWithoutTransforming(decorator.getTagEndOfFile());
  71. assertThat(output.toString()).isEqualTo(
  72. "<table class=\"code\" id=\"\"><tbody>"
  73. + "<tr id=\"1\"><td><pre></pre></td></tr>"
  74. + "<tr id=\"2\"><td><pre></pre></td></tr>"
  75. + "<tr id=\"3\"><td><pre></pre></td></tr>"
  76. + "</tbody></table>"
  77. );
  78. }
  79. public void assertContains(String html, String... strings) {
  80. for (String string : strings) {
  81. assertThat(html).contains(string);
  82. }
  83. }
  84. public void assertNotContains(String html, String... strings) {
  85. for (String string : strings) {
  86. assertThat(html).doesNotContain(string);
  87. }
  88. }
  89. }