]> source.dussan.org Git - sonarqube.git/blob
1c26aaa44008f6be9cb1bfe05949dafb9845f0bf
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.api.batch.sensor.issue.internal;
21
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;
29
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;
33
34 public class DefaultIssueLocationTest {
35
36   private InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
37     .initMetadata("Foo\nBar\n")
38     .build();
39
40   @Test
41   public void should_build() {
42     assertThat(new DefaultIssueLocation()
43       .on(inputFile)
44       .message("pipo bimbo")
45       .message()
46     ).isEqualTo("pipo bimbo");
47   }
48
49   @Test
50   public void not_allowed_to_call_on_twice() {
51     assertThatThrownBy(() -> new DefaultIssueLocation()
52       .on(inputFile)
53       .on(inputFile)
54       .message("Wrong way!"))
55       .isInstanceOf(IllegalStateException.class)
56       .hasMessage("on() already called");
57   }
58
59   @Test
60   public void prevent_too_long_messages() {
61     assertThat(new DefaultIssueLocation()
62       .on(inputFile)
63       .message(StringUtils.repeat("a", 1333)).message()).hasSize(1333);
64
65     assertThat(new DefaultIssueLocation()
66       .on(inputFile)
67       .message(StringUtils.repeat("a", 1334)).message()).hasSize(1333);
68   }
69
70   @Test
71   public void should_ignore_messageFormatting_if_message_is_trimmed() {
72     DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting()
73       .start(1500)
74       .end(1501)
75       .type(MessageFormatting.Type.CODE);
76
77     DefaultIssueLocation location = new DefaultIssueLocation()
78       .message(StringUtils.repeat("a", 2000), List.of(messageFormatting));
79
80     assertThat(location.messageFormattings()).isEmpty();
81   }
82
83   @Test
84   public void should_truncate_messageFormatting_if_necessary() {
85     DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting()
86       .start(1300)
87       .end(1501)
88       .type(MessageFormatting.Type.CODE);
89
90     DefaultIssueLocation location = new DefaultIssueLocation()
91       .message(StringUtils.repeat("a", 2000), List.of(messageFormatting));
92
93     assertThat(location.messageFormattings())
94       .extracting(MessageFormatting::start, MessageFormatting::end)
95       .containsOnly(tuple(1300, 1333));
96   }
97
98   @Test
99   public void should_validate_message_formatting() {
100     List<NewMessageFormatting> messageFormattings = List.of(new DefaultMessageFormatting()
101       .start(1)
102       .end(3)
103       .type(MessageFormatting.Type.CODE));
104     DefaultIssueLocation location = new DefaultIssueLocation();
105
106     assertThatThrownBy(() -> location.message("aa", messageFormattings))
107       .isInstanceOf(IllegalArgumentException.class);
108   }
109
110   @Test
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");
116   }
117
118   @Test
119   public void prevent_null_character_in_message_text_when_builder_has_been_initialized() {
120     assertThatThrownBy(() -> new DefaultIssueLocation()
121       .on(inputFile)
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");
126   }
127 }