]> source.dussan.org Git - sonarqube.git/blob
a8e44a5bb4a0c92afb908dac37c67c00e2531a16
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.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;
30
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;
34
35 public class DefaultIssueLocationTest {
36
37   private InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
38     .initMetadata("Foo\nBar\n")
39     .build();
40
41   @Test
42   public void should_build() {
43     assertThat(new DefaultIssueLocation()
44       .on(inputFile)
45       .message("pipo bimbo")
46       .message()
47     ).isEqualTo("pipo bimbo");
48   }
49
50   @Test
51   public void not_allowed_to_call_on_twice() {
52     assertThatThrownBy(() -> new DefaultIssueLocation()
53       .on(inputFile)
54       .on(inputFile)
55       .message("Wrong way!"))
56       .isInstanceOf(IllegalStateException.class)
57       .hasMessage("on() already called");
58   }
59
60   @Test
61   public void prevent_too_long_messages() {
62     assertThat(new DefaultIssueLocation()
63       .on(inputFile)
64       .message(StringUtils.repeat("a", 1333)).message()).hasSize(1333);
65
66     assertThat(new DefaultIssueLocation()
67       .on(inputFile)
68       .message(StringUtils.repeat("a", 1334)).message()).hasSize(1333);
69   }
70
71   @Test
72   public void should_ignore_messageFormatting_if_message_is_trimmed() {
73     DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting()
74       .start(1500)
75       .end(1501)
76       .type(MessageFormatting.Type.CODE);
77
78     DefaultIssueLocation location = new DefaultIssueLocation()
79       .message(StringUtils.repeat("a", 2000), List.of(messageFormatting));
80
81     assertThat(location.messageFormattings()).isEmpty();
82   }
83
84   @Test
85   public void should_truncate_messageFormatting_if_necessary() {
86     DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting()
87       .start(1300)
88       .end(1501)
89       .type(MessageFormatting.Type.CODE);
90
91     DefaultIssueLocation location = new DefaultIssueLocation()
92       .message(StringUtils.repeat("a", 2000), List.of(messageFormatting));
93
94     assertThat(location.messageFormattings())
95       .extracting(MessageFormatting::start, MessageFormatting::end)
96       .containsOnly(tuple(1300, 1333));
97   }
98
99   @Test
100   public void should_validate_message_formatting() {
101     List<NewMessageFormatting> messageFormattings = List.of(new DefaultMessageFormatting()
102       .start(1)
103       .end(3)
104       .type(MessageFormatting.Type.CODE));
105     DefaultIssueLocation location = new DefaultIssueLocation();
106
107     assertThatThrownBy(() -> location.message("aa", messageFormattings))
108       .isInstanceOf(IllegalArgumentException.class);
109   }
110
111   @Test
112   public void prevent_null_character_in_message_text() {
113     assertThatThrownBy(() -> new DefaultIssueLocation()
114       .message("pipo " + '\u0000' + " bimbo"))
115       .isInstanceOf(IllegalArgumentException.class)
116       .hasMessageContaining("Character \\u0000 is not supported in issue message");
117   }
118
119   @Test
120   public void prevent_null_character_in_message_text_when_builder_has_been_initialized() {
121     assertThatThrownBy(() -> new DefaultIssueLocation()
122       .on(inputFile)
123       .message("pipo " + '\u0000' + " bimbo"))
124       .isInstanceOf(IllegalArgumentException.class)
125       .hasMessageStartingWith("Character \\u0000 is not supported in issue message")
126       .hasMessageEndingWith(", on component: src/Foo.php");
127   }
128
129   @Test
130   public void should_trim_on_default_message_method(){
131     assertThat(new DefaultIssueLocation().message(" message ").message()).isEqualTo("message");
132   }
133
134   @Test
135   public void should_not_trim_on_messageFormattings_message_method(){
136     assertThat(new DefaultIssueLocation().message(" message ", Collections.emptyList()).message()).isEqualTo(" message ");
137   }
138 }