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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
/*
* SonarQube
* Copyright (C) 2009-2021 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.api.config;
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.junit.Test;
import org.sonar.api.Properties;
import org.sonar.api.Property;
import org.sonar.api.PropertyField;
import org.sonar.api.PropertyType;
import org.sonar.api.config.PropertyDefinition.Builder;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.AnnotationUtils;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
public class PropertyDefinitionTest {
@Test
public void should_override_toString() {
PropertyDefinition def = PropertyDefinition.builder("hello").build();
assertThat(def.toString()).isEqualTo("hello");
}
@Test
public void should_create_property() {
PropertyDefinition def = PropertyDefinition.builder("hello")
.name("Hello")
.defaultValue("world")
.category("categ")
.options("de", "en")
.description("desc")
.type(PropertyType.FLOAT)
.onlyOnQualifiers(Qualifiers.MODULE)
.multiValues(true)
.build();
assertThat(def.key()).isEqualTo("hello");
assertThat(def.name()).isEqualTo("Hello");
assertThat(def.defaultValue()).isEqualTo("world");
assertThat(def.category()).isEqualTo("categ");
assertThat(def.options()).containsOnly("de", "en");
assertThat(def.description()).isEqualTo("desc");
assertThat(def.type()).isEqualTo(PropertyType.FLOAT);
assertThat(def.global()).isFalse();
assertThat(def.qualifiers()).containsOnly(Qualifiers.MODULE);
assertThat(def.multiValues()).isTrue();
assertThat(def.fields()).isEmpty();
}
@Test
public void should_create_from_annotation() {
Properties props = AnnotationUtils.getAnnotation(Init.class, Properties.class);
Property prop = props.value()[0];
PropertyDefinition def = PropertyDefinition.create(prop);
assertThat(def.key()).isEqualTo("hello");
assertThat(def.name()).isEqualTo("Hello");
assertThat(def.defaultValue()).isEqualTo("world");
assertThat(def.category()).isEqualTo("categ");
assertThat(def.options()).containsOnly("de", "en");
assertThat(def.description()).isEqualTo("desc");
assertThat(def.type()).isEqualTo(PropertyType.FLOAT);
assertThat(def.global()).isFalse();
assertThat(def.qualifiers()).containsOnly(Qualifiers.PROJECT, Qualifiers.MODULE);
assertThat(def.multiValues()).isTrue();
assertThat(def.fields()).isEmpty();
}
@Test
public void should_create_hidden_property() {
PropertyDefinition def = PropertyDefinition.builder("hello")
.name("Hello")
.hidden()
.build();
assertThat(def.key()).isEqualTo("hello");
assertThat(def.qualifiers()).isEmpty();
assertThat(def.global()).isFalse();
}
@Test
public void should_create_property_with_default_values() {
PropertyDefinition def = PropertyDefinition.builder("hello")
.name("Hello")
.build();
assertThat(def.key()).isEqualTo("hello");
assertThat(def.name()).isEqualTo("Hello");
assertThat(def.defaultValue()).isEmpty();
assertThat(def.category()).isEmpty();
assertThat(def.options()).isEmpty();
assertThat(def.description()).isEmpty();
assertThat(def.type()).isEqualTo(PropertyType.STRING);
assertThat(def.global()).isTrue();
assertThat(def.qualifiers()).isEmpty();
assertThat(def.multiValues()).isFalse();
assertThat(def.fields()).isEmpty();
}
@Test
public void should_create_from_annotation_default_values() {
Properties props = AnnotationUtils.getAnnotation(DefaultValues.class, Properties.class);
Property prop = props.value()[0];
PropertyDefinition def = PropertyDefinition.create(prop);
assertThat(def.key()).isEqualTo("hello");
assertThat(def.name()).isEqualTo("Hello");
assertThat(def.defaultValue()).isEmpty();
assertThat(def.category()).isEmpty();
assertThat(def.options()).isEmpty();
assertThat(def.description()).isEmpty();
assertThat(def.type()).isEqualTo(PropertyType.STRING);
assertThat(def.global()).isTrue();
assertThat(def.qualifiers()).isEmpty();
assertThat(def.multiValues()).isFalse();
assertThat(def.fields()).isEmpty();
}
@Test
public void should_support_property_sets() {
PropertyDefinition def = PropertyDefinition.builder("hello")
.name("Hello")
.fields(
PropertyFieldDefinition.build("first").name("First").description("Description").options("A", "B").build(),
PropertyFieldDefinition.build("second").name("Second").type(PropertyType.INTEGER).build())
.build();
assertThat(def.type()).isEqualTo(PropertyType.PROPERTY_SET);
assertThat(def.fields()).hasSize(2);
assertThat(def.fields().get(0).key()).isEqualTo("first");
assertThat(def.fields().get(0).name()).isEqualTo("First");
assertThat(def.fields().get(0).description()).isEqualTo("Description");
assertThat(def.fields().get(0).type()).isEqualTo(PropertyType.STRING);
assertThat(def.fields().get(0).options()).containsOnly("A", "B");
assertThat(def.fields().get(1).key()).isEqualTo("second");
assertThat(def.fields().get(1).name()).isEqualTo("Second");
assertThat(def.fields().get(1).type()).isEqualTo(PropertyType.INTEGER);
assertThat(def.fields().get(1).options()).isEmpty();
}
@Test
public void should_support_property_sets_from_annotation() {
Properties props = AnnotationUtils.getAnnotation(WithPropertySet.class, Properties.class);
Property prop = props.value()[0];
PropertyDefinition def = PropertyDefinition.create(prop);
assertThat(def.type()).isEqualTo(PropertyType.PROPERTY_SET);
assertThat(def.fields()).hasSize(2);
assertThat(def.fields().get(0).key()).isEqualTo("first");
assertThat(def.fields().get(0).name()).isEqualTo("First");
assertThat(def.fields().get(0).description()).isEqualTo("Description");
assertThat(def.fields().get(0).type()).isEqualTo(PropertyType.STRING);
assertThat(def.fields().get(0).options()).containsOnly("A", "B");
assertThat(def.fields().get(1).key()).isEqualTo("second");
assertThat(def.fields().get(1).name()).isEqualTo("Second");
assertThat(def.fields().get(1).type()).isEqualTo(PropertyType.INTEGER);
assertThat(def.fields().get(1).options()).isEmpty();
}
@Test
public void should_validate_string() {
PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.STRING).build();
assertThat(def.validate(null).isValid()).isTrue();
assertThat(def.validate("").isValid()).isTrue();
assertThat(def.validate(" ").isValid()).isTrue();
assertThat(def.validate("foo").isValid()).isTrue();
}
@Test
public void should_validate_boolean() {
PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.BOOLEAN).build();
assertThat(def.validate(null).isValid()).isTrue();
assertThat(def.validate("").isValid()).isTrue();
assertThat(def.validate(" ").isValid()).isTrue();
assertThat(def.validate("true").isValid()).isTrue();
assertThat(def.validate("false").isValid()).isTrue();
assertThat(def.validate("foo").isValid()).isFalse();
assertThat(def.validate("foo").getErrorKey()).isEqualTo("notBoolean");
}
@Test
public void should_validate_integer() {
PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.INTEGER).build();
assertThat(def.validate(null).isValid()).isTrue();
assertThat(def.validate("").isValid()).isTrue();
assertThat(def.validate(" ").isValid()).isTrue();
assertThat(def.validate("123456").isValid()).isTrue();
assertThat(def.validate("foo").isValid()).isFalse();
assertThat(def.validate("foo").getErrorKey()).isEqualTo("notInteger");
}
@Test
public void validate_long() {
PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.LONG).build();
assertThat(def.validate(null).isValid()).isTrue();
assertThat(def.validate("").isValid()).isTrue();
assertThat(def.validate(" ").isValid()).isTrue();
assertThat(def.validate("123456").isValid()).isTrue();
assertThat(def.validate("foo").isValid()).isFalse();
assertThat(def.validate("foo").getErrorKey()).isEqualTo("notInteger");
}
@Test
public void should_validate_float() {
PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.FLOAT).build();
assertThat(def.validate(null).isValid()).isTrue();
assertThat(def.validate("").isValid()).isTrue();
assertThat(def.validate(" ").isValid()).isTrue();
assertThat(def.validate("123456").isValid()).isTrue();
assertThat(def.validate("3.14").isValid()).isTrue();
assertThat(def.validate("foo").isValid()).isFalse();
assertThat(def.validate("foo").getErrorKey()).isEqualTo("notFloat");
}
@Test
public void validate_regular_expression() {
PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.REGULAR_EXPRESSION).build();
assertThat(def.validate(null).isValid()).isTrue();
assertThat(def.validate("").isValid()).isTrue();
assertThat(def.validate(" ").isValid()).isTrue();
assertThat(def.validate("[a-zA-Z]").isValid()).isTrue();
assertThat(def.validate("[a-zA-Z").isValid()).isFalse();
assertThat(def.validate("[a-zA-Z").getErrorKey()).isEqualTo("notRegexp");
}
@Test
public void should_validate_single_select_list() {
PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.SINGLE_SELECT_LIST).options("de", "en").build();
assertThat(def.validate(null).isValid()).isTrue();
assertThat(def.validate("").isValid()).isTrue();
assertThat(def.validate(" ").isValid()).isTrue();
assertThat(def.validate("de").isValid()).isTrue();
assertThat(def.validate("en").isValid()).isTrue();
assertThat(def.validate("fr").isValid()).isFalse();
assertThat(def.validate("fr").getErrorKey()).isEqualTo("notInOptions");
}
@Test
public void should_auto_detect_password_type() {
PropertyDefinition def = PropertyDefinition.builder("scm.password.secured").name("SCM password").build();
assertThat(def.key()).isEqualTo("scm.password.secured");
assertThat(def.type()).isEqualTo(PropertyType.PASSWORD);
}
@Test
public void PropertyDef() {
PropertyDefinition def = PropertyDefinition.builder("views.license.secured").name("Views license").build();
assertThat(def.key()).isEqualTo("views.license.secured");
assertThat(def.type()).isEqualTo(PropertyType.LICENSE);
}
@Test
public void should_create_json_property_type() {
Builder builder = PropertyDefinition.builder("json-prop").type(PropertyType.JSON).multiValues(false);
assertThatCode(builder::build)
.doesNotThrowAnyException();
}
@Test
public void should_not_authorise_empty_key() {
Builder builder = PropertyDefinition.builder(null);
assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Key must be set");
}
@Test
public void should_not_create_json_multivalue() {
Builder builder = PropertyDefinition.builder("json-prop").type(PropertyType.JSON).multiValues(true);
assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Multivalues are not allowed to be defined for JSON-type property.");
}
@Test
public void should_not_authorize_defining_on_qualifiers_and_hidden() {
Builder builder = PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.PROJECT).hidden();
assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Cannot be hidden and defining qualifiers on which to display");
}
@Test
public void should_not_authorize_defining_ony_on_qualifiers_and_hidden() {
Builder builder = PropertyDefinition.builder("foo").name("foo").onlyOnQualifiers(Qualifiers.PROJECT).hidden();
assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Cannot be hidden and defining qualifiers on which to display");
}
@Test
public void should_not_authorize_defining_on_qualifiers_and_only_on_qualifiers() {
Builder builder = PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.MODULE)
.onlyOnQualifiers(Qualifiers.PROJECT);
assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Cannot define both onQualifiers and onlyOnQualifiers");
}
private static final Set<String> ALLOWED_QUALIFIERS = ImmutableSet.of("TRK", "VW", "BRC", "SVW");
private static final Set<String> NOT_ALLOWED_QUALIFIERS = ImmutableSet.of("FIL", "DIR", "UTS", "", randomAlphabetic(3));
@Test
public void onQualifiers_with_varargs_parameter_fails_with_IAE_when_qualifier_is_not_supported() {
failsWithIAEForUnsupportedQualifiers((builder, qualifier) -> builder.onQualifiers(qualifier));
failsWithIAEForUnsupportedQualifiers((builder, qualifier) -> builder.onQualifiers("TRK", qualifier, "BRC"));
}
@Test
public void onQualifiers_with_list_parameter_fails_with_IAE_when_qualifier_is_not_supported() {
failsWithIAEForUnsupportedQualifiers((builder, qualifier) -> builder.onQualifiers(Collections.singletonList(qualifier)));
failsWithIAEForUnsupportedQualifiers((builder, qualifier) -> builder.onQualifiers(Arrays.asList("TRK", qualifier, "BRC")));
}
@Test
public void onlyOnQualifiers_with_varargs_parameter_fails_with_IAE_when_qualifier_is_not_supported() {
failsWithIAEForUnsupportedQualifiers((builder, qualifier) -> builder.onlyOnQualifiers(qualifier));
failsWithIAEForUnsupportedQualifiers((builder, qualifier) -> builder.onlyOnQualifiers("TRK", qualifier, "BRC"));
}
@Test
public void onlyOnQualifiers_with_list_parameter_fails_with_IAE_when_qualifier_is_not_supported() {
failsWithIAEForUnsupportedQualifiers((builder, qualifier) -> builder.onlyOnQualifiers(Collections.singletonList(qualifier)));
failsWithIAEForUnsupportedQualifiers((builder, qualifier) -> builder.onlyOnQualifiers(Arrays.asList("TRK", qualifier, "BRC")));
}
@Test
public void onQualifiers_with_varargs_parameter_fails_with_NPE_when_qualifier_is_null() {
failsWithNPEForNullQualifiers(builder -> builder.onQualifiers((String) null));
failsWithNPEForNullQualifiers(builder -> builder.onQualifiers("TRK", null, "BRC"));
}
@Test
public void onQualifiers_with_list_parameter_fails_with_NPE_when_qualifier_is_null() {
failsWithNPEForNullQualifiers(builder -> builder.onQualifiers(Collections.singletonList(null)));
failsWithNPEForNullQualifiers(builder -> builder.onlyOnQualifiers("TRK", null, "BRC"));
}
@Test
public void onlyOnQualifiers_with_varargs_parameter_fails_with_NPE_when_qualifier_is_null() {
failsWithNPEForNullQualifiers(builder -> builder.onlyOnQualifiers((String) null));
failsWithNPEForNullQualifiers(builder -> builder.onlyOnQualifiers("TRK", null, "BRC"));
}
@Test
public void onlyOnQualifiers_with_list_parameter_fails_with_NPE_when_qualifier_is_null() {
failsWithNPEForNullQualifiers(builder -> builder.onlyOnQualifiers(Collections.singletonList(null)));
failsWithNPEForNullQualifiers(builder -> builder.onlyOnQualifiers(Arrays.asList("TRK", null, "BRC")));
}
@Test
public void onQualifiers_with_varargs_parameter_accepts_supported_qualifiers() {
acceptsSupportedQualifiers((builder, qualifier) -> builder.onQualifiers(qualifier));
}
@Test
public void onQualifiers_with_list_parameter_accepts_supported_qualifiers() {
acceptsSupportedQualifiers((builder, qualifier) -> builder.onQualifiers(Collections.singletonList(qualifier)));
}
@Test
public void onlyOnQualifiers_with_varargs_parameter_accepts_supported_qualifiers() {
acceptsSupportedQualifiers((builder, qualifier) -> builder.onlyOnQualifiers(qualifier));
}
@Test
public void onlyOnQualifiers_with_list_parameter_accepts_supported_qualifiers() {
acceptsSupportedQualifiers((builder, qualifier) -> builder.onlyOnQualifiers(Collections.singletonList(qualifier)));
}
private static void failsWithIAEForUnsupportedQualifiers(BiConsumer<PropertyDefinition.Builder, String> biConsumer) {
PropertyDefinition.Builder builder = PropertyDefinition.builder(randomAlphabetic(3));
NOT_ALLOWED_QUALIFIERS.forEach(qualifier -> {
try {
biConsumer.accept(builder, qualifier);
fail("A IllegalArgumentException should have been thrown for qualifier " + qualifier);
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Qualifier must be one of [TRK, VW, BRC, SVW, APP]");
}
});
}
private static void acceptsSupportedQualifiers(BiConsumer<PropertyDefinition.Builder, String> biConsumer) {
PropertyDefinition.Builder builder = PropertyDefinition.builder(randomAlphabetic(3));
ALLOWED_QUALIFIERS.forEach(qualifier -> biConsumer.accept(builder, qualifier));
}
private static void failsWithNPEForNullQualifiers(Consumer<PropertyDefinition.Builder> consumer) {
PropertyDefinition.Builder builder = PropertyDefinition.builder(randomAlphabetic(3));
NOT_ALLOWED_QUALIFIERS.forEach(qualifier -> {
try {
consumer.accept(builder);
fail("A NullPointerException should have been thrown for null qualifier");
} catch (NullPointerException e) {
assertThat(e).hasMessage("Qualifier cannot be null");
}
});
}
@Properties(@Property(key = "hello", name = "Hello", defaultValue = "world", description = "desc",
options = {"de", "en"}, category = "categ", type = PropertyType.FLOAT, global = false, project = true, module = true, multiValues = true, propertySetKey = "set"))
static class Init {
}
@Properties(@Property(key = "hello", name = "Hello", fields = {
@PropertyField(key = "first", name = "First", description = "Description", options = {"A", "B"}),
@PropertyField(key = "second", name = "Second", type = PropertyType.INTEGER)}))
static class WithPropertySet {
}
@Properties(@Property(key = "hello", name = "Hello"))
static class DefaultValues {
}
}
|