View Javadoc
1   /*
2     Licensed to the Apache Software Foundation (ASF) under one or more
3     contributor license agreements.  See the NOTICE file distributed with
4     this work for additional information regarding copyright ownership.
5     The ASF licenses this file to You under the Apache License, Version 2.0
6     (the "License"); you may not use this file except in compliance with
7     the License.  You may obtain a copy of the License at
8   
9         http://www.apache.org/licenses/LICENSE-2.0
10  
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
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         //This is behavior is not consistent with the other parsers, but is required for backwards compatibility
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 }