From cf171b2763f6b4f2e079b144bac49d93cfdadb2b Mon Sep 17 00:00:00 2001 From: Sigee Date: Mon, 29 Jul 2024 11:36:53 +0200 Subject: [PATCH 1/2] Rewrite DefaultParserTest using parameterized tests --- .../apache/commons/cli/DefaultParserTest.java | 179 ++++++++++++------ 1 file changed, 122 insertions(+), 57 deletions(-) diff --git a/src/test/java/org/apache/commons/cli/DefaultParserTest.java b/src/test/java/org/apache/commons/cli/DefaultParserTest.java index 0b829e000..d24c8bcf7 100644 --- a/src/test/java/org/apache/commons/cli/DefaultParserTest.java +++ b/src/test/java/org/apache/commons/cli/DefaultParserTest.java @@ -23,14 +23,17 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; import org.apache.commons.cli.DefaultParser.Builder; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; -/** - * TODO Needs a rework using JUnit parameterized tests. - */ public class DefaultParserTest extends AbstractParserTestCase { @Override @@ -83,24 +86,126 @@ public void testDeprecated() throws ParseException { assertFalse(handler.contains(opt3)); } - @Test - public void testLongOptionQuoteHandlingWithoutStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get(); - final String[] args = {"--bfile", "\"quoted string\""}; - + @ParameterizedTest(name = "{index}. {0}") + @ArgumentsSource(ExternalArgumentsProvider.class) + public void testParameterized(final String testName, final CommandLineParser parser, final String[] args, final String expected, + final String option, final String message) throws Exception { final CommandLine cl = parser.parse(options, args); - assertEquals("\"quoted string\"", cl.getOptionValue("b"), "Confirm --bfile \"arg\" keeps quotes"); + assertEquals(expected, cl.getOptionValue(option), message); } - @Test - public void testLongOptionQuoteHandlingWithStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get(); - final String[] args = {"--bfile", "\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("quoted string", cl.getOptionValue("b"), "Confirm --bfile \"arg\" strips quotes"); + static class ExternalArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext context) { + return Stream.of( + /* Arguments: + * 1. test case name + * 2. parser + * 3. input string + * 4. expected option value + * 5. checked option + * 6. assertion message + */ + Arguments.of( + "Long option quote handling DEFAULT behavior", + DefaultParser.builder().get(), + new String[]{"--bfile", "\"quoted string\""}, + "quoted string", + "b", + "Confirm --bfile=\"arg\" strips quotes" + ), + Arguments.of( + "Long option with equals quote handling DEFAULT behavior", + DefaultParser.builder().get(), + new String[]{"--bfile=\"quoted string\""}, + "\"quoted string\"", + "b", + "Confirm --bfile=\"arg\" keeps quotes" + ), + Arguments.of( + "Short option quote handling DEFAULT behavior", + DefaultParser.builder().get(), + new String[]{"-b", "\"quoted string\""}, + "quoted string", + "b", + "Confirm -b\"arg\" strips quotes" + ), + Arguments.of( + "Short option concatenated quote handling DEFAULT behavior", + DefaultParser.builder().get(), + new String[]{"-b\"quoted string\""}, + "\"quoted string\"", + "b", + "Confirm -b\"arg\" keeps quotes" + ), + Arguments.of( + "Long option quote handling WITHOUT strip", + DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get(), + new String[]{"--bfile", "\"quoted string\""}, + "\"quoted string\"", + "b", + "Confirm --bfile \"arg\" keeps quotes" + ), + Arguments.of( + "Long option with equals quote handling WITHOUT strip", + DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get(), + new String[]{"--bfile=\"quoted string\""}, + "\"quoted string\"", + "b", + "Confirm --bfile=\"arg\" keeps quotes" + ), + Arguments.of( + "Short option quote handling WITHOUT strip", + DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get(), + new String[]{"-b", "\"quoted string\""}, + "\"quoted string\"", + "b", + "Confirm -b\"arg\" keeps quotes" + ), + Arguments.of( + "Short option concatenated quote handling WITHOUT strip", + DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get(), + new String[]{"-b\"quoted string\""}, + "\"quoted string\"", + "b", + "Confirm -b\"arg\" keeps quotes" + ), + Arguments.of( + "Long option quote handling WITH strip", + DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get(), + new String[]{"--bfile", "\"quoted string\""}, + "quoted string", + "b", + "Confirm --bfile \"arg\" strips quotes" + ), + Arguments.of( + "Long option With Equals Quote Handling WITH Strip", + DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get(), + new String[]{"--bfile=\"quoted string\""}, + "quoted string", + "b", + "Confirm --bfile=\"arg\" strips quotes" + ), + Arguments.of( + "Short option quote handling WITH strip", + DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get(), + new String[]{"-b", "\"quoted string\""}, + "quoted string", + "b", + "Confirm -b \"arg\" strips quotes" + ), + Arguments.of( + "Short option concatenated quote handling WITH strip", + DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get(), + new String[]{"-b\"quoted string\""}, + "quoted string", + "b", + "Confirm -b\"arg\" strips quotes" + ) + ); + } } @Override @@ -113,26 +218,6 @@ public void testLongOptionWithEqualsQuoteHandling() throws Exception { assertEquals("\"quoted string\"", cl.getOptionValue("b"), "Confirm --bfile=\"arg\" strips quotes"); } - @Test - public void testLongOptionWithEqualsQuoteHandlingWithoutStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get(); - final String[] args = {"--bfile=\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("\"quoted string\"", cl.getOptionValue("b"), "Confirm --bfile=\"arg\" keeps quotes"); - } - - @Test - public void testLongOptionWithEqualsQuoteHandlingWithStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get(); - final String[] args = {"--bfile=\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("quoted string", cl.getOptionValue("b"), "Confirm --bfile=\"arg\" strips quotes"); - } - @Override @Test public void testShortOptionConcatenatedQuoteHandling() throws Exception { @@ -143,24 +228,4 @@ public void testShortOptionConcatenatedQuoteHandling() throws Exception { //This is behavior is not consistent with the other parsers, but is required for backwards compatibility assertEquals("\"quoted string\"", cl.getOptionValue("b"), "Confirm -b\"arg\" keeps quotes"); } - - @Test - public void testShortOptionQuoteHandlingWithoutStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get(); - final String[] args = {"-b", "\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("\"quoted string\"", cl.getOptionValue("b"), "Confirm -b \"arg\" keeps quotes"); - } - - @Test - public void testShortOptionQuoteHandlingWithStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get(); - final String[] args = {"-b", "\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("quoted string", cl.getOptionValue("b"), "Confirm -b \"arg\" strips quotes"); - } } From 71d3cd31077c977795c33315411170651040ed6d Mon Sep 17 00:00:00 2001 From: Sigee Date: Mon, 29 Jul 2024 13:20:58 +0200 Subject: [PATCH 2/2] Disable test cases from parent --- .../org/apache/commons/cli/DefaultParserTest.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/test/java/org/apache/commons/cli/DefaultParserTest.java b/src/test/java/org/apache/commons/cli/DefaultParserTest.java index d24c8bcf7..2f1272ca5 100644 --- a/src/test/java/org/apache/commons/cli/DefaultParserTest.java +++ b/src/test/java/org/apache/commons/cli/DefaultParserTest.java @@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.commons.cli.DefaultParser.Builder; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.ParameterizedTest; @@ -210,22 +211,13 @@ public Stream provideArguments(final ExtensionContext conte @Override @Test + @Disabled("Test case handled in the parameterized tests as \"DEFAULT behavior\"") public void testLongOptionWithEqualsQuoteHandling() throws Exception { - final String[] args = {"--bfile=\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("\"quoted string\"", cl.getOptionValue("b"), "Confirm --bfile=\"arg\" strips quotes"); } @Override @Test + @Disabled("Test case handled in the parameterized tests as \"DEFAULT behavior\"") public void testShortOptionConcatenatedQuoteHandling() throws Exception { - final String[] args = {"-b\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - //This is behavior is not consistent with the other parsers, but is required for backwards compatibility - assertEquals("\"quoted string\"", cl.getOptionValue("b"), "Confirm -b\"arg\" keeps quotes"); } }