]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17745 Message of issue location should be trimmed
authorLéo Geoffroy <99647462+leo-geoffroy-sonarsource@users.noreply.github.com>
Wed, 7 Dec 2022 17:54:00 +0000 (18:54 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 7 Dec 2022 20:02:57 +0000 (20:02 +0000)
sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocation.java
sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java

index 2dc921874667fc01ab7c0c4e4d11626dd5838af8..c9d123f12dc9c81136af5f6b87f71dbe79d5e2bf 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.api.issue.Issue;
 
 import static java.util.Objects.requireNonNull;
 import static org.apache.commons.lang.StringUtils.abbreviate;
+import static org.apache.commons.lang.StringUtils.trim;
 import static org.sonar.api.utils.Preconditions.checkArgument;
 import static org.sonar.api.utils.Preconditions.checkState;
 
@@ -64,7 +65,7 @@ public class DefaultIssueLocation implements NewIssueLocation, IssueLocation {
   @Override
   public DefaultIssueLocation message(String message) {
     validateMessage(message);
-    this.message = abbreviate(message, Issue.MESSAGE_MAX_SIZE);
+    this.message = abbreviate(trim(message), Issue.MESSAGE_MAX_SIZE);
     return this;
   }
 
index 1c26aaa44008f6be9cb1bfe05949dafb9845f0bf..4f7b5e552fc13c7b8f1e3952acfdc3634fb11a72 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.api.batch.sensor.issue.internal;
 
+import java.util.Collections;
 import java.util.List;
 import org.apache.commons.lang.StringUtils;
 import org.junit.Test;
@@ -124,4 +125,14 @@ public class DefaultIssueLocationTest {
       .hasMessageStartingWith("Character \\u0000 is not supported in issue message")
       .hasMessageEndingWith(", on component: src/Foo.php");
   }
+
+  @Test
+  public void should_trim_on_default_message_method(){
+    assertThat(new DefaultIssueLocation().message(" message ").message()).isEqualTo("message");
+  }
+
+  @Test
+  public void should_not_trim_on_messageFormattings_message_method(){
+    assertThat(new DefaultIssueLocation().message(" message ", Collections.emptyList()).message()).isEqualTo(" message ");
+  }
 }