]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5819 Fix handling of empty highlighting rules
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 2 Dec 2014 11:06:39 +0000 (12:06 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 2 Dec 2014 11:06:48 +0000 (12:06 +0100)
server/sonar-server/src/main/java/org/sonar/server/source/DecorationDataHolder.java
server/sonar-server/src/test/java/org/sonar/server/source/HtmlSourceDecoratorTest.java

index 8b368e5ccf97a11f5ea28ca7408ecccab674a0ee..66d2ddbbda94b673dfc412c22d72df9b2c37c29d 100644 (file)
@@ -71,8 +71,12 @@ class DecorationDataHolder {
     String[] rules = syntaxHighlightingRules.split(ENTITY_SEPARATOR);
     for (String rule : rules) {
       String[] ruleFields = rule.split(FIELD_SEPARATOR);
-      insertAndPreserveOrder(new OpeningHtmlTag(Integer.parseInt(ruleFields[0]), ruleFields[2]), openingTagsEntries);
-      insertAndPreserveOrder(Integer.parseInt(ruleFields[1]), closingTagsOffsets);
+      int startOffset = Integer.parseInt(ruleFields[0]);
+      int endOffset = Integer.parseInt(ruleFields[1]);
+      if (startOffset < endOffset) {
+        insertAndPreserveOrder(new OpeningHtmlTag(startOffset, ruleFields[2]), openingTagsEntries);
+        insertAndPreserveOrder(endOffset, closingTagsOffsets);
+      }
     }
   }
 
index 34e101105ebd3086c8d8c15e3d136a2e3d5a9b2b..b7534b7149f5576927880e3177e2d72b2cfa1fde 100644 (file)
@@ -189,6 +189,14 @@ public class HtmlSourceDecoratorTest extends AbstractDaoTestCase {
     assertThat(sourceDecorator.getDecoratedSourceAsHtml(sourceLine, highlighting, symbols)).isEqualTo("<span class=\"k\">package</span> <span class=\"sym-42 sym\">org.polop</span>;");
   }
 
+  @Test
+  public void should_handle_highlighting_too_long() {
+    String sourceLine = "abc";
+    String highlighting = "0,5,c";
+    String symbols = "";
+    assertThat(sourceDecorator.getDecoratedSourceAsHtml(sourceLine, highlighting, symbols)).isEqualTo("<span class=\"c\">abc</span>");
+  }
+
   @Test
   public void should_ignore_missing_highlighting() {
     String sourceLine = "    if (toto < 42) {";
@@ -203,7 +211,16 @@ public class HtmlSourceDecoratorTest extends AbstractDaoTestCase {
 
   @Test
   public void should_ignore_empty_source() {
-    assertThat(sourceDecorator.getDecoratedSourceAsHtml("", (String) null, (String) null)).isEqualTo("");
+    assertThat(sourceDecorator.getDecoratedSourceAsHtml("", "0,1,cppd", "")).isEqualTo("");
   }
+
+  @Test
+  public void should_ignore_empty_rule() {
+    String sourceLine = "@Deprecated";
+    String highlighting = "0,0,a;0,11,a";
+    String symbols = "1,11,1";
+    assertThat(sourceDecorator.getDecoratedSourceAsHtml(sourceLine, highlighting, symbols)).isEqualTo("<span class=\"a\">@<span class=\"sym-1 sym\">Deprecated</span></span>");
+  }
+
 }