3 * Copyright (C) 2009-2024 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.Collections;
23 import java.util.List;
24 import org.apache.commons.lang.StringUtils;
25 import org.junit.Test;
26 import org.sonar.api.batch.fs.InputFile;
27 import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
28 import org.sonar.api.batch.sensor.issue.MessageFormatting;
29 import org.sonar.api.batch.sensor.issue.NewMessageFormatting;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.assertThatThrownBy;
33 import static org.assertj.core.api.AssertionsForClassTypes.tuple;
35 public class DefaultIssueLocationTest {
37 private final InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
38 .initMetadata("Foo\nBar\n")
42 public void should_build() {
43 assertThat(new DefaultIssueLocation()
45 .message("pipo bimbo")
47 ).isEqualTo("pipo bimbo");
51 public void not_allowed_to_call_on_twice() {
52 assertThatThrownBy(() -> new DefaultIssueLocation()
55 .message("Wrong way!"))
56 .isInstanceOf(IllegalStateException.class)
57 .hasMessage("on() already called");
61 public void prevent_too_long_messages() {
62 assertThat(new DefaultIssueLocation()
64 .message(StringUtils.repeat("a", 1333)).message()).hasSize(1333);
66 assertThat(new DefaultIssueLocation()
68 .message(StringUtils.repeat("a", 1334)).message()).hasSize(1333);
72 public void should_ignore_messageFormatting_if_message_is_trimmed() {
73 DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting()
76 .type(MessageFormatting.Type.CODE);
78 DefaultIssueLocation location = new DefaultIssueLocation()
79 .message(StringUtils.repeat("a", 2000), List.of(messageFormatting));
81 assertThat(location.messageFormattings()).isEmpty();
85 public void should_truncate_messageFormatting_if_necessary() {
86 DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting()
89 .type(MessageFormatting.Type.CODE);
91 DefaultIssueLocation location = new DefaultIssueLocation()
92 .message(StringUtils.repeat("a", 2000), List.of(messageFormatting));
94 assertThat(location.messageFormattings())
95 .extracting(MessageFormatting::start, MessageFormatting::end)
96 .containsOnly(tuple(1300, 1333));
100 public void should_validate_message_formatting() {
101 List<NewMessageFormatting> messageFormattings = List.of(new DefaultMessageFormatting()
104 .type(MessageFormatting.Type.CODE));
105 DefaultIssueLocation location = new DefaultIssueLocation();
107 assertThatThrownBy(() -> location.message("aa", messageFormattings))
108 .isInstanceOf(IllegalArgumentException.class);
112 public void message_whenSettingMessage_shouldReplaceNullChar() {
113 assertThat(new DefaultIssueLocation().message("test " + '\u0000' + "123").message()).isEqualTo("test [NULL]123");
117 public void message_whenSettingMessageWithFormattings_shouldReplaceNullChar() {
118 assertThat(new DefaultIssueLocation().message("test " + '\u0000' + "123", Collections.emptyList()).message()).isEqualTo("test [NULL]123");
122 public void should_trim_on_default_message_method(){
123 assertThat(new DefaultIssueLocation().message(" message ").message()).isEqualTo("message");
127 public void should_not_trim_on_messageFormattings_message_method(){
128 assertThat(new DefaultIssueLocation().message(" message ", Collections.emptyList()).message()).isEqualTo(" message ");