http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/option/ParentTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/option/ParentTestCase.java b/src/test/org/apache/commons/cli2/option/ParentTestCase.java deleted file mode 100644 index 5851c25..0000000 --- a/src/test/org/apache/commons/cli2/option/ParentTestCase.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright 2003-2004 The Apache Software Foundation - * - * Licensed 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 ParentTestCase extends OptionTestCase { - public abstract void testProcessParent() throws OptionException; -}
http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/option/PropertyOptionTest.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/option/PropertyOptionTest.java b/src/test/org/apache/commons/cli2/option/PropertyOptionTest.java deleted file mode 100644 index 1d020f5..0000000 --- a/src/test/org/apache/commons/cli2/option/PropertyOptionTest.java +++ /dev/null @@ -1,232 +0,0 @@ -/** - * Copyright 2003-2005 The Apache Software Foundation - * - * Licensed 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.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Set; - -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.commandline.WriteableCommandLineImpl; - -/** - * @author Rob Oxspring - */ -public class PropertyOptionTest extends OptionTestCase { - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() - */ - public void testCanProcess() { - final Option option = new PropertyOption(); - assertTrue(option.canProcess(new WriteableCommandLineImpl(option,null), "-Dmyprop=myval")); - } - - public void testCanProcess_Null() { - final Option option = new PropertyOption(); - assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), (String) null)); - } - - public void testCanProcess_TooShort() { - final Option option = new PropertyOption(); - assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), "-D")); - } - - public void testCanProcess_BadMatch() { - final Option option = new PropertyOption(); - assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null),"-dump")); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() - */ - public void testPrefixes() { - final Option option = new PropertyOption(); - assertContentsEqual(list("-D"), option.getPrefixes()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testProcess() - */ - public void testProcess() throws OptionException { - final Option option = new PropertyOption(); - final List args = list("-Dmyprop=myvalue"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - option.process(commandLine, iterator); - assertEquals("myvalue", commandLine.getProperty("myprop")); - assertFalse(iterator.hasNext()); - assertEquals(1, commandLine.getProperties().size()); - } - - public void testProcess_UnexpectedOptionException() { - final Option option = new PropertyOption(); - final List args = list("--help"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - try { - option.process(commandLine, iterator); - fail("UnexpectedOption"); - } - catch (final OptionException uoe) { - assertEquals(option, uoe.getOption()); - assertEquals( - "Unexpected --help while processing -Dproperty=value", - uoe.getMessage()); - } - } - - public void testProcess_BadPropertyException() throws OptionException { - final Option option = new PropertyOption(); - final List args = list("-Dmyprop"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - option.process(commandLine, iterator); - - assertEquals("true", commandLine.getProperty("myprop")); - } - - public void testProcess_SetToEmpty() throws OptionException { - final Option option = new PropertyOption(); - final List args = list("-Dmyprop="); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - option.process(commandLine, iterator); - assertEquals("", commandLine.getProperty("myprop")); - assertFalse(iterator.hasNext()); - assertEquals(1, commandLine.getProperties().size()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testTriggers() - */ - public void testTriggers() { - final Option option = new PropertyOption(); - - assertContentsEqual(list("-D"), option.getTriggers()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testValidate() - */ - public void testValidate() throws OptionException { - final Option option = new PropertyOption(); - final List args = list("-Dproperty=value"); - final WriteableCommandLine commandLine = commandLine(option, args); - final ListIterator iterator = args.listIterator(); - - option.process(commandLine, iterator); - - option.validate(commandLine); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() - */ - public void testAppendUsage() { - final Option option = new PropertyOption(); - final StringBuffer buffer = new StringBuffer(); - option.appendUsage(buffer, DisplaySetting.ALL, null); - - assertEquals("-D<property>=<value>", buffer.toString()); - } - - public void testAppendUsage_Hidden() { - final Option option = new PropertyOption(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_PROPERTY_OPTION); - option.appendUsage(buffer, settings, null); - - assertEquals("", buffer.toString()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() - */ - public void testGetPreferredName() { - final Option option = new PropertyOption(); - assertEquals("-D", option.getPreferredName()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() - */ - public void testGetDescription() { - final Option option = new PropertyOption(); - assertEquals( - "Passes properties and values to the application", - option.getDescription()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() - */ - public void testHelpLines() { - final Option option = new PropertyOption(); - 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()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() - */ - public void testHelpLines_NoDisplay() { - final Option option = new PropertyOption(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_PROPERTY_OPTION); - final List lines = option.helpLines(0, settings, null); - final Iterator i = lines.iterator(); - - assertFalse(i.hasNext()); - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/option/SwitchTest.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/option/SwitchTest.java b/src/test/org/apache/commons/cli2/option/SwitchTest.java deleted file mode 100644 index ea5e38e..0000000 --- a/src/test/org/apache/commons/cli2/option/SwitchTest.java +++ /dev/null @@ -1,307 +0,0 @@ -/** - * Copyright 2003-2005 The Apache Software Foundation - * - * Licensed 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; -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 SwitchTest - extends ParentTestCase { - public static Switch buildDisplaySwitch() { - final Set aliases = new HashSet(); - aliases.add("d"); - aliases.add("disp"); - - return new Switch("+", "-", "display", aliases, "Sets whether to display to screen", true, - null, null, 'd', null); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.ParentTestCase#testProcessParent() - */ - public void testProcessParent() - throws OptionException { - final Switch option = buildDisplaySwitch(); - final List args = list("+d"); - 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("+d")); - assertTrue(commandLine.hasOption("-display")); - assertEquals(Boolean.TRUE, commandLine.getSwitch("-d")); - assertTrue(commandLine.getValues(option).isEmpty()); - } - - public void testProcessParent_Disabled() - throws OptionException { - final Switch option = buildDisplaySwitch(); - final List args = list("-disp"); - 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("+d")); - assertTrue(commandLine.hasOption("-display")); - assertEquals(Boolean.FALSE, commandLine.getSwitch("-d")); - assertTrue(commandLine.getValues(option).isEmpty()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() - */ - public void testCanProcess() { - final Switch option = buildDisplaySwitch(); - assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "+d")); - } - - public void testCanProcess_BadMatch() { - final Switch option = buildDisplaySwitch(); - assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "-dont")); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() - */ - public void testPrefixes() { - final Switch option = buildDisplaySwitch(); - 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 Switch option = buildDisplaySwitch(); - assertContentsEqual(list("-d", "+d", "-disp", "+disp", "+display", "-display"), - option.getTriggers()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testValidate() - */ - public void testValidate() { - final Parent option = buildDisplaySwitch(); - 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 = buildDisplaySwitch(); - final StringBuffer buffer = new StringBuffer(); - option.appendUsage(buffer, DisplaySetting.ALL, null); - - assertEquals("+display|-display (+d|-d,+disp|-disp)", buffer.toString()); - } - - public void testAppendUsage_NoAlias() { - final Option option = buildDisplaySwitch(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_ALIASES); - option.appendUsage(buffer, settings, null); - - assertEquals("+display|-display", buffer.toString()); - } - - public void testAppendUsage_NoDisabled() { - final Option option = buildDisplaySwitch(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_SWITCH_DISABLED); - option.appendUsage(buffer, settings, null); - - assertEquals("+display (+d,+disp)", buffer.toString()); - } - - public void testAppendUsage_NoEnabled() { - final Option option = buildDisplaySwitch(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_SWITCH_ENABLED); - option.appendUsage(buffer, settings, null); - - assertEquals("-display (-d,-disp)", buffer.toString()); - } - - public void testAppendUsage_NoDisabledOrEnabled() { - final Option option = buildDisplaySwitch(); - final StringBuffer buffer = new StringBuffer(); - final Set settings = new HashSet(DisplaySetting.ALL); - settings.remove(DisplaySetting.DISPLAY_SWITCH_DISABLED); - settings.remove(DisplaySetting.DISPLAY_SWITCH_ENABLED); - option.appendUsage(buffer, settings, null); - - assertEquals("+display (+d,+disp)", buffer.toString()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() - */ - public void testGetPreferredName() { - final Option option = buildDisplaySwitch(); - assertEquals("+display", option.getPreferredName()); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() - */ - public void testGetDescription() { - final Option option = buildDisplaySwitch(); - assertEquals("Sets whether to display to screen", option.getDescription()); - } - - public void testNullPreferredName() { - try { - new Switch("+", "-", null, null, "Sets whether to display to screen", true, null, null, - 'd', null); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT), - exp.getMessage()); - } - } - - public void testEmptyPreferredName() { - try { - new Switch("+", "-", "", null, "Sets whether to display to screen", true, null, null, - 'd', null); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT), - exp.getMessage()); - } - } - - public void testNullAliases() { - try { - new Switch("+", "-", "display", null, "Sets whether to display to screen", true, null, - null, 'd', null); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT), - exp.getMessage()); - } - } - - public void testNullEnablePrefix() { - try { - new Switch(null, "-", "display", null, "Sets whether to display to screen", true, null, - null, 'd', null); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_NO_ENABLED_PREFIX), - exp.getMessage()); - } - } - - public void testNullDisablePrefix() { - try { - new Switch("+", null, "display", null, "Sets whether to display to screen", true, null, - null, 'd', null); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_NO_DISABLED_PREFIX), - exp.getMessage()); - } - } - - public void testEnabledPrefixStartsWithDisabledPrefix() { - try { - new Switch("-", "-", "display", null, "Sets whether to display to screen", true, null, - null, 'd', null); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ENABLED_STARTS_WITH_DISABLED), - exp.getMessage()); - } - } - - public void testDisabledPrefixStartsWithEnabledPrefix() { - try { - new Switch("o", "on", "display", null, "Sets whether to display to screen", true, null, - null, 'd', null); - } catch (IllegalArgumentException exp) { - assertEquals("wrong exception message", - ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_DISABLED_STARTWS_WITH_ENABLED), - exp.getMessage()); - } - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() - */ - public void testHelpLines() { - // TODO Auto-generated method stub - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/resource/ResourceHelperTest.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/resource/ResourceHelperTest.java b/src/test/org/apache/commons/cli2/resource/ResourceHelperTest.java deleted file mode 100644 index 00d4fd5..0000000 --- a/src/test/org/apache/commons/cli2/resource/ResourceHelperTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2003-2005 The Apache Software Foundation - * - * Licensed 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.resource; - -import java.util.Locale; -import java.util.MissingResourceException; -import java.util.ResourceBundle; - -import junit.framework.TestCase; - -/** - * A utility class used to provide internationalisation support. - * - * @author John Keyes - */ -public class ResourceHelperTest extends TestCase { - /** system property */ - private static final String PROP_LOCALE = "org.apache.commons.cli2.resource.bundle"; - - private static ResourceHelper helper; - - /** resource bundle */ - private ResourceBundle bundle; - - public void setUp() { - System.setProperty(PROP_LOCALE, "org.apache.commons.cli2.resource.TestBundle"); - helper = ResourceHelper.getResourceHelper(); - } - - public void tearDown() { - System.setProperty(PROP_LOCALE, "org.apache.commons.cli2.resource.CLIMessageBundle_en_US.properties"); - } - - /** - * Create a new ResourceHelper for the specified class. - * - * @param clazz the Class that requires some resources - */ - public ResourceHelperTest() { - super("ResourceHelperTest"); - } - - public void testOverridden() { - assertEquals("wrong message", "The class name \"ResourceHelper\" is invalid.", helper.getMessage("ClassValidator.bad.classname", "ResourceHelper")); - } - - public void testNewMessage1Param() { - assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message")); - } - - public void testNewMessage2Params() { - assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message", "Some")); - } - - public void testNewMessage3Params() { - assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message", "Some", "might")); - } - - public void testNewMessage4Params() { - assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message", "Some", "might", "say")); - } - - public void testDefaultBundle() { - System.setProperty(PROP_LOCALE, "madeupname.properties"); - helper = ResourceHelper.getResourceHelper(); - assertEquals("wrong message", "The class name \"ResourceHelper\" is invalid.", helper.getMessage("ClassValidator.bad.classname", "ResourceHelper")); - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/resource/TestBundle.properties ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/resource/TestBundle.properties b/src/test/org/apache/commons/cli2/resource/TestBundle.properties deleted file mode 100644 index 3ae6d18..0000000 --- a/src/test/org/apache/commons/cli2/resource/TestBundle.properties +++ /dev/null @@ -1,10 +0,0 @@ -ClassValidator.bad.classname = The class name "{0}" is invalid. - -test.message = Some might say we will find a brighter day. - -test.message1 = {0} might say we will find a brighter day. - -test.message2 = {0} {1} say we will find a brighter day. - -test.message3 = {0} {1} {2} we will find a brighter day. - http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/util/ComparatorsTest.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/util/ComparatorsTest.java b/src/test/org/apache/commons/cli2/util/ComparatorsTest.java deleted file mode 100644 index c631da4..0000000 --- a/src/test/org/apache/commons/cli2/util/ComparatorsTest.java +++ /dev/null @@ -1,220 +0,0 @@ -/** - * Copyright 2003-2004 The Apache Software Foundation - * - * Licensed 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.util; - -import java.util.Collections; -import java.util.List; - -import junit.framework.TestCase; - -import org.apache.commons.cli2.CLITestCase; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.option.CommandTest; -import org.apache.commons.cli2.option.DefaultOptionTest; -import org.apache.commons.cli2.option.GroupTest; -import org.apache.commons.cli2.option.ParentTest; -import org.apache.commons.cli2.option.SwitchTest; - -/** - * @author Rob Oxspring - */ -public class ComparatorsTest extends TestCase { - public void testGroupFirst() { - final Option o1 = GroupTest.buildAntGroup(); - final Option o2 = ParentTest.buildLibParent(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.groupFirst()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o1, o2), - list); - } - - public void testGroupLast() { - final Option o1 = GroupTest.buildAntGroup(); - final Option o2 = ParentTest.buildLibParent(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.groupLast()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o2, o1), - list); - } - - public void testSwitchFirst() { - final Option o1 = SwitchTest.buildDisplaySwitch(); - final Option o2 = ParentTest.buildLibParent(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.switchFirst()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o1, o2), - list); - } - - public void testSwitchLast() { - final Option o1 = SwitchTest.buildDisplaySwitch(); - final Option o2 = ParentTest.buildLibParent(); - //final Option o3 = new SwitchBuilder().withName("hidden").create(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.switchLast()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o2, o1), - list); - } - - public void testCommandFirst() { - final Option o1 = CommandTest.buildCommitCommand(); - final Option o2 = ParentTest.buildLibParent(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.commandFirst()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o1, o2), - list); - } - - public void testCommandLast() { - final Option o1 = CommandTest.buildCommitCommand(); - final Option o2 = ParentTest.buildLibParent(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.commandLast()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o2, o1), - list); - } - - public void testDefaultOptionFirst() { - final Option o1 = DefaultOptionTest.buildHelpOption(); - final Option o2 = CommandTest.buildCommitCommand(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.defaultOptionFirst()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o1, o2), - list); - } - - public void testDefaultOptionLast() { - final Option o1 = DefaultOptionTest.buildHelpOption(); - final Option o2 = CommandTest.buildCommitCommand(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.defaultOptionLast()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o2, o1), - list); - } - - public void testNamedFirst() { - final Option o1 = DefaultOptionTest.buildHelpOption(); - final Option o2 = ParentTest.buildLibParent(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.namedFirst("--help")); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o1, o2), - list); - } - - public void testNamedLast() { - final Option o1 = DefaultOptionTest.buildHelpOption(); - final Option o2 = ParentTest.buildLibParent(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.namedLast("--help")); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o2, o1), - list); - } - - public void testPreferredNameFirst() { - final Option o1 = DefaultOptionTest.buildHelpOption(); - final Option o2 = ParentTest.buildLibParent(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.preferredNameFirst()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o1, o2), - list); - } - - public void testPreferredNameLast() { - final Option o1 = DefaultOptionTest.buildHelpOption(); - final Option o2 = ParentTest.buildLibParent(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.preferredNameLast()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o2, o1), - list); - } - - public void testRequiredFirst() { - final Option o1 = DefaultOptionTest.buildHelpOption(); - final Option o2 = DefaultOptionTest.buildXOption(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.requiredFirst()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o2, o1), - list); - } - - public void testRequiredLast() { - final Option o1 = DefaultOptionTest.buildHelpOption(); - final Option o2 = DefaultOptionTest.buildXOption(); - final List list = CLITestCase.list(o1, o2); - - Collections.sort(list, Comparators.requiredLast()); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o1, o2), - list); - } - - public void testChained() { - final Option o1 = CommandTest.buildCommitCommand(); - final Option o2 = SwitchTest.buildDisplaySwitch(); - final Option o3 = DefaultOptionTest.buildHelpOption(); - final List list = CLITestCase.list(o1, o2, o3); - - Collections.sort( - list, - Comparators.chain( - Comparators.namedFirst("--help"), - Comparators.commandFirst())); - - CLITestCase.assertListContentsEqual( - CLITestCase.list(o3, o1, o2), - list); - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java b/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java deleted file mode 100644 index 72ea141..0000000 --- a/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java +++ /dev/null @@ -1,542 +0,0 @@ -/* - * Copyright 2003-2005 The Apache Software Foundation - * - * Licensed 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.util; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringReader; -import java.io.StringWriter; - -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import junit.framework.TestCase; - -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.Group; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.builder.DefaultOptionBuilder; -import org.apache.commons.cli2.builder.GroupBuilder; -import org.apache.commons.cli2.option.ArgumentTest; -import org.apache.commons.cli2.option.DefaultOptionTest; -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; - -public class HelpFormatterTest - extends TestCase { - private ResourceHelper resources = ResourceHelper.getResourceHelper(); - private HelpFormatter helpFormatter; - private Option verbose; - private Group options; - - public void setUp() { - helpFormatter = new HelpFormatter("|*", "*-*", "*|", 80); - helpFormatter.setDivider("+------------------------------------------------------------------------------+"); - helpFormatter.setHeader("Jakarta Commons CLI"); - helpFormatter.setFooter("Copyright 2003\nApache Software Foundation"); - helpFormatter.setShellCommand("ant"); - - verbose = - new DefaultOptionBuilder().withLongName("verbose") - .withDescription("print the version information and exit") - .create(); - - options = - new GroupBuilder().withName("options").withOption(DefaultOptionTest.buildHelpOption()) - .withOption(ArgumentTest.buildTargetsArgument()) - .withOption(new DefaultOptionBuilder().withLongName("diagnostics") - .withDescription("print information that might be helpful to diagnose or report problems.") - .create()) - .withOption(new DefaultOptionBuilder().withLongName("projecthelp") - .withDescription("print project help information") - .create()).withOption(verbose) - .create(); - - helpFormatter.setGroup(options); - } - - public void testPrint() - throws IOException { - final StringWriter writer = new StringWriter(); - final PrintWriter pw = new PrintWriter(writer); - helpFormatter.setPrintWriter(pw); - helpFormatter.print(); - - // test shell - assertEquals("incorrect shell command", "ant", helpFormatter.getShellCommand()); - - // test group - assertEquals("incorrect group", this.options, helpFormatter.getGroup()); - - // test pagewidth - assertEquals("incorrect page width", 76, helpFormatter.getPageWidth()); - - // test pw - assertEquals("incorrect print writer", pw, helpFormatter.getPrintWriter()); - - // test divider - assertEquals("incorrect divider", - "+------------------------------------------------------------------------------+", - helpFormatter.getDivider()); - - // test header - assertEquals("incorrect header", "Jakarta Commons CLI", helpFormatter.getHeader()); - - // test footer - assertEquals("incorrect footer", "Copyright 2003\nApache Software Foundation", - helpFormatter.getFooter()); - - // test gutters - assertEquals("incorrect left gutter", "|*", helpFormatter.getGutterLeft()); - assertEquals("incorrect right gutter", "*|", helpFormatter.getGutterRight()); - assertEquals("incorrect center gutter", "*-*", helpFormatter.getGutterCenter()); - - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*Jakarta Commons CLI *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*Usage: *|", - reader.readLine()); - assertEquals("|*ant [--help --diagnostics --projecthelp --verbose] [<target1> [<target2> *|", - reader.readLine()); - assertEquals("|*...]] *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*options *-* *|", - reader.readLine()); - assertEquals("|* --help (-?,-h) *-*Displays the help *|", - reader.readLine()); - assertEquals("|* --diagnostics *-*print information that might be helpful to diagnose *|", - reader.readLine()); - assertEquals("|* *-*or report problems. *|", - reader.readLine()); - assertEquals("|* --projecthelp *-*print project help information *|", - reader.readLine()); - assertEquals("|* --verbose *-*print the version information and exit *|", - reader.readLine()); - assertEquals("|* target [target ...]*-*The targets ant should build *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*Copyright 2003 *|", - reader.readLine()); - assertEquals("|*Apache Software Foundation *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertNull(reader.readLine()); - } - - public void testComparator() - throws IOException { - final StringWriter writer = new StringWriter(); - final PrintWriter pw = new PrintWriter(writer); - helpFormatter.setPrintWriter(pw); - - final Comparator comparator = new OptionComparator(); - helpFormatter.setComparator(comparator); - helpFormatter.print(); - - // test comparator - assertEquals("invalid comparator", comparator, helpFormatter.getComparator()); - - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*Jakarta Commons CLI *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*Usage: *|", - reader.readLine()); - assertEquals("|*ant [--verbose --projecthelp --help --diagnostics] [<target1> [<target2> *|", - reader.readLine()); - assertEquals("|*...]] *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*options *-* *|", - reader.readLine()); - assertEquals("|* --verbose *-*print the version information and exit *|", - reader.readLine()); - assertEquals("|* --projecthelp *-*print project help information *|", - reader.readLine()); - assertEquals("|* --help (-?,-h) *-*Displays the help *|", - reader.readLine()); - assertEquals("|* --diagnostics *-*print information that might be helpful to diagnose *|", - reader.readLine()); - assertEquals("|* *-*or report problems. *|", - reader.readLine()); - assertEquals("|* target [target ...]*-*The targets ant should build *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*Copyright 2003 *|", - reader.readLine()); - assertEquals("|*Apache Software Foundation *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertNull(reader.readLine()); - } - - public void testPrintHelp() - throws IOException { - final StringWriter writer = new StringWriter(); - helpFormatter.setPrintWriter(new PrintWriter(writer)); - helpFormatter.printHelp(); - - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*options *-* *|", - reader.readLine()); - assertEquals("|* --help (-?,-h) *-*Displays the help *|", - reader.readLine()); - assertEquals("|* --diagnostics *-*print information that might be helpful to diagnose *|", - reader.readLine()); - assertEquals("|* *-*or report problems. *|", - reader.readLine()); - assertEquals("|* --projecthelp *-*print project help information *|", - reader.readLine()); - assertEquals("|* --verbose *-*print the version information and exit *|", - reader.readLine()); - assertEquals("|* target [target ...]*-*The targets ant should build *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertNull(reader.readLine()); - } - - public void testPrintHelp_WithException() - throws IOException { - final StringWriter writer = new StringWriter(); - helpFormatter.setPrintWriter(new PrintWriter(writer)); - helpFormatter.setException(new OptionException(verbose)); - helpFormatter.printHelp(); - - //System.out.println(writer); - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*--verbose*-*print the version information and exit *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertNull(reader.readLine()); - } - - public void testPrintHelp_TooNarrow() - throws IOException { - final StringWriter writer = new StringWriter(); - helpFormatter = new HelpFormatter("<", "=", ">", 4); - helpFormatter.setGroup(options); - helpFormatter.setPrintWriter(new PrintWriter(writer)); - helpFormatter.printHelp(); - - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("<options = >", reader.readLine()); - assertEquals("< --help (-?,-h) =D>", reader.readLine()); - assertEquals("< =i>", reader.readLine()); - - // lots more lines unchecked - } - - public void testPrintException() - throws IOException { - final StringWriter writer = new StringWriter(); - helpFormatter.setPrintWriter(new PrintWriter(writer)); - helpFormatter.setException(new OptionException(verbose, ResourceConstants.MISSING_OPTION)); - helpFormatter.printException(); - - //System.out.println(writer); - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*Missing option --verbose *|", - reader.readLine()); - assertNull(reader.readLine()); - } - - public void testPrintUsage() - throws IOException { - final StringWriter writer = new StringWriter(); - helpFormatter.setPrintWriter(new PrintWriter(writer)); - helpFormatter.printUsage(); - - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*Usage: *|", - reader.readLine()); - assertEquals("|*ant [--help --diagnostics --projecthelp --verbose] [<target1> [<target2> *|", - reader.readLine()); - assertEquals("|*...]] *|", - reader.readLine()); - assertNull(reader.readLine()); - } - - public void testPrintHeader() - throws IOException { - final StringWriter writer = new StringWriter(); - helpFormatter.setPrintWriter(new PrintWriter(writer)); - helpFormatter.printHeader(); - - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertEquals("|*Jakarta Commons CLI *|", - reader.readLine()); - assertNull(reader.readLine()); - } - - public void testPrintFooter() - throws IOException { - final StringWriter writer = new StringWriter(); - helpFormatter.setPrintWriter(new PrintWriter(writer)); - helpFormatter.printFooter(); - - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("|*Copyright 2003 *|", - reader.readLine()); - assertEquals("|*Apache Software Foundation *|", - reader.readLine()); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertNull(reader.readLine()); - } - - public void testPrintDivider() - throws IOException { - final StringWriter writer = new StringWriter(); - helpFormatter.setPrintWriter(new PrintWriter(writer)); - helpFormatter.printDivider(); - - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("+------------------------------------------------------------------------------+", - reader.readLine()); - assertNull(reader.readLine()); - } - - public void testWrap() { - final Iterator i = HelpFormatter.wrap("Apache Software Foundation", 30).iterator(); - assertEquals("Apache Software Foundation", i.next()); - assertFalse(i.hasNext()); - } - - public void testWrap_WrapNeeded() { - final Iterator i = HelpFormatter.wrap("Apache Software Foundation", 20).iterator(); - assertEquals("Apache Software", i.next()); - assertEquals("Foundation", i.next()); - assertFalse(i.hasNext()); - } - - public void testWrap_BeforeSpace() { - final Iterator i = HelpFormatter.wrap("Apache Software Foundation", 16).iterator(); - assertEquals("Apache Software", i.next()); - assertEquals("Foundation", i.next()); - assertFalse(i.hasNext()); - } - - public void testWrap_AfterSpace() { - final Iterator i = HelpFormatter.wrap("Apache Software Foundation", 17).iterator(); - assertEquals("Apache Software", i.next()); - assertEquals("Foundation", i.next()); - assertFalse(i.hasNext()); - } - - public void testWrap_InWord() { - final Iterator i = HelpFormatter.wrap("Apache Software Foundation", 8).iterator(); - assertEquals("Apache", i.next()); - assertEquals("Software", i.next()); - assertEquals("Foundati", i.next()); - assertEquals("on", i.next()); - assertFalse(i.hasNext()); - } - - public void testWrap_NewLine() { - final Iterator i = HelpFormatter.wrap("\nApache Software Foundation\n", 30).iterator(); - assertEquals("", i.next()); - assertEquals("Apache Software Foundation", i.next()); - assertEquals("", i.next()); - assertFalse(i.hasNext()); - } - - public void testWrap_NewLine2() { - List wrapped = - HelpFormatter.wrap("A really quite long general description of the option with specific alternatives documented:\n" + - " Indented special case\n" + " Alternative scenario", 30); - - final Iterator i = wrapped.iterator(); - - assertEquals("A really quite long general", i.next()); - assertEquals("description of the option", i.next()); - assertEquals("with specific alternatives", i.next()); - assertEquals("documented:", i.next()); - assertEquals(" Indented special case", i.next()); - assertEquals(" Alternative scenario", i.next()); - assertFalse(i.hasNext()); - } - - public void testWrap_Below1Length() { - try { - HelpFormatter.wrap("Apache Software Foundation", -1); - fail("IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals(resources.getMessage(ResourceConstants.HELPFORMATTER_WIDTH_TOO_NARROW, - new Object[] { new Integer(-1) }), e.getMessage()); - } - } - - public void testPad() - throws IOException { - final StringWriter writer = new StringWriter(); - HelpFormatter.pad("hello", 10, new PrintWriter(writer)); - assertEquals("hello ", writer.toString()); - } - - public void testPad_Null() - throws IOException { - final StringWriter writer = new StringWriter(); - HelpFormatter.pad(null, 10, new PrintWriter(writer)); - assertEquals(" ", writer.toString()); - } - - public void testPad_TooLong() - throws IOException { - final StringWriter writer = new StringWriter(); - HelpFormatter.pad("hello world", 10, new PrintWriter(writer)); - assertEquals("hello world", writer.toString()); - } - - public void testPad_TooShort() - throws IOException { - final StringWriter writer = new StringWriter(); - HelpFormatter.pad("hello world", -5, new PrintWriter(writer)); - assertEquals("hello world", writer.toString()); - } - - public void testGutters() - throws IOException { - helpFormatter = new HelpFormatter(null, null, null, 80); - helpFormatter.setShellCommand("ant"); - - final Set lusage = new HashSet(); - lusage.add(DisplaySetting.DISPLAY_ALIASES); - lusage.add(DisplaySetting.DISPLAY_GROUP_NAME); - helpFormatter.setLineUsageSettings(lusage); - - // test line usage - assertEquals("incorrect line usage", lusage, helpFormatter.getLineUsageSettings()); - - final Set fusage = new HashSet(); - fusage.add(DisplaySetting.DISPLAY_PARENT_CHILDREN); - fusage.add(DisplaySetting.DISPLAY_GROUP_ARGUMENT); - fusage.add(DisplaySetting.DISPLAY_GROUP_OUTER); - fusage.add(DisplaySetting.DISPLAY_GROUP_EXPANDED); - fusage.add(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED); - fusage.add(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED); - fusage.add(DisplaySetting.DISPLAY_SWITCH_ENABLED); - fusage.add(DisplaySetting.DISPLAY_SWITCH_DISABLED); - fusage.add(DisplaySetting.DISPLAY_PROPERTY_OPTION); - fusage.add(DisplaySetting.DISPLAY_PARENT_CHILDREN); - fusage.add(DisplaySetting.DISPLAY_PARENT_ARGUMENT); - fusage.add(DisplaySetting.DISPLAY_OPTIONAL); - helpFormatter.setFullUsageSettings(fusage); - - // test line usage - assertEquals("incorrect full usage", fusage, helpFormatter.getFullUsageSettings()); - - final Set dsettings = new HashSet(); - dsettings.add(DisplaySetting.DISPLAY_GROUP_NAME); - dsettings.add(DisplaySetting.DISPLAY_GROUP_EXPANDED); - dsettings.add(DisplaySetting.DISPLAY_GROUP_ARGUMENT); - - helpFormatter.setDisplaySettings(dsettings); - - verbose = - new DefaultOptionBuilder().withLongName("verbose") - .withDescription("print the version information and exit") - .create(); - - options = - new GroupBuilder().withName("options").withOption(DefaultOptionTest.buildHelpOption()) - .withOption(ArgumentTest.buildTargetsArgument()) - .withOption(new DefaultOptionBuilder().withLongName("diagnostics") - .withDescription("print information that might be helpful to diagnose or report problems.") - .create()) - .withOption(new DefaultOptionBuilder().withLongName("projecthelp") - .withDescription("print project help information") - .create()).withOption(verbose) - .create(); - - helpFormatter.setGroup(options); - - // test default gutters - assertEquals("incorrect left gutter", HelpFormatter.DEFAULT_GUTTER_LEFT, - helpFormatter.getGutterLeft()); - assertEquals("incorrect right gutter", HelpFormatter.DEFAULT_GUTTER_RIGHT, - helpFormatter.getGutterRight()); - assertEquals("incorrect center gutter", HelpFormatter.DEFAULT_GUTTER_CENTER, - helpFormatter.getGutterCenter()); - - final StringWriter writer = new StringWriter(); - helpFormatter.setPrintWriter(new PrintWriter(writer)); - helpFormatter.print(); - - final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); - assertEquals("Usage: ", - reader.readLine()); - assertEquals("ant [--help --diagnostics --projecthelp --verbose] [<target1> [<target2> ...]] ", - reader.readLine()); - assertEquals("options ", - reader.readLine()); - assertEquals(" --help (-?,-h) Displays the help ", - reader.readLine()); - assertEquals(" --diagnostics print information that might be helpful to diagnose or ", - reader.readLine()); - assertEquals(" report problems. ", - reader.readLine()); - assertEquals(" --projecthelp print project help information ", - reader.readLine()); - assertEquals(" --verbose print the version information and exit ", - reader.readLine()); - assertEquals(" target [target ...] The targets ant should build ", - reader.readLine()); - assertNull(reader.readLine()); - } -} - - -class OptionComparator implements Comparator { - public int compare(Object o1, - Object o2) { - Option opt1 = (Option) o1; - Option opt2 = (Option) o2; - - return -opt1.getPreferredName().compareTo(opt2.getPreferredName()); - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/validation/ClassValidatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/validation/ClassValidatorTest.java b/src/test/org/apache/commons/cli2/validation/ClassValidatorTest.java deleted file mode 100644 index 467b7b0..0000000 --- a/src/test/org/apache/commons/cli2/validation/ClassValidatorTest.java +++ /dev/null @@ -1,247 +0,0 @@ -/* - * Copyright 2003-2005 The Apache Software Foundation - * - * Licensed 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.validation; - -import java.net.URL; -import java.net.URLClassLoader; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; - -import junit.framework.TestCase; - -import org.apache.commons.cli2.resource.ResourceHelper; - -public class ClassValidatorTest extends TestCase { - - private final static ResourceHelper resources = - ResourceHelper.getResourceHelper(); - - private ClassValidator validator; - - protected void setUp() { - validator = new ClassValidator(); - } - - public void testValidName() throws InvalidArgumentException { - final Object[] array = new Object[] { "MyApp", "org.apache.ant.Main" }; - final List list = Arrays.asList(array); - - validator.validate(list); - - assertEquals("Name is incorrect", "MyApp", list.get(0)); - assertEquals("Name is incorrect", "org.apache.ant.Main", list.get(1)); - } - - public void testNameBadStart() { - final String className = "1stClass"; - final Object[] array = new Object[] { className }; - final List list = Arrays.asList(array); - - try { - validator.validate(list); - fail("Class name cannot start with a number."); - } catch (InvalidArgumentException ive) { - assertEquals( - resources.getMessage( - "ClassValidator.bad.classname", - className), - ive.getMessage()); - } - } - - public void testNameBadEnd() { - final String className = "My.Class."; - - final Object[] array = new Object[] { className }; - final List list = Arrays.asList(array); - - try { - validator.validate(list); - fail("Trailing period not permitted."); - } catch (InvalidArgumentException ive) { - assertEquals( - resources.getMessage( - "ClassValidator.bad.classname", - className), - ive.getMessage()); - } - } - - public void testNameBadMiddle() { - final String className = "My..Class"; - - final Object[] array = new Object[] { className }; - final List list = Arrays.asList(array); - - try { - validator.validate(list); - fail("Two consecutive periods is not permitted."); - } catch (InvalidArgumentException ive) { - assertEquals( - resources.getMessage( - "ClassValidator.bad.classname", - className), - ive.getMessage()); - } - } - - public void testIllegalNameChar() { - final String className = "My?Class"; - - final Object[] array = new Object[] { className }; - final List list = Arrays.asList(array); - - try { - validator.validate(list); - fail("Illegal character not allowed in Class name."); - } catch (InvalidArgumentException ive) { - assertEquals( - resources.getMessage( - "ClassValidator.bad.classname", - className), - ive.getMessage()); - } - } - - public void testLoadable() { - assertFalse("Validator is loadable", validator.isLoadable()); - validator.setLoadable(true); - assertTrue("Validator is NOT loadable", validator.isLoadable()); - validator.setLoadable(false); - assertFalse("Validator is loadable", validator.isLoadable()); - } - - public void testLoadValid() throws InvalidArgumentException { - final Object[] array = - new Object[] { - "org.apache.commons.cli2.Option", - "java.util.Vector" }; - final List list = Arrays.asList(array); - - validator.setLoadable(true); - validator.validate(list); - - final Iterator i = list.iterator(); - assertEquals( - "org.apache.commons.cli2.Option", - ((Class) i.next()).getName()); - assertEquals("java.util.Vector", ((Class) i.next()).getName()); - assertFalse(i.hasNext()); - } - - public void testLoadInvalid() { - final String className = "org.apache.commons.cli2.NonOption"; - - final Object[] array = new Object[] { className, "java.util.Vectors" }; - final List list = Arrays.asList(array); - - validator.setLoadable(true); - - try { - validator.validate(list); - fail("Class Not Found"); - } catch (InvalidArgumentException ive) { - assertEquals( - resources.getMessage( - "ClassValidator.class.notfound", - className), - ive.getMessage()); - } - } - - public void testInstantiate() { - assertFalse("Validator creates instances", validator.isInstance()); - validator.setInstance(true); - assertTrue( - "Validator does NOT create instances", - validator.isInstance()); - validator.setInstance(false); - assertFalse("Validator creates instances", validator.isInstance()); - } - - public void testCreateClassInstance() throws InvalidArgumentException { - final Object[] array = new Object[] { "java.util.Vector" }; - final List list = Arrays.asList(array); - - validator.setInstance(true); - - validator.validate(list); - assertTrue( - "Vector instance NOT found", - list.get(0) instanceof java.util.Vector); - } - - public void testCreateInterfaceInstance() { - final String className = "java.util.Map"; - final Object[] array = new Object[] { className }; - final List list = Arrays.asList(array); - - validator.setInstance(true); - - try { - validator.validate(list); - fail("It's not possible to create a '" + className + "'"); - } - catch (final InvalidArgumentException ive) { - assertEquals( - resources.getMessage( - "ClassValidator.class.create", - className), - ive.getMessage()); - } - } - - public void testCreateProtectedInstance() { - final String className = "org.apache.commons.cli2.validation.protect.ProtectedClass"; - final Object[] array = new Object[] { className }; - final List list = Arrays.asList(array); - - validator.setInstance(true); - - try { - validator.validate(list); - fail("It's not possible to create a '" + className + "'"); - } - catch (final InvalidArgumentException ive) { - assertEquals( - resources.getMessage( - "ClassValidator.class.access", - className, - "Class org.apache.commons.cli2.validation.ClassValidator " + - "can not access a member of class " + - "org.apache.commons.cli2.validation.protect.ProtectedClass " + - "with modifiers \"protected\""), - ive.getMessage()); - } - } - - public void testClassloader() { - assertEquals( - "Wrong classloader found", - validator.getClass().getClassLoader(), - validator.getClassLoader()); - - URLClassLoader classloader = new URLClassLoader(new URL[] { - }); - validator.setClassLoader(classloader); - - assertEquals( - "Wrong classloader found", - classloader, - validator.getClassLoader()); - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/validation/DateValidatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/validation/DateValidatorTest.java b/src/test/org/apache/commons/cli2/validation/DateValidatorTest.java deleted file mode 100644 index b83c1c4..0000000 --- a/src/test/org/apache/commons/cli2/validation/DateValidatorTest.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright 2003-2005 The Apache Software Foundation - * - * Licensed 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.validation; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.Iterator; -import java.util.List; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; - -/** - * JUnit test case for DateValidator. - * - * @author Rob Oxspring - * @author John Keyes - */ -public class DateValidatorTest - extends TestCase { - private static final ResourceHelper resources = ResourceHelper.getResourceHelper(); - public static final DateFormat D_M_YY = new SimpleDateFormat("d/M/yy"); - public static final DateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd"); - private List formats = Arrays.asList(new Object[] { D_M_YY, YYYY_MM_DD }); - - public void testSingleFormatValidate() - throws InvalidArgumentException { - final Object[] array = new Object[] { "23/12/03" }; - final List list = Arrays.asList(array); - final Validator validator = new DateValidator(D_M_YY); - - validator.validate(list); - - final Iterator i = list.iterator(); - assertEquals("2003-12-23", YYYY_MM_DD.format((Date) i.next())); - assertFalse(i.hasNext()); - } - - public void testDefaultDateFormatValidate() - throws InvalidArgumentException { - final Object[] array = new Object[] { "23-Dec-2003" }; - final List list = Arrays.asList(array); - final Validator validator = new DateValidator( new SimpleDateFormat("dd-MMM-yyyy") ); - - validator.validate(list); - - final Iterator i = list.iterator(); - // CLI-40: For some reason, the YYYY_MM_DD object gets quite - // confused here and returns 2003-12-22. If we make a new one - // there is no problem. - assertEquals("2003-12-23", new SimpleDateFormat("yyyy-MM-dd").format((Date) i.next())); - assertFalse(i.hasNext()); - } - - public void testDefaultTimeFormatValidate() - throws InvalidArgumentException { - final Object[] array = new Object[] { "18:00:00" }; - final List list = Arrays.asList(array); - final Validator validator = new DateValidator( new SimpleDateFormat("HH:mm:ss") ); - - validator.validate(list); - - final Iterator i = list.iterator(); - final DateFormat df = new SimpleDateFormat("HH:mm:ss"); - assertEquals("18:00:00", df.format((Date) i.next())); - assertFalse(i.hasNext()); - } - - public void testDefaultDateTimeFormatValidate() - throws InvalidArgumentException { - final Object[] array = new Object[] { "23-Jan-2003 18:00:00" }; - final List list = Arrays.asList(array); - final Validator validator = new DateValidator( new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss") ); - - validator.validate(list); - - final Iterator i = list.iterator(); - final DateFormat df = new SimpleDateFormat("yyyy/M/dd HH:mm:ss"); - assertEquals("2003/1/23 18:00:00", df.format((Date) i.next())); - assertFalse(i.hasNext()); - } - - public void testDefaultValidator() - throws InvalidArgumentException { - final Object[] array = new Object[] { "23/01/03 18:00" }; - final List list = Arrays.asList(array); - final Validator validator = new DateValidator(new SimpleDateFormat("dd/MM/yy HH:mm")); - - validator.validate(list); - - final Iterator i = list.iterator(); - final DateFormat df = new SimpleDateFormat("yyyy/M/dd HH:mm:ss"); - assertEquals("2003/1/23 18:00:00", df.format((Date) i.next())); - assertFalse(i.hasNext()); - } - - public void testValidate() - throws InvalidArgumentException { - final Object[] array = new Object[] { "23/12/03", "2002-10-12" }; - final List list = Arrays.asList(array); - final Validator validator = new DateValidator(formats); - - validator.validate(list); - - final Iterator i = list.iterator(); - assertEquals("2003-12-23", YYYY_MM_DD.format((Date) i.next())); - assertEquals("2002-10-12", YYYY_MM_DD.format((Date) i.next())); - assertFalse(i.hasNext()); - } - - public void testMinimumBounds() - throws InvalidArgumentException { - final DateValidator validator = new DateValidator(formats); - final Calendar cal = Calendar.getInstance(); - - { - final Object[] array = new Object[] { "23/12/03", "2002-10-12" }; - final List list = Arrays.asList(array); - cal.set(2002, 1, 12); - - final Date min = cal.getTime(); - validator.setMinimum(min); - assertTrue("maximum bound is set", validator.getMaximum() == null); - assertEquals("minimum bound is incorrect", min, validator.getMinimum()); - validator.validate(list); - } - - { - final Object[] array = new Object[] { "23/12/03", "2002-10-12" }; - final List list = Arrays.asList(array); - cal.set(2003, 1, 12); - - final Date min = cal.getTime(); - validator.setMinimum(min); - - try { - validator.validate(list); - fail("minimum out of bounds exception not caught"); - } catch (final InvalidArgumentException exp) { - assertEquals(resources.getMessage(ResourceConstants.DATEVALIDATOR_DATE_OUTOFRANGE, - new Object[] { "2002-10-12" }), exp.getMessage()); - } - } - } - - public void testFormats() - throws InvalidArgumentException { - final DateValidator validator = new DateValidator(formats); - assertEquals("date format is incorrect", ((SimpleDateFormat) formats.get(0)).toPattern(), - ((SimpleDateFormat) validator.getFormats()[0]).toPattern()); - assertEquals("date format is incorrect", ((SimpleDateFormat) formats.get(1)).toPattern(), - ((SimpleDateFormat) validator.getFormats()[1]).toPattern()); - } - - public void testMaximumBounds() - throws InvalidArgumentException { - final DateValidator validator = new DateValidator(formats); - final Calendar cal = Calendar.getInstance(); - - { - final Object[] array = new Object[] { "23/12/03", "2002-10-12" }; - final List list = Arrays.asList(array); - cal.set(2004, 1, 12); - - final Date max = cal.getTime(); - validator.setMaximum(max); - assertTrue("minimum bound is set", validator.getMinimum() == null); - assertEquals("maximum bound is incorrect", max, validator.getMaximum()); - validator.validate(list); - } - - { - final Object[] array = new Object[] { "23/12/03", "2004-10-12" }; - final List list = Arrays.asList(array); - cal.set(2004, 1, 12); - - final Date max = cal.getTime(); - validator.setMaximum(max); - - try { - validator.validate(list); - fail("maximum out of bounds exception not caught"); - } catch (final InvalidArgumentException exp) { - assertEquals(resources.getMessage(ResourceConstants.DATEVALIDATOR_DATE_OUTOFRANGE, - new Object[] { "2004-10-12" }), exp.getMessage()); - } - } - } - - public static Test suite() { - Test result = new TestSuite(DateValidatorTest.class); // default behavior - result = new TimeZoneTestSuite("EST", result); // ensure it runs in EST timezone - - return result; - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/test/org/apache/commons/cli2/validation/EnumValidatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/cli2/validation/EnumValidatorTest.java b/src/test/org/apache/commons/cli2/validation/EnumValidatorTest.java deleted file mode 100644 index eb65d3c..0000000 --- a/src/test/org/apache/commons/cli2/validation/EnumValidatorTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2004-2005 The Apache Software Foundation - * - * Licensed 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.validation; - -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - -import junit.framework.TestCase; - -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; - -public class EnumValidatorTest - extends TestCase { - private final static ResourceHelper resources = ResourceHelper.getResourceHelper(); - private final Set enumSet = new TreeSet(Arrays.asList(new Object[] { "red", "green", "blue" })); - - public void testValidate() - throws InvalidArgumentException { - final Object[] array = new Object[] { "red", "green" }; - - { - final List list = Arrays.asList(array); - final EnumValidator validator = new EnumValidator(enumSet); - assertEquals("valid values are incorrect", enumSet, validator.getValidValues()); - validator.validate(list); - - final Iterator i = list.iterator(); - assertEquals("red", i.next()); - assertEquals("green", i.next()); - assertFalse(i.hasNext()); - } - } - - public void testNonMember() { - final Object[] array = new Object[] { "red", "pink" }; - final List list = Arrays.asList(array); - final EnumValidator validator = new EnumValidator(enumSet); - - try { - validator.validate(list); - fail("InvalidArgumentException"); - } catch (InvalidArgumentException e) { - assertEquals(resources.getMessage(ResourceConstants.ENUM_ILLEGAL_VALUE, - new Object[] { "pink", validator.getValuesAsString() }), - e.getMessage()); - } - } -}
