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
|
/*
* 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.application.command;
import com.google.common.collect.ImmutableMap;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sonar.process.MessageException;
import org.sonar.process.Props;
import static java.lang.String.valueOf;
import static org.apache.commons.lang3.RandomStringUtils.secure;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
@RunWith(DataProviderRunner.class)
public class JvmOptionsTest {
private final Random random = new Random();
private final String randomPropertyName = secure().nextAlphanumeric(3);
private final String randomPrefix = "-" + secure().nextAlphabetic(5).toLowerCase(Locale.ENGLISH);
private final String randomValue = secure().nextAlphanumeric(4).toLowerCase(Locale.ENGLISH);
private final Properties properties = new Properties();
private final JvmOptions underTest = new JvmOptions();
@Test
public void constructor_without_arguments_creates_empty_JvmOptions() {
JvmOptions<JvmOptions> testJvmOptions = new JvmOptions<>();
assertThat(testJvmOptions.getAll()).isEmpty();
}
@Test
public void constructor_throws_NPE_if_argument_is_null() {
expectJvmOptionNotNullNPE(() -> new JvmOptions(null));
}
@Test
public void constructor_throws_NPE_if_any_option_prefix_is_null() {
Map<String, String> mandatoryJvmOptions = shuffleThenToMap(
Stream.of(
IntStream.range(0, random.nextInt(10)).mapToObj(i -> new Option("-B", valueOf(i))),
Stream.of(new Option(null, "value")))
.flatMap(s -> s));
assertThatThrownBy(() -> new JvmOptions(mandatoryJvmOptions))
.isInstanceOf(NullPointerException.class)
.hasMessage("JVM option prefix can't be null");
}
@Test
@UseDataProvider("variousEmptyStrings")
public void constructor_throws_IAE_if_any_option_prefix_is_empty(String emptyString) {
Map<String, String> mandatoryJvmOptions = shuffleThenToMap(
Stream.of(
IntStream.range(0, random.nextInt(10)).mapToObj(i -> new Option("-B", valueOf(i))),
Stream.of(new Option(emptyString, "value")))
.flatMap(s -> s));
assertThatThrownBy(() -> new JvmOptions(mandatoryJvmOptions))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("JVM option prefix can't be empty");
}
@Test
public void constructor_throws_IAE_if_any_option_prefix_does_not_start_with_dash() {
String invalidPrefix = secure().nextAlphanumeric(3);
Map<String, String> mandatoryJvmOptions = shuffleThenToMap(
Stream.of(
IntStream.range(0, random.nextInt(10)).mapToObj(i -> new Option("-B", valueOf(i))),
Stream.of(new Option(invalidPrefix, "value")))
.flatMap(s -> s));
expectJvmOptionNotEmptyAndStartByDashIAE(() -> new JvmOptions(mandatoryJvmOptions));
}
@Test
public void constructor_throws_NPE_if_any_option_value_is_null() {
Map<String, String> mandatoryJvmOptions = shuffleThenToMap(
Stream.of(
IntStream.range(0, random.nextInt(10)).mapToObj(i -> new Option("-B", valueOf(i))),
Stream.of(new Option("-prefix", null)))
.flatMap(s -> s));
assertThatThrownBy(() -> new JvmOptions(mandatoryJvmOptions))
.isInstanceOf(NullPointerException.class)
.hasMessage("JVM option value can't be null");
}
@Test
@UseDataProvider("variousEmptyStrings")
public void constructor_accepts_any_empty_option_value(String emptyString) {
Map<String, String> mandatoryJvmOptions = shuffleThenToMap(
Stream.of(
IntStream.range(0, random.nextInt(10)).mapToObj(i -> new Option("-B", valueOf(i))),
Stream.of(new Option("-prefix", emptyString)))
.flatMap(s -> s));
new JvmOptions(mandatoryJvmOptions);
}
@Test
public void add_throws_NPE_if_argument_is_null() {
expectJvmOptionNotNullNPE(() -> underTest.add(null));
}
@Test
@UseDataProvider("variousEmptyStrings")
public void add_throws_IAE_if_argument_is_empty(String emptyString) {
expectJvmOptionNotEmptyAndStartByDashIAE(() -> underTest.add(emptyString));
}
@Test
public void add_throws_IAE_if_argument_does_not_start_with_dash() {
expectJvmOptionNotEmptyAndStartByDashIAE(() -> underTest.add(secure().nextAlphanumeric(3)));
}
@Test
@UseDataProvider("variousEmptyStrings")
public void add_adds_with_trimming(String emptyString) {
underTest.add(emptyString + "-foo" + emptyString);
assertThat(underTest.getAll()).containsOnly("-foo");
}
@Test
public void add_throws_MessageException_if_option_starts_with_prefix_of_mandatory_option_but_has_different_value() {
String[] optionOverrides = {
randomPrefix,
randomPrefix + secure().nextAlphanumeric(1),
randomPrefix + secure().nextAlphanumeric(2),
randomPrefix + secure().nextAlphanumeric(3),
randomPrefix + secure().nextAlphanumeric(4),
randomPrefix + randomValue.substring(1),
randomPrefix + randomValue.substring(2),
randomPrefix + randomValue.substring(3)
};
JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue));
for (String optionOverride : optionOverrides) {
try {
underTest.add(optionOverride);
fail("an MessageException should have been thrown");
} catch (MessageException e) {
assertThat(e.getMessage()).isEqualTo("a JVM option can't overwrite mandatory JVM options. " + optionOverride + " overwrites " + randomPrefix + randomValue);
}
}
}
@Test
public void add_checks_against_mandatory_options_is_case_sensitive() {
String[] optionOverrides = {
randomPrefix,
randomPrefix + secure().nextAlphanumeric(1),
randomPrefix + secure().nextAlphanumeric(2),
randomPrefix + secure().nextAlphanumeric(3),
randomPrefix + secure().nextAlphanumeric(4),
randomPrefix + randomValue.substring(1),
randomPrefix + randomValue.substring(2),
randomPrefix + randomValue.substring(3)
};
JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue));
for (String optionOverride : optionOverrides) {
underTest.add(optionOverride.toUpperCase(Locale.ENGLISH));
}
}
@Test
public void add_accepts_property_equal_to_mandatory_option_and_does_not_add_it_twice() {
JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue));
underTest.add(randomPrefix + randomValue);
assertThat(underTest.getAll()).containsOnly(randomPrefix + randomValue);
}
@Test
public void addFromMandatoryProperty_fails_with_IAE_if_property_does_not_exist() {
expectMissingPropertyIAE(() -> underTest.addFromMandatoryProperty(new Props(properties), this.randomPropertyName), this.randomPropertyName);
}
@Test
public void addFromMandatoryProperty_fails_with_IAE_if_property_contains_an_empty_value() {
expectMissingPropertyIAE(() -> underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName), this.randomPropertyName);
}
@Test
@UseDataProvider("variousEmptyStrings")
public void addFromMandatoryProperty_adds_single_option_of_property_with_trimming(String emptyString) {
properties.put(randomPropertyName, emptyString + "-foo" + emptyString);
underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
assertThat(underTest.getAll()).containsOnly("-foo");
}
@Test
@UseDataProvider("variousEmptyStrings")
public void addFromMandatoryProperty_fails_with_MessageException_if_property_does_not_start_with_dash_after_trimmed(String emptyString) {
properties.put(randomPropertyName, emptyString + "foo -bar");
expectJvmOptionNotEmptyAndStartByDashMessageException(() -> underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName), randomPropertyName, "foo");
}
@Test
@UseDataProvider("variousEmptyStrings")
public void addFromMandatoryProperty_adds_options_of_property_with_trimming(String emptyString) {
properties.put(randomPropertyName, emptyString + "-foo" + emptyString + " -bar" + emptyString + " -duck" + emptyString);
underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
assertThat(underTest.getAll()).containsOnly("-foo", "-bar", "-duck");
}
@Test
public void addFromMandatoryProperty_supports_spaces_inside_options() {
properties.put(randomPropertyName, "-foo bar -duck");
underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
assertThat(underTest.getAll()).containsOnly("-foo bar", "-duck");
}
@Test
public void addFromMandatoryProperty_throws_IAE_if_option_starts_with_prefix_of_mandatory_option_but_has_different_value() {
String[] optionOverrides = {
randomPrefix,
randomPrefix + randomValue.substring(1),
randomPrefix + randomValue.substring(1),
randomPrefix + randomValue.substring(2),
randomPrefix + randomValue.substring(3),
randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(1),
randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(2),
randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(3),
randomPrefix + randomValue + secure().nextAlphanumeric(1)
};
JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue));
for (String optionOverride : optionOverrides) {
try {
properties.put(randomPropertyName, optionOverride);
underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
fail("an MessageException should have been thrown");
} catch (MessageException e) {
assertThat(e.getMessage())
.isEqualTo("a JVM option can't overwrite mandatory JVM options. " +
"The following JVM options defined by property '" + randomPropertyName + "' are invalid: " + optionOverride + " overwrites " + randomPrefix + randomValue);
}
}
}
@Test
public void addFromMandatoryProperty_checks_against_mandatory_options_is_case_sensitive() {
String[] optionOverrides = {
randomPrefix,
randomPrefix + randomValue.substring(1),
randomPrefix + randomValue.substring(1),
randomPrefix + randomValue.substring(2),
randomPrefix + randomValue.substring(3),
randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(1),
randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(2),
randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(3),
randomPrefix + randomValue + secure().nextAlphanumeric(1)
};
JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue));
for (String optionOverride : optionOverrides) {
properties.setProperty(randomPropertyName, optionOverride.toUpperCase(Locale.ENGLISH));
underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
}
}
@Test
public void addFromMandatoryProperty_reports_all_overriding_options_in_single_exception() {
String overriding1 = randomPrefix;
String overriding2 = randomPrefix + randomValue + secure().nextAlphanumeric(1);
properties.setProperty(randomPropertyName, "-foo " + overriding1 + " -bar " + overriding2);
JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue));
assertThatThrownBy(() -> underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName))
.isInstanceOf(MessageException.class)
.hasMessage("a JVM option can't overwrite mandatory JVM options. " +
"The following JVM options defined by property '" + randomPropertyName + "' are invalid: " +
overriding1 + " overwrites " + randomPrefix + randomValue + ", " + overriding2 + " overwrites " + randomPrefix + randomValue);
}
@Test
public void addFromMandatoryProperty_accepts_property_equal_to_mandatory_option_and_does_not_add_it_twice() {
JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue));
properties.put(randomPropertyName, randomPrefix + randomValue);
underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
assertThat(underTest.getAll()).containsOnly(randomPrefix + randomValue);
}
@Test
public void toString_prints_all_jvm_options() {
underTest.add("-foo").add("-bar");
assertThat(underTest).hasToString("[-foo, -bar]");
}
private void expectJvmOptionNotNullNPE(ThrowingCallable callback) {
assertThatThrownBy(callback)
.isInstanceOf(NullPointerException.class)
.hasMessage("a JVM option can't be null");
}
private void expectJvmOptionNotEmptyAndStartByDashIAE(ThrowingCallable callback) {
assertThatThrownBy(callback)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("a JVM option can't be empty and must start with '-'");
}
private void expectJvmOptionNotEmptyAndStartByDashMessageException(ThrowingCallable callback, String randomPropertyName, String option) {
assertThatThrownBy(callback)
.isInstanceOf(MessageException.class)
.hasMessage("a JVM option can't be empty and must start with '-'. " +
"The following JVM options defined by property '" + randomPropertyName + "' are invalid: " + option);
}
public void expectMissingPropertyIAE(ThrowingCallable callback, String randomPropertyName) {
assertThatThrownBy(callback)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Missing property: " + randomPropertyName);
}
@DataProvider()
public static Object[][] variousEmptyStrings() {
return new Object[][] {
{""},
{" "},
{" "}
};
}
private static Map<String, String> shuffleThenToMap(Stream<Option> stream) {
List<Option> options = stream.collect(Collectors.toCollection(ArrayList::new));
Collections.shuffle(options);
Map<String, String> res = new HashMap<>(options.size());
for (Option option : options) {
res.put(option.getPrefix(), option.getValue());
}
return res;
}
private static final class Option {
private final String prefix;
private final String value;
private Option(String prefix, String value) {
this.prefix = prefix;
this.value = value;
}
public String getPrefix() {
return prefix;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "[" + prefix + "-" + value + ']';
}
}
}
|