diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-11-29 03:49:09 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-12-01 20:03:12 +0000 |
commit | 1f5842ee24c4b544acb2c8dea81754809e70dab9 (patch) | |
tree | e6cbcac862d0b3de1273518d0754c789183e9306 /sonar-plugin-api-impl | |
parent | 7f4d7aabc87647aa40386ab2708544e023ca0896 (diff) | |
download | sonarqube-1f5842ee24c4b544acb2c8dea81754809e70dab9.tar.gz sonarqube-1f5842ee24c4b544acb2c8dea81754809e70dab9.zip |
SONAR-17592 Manage formattings for truncation of messages
Diffstat (limited to 'sonar-plugin-api-impl')
2 files changed, 61 insertions, 7 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocation.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocation.java index 89f1c9550f3..2dc92187466 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocation.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocation.java @@ -29,10 +29,10 @@ import org.sonar.api.batch.sensor.issue.IssueLocation; import org.sonar.api.batch.sensor.issue.MessageFormatting; import org.sonar.api.batch.sensor.issue.NewIssueLocation; import org.sonar.api.batch.sensor.issue.NewMessageFormatting; +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,18 +64,28 @@ public class DefaultIssueLocation implements NewIssueLocation, IssueLocation { @Override public DefaultIssueLocation message(String message) { validateMessage(message); - this.message = abbreviate(trim(message), MESSAGE_MAX_SIZE); + this.message = abbreviate(message, Issue.MESSAGE_MAX_SIZE); return this; } @Override - public NewIssueLocation message(String message, List<NewMessageFormatting> newMessageFormattings) { + public DefaultIssueLocation message(String message, List<NewMessageFormatting> newMessageFormattings) { validateMessage(message); validateFormattings(newMessageFormattings, message); - this.message = abbreviate(trim(message), MESSAGE_MAX_SIZE); + this.message = abbreviate(message, Issue.MESSAGE_MAX_SIZE); for (NewMessageFormatting newMessageFormatting : newMessageFormattings) { - messageFormattings.add((MessageFormatting) newMessageFormatting); + DefaultMessageFormatting messageFormatting = (DefaultMessageFormatting) newMessageFormatting; + if (messageFormatting.start() > Issue.MESSAGE_MAX_SIZE) { + continue; + } + if (messageFormatting.end() > Issue.MESSAGE_MAX_SIZE) { + messageFormatting = new DefaultMessageFormatting() + .start(messageFormatting.start()) + .end( Issue.MESSAGE_MAX_SIZE) + .type(messageFormatting.type()); + } + messageFormattings.add(messageFormatting); } return this; } diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java index 128ed060392..1c26aaa4400 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java @@ -19,13 +19,17 @@ */ package org.sonar.api.batch.sensor.issue.internal; +import java.util.List; import org.apache.commons.lang.StringUtils; import org.junit.Test; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; +import org.sonar.api.batch.sensor.issue.MessageFormatting; +import org.sonar.api.batch.sensor.issue.NewMessageFormatting; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.tuple; public class DefaultIssueLocationTest { @@ -56,11 +60,51 @@ public class DefaultIssueLocationTest { public void prevent_too_long_messages() { assertThat(new DefaultIssueLocation() .on(inputFile) - .message(StringUtils.repeat("a", 4000)).message()).hasSize(4000); + .message(StringUtils.repeat("a", 1333)).message()).hasSize(1333); assertThat(new DefaultIssueLocation() .on(inputFile) - .message(StringUtils.repeat("a", 4001)).message()).hasSize(4000); + .message(StringUtils.repeat("a", 1334)).message()).hasSize(1333); + } + + @Test + public void should_ignore_messageFormatting_if_message_is_trimmed() { + DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting() + .start(1500) + .end(1501) + .type(MessageFormatting.Type.CODE); + + DefaultIssueLocation location = new DefaultIssueLocation() + .message(StringUtils.repeat("a", 2000), List.of(messageFormatting)); + + assertThat(location.messageFormattings()).isEmpty(); + } + + @Test + public void should_truncate_messageFormatting_if_necessary() { + DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting() + .start(1300) + .end(1501) + .type(MessageFormatting.Type.CODE); + + DefaultIssueLocation location = new DefaultIssueLocation() + .message(StringUtils.repeat("a", 2000), List.of(messageFormatting)); + + assertThat(location.messageFormattings()) + .extracting(MessageFormatting::start, MessageFormatting::end) + .containsOnly(tuple(1300, 1333)); + } + + @Test + public void should_validate_message_formatting() { + List<NewMessageFormatting> messageFormattings = List.of(new DefaultMessageFormatting() + .start(1) + .end(3) + .type(MessageFormatting.Type.CODE)); + DefaultIssueLocation location = new DefaultIssueLocation(); + + assertThatThrownBy(() -> location.message("aa", messageFormattings)) + .isInstanceOf(IllegalArgumentException.class); } @Test |