1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.scan;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.MessageException;
import org.sonar.core.config.ScannerProperties;
import org.sonar.core.documentation.DefaultDocumentationLinkGenerator;
import org.sonar.scanner.ProjectInfo;
import org.sonar.scanner.bootstrap.GlobalConfiguration;
import static java.lang.String.format;
import static org.apache.commons.lang3.RandomStringUtils.secure;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.core.config.ScannerProperties.BRANCHES_DOC_LINK_SUFFIX;
class ProjectReactorValidatorTest {
private final GlobalConfiguration settings = mock(GlobalConfiguration.class);
private final ProjectInfo projectInfo = mock(ProjectInfo.class);
private final DefaultDocumentationLinkGenerator defaultDocumentationLinkGenerator = mock(DefaultDocumentationLinkGenerator.class);
private final ProjectReactorValidator underTest = new ProjectReactorValidator(settings, defaultDocumentationLinkGenerator);
private static final String LINK_TO_DOC = "link_to_documentation";
@BeforeEach
void prepare() {
when(settings.get(anyString())).thenReturn(Optional.empty());
when(defaultDocumentationLinkGenerator.getDocumentationLink(BRANCHES_DOC_LINK_SUFFIX)).thenReturn(LINK_TO_DOC);
}
@ParameterizedTest
@MethodSource("validKeys")
void not_fail_with_valid_key(String validKey) {
ProjectReactor projectReactor = createProjectReactor(validKey);
underTest.validate(projectReactor);
}
private static Stream<String> validKeys() {
return Stream.of(
"foo",
"123foo",
"foo123",
"1Z3",
"a123",
"123a",
"1:2",
"3-3",
"-:",
"Foobar2",
"foo.bar",
"foo-bar",
"foo:bar",
"foo_bar"
);
}
@Test
void fail_when_invalid_key() {
ProjectReactor reactor = createProjectReactor("foo$bar");
assertThatThrownBy(() -> underTest.validate(reactor))
.isInstanceOf(MessageException.class)
.hasMessageContaining("\"foo$bar\" is not a valid project key. Allowed characters are alphanumeric,"
+ " '-', '_', '.' and ':', with at least one non-digit.");
}
@Test
void fail_when_only_digits() {
ProjectReactor reactor = createProjectReactor("12345");
assertThatThrownBy(() -> underTest.validate(reactor))
.isInstanceOf(MessageException.class)
.hasMessageContaining("\"12345\" is not a valid project key. Allowed characters are alphanumeric, "
+ "'-', '_', '.' and ':', with at least one non-digit.");
}
@Test
void fail_when_backslash_in_key() {
ProjectReactor reactor = createProjectReactor("foo\\bar");
assertThatThrownBy(() -> underTest.validate(reactor))
.isInstanceOf(MessageException.class)
.hasMessageContaining("\"foo\\bar\" is not a valid project key. Allowed characters are alphanumeric, "
+ "'-', '_', '.' and ':', with at least one non-digit.");
}
@Test
void fail_when_branch_name_is_specified_but_branch_plugin_not_present() {
ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
ProjectReactor reactor = new ProjectReactor(def);
when(settings.get(ScannerProperties.BRANCH_NAME)).thenReturn(Optional.of("feature1"));
assertThatThrownBy(() -> underTest.validate(reactor))
.isInstanceOf(MessageException.class)
.hasMessageContaining(format("To use the property \"sonar.branch.name\" and analyze branches, Developer Edition or above is required. See %s for more information.",
LINK_TO_DOC));
}
@Test
void fail_when_pull_request_id_specified_but_branch_plugin_not_present() {
ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
ProjectReactor reactor = new ProjectReactor(def);
when(settings.get(ScannerProperties.PULL_REQUEST_KEY)).thenReturn(Optional.of("#1984"));
assertThatThrownBy(() -> underTest.validate(reactor))
.isInstanceOf(MessageException.class)
.hasMessageContaining(format("To use the property \"sonar.pullrequest.key\" and analyze pull requests, Developer Edition or above is required. See %s for more information.",
LINK_TO_DOC));
}
@Test
void fail_when_pull_request_branch_is_specified_but_branch_plugin_not_present() {
ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
ProjectReactor reactor = new ProjectReactor(def);
when(settings.get(ScannerProperties.PULL_REQUEST_BRANCH)).thenReturn(Optional.of("feature1"));
assertThatThrownBy(() -> underTest.validate(reactor))
.isInstanceOf(MessageException.class)
.hasMessageContaining(format("To use the property \"sonar.pullrequest.branch\" and analyze pull requests, Developer Edition or above is required. See %s for more information.",
LINK_TO_DOC));
}
@Test
void fail_when_pull_request_base_specified_but_branch_plugin_not_present() {
ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
ProjectReactor reactor = new ProjectReactor(def);
when(settings.get(ScannerProperties.PULL_REQUEST_BASE)).thenReturn(Optional.of("feature1"));
assertThatThrownBy(() -> underTest.validate(reactor))
.isInstanceOf(MessageException.class)
.hasMessageContaining(format("To use the property \"sonar.pullrequest.base\" and analyze pull requests, Developer Edition or above is required. See %s for more information.",
LINK_TO_DOC));
}
@ParameterizedTest
@MethodSource("validVersions")
void not_fail_with_valid_version(@Nullable String validVersion) {
when(projectInfo.getProjectVersion()).thenReturn(Optional.ofNullable(validVersion));
ProjectReactor projectReactor = createProjectReactor("foo");
underTest.validate(projectReactor);
}
private static Stream<String> validVersions() {
return Stream.of(
null,
"1.0",
"2017-10-16",
secure().nextAscii(100)
);
}
@ParameterizedTest
@MethodSource("validBuildStrings")
void not_fail_with_valid_buildString(@Nullable String validBuildString) {
when(projectInfo.getBuildString()).thenReturn(Optional.ofNullable(validBuildString));
ProjectReactor projectReactor = createProjectReactor("foo");
underTest.validate(projectReactor);
}
private static Stream<String> validBuildStrings() {
return Stream.of(
null,
"1.0",
"2017-10-16",
secure().nextAscii(100)
);
}
private ProjectReactor createProjectReactor(String projectKey, Consumer<ProjectDefinition>... consumers) {
ProjectDefinition def = ProjectDefinition.create()
.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, projectKey);
Arrays.stream(consumers).forEach(c -> c.accept(def));
return new ProjectReactor(def);
}
}
|