• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.beust.jcommander;
2 
3 import org.testng.Assert;
4 import org.testng.annotations.DataProvider;
5 import org.testng.annotations.Test;
6 
7 import java.util.Arrays;
8 import java.util.LinkedList;
9 import java.util.List;
10 
11 public class CmdTest {
12 
13     @Parameters(commandNames = "--cmd-one")
14     public static class CmdOne {
15     }
16 
17     @Parameters(commandNames = "--cmd-two")
18     class CmdTwo {
19         @Parameter
20         List<String> params = new java.util.LinkedList<String>();
21     }
22 
parseArgs(boolean withDefault, String[] args)23     public String parseArgs(boolean withDefault, String[] args) {
24         JCommander jc = new JCommander();
25         jc.addCommand(new CmdOne());
26         jc.addCommand(new CmdTwo());
27 
28         if (withDefault) {
29             // First check if a command was given, when not prepend default
30             // command (--cmd-two")
31             // In version up to 1.23 JCommander throws an Exception in this
32             // line,
33             // which might be incorrect, at least its not reasonable if the
34             // method
35             // is named "WithoutValidation".
36             jc.parseWithoutValidation(args);
37             if (jc.getParsedCommand() == null) {
38                 LinkedList<String> newArgs = new LinkedList<String>();
39                 newArgs.add("--cmd-two");
40                 newArgs.addAll(Arrays.asList(args));
41                 jc.parse(newArgs.toArray(new String[0]));
42             }
43         } else {
44             jc.parse(args);
45         }
46         return jc.getParsedCommand();
47     }
48 
49     @DataProvider
testData()50     public Object[][] testData() {
51         return new Object[][] {
52                 new Object[] { "--cmd-one", false, new String[] { "--cmd-one" } },
53                 new Object[] { "--cmd-two", false, new String[] { "--cmd-two" } },
54                 new Object[] { "--cmd-two", false,
55                         new String[] { "--cmd-two", "param1", "param2" } },
56                 // This is the relevant test case to test default commands
57                 new Object[] { "--cmd-two", true,
58                         new String[] { "param1", "param2" } } };
59     }
60 
61     @Test(dataProvider = "testData")
testArgsWithoutDefaultCmd(String expected, boolean requireDefault, String[] args)62     public void testArgsWithoutDefaultCmd(String expected,
63             boolean requireDefault, String[] args) {
64         if (!requireDefault) {
65             Assert.assertEquals(parseArgs(false, args), expected);
66         }
67     }
68 
69     @Test(dataProvider = "testData", expectedExceptions = MissingCommandException.class)
testArgsWithoutDefaultCmdFail(String expected, boolean requireDefault, String[] args)70     public void testArgsWithoutDefaultCmdFail(String expected,
71             boolean requireDefault, String[] args) {
72         if (requireDefault) {
73             parseArgs(false, args);
74         } else {
75             throw new MissingCommandException("irrelevant test case");
76         }
77     }
78 
79     // We do not expect a MissingCommandException!
80     @Test(dataProvider = "testData")
testArgsWithDefaultCmd(String expected, boolean requireDefault, String[] args)81     public void testArgsWithDefaultCmd(String expected, boolean requireDefault,
82             String[] args) {
83         Assert.assertEquals(parseArgs(true, args), expected);
84     }
85 
86 }