diff options
author | Guillaume Jambet <guillaume.jambet@sonarsource.com> | 2018-06-06 14:43:17 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-06-08 20:20:52 +0200 |
commit | 6f6f00faf62ea79215031534671bc4f1d9a55856 (patch) | |
tree | 8b4e648d19d763be6f3e685df558fd03e586d763 /sonar-plugin-api/src/test | |
parent | 5620329e9393d7afc4d23835df8b585196df9757 (diff) | |
download | sonarqube-6f6f00faf62ea79215031534671bc4f1d9a55856.tar.gz sonarqube-6f6f00faf62ea79215031534671bc4f1d9a55856.zip |
SONAR-10460 do not allow null character in scanner for issue message
Diffstat (limited to 'sonar-plugin-api/src/test')
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java index fc032947f92..190c22f6c9e 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java @@ -20,6 +20,9 @@ package org.sonar.api.batch.sensor.issue.internal; import org.apache.commons.lang.StringUtils; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -27,17 +30,27 @@ import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.rules.ExpectedException.none; public class DefaultIssueLocationTest { @Rule - public ExpectedException thrown = ExpectedException.none(); + public ExpectedException thrown = none(); private InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php") .initMetadata("Foo\nBar\n") .build(); @Test + public void should_build() { + assertThat(new DefaultIssueLocation() + .on(inputFile) + .message("pipo bimbo") + .message() + ).isEqualTo("pipo bimbo"); + } + + @Test public void not_allowed_to_call_on_twice() { thrown.expect(IllegalStateException.class); thrown.expectMessage("on() already called"); @@ -58,4 +71,37 @@ public class DefaultIssueLocationTest { .message(StringUtils.repeat("a", 4001)).message()).hasSize(4000); } + @Test + public void prevent_null_character_in_message_text() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Character \\u0000 is not supported in issue message"); + + new DefaultIssueLocation() + .message("pipo " + '\u0000' + " bimbo"); + } + + @Test + public void prevent_null_character_in_message_text_when_builder_has_been_initialized() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage(customMatcher("Character \\u0000 is not supported in issue message", ", on component: src/Foo.php")); + + new DefaultIssueLocation() + .on(inputFile) + .message("pipo " + '\u0000' + " bimbo"); + } + + private Matcher<String> customMatcher(String startWith, String endWith) { + return new TypeSafeMatcher<String>() { + @Override + public void describeTo(Description description) { + description.appendText("Invalid message"); + } + + @Override + protected boolean matchesSafely(final String item) { + return item.startsWith(startWith) && item.endsWith(endWith); + } + }; + } + } |