Author: roxspring Date: Wed Jun 15 16:11:14 2005 New Revision: 190828 URL: http://svn.apache.org/viewcvs?rev=190828&view=rev Log: Parser now keeps the normalised list of arguments. Added ParserTest
Added: jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/commandline/ParserTest.java Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java?rev=190828&r1=190827&r2=190828&view=diff ============================================================================== --- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java (original) +++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java Wed Jun 15 16:11:14 2005 @@ -16,7 +16,6 @@ package org.apache.commons.cli2.commandline; import java.io.IOException; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; @@ -68,7 +67,7 @@ // wet up a command line for this group final WriteableCommandLine commandLine = - new WriteableCommandLineImpl(group, new ArrayList()); + new WriteableCommandLineImpl(group, argumentList); // pick up any defaults from the model group.defaults(commandLine); Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java?rev=190828&r1=190827&r2=190828&view=diff ============================================================================== --- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java (original) +++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java Wed Jun 15 16:11:14 2005 @@ -204,4 +204,9 @@ defaultSwitches.put(option, defaultSwitch); } } + + public List getNormalised() { + return Collections.unmodifiableList(normalised); + } + } Added: jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/commandline/ParserTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/commandline/ParserTest.java?rev=190828&view=auto ============================================================================== --- jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/commandline/ParserTest.java (added) +++ jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/commandline/ParserTest.java Wed Jun 15 16:11:14 2005 @@ -0,0 +1,124 @@ +package org.apache.commons.cli2.commandline; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; + +import org.apache.commons.cli2.CommandLine; +import org.apache.commons.cli2.Group; +import org.apache.commons.cli2.OptionException; +import org.apache.commons.cli2.builder.DefaultOptionBuilder; +import org.apache.commons.cli2.builder.GroupBuilder; +import org.apache.commons.cli2.option.DefaultOption; +import org.apache.commons.cli2.util.HelpFormatter; + +import junit.framework.TestCase; + +public class ParserTest extends TestCase { + + private Parser parser; + private DefaultOption verboseOption; + private DefaultOption helpOption; + private Group options; + private HelpFormatter helpFormatter; + private StringWriter out; + private BufferedReader in; + + public void setUp() { + parser = new Parser(); + + final GroupBuilder gBuilder = new GroupBuilder(); + final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder(); + + helpOption = oBuilder.withLongName("help").withShortName("h").create(); + verboseOption = oBuilder.withLongName("verbose").withShortName("v").create(); + options = gBuilder.withOption(helpOption).withOption(verboseOption).create(); + parser.setGroup(options); + + helpFormatter = new HelpFormatter(); + out = new StringWriter(); + helpFormatter.setPrintWriter(new PrintWriter(out)); + parser.setHelpFormatter(helpFormatter); + } + + public void testParse_Successful() throws OptionException { + final CommandLine cl = parser.parse(new String[]{"-hv"}); + + assertTrue(cl.hasOption(helpOption)); + assertTrue(cl.hasOption(verboseOption)); + + assertEquals("--help --verbose",cl.toString()); + + final WriteableCommandLineImpl wcli = (WriteableCommandLineImpl)cl; + assertEquals("[--help, --verbose]",wcli.getNormalised().toString()); + } + + public void testParse_WithUnexpectedOption() { + try { + parser.parse(new String[]{"--unexpected"}); + fail("OptionException"); + } + catch(OptionException e) { + assertEquals(options,e.getOption()); + assertEquals("Unexpected --unexpected while processing --help|--verbose",e.getMessage()); + } + } + + public void testParseAndHelp_Successful() throws IOException { + final CommandLine cl = parser.parseAndHelp(new String[]{"-v"}); + + assertTrue(cl.hasOption(verboseOption)); + assertEquals("",out.getBuffer().toString()); + } + + public void testParseAndHelp_ByHelpOption() throws IOException { + parser.setHelpOption(helpOption); + + assertNull(parser.parseAndHelp(new String[]{"-hv"})); + + inReader(); + assertInReaderUsage(); + assertInReaderEOF(); + } + + public void testParseAndHelp_ByHelpTrigger() throws IOException { + parser.setHelpTrigger("--help"); + + assertNull(parser.parseAndHelp(new String[]{"-hv"})); + + inReader(); + assertInReaderUsage(); + assertInReaderEOF(); + } + + public void testParseAndHelp_WithUnexpectedOption() throws IOException { + assertNull(parser.parseAndHelp(new String[]{"--unexpected"})); + + inReader(); + assertInReaderLine("Unexpected --unexpected while processing --help|--verbose"); + assertInReaderUsage(); + assertInReaderEOF(); + } + + private void assertInReaderUsage() throws IOException { + assertInReaderLine("Usage:"); + assertInReaderLine("[--help --verbose]"); + assertInReaderLine("--help|--verbose"); + assertInReaderLine("--help (-h)"); + assertInReaderLine("--verbose (-v)"); + } + + private void assertInReaderLine(final String string) throws IOException { + assertEquals(string,in.readLine().trim()); + } + + private void assertInReaderEOF() throws IOException { + assertNull(in.readLine()); + } + + private void inReader() { + in = new BufferedReader(new StringReader(out.getBuffer().toString())); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]