summaryrefslogtreecommitdiff
path: root/qpid/java/common/src/test/java/org/apache/qpid/util/CommandLineParserTest.java
blob: 942901f1c0bfc70ec2a10a37bbda75b8a25b9b82 (plain)
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package org.apache.qpid.util;

import junit.framework.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * Unit tests the {@link CommandLineParser} class.
 *
 * <p><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Check that parsing a single flag works ok.
 * <tr><td> Check that parsing multiple flags condensed together works ok.
 * <tr><td> Check that parsing an option with a space between it and its argument works ok.
 * <tr><td> Check that parsing an option with no space between it and its argument works ok.
 * <tr><td> Check that parsing an option with specific argument format works ok.
 * <tr><td> Check that parsing an option with specific argument format fails on bad argument.
 * <tr><td> Check that parsing a flag condensed together with an option fails.
 * <tr><td> Check that parsing a free argument works ok.
 * <tr><td> Check that parsing a free argument with specific format works ok.
 * <tr><td> Check that parsing a free argument with specific format fails on bad argument.
 * <tr><td> Check that parsing a mandatory option works ok.
 * <tr><td> Check that parsing a mandatory free argument works ok.
 * <tr><td> Check that parsing a mandatory option fails when no option is set.
 * <tr><td> Check that parsing a mandatory free argument fails when no argument is specified.
 * <tr><td> Check that parsing an unknown option works when unknowns not errors.
 * <tr><td> Check that parsing an unknown flag fails when unknowns are to be reported as errors.
 * <tr><td> Check that parsing an unknown option fails when unknowns are to be reported as errors.
 * <tr><td> Check that get errors returns a string on errors.
 * <tr><td> Check that get errors returns an empty string on no errors.
 * <tr><td> Check that get usage returns a string.
 * <tr><td> Check that get options in force returns an empty string before parsing.
 * <tr><td> Check that get options in force return a non-empty string after parsing.
 * </table>
 */
public class CommandLineParserTest extends TestCase
{
    private static final Logger log = LoggerFactory.getLogger(CommandLineParserTest.class);

    public CommandLineParserTest(String name)
    {
        super(name);
    }

    /**
     * Compile all the tests for the default test implementation of a traversable state into a test suite.
     */
    public static Test suite()
    {
        // Build a new test suite
        TestSuite suite = new TestSuite("CommandLineParser Tests");

        // Add all the tests defined in this class (using the default constructor)
        suite.addTestSuite(CommandLineParserTest.class);

        return suite;
    }

    /** Check that get errors returns an empty string on no errors. */
    public void testGetErrorsReturnsEmptyStringOnNoErrors() throws Exception
    {
        // Create a command line parser for some flags and options.
        CommandLineParser parser =
            new CommandLineParser(
                new String[][]
                {
                    { "t1", "Test Flag 1." },
                    { "t2", "Test Option 2.", "test" },
                    { "t3", "Test Option 3.", "test", "true" },
                    { "t4", "Test Option 4.", "test", null, "^test$" }
                });

        // Do some legal parsing.
        parser.parseCommandLine(new String[] { "-t1", "-t2test", "-t3test", "-t4test" });

        // Check that the get errors message returns an empty string.
        assertTrue("The errors method did not return an empty string.", "".equals(parser.getErrors()));
    }

    /** Check that get errors returns a string on errors. */
    public void testGetErrorsReturnsStringOnErrors() throws Exception
    {
        // Create a command line parser for some flags and options.
        CommandLineParser parser =
            new CommandLineParser(
                new String[][]
                {
                    { "t1", "Test Flag 1." },
                    { "t2", "Test Option 2.", "test" },
                    { "t3", "Test Option 3.", "test", "true" },
                    { "t4", "Test Option 4.", "test", null, "^test$" }
                });

        try
        {
            // Do some illegal parsing.
            parser.parseCommandLine(new String[] { "-t1", "-t1t2test", "-t4fail" });
        }
        catch (IllegalArgumentException e)
        { }

        // Check that the get errors message returns a string.
        assertTrue("The errors method returned an empty string.",
            !((parser.getErrors() == null) || "".equals(parser.getErrors())));

    }

