@@ -20,8 +20,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2020import static org .junit .jupiter .api .Assertions .assertEquals ;
2121import static org .junit .jupiter .api .Assertions .assertFalse ;
2222import static org .junit .jupiter .api .Assertions .assertNotNull ;
23+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2324import static org .junit .jupiter .api .Assertions .assertTrue ;
24- import static org .junit .jupiter .api .Assertions .fail ;
2525
2626import java .util .Properties ;
2727
@@ -30,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3030
3131@ SuppressWarnings ("deprecation" ) // tests some deprecated classes
3232public class OptionGroupTest {
33+
3334 private Options options ;
3435 private final Parser parser = new PosixParser ();
3536
@@ -64,10 +65,8 @@ public void setUp() {
6465 public void testGetNames () {
6566 final OptionGroup group = new OptionGroup ();
6667 assertFalse (group .isSelected ());
67-
6868 group .addOption (OptionBuilder .create ('a' ));
6969 group .addOption (OptionBuilder .create ('b' ));
70-
7170 assertNotNull (group .getNames (), "null names" );
7271 assertEquals (2 , group .getNames ().size ());
7372 assertTrue (group .getNames ().contains ("a" ));
@@ -77,9 +76,7 @@ public void testGetNames() {
7776 @ Test
7877 public void testNoOptionsExtraArgs () throws Exception {
7978 final String [] args = {"arg1" , "arg2" };
80-
8179 final CommandLine cl = parser .parse (options , args );
82-
8380 assertFalse (cl .hasOption ("r" ), "Confirm -r is NOT set" );
8481 assertFalse (cl .hasOption ("f" ), "Confirm -f is NOT set" );
8582 assertFalse (cl .hasOption ("d" ), "Confirm -d is NOT set" );
@@ -91,9 +88,7 @@ public void testNoOptionsExtraArgs() throws Exception {
9188 @ Test
9289 public void testSingleLongOption () throws Exception {
9390 final String [] args = {"--file" };
94-
9591 final CommandLine cl = parser .parse (options , args );
96-
9792 assertFalse (cl .hasOption ("r" ), "Confirm -r is NOT set" );
9893 assertTrue (cl .hasOption ("f" ), "Confirm -f is set" );
9994 assertFalse (cl .hasOption ("d" ), "Confirm -d is NOT set" );
@@ -105,9 +100,7 @@ public void testSingleLongOption() throws Exception {
105100 @ Test
106101 public void testSingleOption () throws Exception {
107102 final String [] args = {"-r" };
108-
109103 final CommandLine cl = parser .parse (options , args );
110-
111104 assertTrue (cl .hasOption ("r" ), "Confirm -r is set" );
112105 assertFalse (cl .hasOption ("f" ), "Confirm -f is NOT set" );
113106 assertFalse (cl .hasOption ("d" ), "Confirm -d is NOT set" );
@@ -119,9 +112,7 @@ public void testSingleOption() throws Exception {
119112 @ Test
120113 public void testSingleOptionFromGroup () throws Exception {
121114 final String [] args = {"-f" };
122-
123115 final CommandLine cl = parser .parse (options , args );
124-
125116 assertFalse (cl .hasOption ("r" ), "Confirm -r is NOT set" );
126117 assertTrue (cl .hasOption ("f" ), "Confirm -f is set" );
127118 assertFalse (cl .hasOption ("d" ), "Confirm -d is NOT set" );
@@ -135,39 +126,30 @@ public void testToString() {
135126 final OptionGroup group1 = new OptionGroup ();
136127 group1 .addOption (new Option (null , "foo" , false , "Foo" ));
137128 group1 .addOption (new Option (null , "bar" , false , "Bar" ));
138-
139129 if (!"[--bar Bar, --foo Foo]" .equals (group1 .toString ())) {
140130 assertEquals ("[--foo Foo, --bar Bar]" , group1 .toString ());
141131 }
142-
143132 final OptionGroup group2 = new OptionGroup ();
144133 group2 .addOption (new Option ("f" , "foo" , false , "Foo" ));
145134 group2 .addOption (new Option ("b" , "bar" , false , "Bar" ));
146-
147135 if (!"[-b Bar, -f Foo]" .equals (group2 .toString ())) {
148136 assertEquals ("[-f Foo, -b Bar]" , group2 .toString ());
149137 }
150138 }
151139
152140 @ Test
153141 public void testTwoLongOptionsFromGroup () throws Exception {
154- final String [] args = {"--file" , "--directory" };
155-
156- try {
157- parser .parse (options , args );
158- fail ("two arguments from group not allowed" );
159- } catch (final AlreadySelectedException e ) {
160- assertNotNull (e .getOptionGroup (), "null option group" );
161- assertTrue (e .getOptionGroup ().isSelected ());
162- assertEquals ("f" , e .getOptionGroup ().getSelected (), "selected option" );
163- assertEquals ("d" , e .getOption ().getOpt (), "option" );
164- }
142+ final String [] args = { "--file" , "--directory" };
143+ final AlreadySelectedException e = assertThrows (AlreadySelectedException .class , () -> parser .parse (options , args ));
144+ assertNotNull (e .getOptionGroup (), "null option group" );
145+ assertTrue (e .getOptionGroup ().isSelected ());
146+ assertEquals ("f" , e .getOptionGroup ().getSelected (), "selected option" );
147+ assertEquals ("d" , e .getOption ().getOpt (), "option" );
165148 }
166149
167150 @ Test
168151 public void testTwoOptionsFromDifferentGroup () throws Exception {
169152 final String [] args = {"-f" , "-s" };
170-
171153 final CommandLine cl = parser .parse (options , args );
172154 assertFalse (cl .hasOption ("r" ), "Confirm -r is NOT set" );
173155 assertTrue (cl .hasOption ("f" ), "Confirm -f is set" );
@@ -179,26 +161,19 @@ public void testTwoOptionsFromDifferentGroup() throws Exception {
179161
180162 @ Test
181163 public void testTwoOptionsFromGroup () throws Exception {
182- final String [] args = {"-f" , "-d" };
183-
184- try {
185- parser .parse (options , args );
186- fail ("two arguments from group not allowed" );
187- } catch (final AlreadySelectedException e ) {
188- assertNotNull (e .getOptionGroup (), "null option group" );
189- assertTrue (e .getOptionGroup ().isSelected ());
190- assertEquals ("f" , e .getOptionGroup ().getSelected (), "selected option" );
191- assertEquals ("d" , e .getOption ().getOpt (), "option" );
192- }
164+ final String [] args = { "-f" , "-d" };
165+ final AlreadySelectedException e = assertThrows (AlreadySelectedException .class , () -> parser .parse (options , args ));
166+ assertNotNull (e .getOptionGroup (), "null option group" );
167+ assertTrue (e .getOptionGroup ().isSelected ());
168+ assertEquals ("f" , e .getOptionGroup ().getSelected (), "selected option" );
169+ assertEquals ("d" , e .getOption ().getOpt (), "option" );
193170 }
194171
195172 @ Test
196173 public void testTwoOptionsFromGroupWithProperties () throws Exception {
197174 final String [] args = {"-f" };
198-
199175 final Properties properties = new Properties ();
200176 properties .put ("d" , "true" );
201-
202177 final CommandLine cl = parser .parse (options , args , properties );
203178 assertTrue (cl .hasOption ("f" ));
204179 assertFalse (cl .hasOption ("d" ));
@@ -207,9 +182,7 @@ public void testTwoOptionsFromGroupWithProperties() throws Exception {
207182 @ Test
208183 public void testTwoValidLongOptions () throws Exception {
209184 final String [] args = {"--revision" , "--file" };
210-
211185 final CommandLine cl = parser .parse (options , args );
212-
213186 assertTrue (cl .hasOption ("r" ), "Confirm -r is set" );
214187 assertTrue (cl .hasOption ("f" ), "Confirm -f is set" );
215188 assertFalse (cl .hasOption ("d" ), "Confirm -d is NOT set" );
@@ -221,9 +194,7 @@ public void testTwoValidLongOptions() throws Exception {
221194 @ Test
222195 public void testTwoValidOptions () throws Exception {
223196 final String [] args = {"-r" , "-f" };
224-
225197 final CommandLine cl = parser .parse (options , args );
226-
227198 assertTrue (cl .hasOption ("r" ), "Confirm -r is set" );
228199 assertTrue (cl .hasOption ("f" ), "Confirm -f is set" );
229200 assertFalse (cl .hasOption ("d" ), "Confirm -d is NOT set" );
@@ -236,7 +207,6 @@ public void testTwoValidOptions() throws Exception {
236207 public void testValidLongOnlyOptions () throws Exception {
237208 final CommandLine cl1 = parser .parse (options , new String [] {"--export" });
238209 assertTrue (cl1 .hasOption ("export" ), "Confirm --export is set" );
239-
240210 final CommandLine cl2 = parser .parse (options , new String [] {"--import" });
241211 assertTrue (cl2 .hasOption ("import" ), "Confirm --import is set" );
242212 }
0 commit comments