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  package org.apache.commons.cli2.commandline;
18  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.PrintWriter;
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  
25  import org.apache.commons.cli2.CommandLine;
26  import org.apache.commons.cli2.Group;
27  import org.apache.commons.cli2.OptionException;
28  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
29  import org.apache.commons.cli2.builder.GroupBuilder;
30  import org.apache.commons.cli2.option.DefaultOption;
31  import org.apache.commons.cli2.util.HelpFormatter;
32  
33  import junit.framework.TestCase;
34  
35  public class ParserTest extends TestCase {
36      
37      private Parser parser;
38      private DefaultOption verboseOption;
39      private DefaultOption helpOption;
40      private Group options;
41      private HelpFormatter helpFormatter;
42      private StringWriter out;
43      private BufferedReader in;
44  
45      public void setUp() {
46          parser = new Parser();
47          
48          final GroupBuilder gBuilder = new GroupBuilder();
49          final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
50          
51          helpOption = oBuilder.withLongName("help").withShortName("h").create();
52          verboseOption = oBuilder.withLongName("verbose").withShortName("v").create();
53          options = gBuilder.withOption(helpOption).withOption(verboseOption).create();
54          parser.setGroup(options);
55          
56          helpFormatter = new HelpFormatter();
57          out = new StringWriter();
58          helpFormatter.setPrintWriter(new PrintWriter(out));
59          parser.setHelpFormatter(helpFormatter);
60      }
61  
62      public void testParse_Successful() throws OptionException {
63          final CommandLine cl = parser.parse(new String[]{"-hv"});
64          
65          assertTrue(cl.hasOption(helpOption));
66          assertTrue(cl.hasOption(verboseOption));
67          
68          assertEquals("--help --verbose",cl.toString());
69          
70          final WriteableCommandLineImpl wcli = (WriteableCommandLineImpl)cl;
71          assertEquals("[--help, --verbose]",wcli.getNormalised().toString());
72      }
73  
74      public void testParse_WithUnexpectedOption() {
75          try {
76              parser.parse(new String[]{"--unexpected"});
77              fail("OptionException");
78          }
79          catch(OptionException e) {
80              assertEquals(options,e.getOption());
81              assertEquals("Unexpected --unexpected while processing --help|--verbose",e.getMessage());
82          }
83      }
84  
85      public void testParse_WithUnexpectedShortOption() {
86          try {
87              parser.parse(new String[]{"-vx"});
88              fail("OptionException");
89          }
90          catch(OptionException e) {
91              assertEquals(options,e.getOption());
92              assertEquals("Unexpected -x while processing --help|--verbose",e.getMessage());
93          }
94      }
95  
96      public void testParseAndHelp_Successful() throws IOException {
97          final CommandLine cl = parser.parseAndHelp(new String[]{"-v"});
98          
99          assertTrue(cl.hasOption(verboseOption));
100         assertEquals("",out.getBuffer().toString());
101     }
102 
103     public void testParseAndHelp_ByHelpOption() throws IOException {
104         parser.setHelpOption(helpOption);
105         
106         assertNull(parser.parseAndHelp(new String[]{"-hv"}));
107         
108         inReader();
109         assertInReaderUsage();
110         assertInReaderEOF();
111     }
112 
113     public void testParseAndHelp_ByHelpTrigger() throws IOException {
114         parser.setHelpTrigger("--help");
115         
116         assertNull(parser.parseAndHelp(new String[]{"-hv"}));
117         
118         inReader();
119         assertInReaderUsage();
120         assertInReaderEOF();
121     }
122 
123     public void testParseAndHelp_WithUnexpectedOption() throws IOException {
124         assertNull(parser.parseAndHelp(new String[]{"--unexpected"}));
125         
126         inReader();
127         assertInReaderLine("Unexpected --unexpected while processing --help|--verbose");
128         assertInReaderUsage();
129         assertInReaderEOF();
130     }
131 
132     private void assertInReaderUsage() throws IOException {
133         assertInReaderLine("Usage:");
134         assertInReaderLine("[--help --verbose]");
135         assertInReaderLine("--help|--verbose");
136         assertInReaderLine("--help (-h)");
137         assertInReaderLine("--verbose (-v)");
138     }
139 
140     private void assertInReaderLine(final String string) throws IOException {
141         assertEquals(string,in.readLine().trim());
142     }
143 
144     private void assertInReaderEOF() throws IOException {
145         assertNull(in.readLine());
146     }
147 
148     private void inReader() {
149         in = new BufferedReader(new StringReader(out.getBuffer().toString()));
150     }
151 }