您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CodeColorizerTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.apache.commons.io.IOUtils;
  22. import org.junit.Test;
  23. import java.io.IOException;
  24. import java.io.Reader;
  25. import java.io.StringReader;
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.List;
  29. import java.util.concurrent.Callable;
  30. import java.util.concurrent.ExecutionException;
  31. import java.util.concurrent.Executors;
  32. import java.util.concurrent.Future;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. public class CodeColorizerTest {
  35. @Test
  36. public void javaToHtml() throws IOException {
  37. Reader java = readFile("/org/sonar/colorizer/samples/Sample.java");
  38. String html = CodeColorizer.javaToHtml(java, HtmlOptions.DEFAULT);
  39. assertHtml(html);
  40. assertContains(html, "<pre><span class=\"k\">public</span> <span class=\"k\">class</span> Sample {</pre>");
  41. }
  42. @Test
  43. public void shouldSupportWindowsEndOfLines() throws IOException {
  44. Reader windowsFile = readFile("/org/sonar/colorizer/samples/Sample.java", IOUtils.LINE_SEPARATOR_WINDOWS);
  45. String html = CodeColorizer.javaToHtml(windowsFile, HtmlOptions.DEFAULT);
  46. assertHtml(html);
  47. assertContains(html, "<pre><span class=\"k\">public</span> <span class=\"k\">class</span> Sample {</pre>");
  48. }
  49. @Test
  50. public void useHtmlOptions() throws IOException {
  51. Reader java = readFile("/org/sonar/colorizer/samples/Sample.java");
  52. HtmlOptions options = new HtmlOptions(true, "my-table-id", false);
  53. String html = CodeColorizer.javaToHtml(java, options);
  54. assertThat(html).contains("<table class=\"code\" id=\"my-table-id\"");
  55. assertThat(html).doesNotContain("<style");
  56. }
  57. @Test
  58. public void groovyToHtml() throws IOException {
  59. Reader groovy = readFile("/org/sonar/colorizer/samples/Sample.groovy");
  60. String html = CodeColorizer.groovyToHtml(groovy, HtmlOptions.DEFAULT);
  61. assertHtml(html);
  62. assertContains(html, "<pre><span class=\"k\">class</span> Greet {</pre>");
  63. }
  64. @Test
  65. public void mustBeThreadsafe() throws InterruptedException, ExecutionException, IOException {
  66. final int taskCount = 50;
  67. final int threadCount = 5;
  68. class ColorizerTask implements Callable<String> {
  69. Reader java;
  70. ColorizerTask() throws IOException {
  71. this.java = readFile("/org/sonar/colorizer/samples/Sample.java");
  72. }
  73. public String call() throws Exception {
  74. return CodeColorizer.javaToHtml(java, HtmlOptions.ONLY_SYNTAX);
  75. }
  76. }
  77. Collection<Callable<String>> tasks = new ArrayList<>();
  78. for (int i = 0; i < taskCount; i++) {
  79. tasks.add(new ColorizerTask());
  80. }
  81. List<Future<String>> futures = Executors.newFixedThreadPool(threadCount).invokeAll(tasks);
  82. assertThat(futures).hasSize(taskCount);
  83. // all html must be the same
  84. String html = futures.get(0).get();
  85. for (Future<String> future : futures) {
  86. assertThat(html).isEqualTo(future.get());
  87. }
  88. }
  89. @Test
  90. public void shouldEscapeSpecialCharacters() throws Exception {
  91. Reader java = readFile("/org/sonar/colorizer/samples/SampleWithComments.java");
  92. String html = CodeColorizer.javaToHtml(java, HtmlOptions.DEFAULT);
  93. assertHtml(html);
  94. assertContains(html, "<pre> <span class=\"cppd\">/*</span></pre>",
  95. "<pre><span class=\"cppd\"> * This method does &lt;b&gt;something&lt;/b&gt;</span></pre>",
  96. "<pre><span class=\"cppd\"> *</span></pre>",
  97. "<pre><span class=\"cppd\"> * &amp;lt;p&amp;gt;description&amp;lt;/p&amp;gt;</span></pre>",
  98. "<pre><span class=\"cppd\"> */</span></pre>");
  99. }
  100. /**
  101. * @return Reader for specified file with EOL normalized to specified one.
  102. */
  103. private Reader readFile(String path, String eol) throws IOException {
  104. StringBuilder sb = new StringBuilder();
  105. for (String line : IOUtils.readLines(getClass().getResourceAsStream(path))) {
  106. sb.append(line).append(eol);
  107. }
  108. return new StringReader(sb.toString());
  109. }
  110. /**
  111. * @return Reader for specified file with EOL normalized to LF.
  112. */
  113. private Reader readFile(String path) throws IOException {
  114. return readFile(path, IOUtils.LINE_SEPARATOR_UNIX);
  115. }
  116. private void assertHtml(String html) {
  117. assertContains(html, "<table class=\"code\"", "</html>");
  118. }
  119. private void assertContains(String html, String... strings) {
  120. for (String string : strings) {
  121. assertThat(html).contains(string);
  122. }
  123. }
  124. }