001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2.commandline;
018
019import java.io.BufferedReader;
020import java.io.IOException;
021import java.io.PrintWriter;
022import java.io.StringReader;
023import java.io.StringWriter;
024
025import org.apache.commons.cli2.CommandLine;
026import org.apache.commons.cli2.Group;
027import org.apache.commons.cli2.OptionException;
028import org.apache.commons.cli2.builder.DefaultOptionBuilder;
029import org.apache.commons.cli2.builder.GroupBuilder;
030import org.apache.commons.cli2.option.DefaultOption;
031import org.apache.commons.cli2.util.HelpFormatter;
032
033import junit.framework.TestCase;
034
035public class ParserTest extends TestCase {
036    
037    private Parser parser;
038    private DefaultOption verboseOption;
039    private DefaultOption helpOption;
040    private Group options;
041    private HelpFormatter helpFormatter;
042    private StringWriter out;
043    private BufferedReader in;
044
045    public void setUp() {
046        parser = new Parser();
047        
048        final GroupBuilder gBuilder = new GroupBuilder();
049        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
050        
051        helpOption = oBuilder.withLongName("help").withShortName("h").create();
052        verboseOption = oBuilder.withLongName("verbose").withShortName("v").create();
053        options = gBuilder.withOption(helpOption).withOption(verboseOption).create();
054        parser.setGroup(options);
055        
056        helpFormatter = new HelpFormatter();
057        out = new StringWriter();
058        helpFormatter.setPrintWriter(new PrintWriter(out));
059        parser.setHelpFormatter(helpFormatter);
060    }
061
062    public void testParse_Successful() throws OptionException {
063        final CommandLine cl = parser.parse(new String[]{"-hv"});
064        
065        assertTrue(cl.hasOption(helpOption));
066        assertTrue(cl.hasOption(verboseOption));
067        
068        assertEquals("--help --verbose",cl.toString());
069        
070        final WriteableCommandLineImpl wcli = (WriteableCommandLineImpl)cl;
071        assertEquals("[--help, --verbose]",wcli.getNormalised().toString());
072    }
073
074    public void testParse_WithUnexpectedOption() {
075        try {
076            parser.parse(new String[]{"--unexpected"});
077            fail("OptionException");
078        }
079        catch(OptionException e) {
080            assertEquals(options,e.getOption());
081            assertEquals("Unexpected --unexpected while processing --help|--verbose",e.getMessage());
082        }
083    }
084
085    public void testParse_WithUnexpectedShortOption() {
086        try {
087            parser.parse(new String[]{"-vx"});
088            fail("OptionException");
089        }
090        catch(OptionException e) {
091            assertEquals(options,e.getOption());
092            assertEquals("Unexpected -x while processing --help|--verbose",e.getMessage());
093        }
094    }
095
096    public void testParseAndHelp_Successful() throws IOException {
097        final CommandLine cl = parser.parseAndHelp(new String[]{"-v"});
098        
099        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}