diff options
author | Jean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com> | 2013-04-10 13:29:53 +0200 |
---|---|---|
committer | Jean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com> | 2013-04-10 13:29:53 +0200 |
commit | 45038a69bd8ae946a089d86aa2625e7518e54d9d (patch) | |
tree | 4c0b65b0c6cb74684df1548b8847a47e1a7bca20 /sonar-core/src/test | |
parent | abb01b4c357b49fb2e660b59b703048c09fab69a (diff) | |
download | sonarqube-45038a69bd8ae946a089d86aa2625e7518e54d9d.tar.gz sonarqube-45038a69bd8ae946a089d86aa2625e7518e54d9d.zip |
(SONAR-3893) Improve the highlighter API to not depend on sonar-channel and allow to work on multi-line tokens - Added main access point for syntax highlighting
Diffstat (limited to 'sonar-core/src/test')
3 files changed, 91 insertions, 1 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java index 4d3e3d680ba..56f926dda90 100644 --- a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java +++ b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java @@ -38,7 +38,7 @@ public class HtmlTextWrapperTest { HtmlTextWrapper htmlTextWrapper = new HtmlTextWrapper(); String htmlOutput = htmlTextWrapper.wrapTextWithHtml(packageDeclaration, context); - assertThat(htmlOutput).isEqualTo("<tr><td><span class=\"k\">package</span> org.sonar.core.source;"); + assertThat(htmlOutput).isEqualTo("<tr><td><span class=\"k\">package</span> org.sonar.core.source;</td></tr>"); } @Test @@ -133,4 +133,30 @@ public class HtmlTextWrapperTest { "<tr><td>}</td></tr>" + CR_END_OF_LINE + LF_END_OF_LINE ); } + + @Test + public void should_close_tags_at_end_of_file() throws Exception { + + String classDeclarationSample = + "/*" + LF_END_OF_LINE + + " * Header" + LF_END_OF_LINE + + " */" + LF_END_OF_LINE + + LF_END_OF_LINE + + "public class HelloWorld {" + LF_END_OF_LINE + + "}"; + + HighlightingContext context = HighlightingContext.buildFrom("0,16,cppd;18,25,k;25,31,k;"); + + HtmlTextWrapper htmlTextWrapper = new HtmlTextWrapper(); + String htmlOutput = htmlTextWrapper.wrapTextWithHtml(classDeclarationSample, context); + + assertThat(htmlOutput).isEqualTo( + "<tr><td><span class=\"cppd\">/*</span></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"cppd\"> * Header</span></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"cppd\"> */</span></td></tr>" + LF_END_OF_LINE + + "<tr><td></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"k\">public </span><span class=\"k\">class </span>HelloWorld {</td></tr>" + LF_END_OF_LINE + + "<tr><td>}</td></tr>" + ); + } } diff --git a/sonar-core/src/test/java/org/sonar/core/source/SyntaxHighlighterTest.java b/sonar-core/src/test/java/org/sonar/core/source/SyntaxHighlighterTest.java new file mode 100644 index 00000000000..d1fb7111c81 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/source/SyntaxHighlighterTest.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ + +package org.sonar.core.source; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.core.persistence.AbstractDaoTestCase; + +import static org.fest.assertions.Assertions.assertThat; +import static org.sonar.core.source.HtmlTextWrapper.LF_END_OF_LINE; + +public class SyntaxHighlighterTest extends AbstractDaoTestCase { + + @Before + public void setUpDatasets() { + setupData("shared"); + } + + @Test + public void should_highlight_source_with_html() throws Exception { + + SyntaxHighlighter highlighter = new SyntaxHighlighter(getMyBatis()); + + String highlightedSource = highlighter.getHighlightedSourceAsHtml(11L); + + assertThat(highlightedSource).isEqualTo( + "<tr><td><span class=\"cppd\">/*</span></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"cppd\"> * Header</span></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"cppd\"> */</span></td></tr>" + LF_END_OF_LINE + + "<tr><td></td></tr>" + LF_END_OF_LINE + + "<tr><td><span class=\"k\">public </span><span class=\"k\">class </span>HelloWorld {</td></tr>" + LF_END_OF_LINE + + "<tr><td>}</td></tr>" + ); + } + +} diff --git a/sonar-core/src/test/resources/org/sonar/core/source/SyntaxHighlighterTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/source/SyntaxHighlighterTest/shared.xml new file mode 100644 index 00000000000..086fd88f9e4 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/source/SyntaxHighlighterTest/shared.xml @@ -0,0 +1,10 @@ +<dataset> + + <projects id="1" kee="org.apache.struts:struts" enabled="[true]"/> + + <snapshots id="11" project_id="1" islast="[true]" /> + + <snapshot_data id="101" resource_id="1" snapshot_id="11" data="0,16,cppd;18,25,k;25,31,k;" data_type="highlight_syntax" /> + + <snapshot_sources id="101" snapshot_id="11" data="/* * Header */ public class HelloWorld { }" /> +</dataset>
\ No newline at end of file |