Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 13105 invoked from network); 22 Mar 2008 02:51:19 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 22 Mar 2008 02:51:19 -0000 Received: (qmail 47723 invoked by uid 500); 22 Mar 2008 02:50:56 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 47629 invoked by uid 500); 22 Mar 2008 02:50:56 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 47490 invoked by uid 99); 22 Mar 2008 02:50:55 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Mar 2008 19:50:55 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 22 Mar 2008 02:50:18 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 8C37A1A986A; Fri, 21 Mar 2008 19:49:58 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r639941 [13/17] - in /commons/proper/cli/trunk: ./ src/java/org/apache/commons/cli2/ src/java/org/apache/commons/cli2/builder/ src/java/org/apache/commons/cli2/commandline/ src/java/org/apache/commons/cli2/option/ src/java/org/apache/common... Date: Sat, 22 Mar 2008 02:49:46 -0000 To: commits@commons.apache.org From: bayard@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080322024958.8C37A1A986A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ArgumentTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ArgumentTest.java?rev=639941&r1=639940&r2=639941&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ArgumentTest.java (original) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ArgumentTest.java Fri Mar 21 19:49:41 2008 @@ -1,646 +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.text.ParseException; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -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.Argument; -import org.apache.commons.cli2.DisplaySetting; -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.ArgumentBuilder; -import org.apache.commons.cli2.builder.GroupBuilder; -import org.apache.commons.cli2.commandline.WriteableCommandLineImpl; -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; -import org.apache.commons.cli2.validation.DateValidator; -import org.apache.commons.cli2.validation.DateValidatorTest; - -/** - * @author Rob Oxspring - */ -public class ArgumentTest - extends ArgumentTestCase { - private ResourceHelper resources = ResourceHelper.getResourceHelper(); - - public static Argument buildUsernameArgument() { - return new ArgumentImpl("username", "The user to connect as", 1, 1, '\0', '\0', null, - ArgumentImpl.DEFAULT_CONSUME_REMAINING, null, 0); - } - - public static Argument buildHostArgument() { - return new ArgumentImpl("host", "The host name", 2, 3, '\0', ',', null, null, null, 0); - } - - public static Argument buildPathArgument() { - return new ArgumentImpl("path", "The place to look for files", 1, Integer.MAX_VALUE, '=', - ';', null, ArgumentImpl.DEFAULT_CONSUME_REMAINING, null, 0); - } - - public static Argument buildDateLimitArgument() { - return new ArgumentImpl("limit", "the last acceptable date", 0, 1, '=', '\0', - new DateValidator(DateValidatorTest.YYYY_MM_DD), null, null, 0); - } - - public static Argument buildTargetsArgument() { - return new ArgumentImpl("target", "The targets ant should build", 0, Integer.MAX_VALUE, - '\0', ',', null, null, null, 0); - } - - public static Argument buildSizeArgument() { - List defaults = new ArrayList(); - defaults.add("10"); - - return new ArgumentImpl("size", "The number of units", 1, 1, '\0', '\0', null, - ArgumentImpl.DEFAULT_CONSUME_REMAINING, defaults, 0); - } - - public static Argument buildBoundsArgument() { - List defaults = new ArrayList(); - defaults.add("5"); - defaults.add("10"); - - return new ArgumentImpl("size", "The number of units", 2, 2, '\0', '\0', null, - ArgumentImpl.DEFAULT_CONSUME_REMAINING, defaults, 0); - } - - public void testNew() { - try { - new ArgumentImpl("limit", "the last acceptable date", 10, 5, '=', '\0', - new DateValidator(DateValidatorTest.YYYY_MM_DD), null, null, 0); - } catch (IllegalArgumentException e) { - assertEquals(resources.getMessage("Argument.minimum.exceeds.maximum"), e.getMessage()); - } - - { - ArgumentImpl arg = - new ArgumentImpl(null, "the last acceptable date", 5, 5, '=', '\0', - new DateValidator(DateValidatorTest.YYYY_MM_DD), null, null, 0); - assertEquals("wrong arg name", "arg", arg.getPreferredName()); - } - - { - List defaults = new ArrayList(); - - try { - new ArgumentImpl(null, "the last acceptable date", 1, 1, '=', '\0', - new DateValidator(DateValidatorTest.YYYY_MM_DD), null, defaults, 0); - } catch (IllegalArgumentException exp) { - assertEquals(resources.getMessage("Argument.too.few.defaults"), exp.getMessage()); - } - } - - try { - List defaults = new ArrayList(); - defaults.add("1"); - defaults.add("2"); - - new ArgumentImpl(null, "the last acceptable date", 1, 1, '=', '\0', - new DateValidator(DateValidatorTest.YYYY_MM_DD), null, defaults, 0); - } catch (IllegalArgumentException exp) { - assertEquals(resources.getMessage("Argument.too.many.defaults"), exp.getMessage()); - } - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.ArgumentTestCase#testProcessValues() - */ - public void testProcessValues() - throws OptionException { - final Argument option = buildUsernameArgument(); - final List args = list("rob"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.processValues(commandLine, iterator, option); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("username")); - assertEquals("rob", commandLine.getValue(option)); - } - - public void testProcessValues_BoundaryQuotes() - throws OptionException { - final Argument option = buildUsernameArgument(); - final List args = list("\"rob\""); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.processValues(commandLine, iterator, option); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("username")); - assertEquals("rob", commandLine.getValue(option)); - } - - public void testProcessValues_SpareValues() - throws OptionException { - final Argument option = buildUsernameArgument(); - final List args = list("rob", "secret"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.processValues(commandLine, iterator, option); - - assertTrue(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("username")); - assertEquals("rob", commandLine.getValue(option)); - } - - public void testProcessValues_Optional() { - final Argument option = buildTargetsArgument(); - final List args = list(); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - try { - option.processValues(commandLine, iterator, option); - } catch (final OptionException mve) { - assertEquals(option, mve.getOption()); - assertEquals("Missing value(s) target [target ...]", mve.getMessage()); - } - - assertFalse(iterator.hasNext()); - assertFalse(commandLine.hasOption(option)); - assertFalse(commandLine.hasOption("username")); - assertTrue(commandLine.getValues(option).isEmpty()); - } - - public void testProcessValues_Multiple() - throws OptionException { - final Argument option = buildTargetsArgument(); - final List args = list("compile", "test", "docs"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.processValues(commandLine, iterator, option); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("target")); - assertFalse(commandLine.getValues(option).isEmpty()); - assertListContentsEqual(args, commandLine.getValues(option)); - } - - public void testProcessValues_Contracted() - throws OptionException { - final Argument option = buildTargetsArgument(); - final List args = list("compile,test,javadoc", "checkstyle,jdepend"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.processValues(commandLine, iterator, option); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("target")); - assertListContentsEqual(list("compile", "test", "javadoc", "checkstyle", "jdepend"), - commandLine.getValues(option)); - } - - public void testProcessValues_ContractedTooFew() { - final Argument option = buildHostArgument(); - final List args = list("box1"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - try { - option.processValues(commandLine, iterator, option); - option.validate(commandLine); - fail("Expected MissingValueException"); - } catch (OptionException mve) { - assertSame(option, mve.getOption()); - } - } - - public void testProcessValues_ContractedTooMany() { - final Argument option = buildHostArgument(); - final List args = list("box1,box2,box3,box4"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - try { - option.processValues(commandLine, iterator, option); - option.validate(commandLine); - fail("Expected MissingValueException"); - } catch (OptionException mve) { - assertSame(option, mve.getOption()); - } - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() - */ - public void testCanProcess() { - final Argument option = buildTargetsArgument(); - assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "any value")); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() - */ - public void testPrefixes() { - final Argument option = buildTargetsArgument(); - assertTrue(option.getPrefixes().isEmpty()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testProcess() - */ - public void testProcess() - throws OptionException { - final Argument option = buildPathArgument(); - final List args = list("-path=/lib;/usr/lib;/usr/local/lib"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.process(commandLine, iterator); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("path")); - assertListContentsEqual(list("-path=/lib", "/usr/lib", "/usr/local/lib"), - commandLine.getValues(option)); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testTriggers() - */ - public void testTriggers() { - final Argument option = buildTargetsArgument(); - assertTrue(option.getTriggers().isEmpty()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testValidate() - */ - public void testValidate() - throws OptionException { - final Argument option = buildUsernameArgument(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - commandLine.addValue(option, "rob"); - - option.validate(commandLine); - } - - public void testValidate_Minimum() { - final Argument option = buildUsernameArgument(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - try { - option.validate(commandLine); - fail("UnexpectedValue"); - } catch (OptionException mve) { - assertEquals(option, mve.getOption()); - } - } - - public void testRequired() { - { - final Argument arg = buildBoundsArgument(); - - assertTrue("not required", arg.isRequired()); - } - - { - final Argument arg = buildTargetsArgument(); - - assertFalse("should not be required", arg.isRequired()); - } - } - - public void testValidate_Maximum() { - final Argument option = buildUsernameArgument(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - commandLine.addValue(option, "rob"); - commandLine.addValue(option, "oxspring"); - - try { - option.validate(commandLine); - fail("UnexpectedValue"); - } catch (OptionException uve) { - assertEquals(option, uve.getOption()); - } - } - - public void testValidate_Validator() - throws OptionException, ParseException { - final Argument option = buildDateLimitArgument(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - commandLine.addValue(option, "2004-01-01"); - - option.validate(commandLine, option); - assertContentsEqual(Arrays.asList(new Object[] { - DateValidatorTest.YYYY_MM_DD.parse("2004-01-01") - }), commandLine.getValues(option)); - } - - public void testValidate_ValidatorInvalidDate() - throws OptionException, ParseException { - final Argument option = buildDateLimitArgument(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - commandLine.addValue(option, "12-12-2004"); - - try { - option.validate(commandLine, option); - } catch (OptionException exp) { - OptionException e = - new OptionException(option, ResourceConstants.ARGUMENT_UNEXPECTED_VALUE, - "12-12-2004"); - assertEquals("wrong exception message", e.getMessage(), exp.getMessage()); - } - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() - */ - public void testAppendUsage() { - final Option option = buildUsernameArgument(); - final StringBuffer buffer = new StringBuffer(); - option.appendUsage(buffer, DisplaySetting.ALL, null); - - assertEquals("", buffer.toString()); - } - - public void testAppendUsage_Infinite() { - final Option option = buildTargetsArgument(); - final StringBuffer buffer = new StringBuffer(); - option.appendUsage(buffer, DisplaySetting.ALL, null); - - assertEquals("[ [ ...]]", buffer.toString()); - } - - public void testAppendUsage_InfiniteNoOptional() { - final Option option = buildTargetsArgument(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_OPTIONAL); - option.appendUsage(buffer, settings, null); - - assertEquals(" [ ...]", buffer.toString()); - } - - public void testAppendUsage_InfiniteNoNumbering() { - final Option option = buildTargetsArgument(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED); - option.appendUsage(buffer, settings, null); - - assertEquals("[ [ ...]]", buffer.toString()); - } - - public void testAppendUsage_Minimum() { - final Option option = buildHostArgument(); - final StringBuffer buffer = new StringBuffer(); - option.appendUsage(buffer, DisplaySetting.ALL, null); - - assertEquals(" []", buffer.toString()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() - */ - public void testGetPreferredName() { - final Option option = buildPathArgument(); - assertEquals("path", option.getPreferredName()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() - */ - public void testGetDescription() { - final Option option = buildHostArgument(); - assertEquals("The host name", option.getDescription()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() - */ - public void testHelpLines() { - final Option option = buildHostArgument(); - 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()); - - assertFalse(i.hasNext()); - } - - public void testCanProcess_ConsumeRemaining() { - final Option option = buildUsernameArgument(); - - assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "--")); - } - - public void testProcess_ConsumeRemaining() - throws OptionException { - final Option option = buildPathArgument(); - final List args = list("options", "--", "--ignored", "-Dprop=val"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - option.process(commandLine, iterator); - - final List values = commandLine.getValues(option); - assertTrue(values.contains("options")); - assertTrue(values.contains("--ignored")); - assertTrue(values.contains("-Dprop=val")); - assertEquals(3, values.size()); - assertFalse(iterator.hasNext()); - } - - public void testProcess_ConsumeNothing() { - final Option option = buildPathArgument(); - final List args = list("--"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - try { - option.process(commandLine, iterator); - option.validate(commandLine); - fail("Missing Value!"); - } catch (OptionException mve) { - assertEquals(option, mve.getOption()); - assertEquals("Missing value(s) path [path ...]", mve.getMessage()); - } - - assertTrue(commandLine.getValues(option).isEmpty()); - assertFalse(iterator.hasNext()); - } - - // public void testProcess_DefinedDefaultValue() throws OptionException { - // final Option size = buildSizeArgument(); - // final List args = list(); - // final WriteableCommandLine commandLine = commandLine(size, args); - // final ListIterator iterator = args.listIterator(); - // - // size.process(commandLine, iterator); - // - // assertEquals("10", commandLine.getValue(size)); - // } - // - // public void testProcess_DefinedDefaultValues() throws OptionException { - // final Option bounds = buildBoundsArgument(); - // final List args = list(); - // final WriteableCommandLine commandLine = commandLine(bounds, args); - // final ListIterator iterator = args.listIterator(); - // - // bounds.process(commandLine, iterator); - // - // List values = new ArrayList(); - // values.add("5"); - // values.add("10"); - // assertEquals(values, commandLine.getValues(bounds)); - // } - public void testProcess_InterrogatedDefaultValue() - throws OptionException { - final Option size = buildSizeArgument(); - final List args = list(); - final WriteableCommandLine commandLine = commandLine(size, args); - final ListIterator iterator = args.listIterator(); - - size.process(commandLine, iterator); - - assertEquals(new Integer(20), commandLine.getValue(size, new Integer(20))); - } - - public void testTooFewDefaults() { - List defaults = new ArrayList(); - defaults.add("5"); - - try { - new ArgumentImpl("size", "The number of units", 2, 2, '\0', '\0', null, - ArgumentImpl.DEFAULT_CONSUME_REMAINING, defaults, 0); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ARGUMENT_TOO_FEW_DEFAULTS), - exp.getMessage()); - } - } - - public void testTooManyDefaults() { - List defaults = new ArrayList(); - defaults.add("5"); - defaults.add("10"); - defaults.add("15"); - - try { - new ArgumentImpl("size", "The number of units", 2, 2, '\0', '\0', null, - ArgumentImpl.DEFAULT_CONSUME_REMAINING, defaults, 0); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ARGUMENT_TOO_MANY_DEFAULTS), - exp.getMessage()); - } - } - - public void testProcess_InterrogatedDefaultValues() - throws OptionException { - final Option bounds = buildBoundsArgument(); - final List args = list(); - final WriteableCommandLine commandLine = commandLine(bounds, args); - final ListIterator iterator = args.listIterator(); - - bounds.process(commandLine, iterator); - - // test with values - List values = new ArrayList(); - values.add("50"); - values.add("100"); - assertEquals(values, commandLine.getValues(bounds, values)); - - // test without values - assertEquals(Collections.EMPTY_LIST, commandLine.getValues(bounds, null)); - } - - public void testProcess_StripBoundaryQuotes() - throws OptionException { - final Option bounds = buildBoundsArgument(); - final List args = list(); - final WriteableCommandLine commandLine = commandLine(bounds, args); - final ListIterator iterator = args.listIterator(); - - bounds.process(commandLine, iterator); - - List values = new ArrayList(); - values.add("50\""); - values.add("\"100"); - assertEquals(values, commandLine.getValues(bounds, values)); - } - - public void testSourceDestArgument() { - final ArgumentBuilder abuilder = new ArgumentBuilder(); - final GroupBuilder gbuilder = new GroupBuilder(); - final Argument inputfiles = - abuilder.withName("input").withMinimum(0).withMaximum(0).create(); - final Argument bad_outputfile = - abuilder.withName("output").withMinimum(1).withMaximum(2).create(); - - try { - final Argument targets = new SourceDestArgument(inputfiles, bad_outputfile); - } catch (final IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SOURCE_DEST_MUST_ENFORCE_VALUES), - exp.getMessage()); - } - - final Argument outputfile = - abuilder.withName("output").withMinimum(1).withMaximum(1).create(); - - final Argument targets = new SourceDestArgument(inputfiles, outputfile); - final StringBuffer buffer = new StringBuffer("test content"); - targets.appendUsage(buffer, Collections.EMPTY_SET, null); - - assertTrue("buffer not added", buffer.toString().startsWith("test content")); - assertFalse("space added", buffer.charAt(12) == ' '); - } -} +/* * 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.text.ParseException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.HashSet;import java.util.Iterator;i mport java.util.List;import java.util.ListIterator;import java.util.Set;import org.apache.commons.cli2.Argument;import org.apache.commons.cli2.DisplaySetting;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.ArgumentBuilder;import org.apache.commons.cli2.builder.GroupBuilder;import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;import org.apache.commons.cli2.resource.ResourceConstants;import org.apache.commons.cli2.resource.ResourceHelper;import org.apache.commons.cli2.validation.DateValidator;import org.apache.commons.cli2.validation.DateValidatorTest;/** * @author Rob Oxspring */public class ArgumentTest extends ArgumentTestCase { private ResourceHelper resources = ResourceHelper.getResourceHelper(); public static Argument buildUsernameArgument() { return new ArgumentImpl("usernam e", "The user to connect as", 1, 1, '\0', '\0', null, ArgumentImpl.DEFAULT_CONSUME_REMAINING, null, 0); } public static Argument buildHostArgument() { return new ArgumentImpl("host", "The host name", 2, 3, '\0', ',', null, null, null, 0); } public static Argument buildPathArgument() { return new ArgumentImpl("path", "The place to look for files", 1, Integer.MAX_VALUE, '=', ';', null, ArgumentImpl.DEFAULT_CONSUME_REMAINING, null, 0); } public static Argument buildDateLimitArgument() { return new ArgumentImpl("limit", "the last acceptable date", 0, 1, '=', '\0', new DateValidator(DateValidatorTest.YYYY_MM_DD), null, null, 0); } public static Argument buildTargetsArgument() { return new ArgumentImpl("target", "The targets ant should build", 0, Integer.MAX_VALUE, '\0', ',', null, null, null, 0); } pu blic static Argument buildSizeArgument() { List defaults = new ArrayList(); defaults.add("10"); return new ArgumentImpl("size", "The number of units", 1, 1, '\0', '\0', null, ArgumentImpl.DEFAULT_CONSUME_REMAINING, defaults, 0); } public static Argument buildBoundsArgument() { List defaults = new ArrayList(); defaults.add("5"); defaults.add("10"); return new ArgumentImpl("size", "The number of units", 2, 2, '\0', '\0', null, ArgumentImpl.DEFAULT_CONSUME_REMAINING, defaults, 0); } public void testNew() { try { new ArgumentImpl("limit", "the last acceptable date", 10, 5, '=', '\0', new DateValidator(DateValidatorTest.YYYY_MM_DD), null, null, 0); } catch (IllegalArgumentException e) { assertEquals(resources.getMessage("Argument.minimum.exceeds.maximum"), e.getMessage()); } { ArgumentImpl arg = new ArgumentImpl(null, "the last acceptable date", 5, 5, '=', '\0', new DateValidator(DateValidatorTest.YYYY_MM_DD), null, null, 0); assertEquals("wrong arg name", "arg", arg.getPreferredName()); } { List defaults = new ArrayList(); try { new ArgumentImpl(null, "the last acceptable date", 1, 1, '=', '\0', new DateValidator(DateValidatorTest.YYYY_MM_DD), null, defaults, 0); } catch (IllegalArgumentException exp) { assertEquals(resources.getMessage("Argument.too.few.defaults"), exp.getMessage()); } } try { List defaults = new ArrayList(); defaults.add("1"); defaults.add("2"); new ArgumentImpl(null, "the last acceptable date", 1, 1, '=', '\0', new DateValidator(DateValidatorTest.YYYY_MM_DD), nu ll, defaults, 0); } catch (IllegalArgumentException exp) { assertEquals(resources.getMessage("Argument.too.many.defaults"), exp.getMessage()); } } /* * (non-Javadoc) * * @see org.apache.commons.cli2.ArgumentTestCase#testProcessValues() */ public void testProcessValues() throws OptionException { final Argument option = buildUsernameArgument(); final List args = list("rob"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.processValues(commandLine, iterator, option); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("username")); assertEquals("rob", commandLine.getValue(option)); } public void testProcessValues_BoundaryQuotes() throws OptionException { final Argument option = buildUsernameArgument(); final List args = list("\"rob\""); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.processValues(commandLine, iterator, option); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("username")); assertEquals("rob", commandLine.getValue(option)); } public void testProcessValues_SpareValues() throws OptionException { final Argument option = buildUsernameArgument(); final List args = list("rob", "secret"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.processValues(commandLine, iterator, option); assertTrue(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("username")); assertEquals("rob", comma ndLine.getValue(option)); } public void testProcessValues_Optional() { final Argument option = buildTargetsArgument(); final List args = list(); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); try { option.processValues(commandLine, iterator, option); } catch (final OptionException mve) { assertEquals(option, mve.getOption()); assertEquals("Missing value(s) target [target ...]", mve.getMessage()); } assertFalse(iterator.hasNext()); assertFalse(commandLine.hasOption(option)); assertFalse(commandLine.hasOption("username")); assertTrue(commandLine.getValues(option).isEmpty()); } public void testProcessValues_Multiple() throws OptionException { final Argument option = buildTargetsArgument(); final List args = list("compile", "test", "docs"); final WriteableComma ndLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.processValues(commandLine, iterator, option); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("target")); assertFalse(commandLine.getValues(option).isEmpty()); assertListContentsEqual(args, commandLine.getValues(option)); } public void testProcessValues_Contracted() throws OptionException { final Argument option = buildTargetsArgument(); final List args = list("compile,test,javadoc", "checkstyle,jdepend"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.processValues(commandLine, iterator, option); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("target")); assertListContentsEqual(list("compile", "test", "javadoc", "checkstyle", "jdepend"), commandLine.getValues(option)); } public void testProcessValues_ContractedTooFew() { final Argument option = buildHostArgument(); final List args = list("box1"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); try { option.processValues(commandLine, iterator, option); option.validate(commandLine); fail("Expected MissingValueException"); } catch (OptionException mve) { assertSame(option, mve.getOption()); } } public void testProcessValues_ContractedTooMany() { final Argument option = buildHostArgument(); final List args = list("box1,box2,box3,box4"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.list Iterator(); try { option.processValues(commandLine, iterator, option); option.validate(commandLine); fail("Expected MissingValueException"); } catch (OptionException mve) { assertSame(option, mve.getOption()); } } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() */ public void testCanProcess() { final Argument option = buildTargetsArgument(); assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "any value")); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() */ public void testPrefixes() { final Argument option = buildTargetsArgument(); assertTrue(option.getPrefixes().isEmpty()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testProcess() */ public void testProcess() throws OptionException { final Argument option = buildPathArgument(); final List args = list("-path=/lib;/usr/lib;/usr/local/lib"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.process(commandLine, iterator); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("path")); assertListContentsEqual(list("-path=/lib", "/usr/lib", "/usr/local/lib"), commandLine.getValues(option)); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testTriggers() */ public void testTriggers() { final Argument option = buildTargetsArgument(); assertTrue(option.getTriggers().isEmpty()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testValidate() */ public void testValidate() throws Option Exception { final Argument option = buildUsernameArgument(); final WriteableCommandLine commandLine = commandLine(option, list()); commandLine.addValue(option, "rob"); option.validate(commandLine); } public void testValidate_Minimum() { final Argument option = buildUsernameArgument(); final WriteableCommandLine commandLine = commandLine(option, list()); try { option.validate(commandLine); fail("UnexpectedValue"); } catch (OptionException mve) { assertEquals(option, mve.getOption()); } } public void testRequired() { { final Argument arg = buildBoundsArgument(); assertTrue("not required", arg.isRequired()); } { final Argument arg = buildTargetsArgument(); assertFalse("should not be required", arg.isRequired()); } } public void testValidate_Maximum() { final Argument option = buildUsernam eArgument(); final WriteableCommandLine commandLine = commandLine(option, list()); commandLine.addValue(option, "rob"); commandLine.addValue(option, "oxspring"); try { option.validate(commandLine); fail("UnexpectedValue"); } catch (OptionException uve) { assertEquals(option, uve.getOption()); } } public void testValidate_Validator() throws OptionException, ParseException { final Argument option = buildDateLimitArgument(); final WriteableCommandLine commandLine = commandLine(option, list()); commandLine.addValue(option, "2004-01-01"); option.validate(commandLine, option); assertContentsEqual(Arrays.asList(new Object[] { DateValidatorTest.YYYY_MM_DD.parse("2004-01-01") }), commandLine.getValues(option)); } public void testValidate_ValidatorInvalidDate() throw s OptionException, ParseException { final Argument option = buildDateLimitArgument(); final WriteableCommandLine commandLine = commandLine(option, list()); commandLine.addValue(option, "12-12-2004"); try { option.validate(commandLine, option); } catch (OptionException exp) { OptionException e = new OptionException(option, ResourceConstants.ARGUMENT_UNEXPECTED_VALUE, "12-12-2004"); assertEquals("wrong exception message", e.getMessage(), exp.getMessage()); } } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() */ public void testAppendUsage() { final Option option = buildUsernameArgument(); final StringBuffer buffer = new StringBuffer(); option.appendUsage(buffer, DisplaySetting.ALL, null); assertEquals("", buffer.toString()); } public void testAppendUs age_Infinite() { final Option option = buildTargetsArgument(); final StringBuffer buffer = new StringBuffer(); option.appendUsage(buffer, DisplaySetting.ALL, null); assertEquals("[ [ ...]]", buffer.toString()); } public void testAppendUsage_InfiniteNoOptional() { final Option option = buildTargetsArgument(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_OPTIONAL); option.appendUsage(buffer, settings, null); assertEquals(" [ ...]", buffer.toString()); } public void testAppendUsage_InfiniteNoNumbering() { final Option option = buildTargetsArgument(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED); option.appendUsage(buffer , settings, null); assertEquals("[ [ ...]]", buffer.toString()); } public void testAppendUsage_Minimum() { final Option option = buildHostArgument(); final StringBuffer buffer = new StringBuffer(); option.appendUsage(buffer, DisplaySetting.ALL, null); assertEquals(" []", buffer.toString()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() */ public void testGetPreferredName() { final Option option = buildPathArgument(); assertEquals("path", option.getPreferredName()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() */ public void testGetDescription() { final Option option = buildHostArgument(); assertEquals("The host name", option.getDescription()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#te stHelpLines() */ public void testHelpLines() { final Option option = buildHostArgument(); 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()); assertFalse(i.hasNext()); } public void testCanProcess_ConsumeRemaining() { final Option option = buildUsernameArgument(); assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "--")); } public void testProcess_ConsumeRemaining() throws OptionException { final Option option = buildPathArgument(); final List args = list("options", "--", "--ignored", "-Dprop=val"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.process(commandLine, iterator); f inal List values = commandLine.getValues(option); assertTrue(values.contains("options")); assertTrue(values.contains("--ignored")); assertTrue(values.contains("-Dprop=val")); assertEquals(3, values.size()); assertFalse(iterator.hasNext()); } public void testProcess_ConsumeNothing() { final Option option = buildPathArgument(); final List args = list("--"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); try { option.process(commandLine, iterator); option.validate(commandLine); fail("Missing Value!"); } catch (OptionException mve) { assertEquals(option, mve.getOption()); assertEquals("Missing value(s) path [path ...]", mve.getMessage()); } assertTrue(commandLine.getValues(option).isEmpty()); assertFalse(iterator.hasNext()); } // public void t estProcess_DefinedDefaultValue() throws OptionException { // final Option size = buildSizeArgument(); // final List args = list(); // final WriteableCommandLine commandLine = commandLine(size, args); // final ListIterator iterator = args.listIterator(); // // size.process(commandLine, iterator); // // assertEquals("10", commandLine.getValue(size)); // } // // public void testProcess_DefinedDefaultValues() throws OptionException { // final Option bounds = buildBoundsArgument(); // final List args = list(); // final WriteableCommandLine commandLine = commandLine(bounds, args); // final ListIterator iterator = args.listIterator(); // // bounds.process(commandLine, iterator); // // List values = new ArrayList(); // values.add("5"); // values.add("10"); // assertEquals(values, commandLine.getValues(bounds) ); // } public void testProcess_InterrogatedDefaultValue() throws OptionException { final Option size = buildSizeArgument(); final List args = list(); final WriteableCommandLine commandLine = commandLine(size, args); final ListIterator iterator = args.listIterator(); size.process(commandLine, iterator); assertEquals(new Integer(20), commandLine.getValue(size, new Integer(20))); } public void testTooFewDefaults() { List defaults = new ArrayList(); defaults.add("5"); try { new ArgumentImpl("size", "The number of units", 2, 2, '\0', '\0', null, ArgumentImpl.DEFAULT_CONSUME_REMAINING, defaults, 0); } catch (IllegalArgumentException exp) { assertEquals("wrong exception message", ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ARGUMENT_TOO_FEW_DEFAULTS), exp.getMessage()); } } public void testTooManyDefaults() { List defaults = new ArrayList(); defaults.add("5"); defaults.add("10"); defaults.add("15"); try { new ArgumentImpl("size", "The number of units", 2, 2, '\0', '\0', null, ArgumentImpl.DEFAULT_CONSUME_REMAINING, defaults, 0); } catch (IllegalArgumentException exp) { assertEquals("wrong exception message", ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ARGUMENT_TOO_MANY_DEFAULTS), exp.getMessage()); } } public void testProcess_InterrogatedDefaultValues() throws OptionException { final Option bounds = buildBoundsArgument(); final List args = list(); final WriteableCommandLine commandLine = commandLine(bounds, args); final ListIterator iterator = args.listIterator(); bounds.process(commandLine, iterator); // test with values List values = new ArrayList(); values.add("50"); values.add("100"); assertEquals(values, commandLine.getValues(bounds, values)); // test without values assertEquals(Collections.EMPTY_LIST, commandLine.getValues(bounds, null)); } public void testProcess_StripBoundaryQuotes() throws OptionException { final Option bounds = buildBoundsArgument(); final List args = list(); final WriteableCommandLine commandLine = commandLine(bounds, args); final ListIterator iterator = args.listIterator(); bounds.process(commandLine, iterator); List values = new ArrayList(); values.add("50\""); values.add("\"100"); assertEquals(values, commandLine.getValues(bounds, values)); } public void testSourceDestArgument() { final ArgumentBuilder abuilder = new ArgumentBuilder(); final GroupBuilder gbuilder = new GroupBuilder(); final Argument inputfiles = abuilder.withName("input").withMinimum(0).withMaximum(0).create(); final Argument bad_outputfile = abuilder.withName("output").withMinimum(1).withMaximum(2).create(); try { final Argument targets = new SourceDestArgument(inputfiles, bad_outputfile); } catch (final IllegalArgumentException exp) { assertEquals("wrong exception message", ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SOURCE_DEST_MUST_ENFORCE_VALUES), exp.getMessage()); } final Argument outputfile = abuilder.withName("output").withMinimum(1).withMaximum(1).create(); final Argument targets = new SourceDestArgument(inputfiles, outputfile); final StringBuffer buffer = new StringBuffer("test content"); targets.appendUsage(buffer, Collections.EMPTY_SET, null); assertTrue("buffer not added", buffer.toString().startsWith("test content")); assertFalse("space added", buffer.charAt(12) == ' '); }} \ No newline at end of file Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ArgumentTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ArgumentTestCase.java?rev=639941&r1=639940&r2=639941&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ArgumentTestCase.java (original) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ArgumentTestCase.java Fri Mar 21 19:49:41 2008 @@ -1,28 +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 ArgumentTestCase extends OptionTestCase { - - public abstract void testProcessValues() 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 ArgumentTestCase extends OptionTestCase { public abstract void testProcessValues() throws OptionException;} \ No newline at end of file Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/CommandTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/CommandTest.java?rev=639941&r1=639940&r2=639941&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/CommandTest.java (original) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/CommandTest.java Fri Mar 21 19:49:41 2008 @@ -1,249 +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.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.ListIterator; -import java.util.Set; - -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.Parent; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.commandline.WriteableCommandLineImpl; -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; - -/** - * @author Rob Oxspring - * - * To change the template for this generated type comment go to - * Window>Preferences>Java>Code Generation>Code and Comments - */ -public class CommandTest - extends ParentTestCase { - public static Command buildStartCommand() { - return new Command("start", "Begins the process", Collections.singleton("go"), false, null, - null, 0); - } - - public static Command buildCommitCommand() { - return new Command("commit", "Commit the changes to the database", null, true, null, null, 0); - } - - public static Command buildLoginCommand() { - return new Command("login", "Initiates a session for the user", null, false, - ArgumentTest.buildUsernameArgument(), null, 0); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.ParentTestCase#testProcessParent() - */ - public void testProcessParent() - throws OptionException { - final Command option = buildStartCommand(); - final List args = list("go"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.processParent(commandLine, iterator); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("start")); - assertTrue(commandLine.hasOption("go")); - assertTrue(commandLine.getValues(option).isEmpty()); - } - - public void testProcessParent_Spare() - throws OptionException { - final Command option = buildLoginCommand(); - final List args = list("login", "rob"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.processParent(commandLine, iterator); - - assertEquals("rob", iterator.next()); - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("login")); - assertTrue(commandLine.getValues(option).isEmpty()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() - */ - public void testCanProcess() { - final Command option = buildStartCommand(); - assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "start")); - } - - public void testCanProcess_BadMatch() { - final Command option = buildStartCommand(); - assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "stop")); - } - - public void testCanProcess_Alias() { - final Command option = buildStartCommand(); - assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "go")); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() - */ - public void testPrefixes() { - final Command option = buildStartCommand(); - assertTrue(option.getPrefixes().isEmpty()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testProcess() - */ - public void testProcess() - throws OptionException { - final Command option = buildLoginCommand(); - final List args = list("login", "rob"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.process(commandLine, iterator); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("login")); - assertEquals("rob", commandLine.getValue(option)); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testTriggers() - */ - public void testTriggers() { - final Command option = buildStartCommand(); - final Set triggers = option.getTriggers(); - assertContentsEqual(list("start", "go"), triggers); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testValidate() - */ - public void testValidate() { - final Parent option = buildCommitCommand(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - try { - option.validate(commandLine); - fail("Missing an option"); - } catch (OptionException moe) { - assertSame(option, moe.getOption()); - } - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() - */ - public void testAppendUsage() { - final Option option = buildStartCommand(); - final StringBuffer buffer = new StringBuffer(); - option.appendUsage(buffer, DisplaySetting.ALL, null); - - assertEquals("[start (go)]", buffer.toString()); - } - - public void testNullPreferredName() { - try { - new Command(null, "", Collections.singleton("go"), false, null, null, 0); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception name", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT), - exp.getMessage()); - } - } - - public void testEmotyPreferredName() { - try { - new Command("", "", Collections.singleton("go"), false, null, null, 0); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception name", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT), - exp.getMessage()); - } - } - - public void testAppendUsage_NoOptional() { - final Option option = buildStartCommand(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_OPTIONAL); - option.appendUsage(buffer, settings, null); - - assertEquals("start (go)", buffer.toString()); - } - - public void testAppendUsage_NoAlias() { - final Option option = buildStartCommand(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_ALIASES); - option.appendUsage(buffer, settings, null); - - assertEquals("[start]", buffer.toString()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() - */ - public void testGetPreferredName() { - final Option option = buildStartCommand(); - assertEquals("start", option.getPreferredName()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() - */ - public void testGetDescription() { - final Option option = buildLoginCommand(); - assertEquals("Initiates a session for the user", option.getDescription()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() - */ - public void testHelpLines() { - // TODO Auto-generated method stub - } -} +/** * 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.Collections;import java.util.HashSet;import java.util.List;import java.util.ListIterator;import java.util.Set;import org.apache.commons.cli2.Displ aySetting;import org.apache.commons.cli2.Option;import org.apache.commons.cli2.OptionException;import org.apache.commons.cli2.Parent;import org.apache.commons.cli2.WriteableCommandLine;import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;import org.apache.commons.cli2.resource.ResourceConstants;import org.apache.commons.cli2.resource.ResourceHelper;/** * @author Rob Oxspring * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class CommandTest extends ParentTestCase { public static Command buildStartCommand() { return new Command("start", "Begins the process", Collections.singleton("go"), false, null, null, 0); } public static Command buildCommitCommand() { return new Command("commit", "Commit the changes to the database", null, true, null, null, 0); } public static Command buildLoginCommand() { return new Command(" login", "Initiates a session for the user", null, false, ArgumentTest.buildUsernameArgument(), null, 0); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.ParentTestCase#testProcessParent() */ public void testProcessParent() throws OptionException { final Command option = buildStartCommand(); final List args = list("go"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.processParent(commandLine, iterator); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("start")); assertTrue(commandLine.hasOption("go")); assertTrue(commandLine.getValues(option).isEmpty()); } public void testProcessParent_Spare() throws OptionException { final Command option = buildLoginCommand(); final List args = l ist("login", "rob"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.processParent(commandLine, iterator); assertEquals("rob", iterator.next()); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("login")); assertTrue(commandLine.getValues(option).isEmpty()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() */ public void testCanProcess() { final Command option = buildStartCommand(); assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "start")); } public void testCanProcess_BadMatch() { final Command option = buildStartCommand(); assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "stop")); } public void testCanProcess_Alias() { final Co mmand option = buildStartCommand(); assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "go")); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() */ public void testPrefixes() { final Command option = buildStartCommand(); assertTrue(option.getPrefixes().isEmpty()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testProcess() */ public void testProcess() throws OptionException { final Command option = buildLoginCommand(); final List args = list("login", "rob"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.process(commandLine, iterator); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("login")); assertEquals("rob", commandL ine.getValue(option)); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testTriggers() */ public void testTriggers() { final Command option = buildStartCommand(); final Set triggers = option.getTriggers(); assertContentsEqual(list("start", "go"), triggers); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testValidate() */ public void testValidate() { final Parent option = buildCommitCommand(); final WriteableCommandLine commandLine = commandLine(option, list()); try { option.validate(commandLine); fail("Missing an option"); } catch (OptionException moe) { assertSame(option, moe.getOption()); } } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() */ public void testAppendUsage() { final Option option = buildStartCommand(); fina l StringBuffer buffer = new StringBuffer(); option.appendUsage(buffer, DisplaySetting.ALL, null); assertEquals("[start (go)]", buffer.toString()); } public void testNullPreferredName() { try { new Command(null, "", Collections.singleton("go"), false, null, null, 0); } catch (IllegalArgumentException exp) { assertEquals("wrong exception name", ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT), exp.getMessage()); } } public void testEmotyPreferredName() { try { new Command("", "", Collections.singleton("go"), false, null, null, 0); } catch (IllegalArgumentException exp) { assertEquals("wrong exception name", ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT), exp.getMessage()); } } public void testAppendUsage_NoOptional() { final Option option = buildStartCommand(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_OPTIONAL); option.appendUsage(buffer, settings, null); assertEquals("start (go)", buffer.toString()); } public void testAppendUsage_NoAlias() { final Option option = buildStartCommand(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_ALIASES); option.appendUsage(buffer, settings, null); assertEquals("[start]", buffer.toString()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() */ public void testGetPreferredName() { final Option option = buildStartCommand(); assertEquals("start", o ption.getPreferredName()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() */ public void testGetDescription() { final Option option = buildLoginCommand(); assertEquals("Initiates a session for the user", option.getDescription()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() */ public void testHelpLines() { // TODO Auto-generated method stub }} \ No newline at end of file Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/DefaultOptionTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/DefaultOptionTest.java?rev=639941&r1=639940&r2=639941&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/DefaultOptionTest.java (original) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/DefaultOptionTest.java Fri Mar 21 19:49:41 2008 @@ -1,227 +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.HashSet; -import java.util.List; -import java.util.ListIterator; -import java.util.Set; - -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.Parent; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.commandline.WriteableCommandLineImpl; - -/** - * @author roberto - * - * To change the template for this generated type comment go to - * Window>Preferences>Java>Code Generation>Code and Comments - */ -public class DefaultOptionTest extends ParentTestCase { - - public static DefaultOption buildHelpOption() { - final Set aliases = new HashSet(list("-h", "-?")); - return new DefaultOption( - "-", - "--", - true, - "--help", - "Displays the help", - aliases, - aliases, - false, - null, - null, - 'h'); - } - - public static DefaultOption buildXOption() { - return new DefaultOption( - "-", - "--", - true, - "-X", - "This is needed", - null, - null, - true, - null, - null, - 'X'); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.ParentTestCase#testProcessParent() - */ - public void testProcessParent() throws OptionException { - final DefaultOption option = buildHelpOption(); - final List args = list("--help"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.processParent(commandLine, iterator); - - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("--help")); - assertTrue(commandLine.hasOption("-?")); - assertTrue(commandLine.getValues(option).isEmpty()); - } - - public void testProcessParent_Burst() throws OptionException { - final DefaultOption option = buildHelpOption(); - final List args = list("-help"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - option.processParent(commandLine, iterator); - - assertEquals("-elp", iterator.next()); - assertFalse(iterator.hasNext()); - assertTrue(commandLine.hasOption(option)); - assertTrue(commandLine.hasOption("--help")); - assertTrue(commandLine.hasOption("-?")); - assertTrue(commandLine.getValues(option).isEmpty()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() - */ - public void testCanProcess() { - final DefaultOption option = buildHelpOption(); - assertTrue(option.canProcess(new WriteableCommandLineImpl(option,null), "-?")); - } - - public void testCanProcess_BadMatch() { - final DefaultOption option = buildHelpOption(); - assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), "-H")); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() - */ - public void testPrefixes() { - final DefaultOption option = buildHelpOption(); - assertContentsEqual(list("-", "--"), option.getPrefixes()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testProcess() - */ - public void testProcess() { - // TODO Auto-generated method stub - - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testTriggers() - */ - public void testTriggers() { - final DefaultOption option = buildHelpOption(); - assertContentsEqual(list("-?", "-h", "--help"), option.getTriggers()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testValidate() - */ - public void testValidate() { - final Parent option = buildXOption(); - final WriteableCommandLine commandLine = commandLine(option, list()); - - try { - option.validate(commandLine); - fail("Missing an option"); - } - catch (OptionException moe) { - assertSame(option, moe.getOption()); - } - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() - */ - public void testAppendUsage() { - final Option option = buildHelpOption(); - final StringBuffer buffer = new StringBuffer(); - option.appendUsage(buffer, DisplaySetting.ALL, null); - - assertEquals("[--help (-?,-h)]", buffer.toString()); - } - - public void testAppendUsage_NoOptional() { - final Option option = buildHelpOption(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_OPTIONAL); - option.appendUsage(buffer, settings, null); - - assertEquals("--help (-?,-h)", buffer.toString()); - } - - public void testAppendUsage_NoAlias() { - final Option option = buildHelpOption(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_ALIASES); - option.appendUsage(buffer, settings, null); - - assertEquals("[--help]", buffer.toString()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() - */ - public void testGetPreferredName() { - final Option option = buildHelpOption(); - assertEquals("--help", option.getPreferredName()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() - */ - public void testGetDescription() { - final Option option = buildHelpOption(); - assertEquals("Displays the help", option.getDescription()); - } - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() - */ - public void testHelpLines() { - // TODO Auto-generated method stub - } -} +/** * 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.HashSet;import java.util.List;import java.util.ListIterator;import java.util.Set;import org.apache.commons.cli2.DisplaySetting;import org.apache.c ommons.cli2.Option;import org.apache.commons.cli2.OptionException;import org.apache.commons.cli2.Parent;import org.apache.commons.cli2.WriteableCommandLine;import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;/** * @author roberto * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class DefaultOptionTest extends ParentTestCase { public static DefaultOption buildHelpOption() { final Set aliases = new HashSet(list("-h", "-?")); return new DefaultOption( "-", "--", true, "--help", "Displays the help", aliases, aliases, false, null, null, 'h'); } public static DefaultOption buildXOption() { return new DefaultOption( "-", "--", true, "-X", "This is needed", null, null, true, null, null, 'X'); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.ParentTestCase#testProcessParent() */ public void testProcessParent() throws OptionException { final DefaultOption option = buildHelpOption(); final List args = list("--help"); final WriteableCommandLine commandLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.processParent(commandLine, iterator); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("--help")); assertTrue(commandLine.hasOption("-?")); assertTrue(commandLine.getValues(option).isEmpty()); } public void testProcessParent_Burst() throws OptionException { final DefaultOption option = buildHelpOption(); final List args = list("-help"); final WriteableCommandLine comman dLine = commandLine(option, args); final ListIterator iterator = args.listIterator(); option.processParent(commandLine, iterator); assertEquals("-elp", iterator.next()); assertFalse(iterator.hasNext()); assertTrue(commandLine.hasOption(option)); assertTrue(commandLine.hasOption("--help")); assertTrue(commandLine.hasOption("-?")); assertTrue(commandLine.getValues(option).isEmpty()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() */ public void testCanProcess() { final DefaultOption option = buildHelpOption(); assertTrue(option.canProcess(new WriteableCommandLineImpl(option,null), "-?")); } public void testCanProcess_BadMatch() { final DefaultOption option = buildHelpOption(); assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), "-H")); } /* * (non-Javadoc) * * @see org.apache.commons.cli 2.OptionTestCase#testPrefixes() */ public void testPrefixes() { final DefaultOption option = buildHelpOption(); assertContentsEqual(list("-", "--"), option.getPrefixes()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testProcess() */ public void testProcess() { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testTriggers() */ public void testTriggers() { final DefaultOption option = buildHelpOption(); assertContentsEqual(list("-?", "-h", "--help"), option.getTriggers()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testValidate() */ public void testValidate() { final Parent option = buildXOption(); final WriteableCommandLine commandLine = commandLine(option, list()); try { option.validate(commandLine); fail("Missing an option"); } catch (OptionException moe) { assertSame(option, moe.getOption()); } } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() */ public void testAppendUsage() { final Option option = buildHelpOption(); final StringBuffer buffer = new StringBuffer(); option.appendUsage(buffer, DisplaySetting.ALL, null); assertEquals("[--help (-?,-h)]", buffer.toString()); } public void testAppendUsage_NoOptional() { final Option option = buildHelpOption(); final StringBuffer buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_OPTIONAL); option.appendUsage(buffer, settings, null); assertEquals("--help (-?,-h)", buffer.toString()); } public void testAppendUsage_NoAlias() { final Option option = buildHelpOption(); final StringBuffe r buffer = new StringBuffer(); final Set settings = new HashSet(DisplaySetting.ALL); settings.remove(DisplaySetting.DISPLAY_ALIASES); option.appendUsage(buffer, settings, null); assertEquals("[--help]", buffer.toString()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() */ public void testGetPreferredName() { final Option option = buildHelpOption(); assertEquals("--help", option.getPreferredName()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() */ public void testGetDescription() { final Option option = buildHelpOption(); assertEquals("Displays the help", option.getDescription()); } /* * (non-Javadoc) * * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() */ public void testHelpLines() { // TODO Auto-generated method stub }} \ No newline at end of file