You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2008/03/22 04:08:35 UTC
svn commit: r639943 [16/17] - in /commons/proper/cli/trunk/src:
java/org/apache/commons/cli2/ java/org/apache/commons/cli2/builder/
java/org/apache/commons/cli2/commandline/
java/org/apache/commons/cli2/option/ java/org/apache/commons/cli2/resource/
ja...
Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/ComparatorsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/ComparatorsTest.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/ComparatorsTest.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/ComparatorsTest.java Fri Mar 21 20:08:23 2008
@@ -1 +1,221 @@
-/** * 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.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;i
mport 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( CLITestC
ase.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")); CLI
TestCase.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); }}
\ No newline at end of file
+/**
+ * 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.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);
+ }
+}
Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java Fri Mar 21 20:08:23 2008
@@ -1 +1,545 @@
-/* * 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.util;import java.io.BufferedReader;import java.io.IOException;import java.io.PrintWriter;import java.io.StringReader;import java.io.StringWriter;import java.util.Collect
ions;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("Apache 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", "Apache 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("|*Apache 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 compa
rator assertEquals("invalid comparator", comparator, helpFormatter.getComparator()); final BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); assertEquals("+------------------------------------------------------------------------------+", reader.readLine()); assertEquals("|*Apache 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 problem
s. *|", 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.setPrintWri
ter(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("|*Apache 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_Wr
apNeeded() { 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()); assertEqu
als("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 s
pecial 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.toStrin
g()); } 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.DISPLA
Y_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 Defaul
tOptionBuilder().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 Strin
gReader(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()); }}
\ No newline at end of file
+/*
+ * 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.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("Apache 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", "Apache 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("|*Apache 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("|*Apache 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("|*Apache 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());
+ }
+}
Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/validation/ClassValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/validation/ClassValidatorTest.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/validation/ClassValidatorTest.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/validation/ClassValidatorTest.java Fri Mar 21 20:08:23 2008
@@ -1 +1,248 @@
-/* * 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.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;impor
t 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 (In
validArgumentException 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); asser
tTrue("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()); }}
\ No newline at end of file
+/*
+ * 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.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());
+ }
+}