@@ -23,14 +23,18 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2323
2424import java .util .HashSet ;
2525import java .util .Set ;
26+ import java .util .stream .Stream ;
2627
2728import org .apache .commons .cli .DefaultParser .Builder ;
2829import org .junit .jupiter .api .BeforeEach ;
30+ import org .junit .jupiter .api .Disabled ;
2931import org .junit .jupiter .api .Test ;
32+ import org .junit .jupiter .api .extension .ExtensionContext ;
33+ import org .junit .jupiter .params .ParameterizedTest ;
34+ import org .junit .jupiter .params .provider .Arguments ;
35+ import org .junit .jupiter .params .provider .ArgumentsProvider ;
36+ import org .junit .jupiter .params .provider .ArgumentsSource ;
3037
31- /**
32- * TODO Needs a rework using JUnit parameterized tests.
33- */
3438public class DefaultParserTest extends AbstractParserTestCase {
3539
3640 @ Override
@@ -83,84 +87,137 @@ public void testDeprecated() throws ParseException {
8387 assertFalse (handler .contains (opt3 ));
8488 }
8589
86- @ Test
87- public void testLongOptionQuoteHandlingWithoutStrip () throws Exception {
88- parser = DefaultParser .builder ().setStripLeadingAndTrailingQuotes (false ).get ();
89- final String [] args = {"--bfile" , "\" quoted string\" " };
90-
90+ @ ParameterizedTest (name = "{index}. {0}" )
91+ @ ArgumentsSource (ExternalArgumentsProvider .class )
92+ public void testParameterized (final String testName , final CommandLineParser parser , final String [] args , final String expected ,
93+ final String option , final String message ) throws Exception {
9194 final CommandLine cl = parser .parse (options , args );
9295
93- assertEquals (" \" quoted string \" " , cl .getOptionValue ("b" ), "Confirm --bfile \" arg \" keeps quotes" );
96+ assertEquals (expected , cl .getOptionValue (option ), message );
9497 }
9598
96- @ Test
97- public void testLongOptionQuoteHandlingWithStrip () throws Exception {
98- parser = DefaultParser .builder ().setStripLeadingAndTrailingQuotes (true ).get ();
99- final String [] args = {"--bfile" , "\" quoted string\" " };
100-
101- final CommandLine cl = parser .parse (options , args );
102-
103- assertEquals ("quoted string" , cl .getOptionValue ("b" ), "Confirm --bfile \" arg\" strips quotes" );
99+ static class ExternalArgumentsProvider implements ArgumentsProvider {
100+
101+ @ Override
102+ public Stream <? extends Arguments > provideArguments (final ExtensionContext context ) {
103+ return Stream .of (
104+ /* Arguments:
105+ * 1. test case name
106+ * 2. parser
107+ * 3. input string
108+ * 4. expected option value
109+ * 5. checked option
110+ * 6. assertion message
111+ */
112+ Arguments .of (
113+ "Long option quote handling DEFAULT behavior" ,
114+ DefaultParser .builder ().get (),
115+ new String []{"--bfile" , "\" quoted string\" " },
116+ "quoted string" ,
117+ "b" ,
118+ "Confirm --bfile=\" arg\" strips quotes"
119+ ),
120+ Arguments .of (
121+ "Long option with equals quote handling DEFAULT behavior" ,
122+ DefaultParser .builder ().get (),
123+ new String []{"--bfile=\" quoted string\" " },
124+ "\" quoted string\" " ,
125+ "b" ,
126+ "Confirm --bfile=\" arg\" keeps quotes"
127+ ),
128+ Arguments .of (
129+ "Short option quote handling DEFAULT behavior" ,
130+ DefaultParser .builder ().get (),
131+ new String []{"-b" , "\" quoted string\" " },
132+ "quoted string" ,
133+ "b" ,
134+ "Confirm -b\" arg\" strips quotes"
135+ ),
136+ Arguments .of (
137+ "Short option concatenated quote handling DEFAULT behavior" ,
138+ DefaultParser .builder ().get (),
139+ new String []{"-b\" quoted string\" " },
140+ "\" quoted string\" " ,
141+ "b" ,
142+ "Confirm -b\" arg\" keeps quotes"
143+ ),
144+ Arguments .of (
145+ "Long option quote handling WITHOUT strip" ,
146+ DefaultParser .builder ().setStripLeadingAndTrailingQuotes (false ).get (),
147+ new String []{"--bfile" , "\" quoted string\" " },
148+ "\" quoted string\" " ,
149+ "b" ,
150+ "Confirm --bfile \" arg\" keeps quotes"
151+ ),
152+ Arguments .of (
153+ "Long option with equals quote handling WITHOUT strip" ,
154+ DefaultParser .builder ().setStripLeadingAndTrailingQuotes (false ).get (),
155+ new String []{"--bfile=\" quoted string\" " },
156+ "\" quoted string\" " ,
157+ "b" ,
158+ "Confirm --bfile=\" arg\" keeps quotes"
159+ ),
160+ Arguments .of (
161+ "Short option quote handling WITHOUT strip" ,
162+ DefaultParser .builder ().setStripLeadingAndTrailingQuotes (false ).get (),
163+ new String []{"-b" , "\" quoted string\" " },
164+ "\" quoted string\" " ,
165+ "b" ,
166+ "Confirm -b\" arg\" keeps quotes"
167+ ),
168+ Arguments .of (
169+ "Short option concatenated quote handling WITHOUT strip" ,
170+ DefaultParser .builder ().setStripLeadingAndTrailingQuotes (false ).get (),
171+ new String []{"-b\" quoted string\" " },
172+ "\" quoted string\" " ,
173+ "b" ,
174+ "Confirm -b\" arg\" keeps quotes"
175+ ),
176+ Arguments .of (
177+ "Long option quote handling WITH strip" ,
178+ DefaultParser .builder ().setStripLeadingAndTrailingQuotes (true ).get (),
179+ new String []{"--bfile" , "\" quoted string\" " },
180+ "quoted string" ,
181+ "b" ,
182+ "Confirm --bfile \" arg\" strips quotes"
183+ ),
184+ Arguments .of (
185+ "Long option With Equals Quote Handling WITH Strip" ,
186+ DefaultParser .builder ().setStripLeadingAndTrailingQuotes (true ).get (),
187+ new String []{"--bfile=\" quoted string\" " },
188+ "quoted string" ,
189+ "b" ,
190+ "Confirm --bfile=\" arg\" strips quotes"
191+ ),
192+ Arguments .of (
193+ "Short option quote handling WITH strip" ,
194+ DefaultParser .builder ().setStripLeadingAndTrailingQuotes (true ).get (),
195+ new String []{"-b" , "\" quoted string\" " },
196+ "quoted string" ,
197+ "b" ,
198+ "Confirm -b \" arg\" strips quotes"
199+ ),
200+ Arguments .of (
201+ "Short option concatenated quote handling WITH strip" ,
202+ DefaultParser .builder ().setStripLeadingAndTrailingQuotes (true ).get (),
203+ new String []{"-b\" quoted string\" " },
204+ "quoted string" ,
205+ "b" ,
206+ "Confirm -b\" arg\" strips quotes"
207+ )
208+ );
209+ }
104210 }
105211
106212 @ Override
107213 @ Test
214+ @ Disabled ("Test case handled in the parameterized tests as \" DEFAULT behavior\" " )
108215 public void testLongOptionWithEqualsQuoteHandling () throws Exception {
109- final String [] args = {"--bfile=\" quoted string\" " };
110-
111- final CommandLine cl = parser .parse (options , args );
112-
113- assertEquals ("\" quoted string\" " , cl .getOptionValue ("b" ), "Confirm --bfile=\" arg\" strips quotes" );
114- }
115-
116- @ Test
117- public void testLongOptionWithEqualsQuoteHandlingWithoutStrip () throws Exception {
118- parser = DefaultParser .builder ().setStripLeadingAndTrailingQuotes (false ).get ();
119- final String [] args = {"--bfile=\" quoted string\" " };
120-
121- final CommandLine cl = parser .parse (options , args );
122-
123- assertEquals ("\" quoted string\" " , cl .getOptionValue ("b" ), "Confirm --bfile=\" arg\" keeps quotes" );
124- }
125-
126- @ Test
127- public void testLongOptionWithEqualsQuoteHandlingWithStrip () throws Exception {
128- parser = DefaultParser .builder ().setStripLeadingAndTrailingQuotes (true ).get ();
129- final String [] args = {"--bfile=\" quoted string\" " };
130-
131- final CommandLine cl = parser .parse (options , args );
132-
133- assertEquals ("quoted string" , cl .getOptionValue ("b" ), "Confirm --bfile=\" arg\" strips quotes" );
134216 }
135217
136218 @ Override
137219 @ Test
220+ @ Disabled ("Test case handled in the parameterized tests as \" DEFAULT behavior\" " )
138221 public void testShortOptionConcatenatedQuoteHandling () throws Exception {
139- final String [] args = {"-b\" quoted string\" " };
140-
141- final CommandLine cl = parser .parse (options , args );
142-
143- //This is behavior is not consistent with the other parsers, but is required for backwards compatibility
144- assertEquals ("\" quoted string\" " , cl .getOptionValue ("b" ), "Confirm -b\" arg\" keeps quotes" );
145- }
146-
147- @ Test
148- public void testShortOptionQuoteHandlingWithoutStrip () throws Exception {
149- parser = DefaultParser .builder ().setStripLeadingAndTrailingQuotes (false ).get ();
150- final String [] args = {"-b" , "\" quoted string\" " };
151-
152- final CommandLine cl = parser .parse (options , args );
153-
154- assertEquals ("\" quoted string\" " , cl .getOptionValue ("b" ), "Confirm -b \" arg\" keeps quotes" );
155- }
156-
157- @ Test
158- public void testShortOptionQuoteHandlingWithStrip () throws Exception {
159- parser = DefaultParser .builder ().setStripLeadingAndTrailingQuotes (true ).get ();
160- final String [] args = {"-b" , "\" quoted string\" " };
161-
162- final CommandLine cl = parser .parse (options , args );
163-
164- assertEquals ("quoted string" , cl .getOptionValue ("b" ), "Confirm -b \" arg\" strips quotes" );
165222 }
166223}
0 commit comments