    /** Check that get options in force returns an empty string before parsing. */
    public void testGetOptionsInForceReturnsEmptyStringBeforeParsing() throws Exception
    {
        // Create a command line parser for some flags and options.
        CommandLineParser parser =
            new CommandLineParser(
                new String[][]
                {
                    { "t1", "Test Flag 1." },
                    { "t2", "Test Option 2.", "test" },
                    { "t3", "Test Option 3.", "test", "true" },
                    { "t4", "Test Option 4.", "test", null, "^test$" }
                });

        // Check that the options in force method returns an empty string.
        assertTrue("The options in force method did not return an empty string.", "".equals(parser.getOptionsInForce()));
    }

    /** Check that get options in force return a non-empty string after parsing. */
    public void testGetOptionsInForceReturnsNonEmptyStringAfterParsing() throws Exception
    {
        // Create a command line parser for some flags and options.
        CommandLineParser parser =
            new CommandLineParser(
                new String[][]
                {
                    { "t1", "Test Flag 1." },
                    { "t2", "Test Option 2.", "test" },
                    { "t3", "Test Option 3.", "test", "true" },
                    { "t4", "Test Option 4.", "test", null, "^test$" }
                });

        // Do some parsing.
        parser.parseCommandLine(new String[] { "-t1", "-t2test", "-t3test", "-t4test" });

        // Check that the options in force method returns a string.
        assertTrue("The options in force method did not return a non empty string.",
            !((parser.getOptionsInForce() == null) || "".equals(parser.getOptionsInForce())));
    }

    /** Check that get usage returns a string. */
    public void testGetUsageReturnsString() throws Exception
    {
        // Create a command line parser for some flags and options.
        CommandLineParser parser =
            new CommandLineParser(
                new String[][]
                {
                    { "t1", "Test Flag 1." },
                    { "t2", "Test Option 2.", "test" },
                    { "t3", "Test Option 3.", "test", "true" },
                    { "t4", "Test Option 4.", "test", null, "^test$" }
                });

        // Check that the usage method returns a string.
        assertTrue("The usage method did not return a non empty string.",
            !((parser.getUsage() == null) || "".equals(parser.getUsage())));
    }

    /** Check that parsing multiple flags condensed together works ok. */
    public void testParseCondensedFlagsOk() throws Exception
    {
        // Create a command line parser for multiple flags.
        CommandLineParser parser =
            new CommandLineParser(
                new String[][]
                {
                    { "t1", "Test Flag 1." },
                    { "t2", "Test Flag 2." },
                    { "t3", "Test Flag 3." }
                });

        // Parse a command line with the flags set and condensed together.
        Properties testProps = parser.parseCommandLine(new String[] { "-t1t2t3" });

        // Check that the flags were set in the parsed properties.
        assertTrue("The t1 flag was not \"true\", it was: " + testProps.get("t1"), "true".equals(testProps.get("t1")));
        assertTrue("The t2 flag was not \"true\", it was: " + testProps.get("t2"), "true".equals(testProps.get("t2")));
        assertTrue("The t3 flag was not \"true\", it was: " + testProps.get("t3"), "true".equals(testProps.get("t3")));
    }

    /** Check that parsing a flag condensed together with an option fails. */
    public void testParseFlagCondensedWithOptionFails() throws Exception
    {
        // Create a command line parser for a flag and an option.
        CommandLineParser parser =
            new CommandLineParser(new String[][]
                {
                    { "t1", "Test Flag 1." },
                    { "t2", "Test Option 2.", "test" }
                });

        // Check that the parser reports an error.
        boolean testPassed = false;

        try
        {
            // Parse a command line with the flag and option condensed together.
            Properties testProps = parser.parseCommandLine(new String[] { "-t1t2" });
        }
        catch (IllegalArgumentException e)
        {
            testPassed = true;
        }

        assertTrue("IllegalArgumentException not thrown when a flag and option are condensed together.", testPassed);
    }

    /** Check that parsing a free argument with specific format fails on bad argument. */
    public void testParseFormattedFreeArgumentFailsBadArgument() throws Exception
    {
        // Create a command line parser for a formatted free argument.
        CommandLineParser parser =
            new CommandLineParser(new String[][]
                {
                    { "1", "Test Free Argument.", "test", null, "^test$" }
                });

        // Check that the parser signals an error for a badly formatted argument.
        boolean testPassed = false;

        try
        {
            // Parse a command line with this option set incorrectly.
            Properties testProps = parser.parseCommandLine(new String[] { "fail" });
        }
        catch (IllegalArgumentException e)
        {
            testPassed = true;
        }

        assertTrue("IllegalArgumentException not thrown when a badly formatted argument was set.", testPassed);
    }

