3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.api.batch.sensor.issue.internal;
22 import java.util.List;
23 import org.apache.commons.lang.StringUtils;
24 import org.junit.Test;
25 import org.sonar.api.batch.fs.InputFile;
26 import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
27 import org.sonar.api.batch.sensor.issue.MessageFormatting;
28 import org.sonar.api.batch.sensor.issue.NewMessageFormatting;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.core.api.Assertions.assertThatThrownBy;
32 import static org.assertj.core.api.AssertionsForClassTypes.tuple;
34 public class DefaultIssueLocationTest {
36 private InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
37 .initMetadata("Foo\nBar\n")
41 public void should_build() {
42 assertThat(new DefaultIssueLocation()
44 .message("pipo bimbo")
46 ).isEqualTo("pipo bimbo");
50 public void not_allowed_to_call_on_twice() {
51 assertThatThrownBy(() -> new DefaultIssueLocation()
54 .message("Wrong way!"))
55 .isInstanceOf(IllegalStateException.class)
56 .hasMessage("on() already called");
60 public void prevent_too_long_messages() {
61 assertThat(new DefaultIssueLocation()
63 .message(StringUtils.repeat("a", 1333)).message()).hasSize(1333);
65 assertThat(new DefaultIssueLocation()
67 .message(StringUtils.repeat("a", 1334)).message()).hasSize(1333);
71 public void should_ignore_messageFormatting_if_message_is_trimmed() {
72 DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting()
75 .type(MessageFormatting.Type.CODE);
77 DefaultIssueLocation location = new DefaultIssueLocation()
78 .message(StringUtils.repeat("a", 2000), List.of(messageFormatting));
80 assertThat(location.messageFormattings()).isEmpty();
84 public void should_truncate_messageFormatting_if_necessary() {
85 DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting()
88 .type(MessageFormatting.Type.CODE);
90 DefaultIssueLocation location = new DefaultIssueLocation()
91 .message(StringUtils.repeat("a", 2000), List.of(messageFormatting));
93 assertThat(location.messageFormattings())
94 .extracting(MessageFormatting::start, MessageFormatting::end)
95 .containsOnly(tuple(1300, 1333));
99 public void should_validate_message_formatting() {
100 List<NewMessageFormatting> messageFormattings = List.of(new DefaultMessageFormatting()
103 .type(MessageFormatting.Type.CODE));
104 DefaultIssueLocation location = new DefaultIssueLocation();
106 assertThatThrownBy(() -> location.message("aa", messageFormattings))
107 .isInstanceOf(IllegalArgumentException.class);
111 public void prevent_null_character_in_message_text() {
112 assertThatThrownBy(() -> new DefaultIssueLocation()
113 .message("pipo " + '\u0000' + " bimbo"))
114 .isInstanceOf(IllegalArgumentException.class)
115 .hasMessageContaining("Character \\u0000 is not supported in issue message");
119 public void prevent_null_character_in_message_text_when_builder_has_been_initialized() {
120 assertThatThrownBy(() -> new DefaultIssueLocation()
122 .message("pipo " + '\u0000' + " bimbo"))
123 .isInstanceOf(IllegalArgumentException.class)
124 .hasMessageStartingWith("Character \\u0000 is not supported in issue message")
125 .hasMessageEndingWith(", on component: src/Foo.php");