]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5869 Restore built-in Java Colorizer support for test files
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 17 Dec 2014 16:13:10 +0000 (17:13 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 17 Dec 2014 16:13:10 +0000 (17:13 +0100)
sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java
sonar-batch/src/test/java/org/sonar/batch/source/CodeColorizersTest.java
sonar-batch/src/test/resources/org/sonar/batch/source/CodeColorizersTest/Person.java [new file with mode: 0644]

index e3c5e8387a88f137af0bdf706e9fcd2e1628c80d..d4df1fafd61d97bf111bacc62929df73538d086b 100644 (file)
@@ -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<Tokenizer> 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();
     }
index 3932cdbb630801f6839dc0452920762e23481e49..4fa2716b081d736e7a3513e273aacfa4bafc3fc0 100644 (file)
@@ -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.<CodeColorizerFormat>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 (file)
index 0000000..c5cc979
--- /dev/null
@@ -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
+  }
+}