18
18
*/
19
19
20
20
/*
21
+ * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
21
22
* Portions Copyright (c) 2017, Steven Haehn.
22
23
*/
23
24
package org .opengrok .indexer .util ;
30
31
import org .opengrok .indexer .index .Indexer ;
31
32
32
33
import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
34
+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
33
35
import static org .junit .jupiter .api .Assertions .assertEquals ;
34
36
import static org .junit .jupiter .api .Assertions .assertFalse ;
35
37
import static org .junit .jupiter .api .Assertions .assertNotNull ;
36
38
import static org .junit .jupiter .api .Assertions .assertNull ;
39
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
37
40
import static org .junit .jupiter .api .Assertions .assertTrue ;
38
41
39
42
/**
40
43
* @author shaehn
41
44
*/
42
- public class OptionParserTest {
45
+ class OptionParserTest {
43
46
44
47
int actionCounter ;
45
48
@@ -50,7 +53,7 @@ public void setUp() {
50
53
51
54
// Scan parser should ignore all options it does not recognize.
52
55
@ Test
53
- public void scanParserIgnoreUnrecognizableOptions () throws ParseException {
56
+ void scanParserIgnoreUnrecognizableOptions () throws ParseException {
54
57
55
58
String configPath = "/the/config/path" ;
56
59
@@ -69,7 +72,7 @@ public void scanParserIgnoreUnrecognizableOptions() throws ParseException {
69
72
// Validate that option can have multiple names
70
73
// with both short and long versions.
71
74
@ Test
72
- public void optionNameAliases () throws ParseException {
75
+ void optionNameAliases () throws ParseException {
73
76
74
77
OptionParser opts = OptionParser .execute (parser -> {
75
78
@@ -91,7 +94,7 @@ public void optionNameAliases() throws ParseException {
91
94
// Show that parser will throw exception
92
95
// when option is not recognized.
93
96
@ Test
94
- public void unrecognizedOption () {
97
+ void unrecognizedOption () {
95
98
96
99
OptionParser opts = OptionParser .execute (parser -> {
97
100
parser .on ("-?" , "--help" ).execute (v -> {
@@ -110,7 +113,7 @@ public void unrecognizedOption() {
110
113
111
114
// Show that parser will throw exception when missing option value
112
115
@ Test
113
- public void missingOptionValue () {
116
+ void missingOptionValue () {
114
117
115
118
OptionParser opts = OptionParser .execute (parser -> {
116
119
parser .on ("-a=VALUE" ).execute (v -> {
@@ -131,7 +134,7 @@ public void missingOptionValue() {
131
134
// it is glued next to the option (eg. -xValue), or comes
132
135
// as a following argument (eg. -x Value)
133
136
@ Test
134
- public void shortOptionValue () throws ParseException {
137
+ void shortOptionValue () throws ParseException {
135
138
136
139
OptionParser opts = OptionParser .execute (parser -> {
137
140
parser .on ("-a=VALUE" ).execute (v -> {
@@ -152,7 +155,7 @@ public void shortOptionValue() throws ParseException {
152
155
// Validate the ability of parser to convert
153
156
// string option values into internal data types.
154
157
@ Test
155
- public void testSupportedDataCoercion () throws ParseException {
158
+ void testSupportedDataCoercion () throws ParseException {
156
159
157
160
OptionParser opts = OptionParser .execute (parser -> {
158
161
@@ -227,7 +230,7 @@ public void testSupportedDataCoercion() throws ParseException {
227
230
// Make sure that option can take specific addOption of values
228
231
// and when an candidate values is seen, an exception is given.
229
232
@ Test
230
- public void specificOptionValues () {
233
+ void specificOptionValues () {
231
234
232
235
OptionParser opts = OptionParser .execute (parser -> {
233
236
String [] onOff = {"on" , "off" };
@@ -253,7 +256,7 @@ public void specificOptionValues() {
253
256
254
257
// See that option value matches a regular expression
255
258
@ Test
256
- public void optionValuePatternMatch () {
259
+ void optionValuePatternMatch () {
257
260
258
261
OptionParser opts = OptionParser .execute (parser -> {
259
262
@@ -286,7 +289,7 @@ public void optionValuePatternMatch() {
286
289
287
290
// Verify option may have non-required value
288
291
@ Test
289
- public void missingValueOnOptionAllowed () throws ParseException {
292
+ void missingValueOnOptionAllowed () throws ParseException {
290
293
291
294
OptionParser opts = OptionParser .execute (parser -> {
292
295
@@ -347,7 +350,7 @@ public void missingValueOnOptionAllowed() throws ParseException {
347
350
348
351
// Verify default option summary
349
352
@ Test
350
- public void defaultOptionSummary () {
353
+ void defaultOptionSummary () {
351
354
OptionParser opts = OptionParser .execute (parser -> {
352
355
parser .on ("--help" ).execute (v -> {
353
356
String summary = parser .getUsage ();
@@ -369,7 +372,7 @@ public void defaultOptionSummary() {
369
372
// Therefore, must be able to catch when option entry matches more
370
373
// than one entry.
371
374
@ Test
372
- public void catchAmbigousOptions () {
375
+ void catchAmbigousOptions () {
373
376
OptionParser opts = OptionParser .execute (parser -> {
374
377
parser .on ("--help" );
375
378
parser .on ("--help-me-out" );
@@ -386,7 +389,7 @@ public void catchAmbigousOptions() {
386
389
387
390
// Allow user to enter an initial substring to long option names
388
391
@ Test
389
- public void allowInitialSubstringOptionNames () throws ParseException {
392
+ void allowInitialSubstringOptionNames () throws ParseException {
390
393
OptionParser opts = OptionParser .execute (parser -> {
391
394
parser .on ("--help-me-out" ).execute (v -> actionCounter ++);
392
395
});
@@ -398,7 +401,7 @@ public void allowInitialSubstringOptionNames() throws ParseException {
398
401
399
402
// Specific test to evalutate the internal option candidate method
400
403
@ Test
401
- public void testInitialSubstringOptionNames () throws ParseException {
404
+ void testInitialSubstringOptionNames () throws ParseException {
402
405
OptionParser opts = OptionParser .execute (parser -> {
403
406
parser .on ("--help-me-out" );
404
407
parser .on ("--longOption" );
@@ -411,7 +414,7 @@ public void testInitialSubstringOptionNames() throws ParseException {
411
414
412
415
// Catch duplicate option names in parser construction.
413
416
@ Test
414
- public void catchDuplicateOptionNames () {
417
+ void catchDuplicateOptionNames () {
415
418
try {
416
419
OptionParser .execute (parser -> {
417
420
parser .on ("--duplicate" );
@@ -425,7 +428,7 @@ public void catchDuplicateOptionNames() {
425
428
426
429
// Catch single '-' in argument list
427
430
@ Test
428
- public void catchNamelessOption () {
431
+ void catchNamelessOption () {
429
432
OptionParser opts = OptionParser .execute (parser -> {
430
433
parser .on ("--help-me-out" );
431
434
});
@@ -441,9 +444,9 @@ public void catchNamelessOption() {
441
444
442
445
// Fail options put into Indexer.java that do not have a description.
443
446
@ Test
444
- public void catchIndexerOptionsWithoutDescription () throws NoSuchFieldException , IllegalAccessException , ParseException {
447
+ void catchIndexerOptionsWithoutDescription () throws NoSuchFieldException , IllegalAccessException , ParseException {
445
448
String [] argv = {"---unitTest" };
446
- Indexer .parseOptions (argv );
449
+ assertDoesNotThrow (() -> Indexer .parseOptions (argv ) );
447
450
448
451
// Use reflection to get the option parser from Indexer.
449
452
Field f = Indexer .class .getDeclaredField ("optParser" );
@@ -465,4 +468,20 @@ public void catchIndexerOptionsWithoutDescription() throws NoSuchFieldException,
465
468
assertNull (o .description );
466
469
}
467
470
}
471
+
472
+ @ Test
473
+ void testAddLongDescription () {
474
+ String longDescription = "A" .repeat (OptionParser .Option .MAX_DESCRIPTION_LINE_LENGTH + 1 );
475
+
476
+ assertThrows (IllegalArgumentException .class , () ->
477
+ OptionParser .execute (parser -> {
478
+ parser .on ("--foo" , longDescription );
479
+ }));
480
+ }
481
+
482
+ @ Test
483
+ void testParseOptions () {
484
+ String [] argv = {"---unitTest" };
485
+ assertDoesNotThrow (() -> Indexer .parseOptions (argv ));
486
+ }
468
487
}
0 commit comments