    /** Check that parsing a free argument with specific format works ok. */
    public void testParseFormattedFreeArgumentOk() throws Exception
    {
        // Create a command line parser for a formatted free argument.
        CommandLineParser parser =
            new CommandLineParser(new String[][]
                {
                    { "1", "Test Free Argument.", "test", null, "^test$" }
                });

        // Parse a command line with this argument set correctly.
        Properties testProps = parser.parseCommandLine(new String[] { "test" });

        // Check that the resultant properties contains the correctly parsed option.
        assertTrue("The first free argument was not equal to \"test\" but was: " + testProps.get("1"),
            "test".equals(testProps.get("1")));
    }

    /** Check that parsing an option with specific argument format fails on bad argument. */
    public void testParseFormattedOptionArgumentFailsBadArgument() throws Exception
    {
        // Create a command line parser for a formatted option.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "t", "Test Option.", "test", null, "^test$" }
                });

        // Check that the parser signals an error for a badly formatted argument.
        boolean testPassed = false;

        try
        {
            // Parse a command line with this option set incorrectly.
            Properties testProps = parser.parseCommandLine(new String[] { "-t", "fail" });
        }
        catch (IllegalArgumentException e)
        {
            testPassed = true;
        }

        assertTrue("IllegalArgumentException not thrown when a badly formatted argument was set.", testPassed);
    }

    /** Check that parsing an option with specific argument format works ok. */
    public void testParseFormattedOptionArgumentOk() throws Exception
    {
        // Create a command line parser for a formatted option.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "t", "Test Option.", "test", null, "^test$" }
                });

        // Parse a command line with this option set correctly.
        Properties testProps = parser.parseCommandLine(new String[] { "-t", "test" });

        // Check that the resultant properties contains the correctly parsed option.
        assertTrue("The test option was not equal to \"test\" but was: " + testProps.get("t"),
            "test".equals(testProps.get("t")));
    }

    /** Check that parsing a free argument works ok. */
    public void testParseFreeArgumentOk() throws Exception
    {
        // Create a command line parser for a free argument.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "1", "Test Free Argument.", "test" }
                });

        // Parse a command line with this argument set.
        Properties testProps = parser.parseCommandLine(new String[] { "test" });

        // Check that the resultant properties contains the correctly parsed option.
        assertTrue("The first free argument was not equal to \"test\" but was: " + testProps.get("1"),
            "test".equals(testProps.get("1")));
    }

    /** Check that parsing a mandatory option works ok. */
    public void testParseMandatoryOptionOk() throws Exception
    {
        // Create a command line parser for a mandatory option.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "t", "Test Option.", "test", "true" }
                });

        // Parse a command line with this option set correctly.
        Properties testProps = parser.parseCommandLine(new String[] { "-t", "test" });

        // Check that the resultant properties contains the correctly parsed option.
        assertTrue("The test option was not equal to \"test\" but was: " + testProps.get("t"),
            "test".equals(testProps.get("t")));
    }

    /** Check that parsing a mandatory free argument works ok. */
    public void testParseMandatoryFreeArgumentOk() throws Exception
    {
        // Create a command line parser for a mandatory free argument.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "1", "Test Option.", "test", "true" }
                });

        // Parse a command line with this argument set.
        Properties testProps = parser.parseCommandLine(new String[] { "test" });

        // Check that the resultant properties contains the correctly parsed option.
        assertTrue("The first free argument was not equal to \"test\" but was: " + testProps.get("1"),
            "test".equals(testProps.get("1")));
    }

    /** Check that parsing a mandatory free argument fails when no argument is specified. */
    public void testParseManadatoryFreeArgumentFailsNoArgument() throws Exception
    {
        // Create a command line parser for a mandatory free argument.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "1", "Test Option.", "test", "true" }
                });

        // Check that parsing fails when this mandatory free argument is missing.
        boolean testPassed = false;

        try
        {
            // Parse a command line with this free argument not set.
            Properties testProps = parser.parseCommandLine(new String[] {});
        }
        catch (IllegalArgumentException e)
        {
            testPassed = true;
        }

        // Check that the resultant properties contains the correctly parsed option.
        assertTrue("IllegalArgumentException not thrown for a missing mandatory option.", testPassed);
    }

    /** Check that parsing a mandatory option fails when no option is set. */
    public void testParseMandatoryFailsNoOption() throws Exception
    {
        // Create a command line parser for a mandatory option.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "t", "Test Option.", "test", "true" }
                });

        // Check that parsing fails when this mandatory option is missing.
        boolean testPassed = false;

        try
        {
            // Parse a command line with this option not set.
            Properties testProps = parser.parseCommandLine(new String[] {});
        }
        catch (IllegalArgumentException e)
        {
            testPassed = true;
        }

        // Check that the resultant properties contains the correctly parsed option.
        assertTrue("IllegalArgumentException not thrown for a missing mandatory option.", testPassed);
    }

    /** Check that parsing an option with no space between it and its argument works ok. */
    public void testParseOptionWithNoSpaceOk() throws Exception
    {
        // Create a command line parser for an option.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "t", "Test Option.", "test" }
                });

        // Parse a command line with this option set with no space.
        Properties testProps = parser.parseCommandLine(new String[] { "-ttest" });

        // Check that the resultant properties contains the correctly parsed option.
        assertTrue("The test option was not equal to \"test\" but was: " + testProps.get("t"),
            "test".equals(testProps.get("t")));
    }

    /** Check that parsing an option with a space between it and its argument works ok. */
    public void testParseOptionWithSpaceOk() throws Exception
    {
        // Create a command line parser for an option.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "t", "Test Option.", "test" }
                });

        // Parse a command line with this option set with a space.
        Properties testProps = parser.parseCommandLine(new String[] { "-t", "test" });

        // Check that the resultant properties contains the correctly parsed option.
        assertTrue("The test option was not equal to \"test\" but was: " + testProps.get("t"),
            "test".equals(testProps.get("t")));
    }

    /** Check that parsing a single flag works ok. */
    public void testParseSingleFlagOk() throws Exception
    {
        // Create a command line parser for a single flag.
        CommandLineParser parser = new CommandLineParser(new String[][]
                {
                    { "t", "Test Flag." }
                });

        // Parse a command line with the single flag set.
        Properties testProps = parser.parseCommandLine(new String[] { "-t" });

        // Check that the flag is set in the parsed properties.
        assertTrue("The t flag was not \"true\", it was: " + testProps.get("t"), "true".equals(testProps.get("t")));

        // Reset the parser.
        parser.reset();

        // Parse a command line with the single flag not set.
        testProps = parser.parseCommandLine(new String[] {});

        // Check that the flag is cleared in the parsed properties.
        assertTrue("The t flag was not \"false\", it was: " + testProps.get("t"), "false".equals(testProps.get("t")));
    }

    /** Check that parsing an unknown option works when unknowns not errors. */
    public void testParseUnknownOptionOk() throws Exception
    {
        // Create a command line parser for no flags or options
        CommandLineParser parser = new CommandLineParser(new String[][] {});

        // Check that parsing does not fail on an unknown flag.
        try
        {
            parser.parseCommandLine(new String[] { "-t" });
        }
        catch (IllegalArgumentException e)
        {
            fail("The parser threw an IllegalArgumentException on an unknown flag when errors on unkowns is off.");
        }
    }

    /** Check that parsing an unknown flag fails when unknowns are to be reported as errors. */
    public void testParseUnknownFlagFailsWhenUnknownsAreErrors() throws Exception
    {
        // Create a command line parser for no flags or options
        CommandLineParser parser = new CommandLineParser(new String[][] {});

        // Turn on fail on unknowns mode.
        parser.setErrorsOnUnknowns(true);

        // Check that parsing fails on an unknown flag.
        boolean testPassed = false;

        try
        {
            parser.parseCommandLine(new String[] { "-t" });
        }
        catch (IllegalArgumentException e)
        {
            testPassed = true;
        }

        assertTrue("IllegalArgumentException not thrown for an unknown flag when errors on unknowns mode is on.",
            testPassed);
    }

    /** Check that parsing an unknown option fails when unknowns are to be reported as errors. */
    public void testParseUnknownOptionFailsWhenUnknownsAreErrors() throws Exception
    {
        // Create a command line parser for no flags or options
        CommandLineParser parser = new CommandLineParser(new String[][] {});

        // Turn on fail on unknowns mode.
        parser.setErrorsOnUnknowns(true);

        // Check that parsing fails on an unknown flag.
        boolean testPassed = false;

        try
        {
            parser.parseCommandLine(new String[] { "-t", "test" });
        }
        catch (IllegalArgumentException e)
        {
            testPassed = true;
        }

        assertTrue("IllegalArgumentException not thrown for an unknown option when errors on unknowns mode is on.",
            testPassed);
    }
}