Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTest.java?rev=639941&r1=639940&r2=639941&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTest.java (original) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTest.java Fri Mar 21 19:49:41 2008 @@ -1,440 +1 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli2.option; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Set; - -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.Group; -import org.apache.commons.cli2.HelpLine; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.builder.DefaultOptionBuilder; -import org.apache.commons.cli2.builder.GroupBuilder; -import org.apache.commons.cli2.commandline.WriteableCommandLineImpl; - -/** - * @author Rob Oxspring - */ -public class GroupTest - extends GroupTestCase { - public static final Command COMMAND_START = - new Command("start", "Starts the server", null, false, null, null, 0); - public static final Command COMMAND_STOP = - new Command("stop", "Stops the server", null, false, null, null, 0); - public static final Command COMMAND_RESTART = - new Command("restart", "Stops and starts the server", null, false, null, null, 0); - public static final Command COMMAND_GRACEFUL = - new Command("graceful", "Restarts the server without interruption", null, false, null, - null, 0); - - public static Group buildApacheCommandGroup() { - final List options = new ArrayList(); - options.add(COMMAND_GRACEFUL); - options.add(COMMAND_RESTART); - options.add(COMMAND_START); - options.add(COMMAND_STOP); - - return new GroupImpl(options, "httpd-cmds", "The command to pass to the server", 1, 1); - } - - public static Group buildApachectlGroup() { - final List options = new ArrayList(); - options.add(DefaultOptionTest.buildHelpOption()); - options.add(ParentTest.buildKParent()); - - return new GroupImpl(options, "apachectl", "Controls the apache http deamon", 0, - Integer.MAX_VALUE); - } - - public static Group buildAntGroup() { - final List options = new ArrayList(); - options.add(DefaultOptionTest.buildHelpOption()); - options.add(ArgumentTest.buildTargetsArgument()); - - return new GroupImpl(options, "ant", "The options for ant", 0, Integer.MAX_VALUE); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.GroupTestCase#testProcessAnonymousArguments() - */ - public void testProcessAnonymousArguments() - throws OptionException { - final Group option = buildAntGroup(); - final List args = list("compile,test", "dist"); - final ListIterator iterator = args.listIterator(); - final WriteableCommandLine commandLine = commandLine(option, args); - option.process(commandLine, iterator); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption("target")); - assertListContentsEqual(commandLine.getValues("target"), args); - assertListContentsEqual(list("compile", "test", "dist"), args); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.GroupTestCase#testProcessOptions() - */ - public void testProcessOptions() - throws OptionException { - final Group option = buildApachectlGroup(); - final List args = list("-?", "-k"); - final ListIterator iterator = args.listIterator(); - final WriteableCommandLine commandLine = commandLine(option, args); - option.process(commandLine, iterator); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption("--help")); - assertTrue(commandLine.hasOption("-k")); - assertFalse(commandLine.hasOption("start")); - assertListContentsEqual(list("--help", "-k"), args); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() - */ - public void testCanProcess() { - final Group option = buildApacheCommandGroup(); - assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "start")); - } - - public void testCanProcess_BadMatch() { - final Group option = buildApacheCommandGroup(); - assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "begin")); - } - - public void testCanProcess_NullMatch() { - final Group option = buildApacheCommandGroup(); - assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), (String) null)); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() - */ - public void testPrefixes() { - final Group option = buildApachectlGroup(); - assertContentsEqual(list("-", "--"), option.getPrefixes()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testProcess() - */ - public void testProcess() - throws OptionException { - final Group option = buildAntGroup(); - final List args = list("--help", "compile,test", "dist"); - final ListIterator iterator = args.listIterator(); - final WriteableCommandLine commandLine = commandLine(option, args); - option.process(commandLine, iterator); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption("-?")); - assertListContentsEqual(list("compile", "test", "dist"), commandLine.getValues("target")); - } - - public void testProcess_Nested() - throws OptionException { - final Group option = buildApachectlGroup(); - final List args = list("-h", "-k", "graceful"); - final ListIterator iterator = args.listIterator(); - final WriteableCommandLine commandLine = commandLine(option, args); - option.process(commandLine, iterator); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption("-?")); - assertTrue(commandLine.hasOption("-k")); - assertTrue(commandLine.hasOption("graceful")); - assertFalse(commandLine.hasOption("stop")); - assertTrue(commandLine.getValues("start").isEmpty()); - assertListContentsEqual(list("--help", "-k", "graceful"), args); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testTriggers() - */ - public void testTriggers() { - final Group option = buildApachectlGroup(); - assertContentsEqual(list("--help", "-?", "-h", "-k"), option.getTriggers()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testValidate() - */ - public void testValidate() - throws OptionException { - final Group option = buildApacheCommandGroup(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - commandLine.addOption(COMMAND_RESTART); - - option.validate(commandLine); - } - - public void testValidate_UnexpectedOption() { - final Group option = buildApacheCommandGroup(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - commandLine.addOption(COMMAND_RESTART); - commandLine.addOption(COMMAND_GRACEFUL); - - try { - option.validate(commandLine); - fail("Too many options"); - } catch (OptionException uoe) { - assertEquals(option, uoe.getOption()); - } - } - - public void testValidate_MissingOption() { - final Group option = buildApacheCommandGroup(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - try { - option.validate(commandLine); - fail("Missing an option"); - } catch (OptionException moe) { - assertEquals(option, moe.getOption()); - } - } - - public void testValidate_RequiredChild() - throws OptionException { - final Option required = - new DefaultOptionBuilder().withLongName("required").withRequired(true).create(); - final Option optional = - new DefaultOptionBuilder().withLongName("optional").withRequired(false).create(); - final Group group = - new GroupBuilder().withOption(required).withOption(optional).withMinimum(1).create(); - - WriteableCommandLine commandLine; - - commandLine = commandLine(group, list()); - - try { - group.validate(commandLine); - fail("Missing option 'required'"); - } catch (OptionException moe) { - assertEquals(required, moe.getOption()); - } - - commandLine = commandLine(group, list()); - commandLine.addOption(optional); - - try { - group.validate(commandLine); - fail("Missing option 'required'"); - } catch (OptionException moe) { - assertEquals(required, moe.getOption()); - } - - commandLine = commandLine(group, list()); - commandLine.addOption(required); - group.validate(commandLine); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() - */ - public void testAppendUsage() { - final Option option = buildApacheCommandGroup(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - - //settings.remove(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED); - option.appendUsage(buffer, settings, null); - - assertEquals("httpd-cmds (graceful|restart|start|stop)", buffer.toString()); - } - - public void testAppendUsage_NoOptional() { - final Option option = buildApacheCommandGroup(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_OPTIONAL); - option.appendUsage(buffer, settings, null); - - assertEquals("httpd-cmds (graceful|restart|start|stop)", buffer.toString()); - } - - public void testAppendUsage_NoExpand() { - final Option option = buildApacheCommandGroup(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_GROUP_EXPANDED); - option.appendUsage(buffer, settings, null); - - assertEquals("httpd-cmds", buffer.toString()); - } - - public void testAppendUsage_NoExpandOrName() { - final Option option = buildApacheCommandGroup(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_GROUP_EXPANDED); - settings.remove(DisplaySetting.DISPLAY_GROUP_NAME); - option.appendUsage(buffer, settings, null); - - assertEquals("httpd-cmds", buffer.toString()); - } - - public void testAppendUsage_NoName() { - final Option option = buildApacheCommandGroup(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_GROUP_NAME); - option.appendUsage(buffer, settings, null); - - assertEquals("graceful|restart|start|stop", buffer.toString()); - } - - public void testAppendUsage_WithArgs() { - final Option option = buildAntGroup(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_GROUP_OUTER); - option.appendUsage(buffer, settings, null); - - assertEquals("[ant (--help (-?,-h)) [<target1> [<target2> ...]]]", buffer.toString()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() - */ - public void testGetPreferredName() { - final Option option = buildAntGroup(); - assertEquals("ant", option.getPreferredName()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() - */ - public void testGetDescription() { - final Option option = buildApachectlGroup(); - assertEquals("Controls the apache http deamon", option.getDescription()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() - */ - public void testHelpLines() { - final Option option = buildApacheCommandGroup(); - final List lines = option.helpLines(0, DisplaySetting.ALL, null); - final Iterator i = lines.iterator(); - - final HelpLine line1 = (HelpLine) i.next(); - assertEquals(0, line1.getIndent()); - assertEquals(option, line1.getOption()); - - final HelpLine line2 = (HelpLine) i.next(); - assertEquals(1, line2.getIndent()); - assertEquals(COMMAND_GRACEFUL, line2.getOption()); - - final HelpLine line3 = (HelpLine) i.next(); - assertEquals(1, line3.getIndent()); - assertEquals(COMMAND_RESTART, line3.getOption()); - - final HelpLine line4 = (HelpLine) i.next(); - assertEquals(1, line4.getIndent()); - assertEquals(COMMAND_START, line4.getOption()); - - final HelpLine line5 = (HelpLine) i.next(); - assertEquals(1, line5.getIndent()); - assertEquals(COMMAND_STOP, line5.getOption()); - - assertFalse(i.hasNext()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() - */ - public void testHelpLines_NoExpanded() { - final Option option = buildApacheCommandGroup(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_GROUP_EXPANDED); - - final List lines = option.helpLines(0, settings, null); - final Iterator i = lines.iterator(); - - final HelpLine line1 = (HelpLine) i.next(); - assertEquals(0, line1.getIndent()); - assertEquals(option, line1.getOption()); - - assertFalse(i.hasNext()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() - */ - public void testHelpLines_NoName() { - final Option option = buildApacheCommandGroup(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_GROUP_NAME); - - final List lines = option.helpLines(0, settings, null); - final Iterator i = lines.iterator(); - - final HelpLine line2 = (HelpLine) i.next(); - assertEquals(1, line2.getIndent()); - assertEquals(COMMAND_GRACEFUL, line2.getOption()); - - final HelpLine line3 = (HelpLine) i.next(); - assertEquals(1, line3.getIndent()); - assertEquals(COMMAND_RESTART, line3.getOption()); - - final HelpLine line4 = (HelpLine) i.next(); - assertEquals(1, line4.getIndent()); - assertEquals(COMMAND_START, line4.getOption()); - - final HelpLine line5 = (HelpLine) i.next(); - assertEquals(1, line5.getIndent()); - assertEquals(COMMAND_STOP, line5.getOption()); - - assertFalse(i.hasNext()); - } -} +/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.option;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Set;import org.ap ache.commons.cli2.DisplaySetting;import org.apache.commons.cli2.Group;import org.apache.commons.cli2.HelpLine;import org.apache.commons.cli2.Option;import org.apache.commons.cli2.OptionException;import org.apache.commons.cli2.WriteableCommandLine;import org.apache.commons.cli2.builder.DefaultOptionBuilder;import org.apache.commons.cli2.builder.GroupBuilder;import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;/** * @author Rob Oxspring */public class GroupTest extends GroupTestCase { public static final Command COMMAND_START = new Command("start", "Starts the server", null, false, null, null, 0); public static final Command COMMAND_STOP = new Command("stop", "Stops the server", null, false, null, null, 0); public static final Command COMMAND_RESTART = new Command("restart", "Stops and starts the server", null, false, null, null, 0); public static final Command COMMAND_GRACEFUL = new Command("graceful", "Restarts the se rver without interruption", null, false, null, null, 0); public static Group buildApacheCommandGroup() { final List options = new ArrayList(); options.add(COMMAND_GRACEFUL); options.add(COMMAND_RESTART); options.add(COMMAND_START); options.add(COMMAND_STOP); return new GroupImpl(options, "httpd-cmds", "The command to pass to the server", 1, 1); } public static Group buildApachectlGroup() { final List options = new ArrayList(); options.add(DefaultOptionTest.buildHelpOption()); options.add(ParentTest.buildKParent()); return new GroupImpl(options, "apachectl", "Controls the apache http deamon", 0, Integer.MAX_VALUE); } public static Group buildAntGroup() { final List options = new ArrayList(); options.add(DefaultOptionTest.buildHelpOption()); options.add(ArgumentTest.buildTargetsArgument()); return new GroupImpl(options, "ant", "The options for ant", 0, Integer.MAX_VALUE); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.GroupTestCase#testProcessAnonymousArguments() */ public void testProcessAnonymousArguments() throws OptionException { final Group option = buildAntGroup(); final List args = list("compile,test", "dist"); final ListIterator iterator = args.listIterator(); final WriteableCommandLine commandLine = commandLine(option, args); option.process(commandLine, iterator); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption("target")); assertListContentsEqual(commandLine.getValues("target"), args); assertListContentsEqual(list("compile", "test", "dist"), args); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.GroupTestCase#testProcessOptions() */ public void testProcessOptions() throws OptionException { final Group option = buildApa chectlGroup(); final List args = list("-?", "-k"); final ListIterator iterator = args.listIterator(); final WriteableCommandLine commandLine = commandLine(option, args); option.process(commandLine, iterator); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption("--help")); assertTrue(commandLine.hasOption("-k")); assertFalse(commandLine.hasOption("start")); assertListContentsEqual(list("--help", "-k"), args); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() */ public void testCanProcess() { final Group option = buildApacheCommandGroup(); assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "start")); } public void testCanProcess_BadMatch() { final Group option = buildApacheCommandGroup(); assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "begin")); } public void testCanProcess_NullMatch() { final Group option = buildApacheCommandGroup(); assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), (String) null)); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() */ public void testPrefixes() { final Group option = buildApachectlGroup(); assertContentsEqual(list("-", "--"), option.getPrefixes()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testProcess() */ public void testProcess() throws OptionException { final Group option = buildAntGroup(); final List args = list("--help", "compile,test", "dist"); final ListIterator iterator = args.listIterator(); final WriteableCommandLine commandLine = commandLine(option, args); option.process(commandLine, iterator); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption("-?" )); assertListContentsEqual(list("compile", "test", "dist"), commandLine.getValues("target")); } public void testProcess_Nested() throws OptionException { final Group option = buildApachectlGroup(); final List args = list("-h", "-k", "graceful"); final ListIterator iterator = args.listIterator(); final WriteableCommandLine commandLine = commandLine(option, args); option.process(commandLine, iterator); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption("-?")); assertTrue(commandLine.hasOption("-k")); assertTrue(commandLine.hasOption("graceful")); assertFalse(commandLine.hasOption("stop")); assertTrue(commandLine.getValues("start").isEmpty()); assertListContentsEqual(list("--help", "-k", "graceful"), args); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testTriggers() */ public void testTriggers() { final G roup option = buildApachectlGroup(); assertContentsEqual(list("--help", "-?", "-h", "-k"), option.getTriggers()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testValidate() */ public void testValidate() throws OptionException { final Group option = buildApacheCommandGroup(); final WriteableCommandLine commandLine = commandLine(option, list()); commandLine.addOption(COMMAND_RESTART); option.validate(commandLine); } public void testValidate_UnexpectedOption() { final Group option = buildApacheCommandGroup(); final WriteableCommandLine commandLine = commandLine(option, list()); commandLine.addOption(COMMAND_RESTART); commandLine.addOption(COMMAND_GRACEFUL); try { option.validate(commandLine); fail("Too many options"); } catch (OptionException uoe) { assertEquals(option, uoe.getOption()); } } pub lic void testValidate_MissingOption() { final Group option = buildApacheCommandGroup(); final WriteableCommandLine commandLine = commandLine(option, list()); try { option.validate(commandLine); fail("Missing an option"); } catch (OptionException moe) { assertEquals(option, moe.getOption()); } } public void testValidate_RequiredChild() throws OptionException { final Option required = new DefaultOptionBuilder().withLongName("required").withRequired(true).create(); final Option optional = new DefaultOptionBuilder().withLongName("optional").withRequired(false).create(); final Group group = new GroupBuilder().withOption(required).withOption(optional).withMinimum(1).create(); WriteableCommandLine commandLine; commandLine = commandLine(group, list()); try { group.validate(commandLine); fail("Missing option 'required'"); } catch (OptionException moe) { assertEquals(required, moe.getOption()); } commandLine = commandLine(group, list()); commandLine.addOption(optional); try { group.validate(commandLine); fail("Missing option 'required'"); } catch (OptionException moe) { assertEquals(required, moe.getOption()); } commandLine = commandLine(group, list()); commandLine.addOption(required); group.validate(commandLine); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() */ public void testAppendUsage() { final Option option = buildApacheCommandGroup(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); //settings.remove(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED); option.appendUsage(buffer, settings, null); assertEquals("htt pd-cmds (graceful|restart|start|stop)", buffer.toString()); } public void testAppendUsage_NoOptional() { final Option option = buildApacheCommandGroup(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_OPTIONAL); option.appendUsage(buffer, settings, null); assertEquals("httpd-cmds (graceful|restart|start|stop)", buffer.toString()); } public void testAppendUsage_NoExpand() { final Option option = buildApacheCommandGroup(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_GROUP_EXPANDED); option.appendUsage(buffer, settings, null); assertEquals("httpd-cmds", buffer.toString()); } public void testAppendUsage_NoExpandOrName() { final Option option = buildApacheCommandGroup(); final S tringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_GROUP_EXPANDED); settings.remove(DisplaySetting.DISPLAY_GROUP_NAME); option.appendUsage(buffer, settings, null); assertEquals("httpd-cmds", buffer.toString()); } public void testAppendUsage_NoName() { final Option option = buildApacheCommandGroup(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_GROUP_NAME); option.appendUsage(buffer, settings, null); assertEquals("graceful|restart|start|stop", buffer.toString()); } public void testAppendUsage_WithArgs() { final Option option = buildAntGroup(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_GRO UP_OUTER); option.appendUsage(buffer, settings, null); assertEquals("[ant (--help (-?,-h)) [<target1> [<target2> ...]]]", buffer.toString()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() */ public void testGetPreferredName() { final Option option = buildAntGroup(); assertEquals("ant", option.getPreferredName()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() */ public void testGetDescription() { final Option option = buildApachectlGroup(); assertEquals("Controls the apache http deamon", option.getDescription()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() */ public void testHelpLines() { final Option option = buildApacheCommandGroup(); final List lines = option.helpLines(0, DisplaySetting.ALL, null); final Iter ator i = lines.iterator(); final HelpLine line1 = (HelpLine) i.next(); assertEquals(0, line1.getIndent()); assertEquals(option, line1.getOption()); final HelpLine line2 = (HelpLine) i.next(); assertEquals(1, line2.getIndent()); assertEquals(COMMAND_GRACEFUL, line2.getOption()); final HelpLine line3 = (HelpLine) i.next(); assertEquals(1, line3.getIndent()); assertEquals(COMMAND_RESTART, line3.getOption()); final HelpLine line4 = (HelpLine) i.next(); assertEquals(1, line4.getIndent()); assertEquals(COMMAND_START, line4.getOption()); final HelpLine line5 = (HelpLine) i.next(); assertEquals(1, line5.getIndent()); assertEquals(COMMAND_STOP, line5.getOption()); assertFalse(i.hasNext()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() */ public void testHelpLines_NoExpanded() { final Option option = buil dApacheCommandGroup(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_GROUP_EXPANDED); final List lines = option.helpLines(0, settings, null); final Iterator i = lines.iterator(); final HelpLine line1 = (HelpLine) i.next(); assertEquals(0, line1.getIndent()); assertEquals(option, line1.getOption()); assertFalse(i.hasNext()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() */ public void testHelpLines_NoName() { final Option option = buildApacheCommandGroup(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_GROUP_NAME); final List lines = option.helpLines(0, settings, null); final Iterator i = lines.iterator(); final HelpLine line2 = (HelpLine) i.next(); assertEquals(1, line2.getIndent()); assertEquals(COMMAND_GRACE FUL, line2.getOption()); final HelpLine line3 = (HelpLine) i.next(); assertEquals(1, line3.getIndent()); assertEquals(COMMAND_RESTART, line3.getOption()); final HelpLine line4 = (HelpLine) i.next(); assertEquals(1, line4.getIndent()); assertEquals(COMMAND_START, line4.getOption()); final HelpLine line5 = (HelpLine) i.next(); assertEquals(1, line5.getIndent()); assertEquals(COMMAND_STOP, line5.getOption()); assertFalse(i.hasNext()); }} \ No newline at end of file
Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTestCase.java?rev=639941&r1=639940&r2=639941&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTestCase.java (original) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTestCase.java Fri Mar 21 19:49:41 2008 @@ -1,31 +1 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli2.option; - -import org.apache.commons.cli2.OptionException; - -/** - * @author Rob Oxspring - */ -public abstract class GroupTestCase - extends OptionTestCase { - public abstract void testProcessOptions() - throws OptionException; - - public abstract void testProcessAnonymousArguments() - throws OptionException; -} +/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.option;import org.apache.commons.cli2.OptionException;/** * @author Rob Oxspring */public abstract class GroupTestCase extends OptionTestCase { public abstract void testProcessOptions() throws OptionException; public abstract void testProcessAnonymousArguments() throws OptionException;} \ No newline at end of file Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java?rev=639941&r1=639940&r2=639941&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java (original) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java Fri Mar 21 19:49:41 2008 @@ -1,192 +1 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli2.option; - -import org.apache.commons.cli2.CLITestCase; -import org.apache.commons.cli2.CommandLine; -import org.apache.commons.cli2.Group; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.builder.ArgumentBuilder; -import org.apache.commons.cli2.builder.DefaultOptionBuilder; -import org.apache.commons.cli2.builder.GroupBuilder; -import org.apache.commons.cli2.commandline.Parser; -import org.apache.commons.cli2.util.HelpFormatter; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringReader; -import java.io.StringWriter; - -import java.util.ArrayList; -import java.util.List; - - -/** - * Test to exercise nested groups developed to demonstrate bug 32533 - */ -public class NestedGroupTest extends CLITestCase { - final static DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); - final static ArgumentBuilder abuilder = new ArgumentBuilder(); - final static GroupBuilder gbuilder = new GroupBuilder(); - - static Group buildActionGroup() { - return gbuilder.withName("Action").withDescription("Action") - .withMinimum(1).withMaximum(1) - .withOption(obuilder.withId(5).withShortName("e") - .withLongName("encrypt") - .withDescription("Encrypt input") - .create()) - .withOption(obuilder.withId(6).withShortName("d") - .withLongName("decrypt") - .withDescription("Decrypt input") - .create()).create(); - } - - static Group buildAlgorithmGroup() { - return gbuilder.withName("Algorithm") - .withDescription("Encryption Algorithm").withMaximum(1) - .withOption(obuilder.withId(0).withShortName("b") - .withLongName("blowfish") - .withDescription("Blowfish").create()) - .withOption(obuilder.withId(1).withShortName("3") - .withLongName("3DES") - .withDescription("Triple DES") - .create()).create(); - } - - static Group buildInputGroup() { - return gbuilder.withName("Input").withDescription("Input").withMinimum(1) - .withMaximum(1) - .withOption(obuilder.withId(2).withShortName("f") - .withLongName("file") - .withDescription("Input file") - .withArgument(abuilder.withName( - "file").withMinimum(1).withMaximum(1).create()).create()) - .withOption(obuilder.withId(3).withShortName("s") - .withLongName("string") - .withDescription("Input string") - .withArgument(abuilder.withName( - "string").withMinimum(1).withMaximum(1).create()).create()) - .create(); - } - - static Group buildEncryptionServiceGroup(Group[] nestedGroups) { - gbuilder.withName("encryptionService") - .withOption(obuilder.withId(4).withShortName("h") - .withLongName("help") - .withDescription("Print this message") - .create()).withOption(obuilder.withShortName( - "k").withLongName("key").withDescription("Encryption key") - .create()); - - for (int i = 0; i < nestedGroups.length; i++) { - gbuilder.withOption(nestedGroups[i]); - } - - return gbuilder.create(); - } - - public void testNestedGroup() - throws OptionException { - final String[] args = { - "-eb", - "--file", - "/tmp/filename.txt" - }; - - Group[] nestedGroups = { - buildActionGroup(), - buildAlgorithmGroup(), - buildInputGroup() - }; - - Parser parser = new Parser(); - parser.setGroup(buildEncryptionServiceGroup(nestedGroups)); - - CommandLine commandLine = parser.parse(args); - - assertTrue("/tmp/filename.txt".equals(commandLine.getValue("-f"))); - assertTrue(commandLine.hasOption("-e")); - assertTrue(commandLine.hasOption("-b")); - assertFalse(commandLine.hasOption("-d")); - } - - public void testNestedGroupHelp() { - Group[] nestedGroups = { - buildActionGroup(), - buildAlgorithmGroup(), - buildInputGroup() - }; - - HelpFormatter helpFormatter = new HelpFormatter(); - helpFormatter.setGroup(buildEncryptionServiceGroup(nestedGroups)); - - final StringWriter out = new StringWriter(); - helpFormatter.setPrintWriter(new PrintWriter(out)); - - try { - helpFormatter.print(); - - final BufferedReader bufferedReader = new BufferedReader(new StringReader( - out.toString())); - final String[] expected = new String[] { - "Usage: ", - " [-h -k -e|-d -b|-3 -f <file>|-s <string>] ", - "encryptionService ", - " -h (--help) Print this message ", - " -k (--key) Encryption key ", - " Action Action ", - " -e (--encrypt) Encrypt input ", - " -d (--decrypt) Decrypt input ", - " Algorithm Encryption Algorithm ", - " -b (--blowfish) Blowfish ", - " -3 (--3DES) Triple DES ", - " Input Input ", - " -f (--file) file Input file ", - " -s (--string) string Input string " - }; - - List actual = new ArrayList(expected.length); - String input; - - while ((input = bufferedReader.readLine()) != null) { - actual.add(input); - } - - // Show they are the same number of lines - assertEquals("Help text lines should be " + expected.length, - actual.size(), expected.length); - - for (int i = 0; i < expected.length; i++) { - if (!expected[i].equals(actual.get(i))) { - for (int x = 0; x < expected.length; i++) { - System.out.println(" " + expected[i]); - System.out.println((expected[i].equals(actual.get(i)) - ? "== " - : "!= ") + actual.get(i)); - } - } - - assertEquals(expected[i], actual.get(i)); - } - } - catch (IOException e) { - fail(e.getLocalizedMessage()); - } - } -} +/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.option;import org.apache.commons.cli2.CLITestCase;import org.apache.commons.cli2.CommandLine;import org.apache.commons.cli2.Group;import org.apache.commons.cli2.OptionExc eption;import org.apache.commons.cli2.builder.ArgumentBuilder;import org.apache.commons.cli2.builder.DefaultOptionBuilder;import org.apache.commons.cli2.builder.GroupBuilder;import org.apache.commons.cli2.commandline.Parser;import org.apache.commons.cli2.util.HelpFormatter;import java.io.BufferedReader;import java.io.IOException;import java.io.PrintWriter;import java.io.StringReader;import java.io.StringWriter;import java.util.ArrayList;import java.util.List;/** * Test to exercise nested groups developed to demonstrate bug 32533 */public class NestedGroupTest extends CLITestCase { final static DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); final static ArgumentBuilder abuilder = new ArgumentBuilder(); final static GroupBuilder gbuilder = new GroupBuilder(); static Group buildActionGroup() { return gbuilder.withName("Action").withDescription("Action") .withMinimum(1).withMaximum(1) .withOption(obuilde r.withId(5).withShortName("e") .withLongName("encrypt") .withDescription("Encrypt input") .create()) .withOption(obuilder.withId(6).withShortName("d") .withLongName("decrypt") .withDescription("Decrypt input") .create()).create(); } static Group buildAlgorithmGroup() { return gbuilder.withName("Algorithm") .withDescription("Encryption Algorithm").withMaximum(1) .withOption(obuilder.withId(0).withShortName("b") .withLongName("blowfish") .withDescription("Blowfish").create()) .withOption(obuilder.withId(1).withShortName("3") .withLongName("3DES") .withDescription("Triple DES") .create()).create(); } static Group buildInputGroup() { return gbuilder.withName("Input").withDescription("Input").withMinimum(1) .withMaximum(1) .withOption(obuilder.withId(2).withShortName("f") .withLongName("file") .withDescription("Input file") .withArgument(abuilder.withName( "file").withMinimum(1).withMaximum(1).create()).create()) .withOption(obuilder.withId(3).withShortName("s") .withLongName("string") .withDescription("Input string") .withArgument(abuilder.withName( "string").withMinimum(1).withMaximum(1).create()).create()) .create(); } static Group buildEncryptionServiceGroup(Group[] nestedGroups) { gbuilder.withName("encryptionService") .withOption(obuilder.withId(4).withShortName("h") .withLongName("help") .withDescription("Print this message") .create()).withOption(obuilder.withShortName( "k").withLongName("key").withDescription("Encryption key") .create()); for (int i = 0; i < nestedGroups.length; i++) { gbuilder.withOption(nestedGroups[i]); } return gbuilder.create(); } public void testNestedGroup() throws OptionException { final String[] args = { "-eb", "--file", "/tmp/filename.txt" }; Group[] nestedGroups = { buildActionGroup(), buildAlgorithmGroup(), buildInputGroup() }; Parser parser = new Parser(); parser.setGroup(buildEncryptionServiceGroup(nestedGroups)); CommandLine commandLine = parser.parse(args); assertTrue("/tmp/filename.txt".equals(commandLine.getValue("-f"))); assertTrue(commandLine.hasOption("-e")); assertTrue(commandLine.hasOption("-b")); assertFalse(commandLine.hasOption("-d")); } public void testNestedGroupHelp() { Group[] nestedGroups = { buildActionGroup(), buildAlgorithmGroup(), buildInputGroup() }; HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.setGroup(buildEncryptionServiceGroup(nestedGroups)); final StringWriter out = new StringWriter(); helpFormatter.setPrintWriter(new PrintWriter(out)); try { helpFormatter.print(); final BufferedReader bufferedReader = new BufferedReader(new StringReader( out.toString())); final String[] expected = new String[] { "Usage: ", " [-h -k -e|-d -b|-3 -f <file>|-s <string>] ", "encryptionService ", " -h (--help) Print this message ", " -k (--key) Encryption key ", " Action Action ", " -e (--encrypt) Encrypt input ", " -d (--decrypt) Decry pt input ", " Algorithm Encryption Algorithm ", " -b (--blowfish) Blowfish ", " -3 (--3DES) Triple DES ", " Input Input ", " -f (--file) file Input file ", " -s (--string) string Input string " }; List actual = new ArrayList(expected.length); String input; while ((input = bufferedReader.readLine()) != null) { actual.add(input); } // Show they are the same number of lines assertEquals("Help text lines should be " + ex pected.length, actual.size(), expected.length); for (int i = 0; i < expected.length; i++) { if (!expected[i].equals(actual.get(i))) { for (int x = 0; x < expected.length; i++) { System.out.println(" " + expected[i]); System.out.println((expected[i].equals(actual.get(i)) ? "== " : "!= ") + actual.get(i)); } } assertEquals(expected[i], actual.get(i)); } } catch (IOException e) { fail(e.getLocalizedMessage()); } }} \ No newline at end of file Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/OptionTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/OptionTestCase.java?rev=639941&r1=639940&r2=639941&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/OptionTestCase.java (original) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/OptionTestCase.java Fri Mar 21 19:49:41 2008 @@ -1,55 +1 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli2.option; - -import java.util.List; - -import org.apache.commons.cli2.CLITestCase; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.commandline.WriteableCommandLineImpl; - -/** - * @author Rob Oxspring - */ -public abstract class OptionTestCase extends CLITestCase { - - public static WriteableCommandLine commandLine( - final Option option, - final List args) { - return new WriteableCommandLineImpl(option, args); - } - - public abstract void testTriggers(); - - public abstract void testPrefixes(); - - public abstract void testCanProcess(); - - public abstract void testProcess() throws OptionException; - - public abstract void testValidate() throws OptionException; - - public abstract void testAppendUsage() throws OptionException; - - public abstract void testGetPreferredName(); - - public abstract void testGetDescription(); - - public abstract void testHelpLines(); -} +/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.option;import java.util.List;import org.apache.commons.cli2.CLITestCase;import org.apache.commons.cli2.Option;import org.apache.commons.cli2.OptionException;import org.ap ache.commons.cli2.WriteableCommandLine;import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;/** * @author Rob Oxspring */public abstract class OptionTestCase extends CLITestCase { public static WriteableCommandLine commandLine( final Option option, final List args) { return new WriteableCommandLineImpl(option, args); } public abstract void testTriggers(); public abstract void testPrefixes(); public abstract void testCanProcess(); public abstract void testProcess() throws OptionException; public abstract void testValidate() throws OptionException; public abstract void testAppendUsage() throws OptionException; public abstract void testGetPreferredName(); public abstract void testGetDescription(); public abstract void testHelpLines();} \ No newline at end of file
