1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.cli;
19
20 import static org.junit.Assert.assertEquals;
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 public class DefaultParserTest extends AbstractParserTestCase {
26
27 @Override
28 @Before
29 public void setUp() {
30 super.setUp();
31 parser = new DefaultParser();
32 }
33
34 @Test
35 public void testBuilder() {
36 parser = DefaultParser.builder()
37 .setStripLeadingAndTrailingQuotes(false)
38 .setAllowPartialMatching(false)
39 .build();
40 assertEquals(DefaultParser.class, parser.getClass());
41 }
42
43 @Test
44 public void testLongOptionQuoteHandlingWithoutStrip() throws Exception {
45 parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build();
46 final String[] args = {"--bfile", "\"quoted string\""};
47
48 final CommandLine cl = parser.parse(options, args);
49
50 assertEquals("Confirm --bfile \"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b"));
51 }
52
53 @Test
54 public void testLongOptionQuoteHandlingWithStrip() throws Exception {
55 parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build();
56 final String[] args = {"--bfile", "\"quoted string\""};
57
58 final CommandLine cl = parser.parse(options, args);
59
60 assertEquals("Confirm --bfile \"arg\" strips quotes", "quoted string", cl.getOptionValue("b"));
61 }
62
63 @Override
64 @Test
65 public void testLongOptionWithEqualsQuoteHandling() throws Exception {
66 final String[] args = {"--bfile=\"quoted string\""};
67
68 final CommandLine cl = parser.parse(options, args);
69
70 assertEquals("Confirm --bfile=\"arg\" strips quotes", "\"quoted string\"", cl.getOptionValue("b"));
71 }
72
73 @Test
74 public void testLongOptionWithEqualsQuoteHandlingWithoutStrip() throws Exception {
75 parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build();
76 final String[] args = {"--bfile=\"quoted string\""};
77
78 final CommandLine cl = parser.parse(options, args);
79
80 assertEquals("Confirm --bfile=\"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b"));
81 }
82
83 @Test
84 public void testLongOptionWithEqualsQuoteHandlingWithStrip() throws Exception {
85 parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build();
86 final String[] args = {"--bfile=\"quoted string\""};
87
88 final CommandLine cl = parser.parse(options, args);
89
90 assertEquals("Confirm --bfile=\"arg\" strips quotes", "quoted string", cl.getOptionValue("b"));
91 }
92
93 @Override
94 @Test
95 public void testShortOptionConcatenatedQuoteHandling() throws Exception {
96 final String[] args = {"-b\"quoted string\""};
97
98 final CommandLine cl = parser.parse(options, args);
99
100
101 assertEquals("Confirm -b\"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b"));
102 }
103
104 @Test
105 public void testShortOptionQuoteHandlingWithoutStrip() throws Exception {
106 parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build();
107 final String[] args = {"-b", "\"quoted string\""};
108
109 final CommandLine cl = parser.parse(options, args);
110
111 assertEquals("Confirm -b \"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b"));
112 }
113
114 @Test
115 public void testShortOptionQuoteHandlingWithStrip() throws Exception {
116 parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build();
117 final String[] args = {"-b", "\"quoted string\""};
118
119 final CommandLine cl = parser.parse(options, args);
120
121 assertEquals("Confirm -b \"arg\" strips quotes", "quoted string", cl.getOptionValue("b"));
122 }
123 }