From 6d5a9ed67e32c23fb4edf87ab00559f5a3054dee Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Tue, 2 Dec 2014 12:06:39 +0100 Subject: [PATCH] SONAR-5819 Fix handling of empty highlighting rules --- .../server/source/DecorationDataHolder.java | 8 ++++++-- .../source/HtmlSourceDecoratorTest.java | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/DecorationDataHolder.java b/server/sonar-server/src/main/java/org/sonar/server/source/DecorationDataHolder.java index 8b368e5ccf9..66d2ddbbda9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/DecorationDataHolder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/DecorationDataHolder.java @@ -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); + } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/HtmlSourceDecoratorTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/HtmlSourceDecoratorTest.java index 34e101105eb..b7534b7149f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/HtmlSourceDecoratorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/HtmlSourceDecoratorTest.java @@ -189,6 +189,14 @@ public class HtmlSourceDecoratorTest extends AbstractDaoTestCase { assertThat(sourceDecorator.getDecoratedSourceAsHtml(sourceLine, highlighting, symbols)).isEqualTo("package org.polop;"); } + @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("abc"); + } + @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("@Deprecated"); + } + } -- 2.39.5