/
RulesDefinitionXmlLoaderTest.java
319 lines (288 loc) · 15.4 KB
/
RulesDefinitionXmlLoaderTest.java
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* C++ Community Plugin (cxx plugin)
* Copyright (C) 2010-2023 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* 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.
*/
// copy of https://github.com/SonarSource/sonar-plugin-api/blob/master/plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java
// Don't remove copyright below!
/*
* Sonar Plugin API
* Copyright (C) 2009-2022 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.cxx.sensors.utils;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.sonar.api.impl.server.RulesDefinitionContext;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.rule.RulesDefinition;
public class RulesDefinitionXmlLoaderTest {
RulesDefinitionXmlLoader underTest = new RulesDefinitionXmlLoader();
@Test
public void parse_xml() {
InputStream input = getClass()
.getResourceAsStream("/org/sonar/cxx/sensors/utils/RulesDefinitionXmlLoader/rules.xml");
RulesDefinition.Repository repository = load(input, StandardCharsets.UTF_8.name());
assertThat(repository.rules()).hasSize(2);
RulesDefinition.Rule rule = repository.rule("complete");
assertThat(rule.key()).isEqualTo("complete");
assertThat(rule.name()).isEqualTo("Complete");
assertThat(rule.htmlDescription()).isEqualTo("Description of Complete");
assertThat(rule.severity()).isEqualTo(Severity.BLOCKER);
assertThat(rule.template()).isTrue();
assertThat(rule.status()).isEqualTo(RuleStatus.BETA);
assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/LocalVariableName");
assertThat(rule.type()).isEqualTo(RuleType.BUG);
assertThat(rule.tags()).containsOnly("misra", "spring");
assertThat(rule.params()).hasSize(2);
RulesDefinition.Param ignore = rule.param("ignore");
assertThat(ignore.key()).isEqualTo("ignore");
assertThat(ignore.description()).isEqualTo("Ignore ?");
assertThat(ignore.defaultValue()).isEqualTo("false");
rule = repository.rule("minimal");
assertThat(rule.key()).isEqualTo("minimal");
assertThat(rule.name()).isEqualTo("Minimal");
assertThat(rule.htmlDescription()).isEqualTo("Description of Minimal");
assertThat(rule.params()).isEmpty();
assertThat(rule.status()).isEqualTo(RuleStatus.READY);
assertThat(rule.severity()).isEqualTo(Severity.MAJOR);
assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL);
}
@Test
public void fail_if_missing_rule_key() {
assertThatThrownBy(() -> load(IOUtils.toInputStream("<rules><rule><name>Foo</name></rule></rules>"),
StandardCharsets.UTF_8.name()))
.isInstanceOf(IllegalStateException.class);
}
@Test
public void fail_if_missing_property_key() {
assertThatThrownBy(() -> load(IOUtils.toInputStream(
"<rules><rule><key>foo</key><name>Foo</name><param></param></rule></rules>"),
StandardCharsets.UTF_8.name()))
.isInstanceOf(IllegalStateException.class);
}
@Test
public void fail_on_invalid_rule_parameter_type() {
assertThatThrownBy(() -> load(IOUtils.toInputStream(
"<rules><rule><key>foo</key><name>Foo</name><param><key>key</key><type>INVALID</type></param></rule></rules>"),
StandardCharsets.UTF_8.name()))
.isInstanceOf(IllegalStateException.class);
}
@Test
public void fail_if_invalid_xml() {
InputStream input = getClass().getResourceAsStream(
"/org/sonar/cxx/sensors/utils/RulesDefinitionXmlLoader/invalid.xml");
assertThatThrownBy(() -> load(input, StandardCharsets.UTF_8.name()))
.isInstanceOf(IllegalStateException.class)
.hasMessage("XML is not valid");
}
@Test
public void test_utf8_encoding() {
InputStream input = getClass().getResourceAsStream("/org/sonar/cxx/sensors/utils/RulesDefinitionXmlLoader/utf8.xml");
RulesDefinition.Repository repository = load(input, StandardCharsets.UTF_8.name());
assertThat(repository.rules()).hasSize(1);
RulesDefinition.Rule rule = repository.rules().get(0);
assertThat(rule.key()).isEqualTo("com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck");
assertThat(rule.name()).isEqualTo("M & M");
assertThat(rule.htmlDescription().charAt(0)).isEqualTo('\u00E9');
assertThat(rule.htmlDescription().charAt(1)).isEqualTo('\u00E0');
assertThat(rule.htmlDescription().charAt(2)).isEqualTo('\u0026');
}
@Test
public void test_utf8_encoding_with_bom() {
InputStream input = getClass().getResourceAsStream(
"/org/sonar/cxx/sensors/utils/RulesDefinitionXmlLoader/utf8-with-bom.xml");
RulesDefinition.Repository repository = load(input, StandardCharsets.UTF_8.name());
assertThat(repository.rules()).hasSize(1);
RulesDefinition.Rule rule = repository.rules().get(0);
assertThat(rule.key()).isEqualTo("com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck");
assertThat(rule.name()).isEqualTo("M & M");
assertThat(rule.htmlDescription().charAt(0)).isEqualTo('\u00E9');
assertThat(rule.htmlDescription().charAt(1)).isEqualTo('\u00E0');
assertThat(rule.htmlDescription().charAt(2)).isEqualTo('\u0026');
}
@Test
public void support_deprecated_format() {
// the deprecated format uses some attributes instead of nodes
InputStream input = getClass().getResourceAsStream(
"/org/sonar/cxx/sensors/utils/RulesDefinitionXmlLoader/deprecated.xml");
RulesDefinition.Repository repository = load(input, StandardCharsets.UTF_8.name());
assertThat(repository.rules()).hasSize(1);
RulesDefinition.Rule rule = repository.rules().get(0);
assertThat(rule.key()).isEqualTo("org.sonar.it.checkstyle.MethodsCountCheck");
assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/org.sonar.it.checkstyle.MethodsCountCheck");
assertThat(rule.severity()).isEqualTo(Severity.CRITICAL);
assertThat(rule.htmlDescription()).isEqualTo("Count methods");
assertThat(rule.param("minMethodsCount")).isNotNull();
}
@Test
public void test_default_values() {
String xml = "" + "<rules>" + " <rule>"
+ " <key>1</key>" + " <name>One</name>" + " <description>Desc</description>"
+ " </rule>" + "</rules>";
RulesDefinition.Rule rule = load(xml).rule("1");
assertThat(rule.severity()).isEqualTo("MAJOR");
assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL);
DebtRemediationFunction function = rule.debtRemediationFunction();
assertThat(function).isNotNull();
assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
assertThat(function.gapMultiplier()).isNull();
assertThat(function.baseEffort()).isEqualTo("5min");
}
@Test
public void test_default_info_values() {
String xml = "" + "<rules>" + " <rule>"
+ " <key>1</key>" + " <name>One</name>" + " <description>Desc</description>"
+ " <severity>INFO</severity>"
+ " </rule>" + "</rules>";
RulesDefinition.Rule rule = load(xml).rule("1");
assertThat(rule.severity()).isEqualTo("INFO");
assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL);
DebtRemediationFunction function = rule.debtRemediationFunction();
assertThat(function).isNull();
}
@Test
public void test_linear_remediation_function() {
String xml = "" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>"
+ " <description>Desc</description>" + " <gapDescription>lines</gapDescription>"
+ " <remediationFunction>LINEAR</remediationFunction>"
+ " <remediationFunctionGapMultiplier>2d 3h</remediationFunctionGapMultiplier>" + " </rule>"
+ "</rules>";
RulesDefinition.Rule rule = load(xml).rule("1");
assertThat(rule.gapDescription()).isEqualTo("lines");
DebtRemediationFunction function = rule.debtRemediationFunction();
assertThat(function).isNotNull();
assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
assertThat(function.gapMultiplier()).isEqualTo("2d3h");
assertThat(function.baseEffort()).isNull();
}
@Test
public void test_linear_with_offset_remediation_function() {
String xml = "" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>"
+ " <description>Desc</description>"
+ " <effortToFixDescription>lines</effortToFixDescription>"
+ " <remediationFunction>LINEAR_OFFSET</remediationFunction>"
+ " <remediationFunctionGapMultiplier>2d 3h</remediationFunctionGapMultiplier>"
+ " <remediationFunctionBaseEffort>5min</remediationFunctionBaseEffort>" + " </rule>"
+ "</rules>";
RulesDefinition.Rule rule = load(xml).rule("1");
assertThat(rule.gapDescription()).isEqualTo("lines");
DebtRemediationFunction function = rule.debtRemediationFunction();
assertThat(function).isNotNull();
assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
assertThat(function.gapMultiplier()).isEqualTo("2d3h");
assertThat(function.baseEffort()).isEqualTo("5min");
}
@Test
public void test_constant_remediation_function() {
String xml = "" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>"
+ " <description>Desc</description>"
+ " <remediationFunction>CONSTANT_ISSUE</remediationFunction>"
+ " <remediationFunctionBaseEffort>5min</remediationFunctionBaseEffort>" + " </rule>"
+ "</rules>";
RulesDefinition.Rule rule = load(xml).rule("1");
DebtRemediationFunction function = rule.debtRemediationFunction();
assertThat(function).isNotNull();
assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
assertThat(function.gapMultiplier()).isNull();
assertThat(function.baseEffort()).isEqualTo("5min");
}
@Test
public void fail_if_invalid_remediation_function() {
assertThatThrownBy(() -> load("" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>"
+ " <description>Desc</description>"
+ " <remediationFunction>UNKNOWN</remediationFunction>" + " </rule>"
+ "</rules>"))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Fail to load the rule with key [squid:1]")
.hasCauseInstanceOf(IllegalArgumentException.class)
.hasRootCauseMessage("No enum constant org.sonar.api.server.debt.DebtRemediationFunction.Type.UNKNOWN");
}
@Test
public void markdown_description() {
String xml = "" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>"
+ " <description>Desc</description>" + " <descriptionFormat>MARKDOWN</descriptionFormat>"
+ " </rule>" + "</rules>";
RulesDefinition.Rule rule = load(xml).rule("1");
assertThat(rule.markdownDescription()).isEqualTo("Desc");
assertThat(rule.htmlDescription()).isNull();
}
@Test
public void fail_if_unsupported_description_format() {
String xml = "" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>"
+ " <description>Desc</description>" + " <descriptionFormat>UNKNOWN</descriptionFormat>"
+ " </rule>" + "</rules>";
assertThatThrownBy(() -> load(xml).rule("1"))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Fail to load the rule with key [squid:1]")
.hasCauseInstanceOf(IllegalArgumentException.class)
.hasRootCauseMessage(
"No enum constant org.sonar.cxx.sensors.utils.RulesDefinitionXmlLoader.DescriptionFormat.UNKNOWN");
}
@Test
public void test_deprecated_remediation_function() {
String xml = "" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>"
+ " <description>Desc</description>"
+ " <effortToFixDescription>lines</effortToFixDescription>"
+ " <debtRemediationFunction>LINEAR_OFFSET</debtRemediationFunction>"
+ " <debtRemediationFunctionCoefficient>2d 3h</debtRemediationFunctionCoefficient>"
+ " <debtRemediationFunctionOffset>5min</debtRemediationFunctionOffset>" + " </rule>"
+ "</rules>";
RulesDefinition.Rule rule = load(xml).rule("1");
assertThat(rule.gapDescription()).isEqualTo("lines");
DebtRemediationFunction function = rule.debtRemediationFunction();
assertThat(function).isNotNull();
assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
assertThat(function.gapMultiplier()).isEqualTo("2d3h");
assertThat(function.baseEffort()).isEqualTo("5min");
}
private RulesDefinition.Repository load(InputStream input, String encoding) {
RulesDefinition.Context context = new RulesDefinitionContext();
RulesDefinition.NewRepository newRepository = context.createRepository("squid", "java");
underTest.load(newRepository, input, encoding);
newRepository.done();
return context.repository("squid");
}
private RulesDefinition.Repository load(String xml) {
RulesDefinition.Context context = new RulesDefinitionContext();
RulesDefinition.NewRepository newRepository = context.createRepository("squid", "java");
underTest.load(newRepository, new StringReader(xml));
newRepository.done();
return context.repository("squid");
}
}