• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.beust.jcommander;
2 
3 import com.beust.jcommander.JCommanderTest.BaseArgs;
4 import com.beust.jcommander.JCommanderTest.ConfigureArgs;
5 
6 import org.testng.Assert;
7 import org.testng.annotations.Test;
8 
9 @Test
10 public class FinderTest {
caseInsensitiveOption()11   public void caseInsensitiveOption() {
12     class Arg {
13 
14       @Parameter(names = { "-p", "--param" })
15       private String param;
16     }
17     Arg a = new Arg();
18     JCommander jc = new JCommander(a);
19     jc.setCaseSensitiveOptions(false);
20     jc.parse(new String[] { "--PARAM", "foo" });
21     Assert.assertEquals(a.param, "foo");
22   }
23 
caseInsensitiveCommand()24   public void caseInsensitiveCommand() {
25     BaseArgs a = new BaseArgs();
26     ConfigureArgs conf = new ConfigureArgs();
27     JCommander jc = new JCommander(a);
28     jc.addCommand(conf);
29     jc.setCaseSensitiveOptions(false);
30 //    jc.setCaseSensitiveCommands(false);
31     jc.parse("--CONFIGURE");
32     String command = jc.getParsedCommand();
33     Assert.assertEquals(command, "--configure");
34   }
35 
abbreviatedOptions()36   public void abbreviatedOptions() {
37     class Arg {
38       @Parameter(names = { "-p", "--param" })
39       private String param;
40     }
41     Arg a = new Arg();
42     JCommander jc = new JCommander(a);
43     jc.setAllowAbbreviatedOptions(true);
44     jc.parse(new String[] { "--par", "foo" });
45     Assert.assertEquals(a.param, "foo");
46   }
47 
abbreviatedOptionsCaseInsensitive()48   public void abbreviatedOptionsCaseInsensitive() {
49     class Arg {
50       @Parameter(names = { "-p", "--param" })
51       private String param;
52     }
53     Arg a = new Arg();
54     JCommander jc = new JCommander(a);
55     jc.setCaseSensitiveOptions(false);
56     jc.setAllowAbbreviatedOptions(true);
57     jc.parse(new String[] { "--PAR", "foo" });
58     Assert.assertEquals(a.param, "foo");
59   }
60 
61   @Test(expectedExceptions = ParameterException.class)
ambiguousAbbreviatedOptions()62   public void ambiguousAbbreviatedOptions() {
63     class Arg {
64       @Parameter(names = { "--param" })
65       private String param;
66       @Parameter(names = { "--parb" })
67       private String parb;
68     }
69     Arg a = new Arg();
70     JCommander jc = new JCommander(a);
71     jc.setAllowAbbreviatedOptions(true);
72     jc.parse(new String[] { "--par", "foo" });
73     Assert.assertEquals(a.param, "foo");
74   }
75 
76   @Test(expectedExceptions = ParameterException.class)
ambiguousAbbreviatedOptionsCaseInsensitive()77   public void ambiguousAbbreviatedOptionsCaseInsensitive() {
78     class Arg {
79       @Parameter(names = { "--param" })
80       private String param;
81       @Parameter(names = { "--parb" })
82       private String parb;
83     }
84     Arg a = new Arg();
85     JCommander jc = new JCommander(a);
86     jc.setCaseSensitiveOptions(false);
87     jc.setAllowAbbreviatedOptions(true);
88     jc.parse(new String[] { "--PAR", "foo" });
89     Assert.assertEquals(a.param, "foo");
90   }
91 
92   @Test(enabled = false)
main(String[] args)93   public static void main(String[] args) throws Exception {
94     new FinderTest().ambiguousAbbreviatedOptionsCaseInsensitive();
95   }
96 
97 }
98