]> source.dussan.org Git - sonarqube.git/blob
6e06b926ddd576c3332008950c6f6fe9dab2527a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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 final 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 message_whenSettingMessage_shouldReplaceNullChar() {
113     assertThat(new DefaultIssueLocation().message("test " + '\u0000' + "123").message()).isEqualTo("test [NULL]123");
114   }
115
116   @Test
117   public void message_whenSettingMessageWithFormattings_shouldReplaceNullChar() {
118     assertThat(new DefaultIssueLocation().message("test " + '\u0000' + "123", Collections.emptyList()).message()).isEqualTo("test [NULL]123");
119   }
120
121   @Test
122   public void should_trim_on_default_message_method(){
123     assertThat(new DefaultIssueLocation().message(" message ").message()).isEqualTo("message");
124   }
125
126   @Test
127   public void should_not_trim_on_messageFormattings_message_method(){
128     assertThat(new DefaultIssueLocation().message(" message ", Collections.emptyList()).message()).isEqualTo(" message ");
129   }
130 }