From 91d2f185bd463b8a642645b3b82a369697b5432c Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 17 Dec 2014 17:13:10 +0100 Subject: [PATCH] SONAR-5869 Restore built-in Java Colorizer support for test files --- .../org/sonar/batch/source/CodeColorizers.java | 9 ++++++++- .../sonar/batch/source/CodeColorizersTest.java | 18 +++++++++++++++--- .../source/CodeColorizersTest/Person.java | 12 ++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/source/CodeColorizersTest/Person.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java b/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java index e3c5e8387a8..d4df1fafd61 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java +++ b/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java @@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; import org.sonar.api.web.CodeColorizerFormat; import org.sonar.batch.highlighting.SyntaxHighlightingData; +import org.sonar.colorizer.CodeColorizer; import org.sonar.colorizer.Tokenizer; import javax.annotation.CheckForNull; @@ -71,7 +72,13 @@ public class CodeColorizers implements BatchComponent { CodeColorizerFormat format = byLang.get(language); List tokenizers; if (format == null) { - return null; + // Workaround for Java test code since Java plugin only provides highlighting for main source and no colorizer + // TODO can be dropped when Java plugin embed its own CodeColorizerFormat of (better) provides highlighting for tests + if ("java".equals(language)) { + tokenizers = CodeColorizer.Format.JAVA.getTokenizers(); + } else { + return null; + } } else { tokenizers = format.getTokenizers(); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/CodeColorizersTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/CodeColorizersTest.java index 3932cdbb630..4fa2716b081 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/source/CodeColorizersTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/source/CodeColorizersTest.java @@ -41,7 +41,8 @@ import static org.fest.assertions.Assertions.assertThat; public class CodeColorizersTest { - private static final String HIGHLIGHTING = "0,4,cppd;5,11,cppd;12,15,cppd;16,19,k;29,37,k;65,69,k;85,93,cd;98,102,k;112,114,s;120,124,k"; + private static final String HIGHLIGHTING_JS = "0,4,cppd;5,11,cppd;12,15,cppd;16,19,k;29,37,k;65,69,k;85,93,cd;98,102,k;112,114,s;120,124,k"; + private static final String HIGHLIGHTING_JAVA = "0,4,j;5,11,j;12,15,j;16,22,k;23,28,k;43,50,k;51,54,k;67,78,a;81,87,k;88,92,k;97,100,k;142,146,k;162,170,cd"; @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -53,7 +54,7 @@ public class CodeColorizersTest { SyntaxHighlightingData syntaxHighlighting = codeColorizers.toSyntaxHighlighting(jsFile, "UTF-8", "js"); - assertThat(syntaxHighlighting.writeString()).isEqualTo(HIGHLIGHTING); + assertThat(syntaxHighlighting.writeString()).isEqualTo(HIGHLIGHTING_JS); } @@ -68,7 +69,18 @@ public class CodeColorizersTest { SyntaxHighlightingData syntaxHighlighting = codeColorizers.toSyntaxHighlighting(fileWithBom, "UTF-8", "js"); - assertThat(syntaxHighlighting.writeString()).isEqualTo(HIGHLIGHTING); + assertThat(syntaxHighlighting.writeString()).isEqualTo(HIGHLIGHTING_JS); + } + + @Test + public void shouldSupportJavaIfNotProvidedByJavaPluginForBackwardCompatibility() throws Exception { + CodeColorizers codeColorizers = new CodeColorizers(Arrays.asList()); + + File javaFile = new File(this.getClass().getResource("CodeColorizersTest/Person.java").toURI()); + + SyntaxHighlightingData syntaxHighlighting = codeColorizers.toSyntaxHighlighting(javaFile, "UTF-8", "java"); + + assertThat(syntaxHighlighting.writeString()).isEqualTo(HIGHLIGHTING_JAVA); } diff --git a/sonar-batch/src/test/resources/org/sonar/batch/source/CodeColorizersTest/Person.java b/sonar-batch/src/test/resources/org/sonar/batch/source/CodeColorizersTest/Person.java new file mode 100644 index 00000000000..c5cc9793730 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/source/CodeColorizersTest/Person.java @@ -0,0 +1,12 @@ +/** + * Doc + */ +public class Person { + + private int first; + + @Deprecated + public void foo(int first, String last, Double middle) { + this.first = first; // First + } +} -- 2.39.5