You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ro...@apache.org on 2004/02/25 00:59:44 UTC
cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli2 WriteableCommandLineTest.java CommandLineTest.java
roxspring 2004/02/24 15:59:44
Modified: cli/src/test/org/apache/commons/cli2 Tag:
RESEARCH_CLI_2_ROXSPRING CommandLineTest.java
Added: cli/src/test/org/apache/commons/cli2 Tag:
RESEARCH_CLI_2_ROXSPRING
WriteableCommandLineTest.java
Log:
More in the CommandLine testing framework
Revision Changes Path
No revision
No revision
1.1.2.8 +240 -30 jakarta-commons/cli/src/test/org/apache/commons/cli2/Attic/CommandLineTest.java
Index: CommandLineTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli2/Attic/CommandLineTest.java,v
retrieving revision 1.1.2.7
retrieving revision 1.1.2.8
diff -u -r1.1.2.7 -r1.1.2.8
--- CommandLineTest.java 17 Feb 2004 21:36:39 -0000 1.1.2.7
+++ CommandLineTest.java 24 Feb 2004 23:59:44 -0000 1.1.2.8
@@ -15,18 +15,228 @@
*/
package org.apache.commons.cli2;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
-import org.apache.commons.cli2.builders.*;
+import org.apache.commons.cli2.builders.DefaultOptionBuilder;
+import org.apache.commons.cli2.builders.GroupBuilder;
import org.apache.commons.cli2.commandline.Parser;
-import org.apache.commons.cli2.impl.*;
+import org.apache.commons.cli2.impl.ArgumentTest;
+import org.apache.commons.cli2.impl.CommandTest;
+import org.apache.commons.cli2.impl.DefaultOptionTest;
+import org.apache.commons.cli2.impl.OptionTestCase;
+import org.apache.commons.cli2.impl.PropertyOption;
+import org.apache.commons.cli2.impl.SwitchTest;
+
+public abstract class CommandLineTest extends CLITestCase {
+
+ protected abstract CommandLine createCommandLine();
+
+ public final Option present = new DefaultOptionBuilder().withLongName("present").withLongName("alsopresent").create();
+ public final Option missing = new DefaultOptionBuilder().withLongName("missing").create();
+ public final Option multiple = new DefaultOptionBuilder().withLongName("multiple").create();
+
+ public final Option root = new GroupBuilder().withOption(present).withOption(missing).withOption(multiple).create();
+
+ private CommandLine commandLine;
+
+ /*
+ * @see TestCase#setUp()
+ */
+ public void setUp() throws Exception {
+ super.setUp();
+ commandLine = createCommandLine();
+ }
+ /*
+ * Class to test for boolean hasOption(String)
+ */
+ public final void testHasOptionString() {
+ System.out.println(commandLine);
+ assertTrue(commandLine.hasOption("--present"));
+ assertTrue(commandLine.hasOption("--alsopresent"));
+ assertFalse(commandLine.hasOption("--missing"));
+ }
+
+ /*
+ * Class to test for boolean hasOption(Option)
+ */
+ public final void testHasOptionOption() {
+ assertTrue(commandLine.hasOption(present));
+ assertFalse(commandLine.hasOption(missing));
+ }
+ public final void testGetOption() {
+ assertSame(present,commandLine.getOption("--present"));
+ assertSame(present,commandLine.getOption("--alsopresent"));
+ //TODO decide whether the following assertion is valid
+ //assertSame(missing,commandLine.getOption("--missing"));
+ }
+ /*
+ * Class to test for List getValues(String)
+ */
+ public final void testGetValuesString() {
+ assertListContentsEqual(list("present value"),commandLine.getValues("--present"));
+ assertTrue(commandLine.getValues("--missing").isEmpty());
+ }
+ /*
+ * Class to test for List getValues(String, List)
+ */
+ public final void testGetValuesStringList() {
+ assertListContentsEqual(list("present value"),commandLine.getValues("--present",null));
+ assertListContentsEqual(list("present value"),commandLine.getValues("--alsopresent",null));
+ assertTrue(commandLine.getValues("--missing",null).isEmpty());
+ final List def = Collections.singletonList("default value");
+ assertSame(def,commandLine.getValues("--missing",def));
+ }
+ /*
+ * Class to test for List getValues(Option)
+ */
+ public final void testGetValuesOption() {
+ assertListContentsEqual(list("present value"),commandLine.getValues(present));
+ assertTrue(commandLine.getValues(missing).isEmpty());
+ }
+ /*
+ * Class to test for List getValues(Option, List)
+ */
+ public final void testGetValuesOptionList() {
+ assertListContentsEqual(list("present value"),commandLine.getValues(present));
+ assertTrue(commandLine.getValues(missing,null).isEmpty());
+ final List defs = Collections.singletonList("custom default");
+ assertSame(defs,commandLine.getValues(missing,defs));
+ }
+ /*
+ * Class to test for Object getValue(String)
+ */
+ public final void testGetValueString() {
+ assertEquals("present value",commandLine.getValue("--present"));
+ assertEquals("present value",commandLine.getValue("--alsopresent"));
+ assertNull(commandLine.getValue("--missing"));
+ try{
+ commandLine.getValue("--multiple");
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Object getValue(String, Object)
+ */
+ public final void testGetValueStringObject() {
+ assertEquals("present value",commandLine.getValue("--present","default value"));
+ assertEquals("present value",commandLine.getValue("--alsopresent","default value"));
+ assertEquals("default value",commandLine.getValue("--missing","default value"));
+ try{
+ commandLine.getValue("--multiple");
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Object getValue(Option)
+ */
+ public final void testGetValueOption() {
+ assertEquals("present value",commandLine.getValue(present));
+ assertNull(commandLine.getValue(missing));
+ try{
+ commandLine.getValue(multiple);
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Object getValue(Option, Object)
+ */
+ public final void testGetValueOptionObject() {
+ assertEquals("present value",commandLine.getValue(present,"default value"));
+ assertEquals("default value",commandLine.getValue(missing,"default value"));
+ try{
+ commandLine.getValue(multiple);
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Boolean getSwitch(String)
+ */
+ public final void testGetSwitchString() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--present"));
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--alsopresent"));
+ assertNull(commandLine.getSwitch("--missing"));
+ }
+ /*
+ * Class to test for Boolean getSwitch(String, Boolean)
+ */
+ public final void testGetSwitchStringBoolean() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--present",Boolean.FALSE));
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--alsopresent",Boolean.FALSE));
+ assertEquals(Boolean.FALSE,commandLine.getSwitch("--missing",Boolean.FALSE));
+ }
+ /*
+ * Class to test for Boolean getSwitch(Option)
+ */
+ public final void testGetSwitchOption() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch(present));
+ assertNull(commandLine.getSwitch(missing));
+ }
+ /*
+ * Class to test for Boolean getSwitch(Option, Boolean)
+ */
+ public final void testGetSwitchOptionBoolean() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch(present,Boolean.FALSE));
+ assertEquals(Boolean.FALSE,commandLine.getSwitch(missing,Boolean.FALSE));
+ }
+ /*
+ * Class to test for String getProperty(String)
+ */
+ public final void testGetPropertyString() {
+ assertEquals("present property",commandLine.getProperty("present"));
+ assertNull(commandLine.getProperty("missing"));
+ }
+ /*
+ * Class to test for String getProperty(String, String)
+ */
+ public final void testGetPropertyStringString() {
+ assertEquals("present property",commandLine.getProperty("present","default property"));
+ assertEquals("default property",commandLine.getProperty("missing","default property"));
+ }
+ public final void testGetProperties() {
+ assertContentsEqual(list("present"),commandLine.getProperties());
+ }
+ /*
+ * Class to test for int getOptionCount(String)
+ */
+ public final void testGetOptionCountString() {
+ // one option, one switch
+ assertEquals(2,commandLine.getOptionCount("--present"));
+ assertEquals(2,commandLine.getOptionCount("--alsopresent"));
+ assertEquals(0,commandLine.getOptionCount("--missing"));
+ }
+ /*
+ * Class to test for int getOptionCount(Option)
+ */
+ public final void testGetOptionCountOption() {
+ // one option, one switch
+ assertEquals(2,commandLine.getOptionCount(present));
+ assertEquals(0,commandLine.getOptionCount(missing));
+ }
+ public final void testGetOptions() {
+ //TODO Implement getOptions().
+ }
+ public final void testGetOptionTriggers() {
+ //TODO Implement getOptionTriggers().
+ }
+
+
+ // OLD TESTS FOLLOW
-import junit.framework.TestCase;
-
-public class CommandLineTest extends TestCase {
-
- public void testProperties() {
+ public final void testProperties() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -47,7 +257,7 @@
assertEquals("myval3", commandLine.getProperty("myprop2"));
}
- public void testOptions() {
+ public final void testOptions() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -66,7 +276,7 @@
assertTrue(commandLine.hasOption("go"));
}
- public void testValues() {
+ public final void testValues() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -114,7 +324,7 @@
commandLine.getValues(start).add("file3");
}
- public void testSwitches() {
+ public final void testSwitches() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -136,7 +346,7 @@
}
}
- public void testSwitches_True() {
+ public final void testSwitches_True() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -148,7 +358,7 @@
assertSame(Boolean.TRUE, commandLine.getSwitch(start));
}
- public void testSwitches_False() {
+ public final void testSwitches_False() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -160,23 +370,23 @@
assertSame(Boolean.FALSE, commandLine.getSwitch(start));
}
- public void testLooksLikeOption() {
- final Option option = new PropertyOption();
- final List args = OptionTestCase.list();
- final WriteableCommandLine commandLine =
- OptionTestCase.commandLine(option, args);
-
- assertTrue(commandLine.looksLikeOption("-D"));
- assertFalse(commandLine.looksLikeOption("--help"));
- assertFalse(commandLine.looksLikeOption("+display"));
- assertFalse(commandLine.looksLikeOption("myprefix"));
- assertFalse(commandLine.looksLikeOption("myprefix2"));
- assertFalse(commandLine.looksLikeOption("myprefference"));
- assertFalse(commandLine.looksLikeOption("/SCANDISK"));
- assertFalse(commandLine.looksLikeOption("update"));
- }
+// public final void testLooksLikeOption() {
+// final Option option = new PropertyOption();
+// final List args = OptionTestCase.list();
+// final WriteableCommandLine commandLine =
+// OptionTestCase.commandLine(option, args);
+//
+// assertTrue(commandLine.looksLikeOption("-D"));
+// assertFalse(commandLine.looksLikeOption("--help"));
+// assertFalse(commandLine.looksLikeOption("+display"));
+// assertFalse(commandLine.looksLikeOption("myprefix"));
+// assertFalse(commandLine.looksLikeOption("myprefix2"));
+// assertFalse(commandLine.looksLikeOption("myprefference"));
+// assertFalse(commandLine.looksLikeOption("/SCANDISK"));
+// assertFalse(commandLine.looksLikeOption("update"));
+// }
- public void testGetOptions_Order() throws OptionException {
+ public final void testGetOptions_Order() throws OptionException {
final Option help = DefaultOptionTest.buildHelpOption();
final Option login = CommandTest.buildLoginCommand();
final Option targets = ArgumentTest.buildTargetsArgument();
@@ -208,7 +418,7 @@
assertFalse(i.hasNext());
}
- public void testGetOptionCount() throws OptionException {
+ public final void testGetOptionCount() throws OptionException {
final Option help = DefaultOptionTest.buildHelpOption();
final Option login = CommandTest.buildLoginCommand();
final Option targets = ArgumentTest.buildTargetsArgument();
@@ -241,7 +451,7 @@
assertEquals(0, cl.getOptionCount(display));
}
- public void testGetOptionCount_Strings() throws OptionException {
+ public final void testGetOptionCount_Strings() throws OptionException {
final Option help = DefaultOptionTest.buildHelpOption();
final Option login = CommandTest.buildLoginCommand();
final Option targets = ArgumentTest.buildTargetsArgument();
No revision
Index: CommandLineTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli2/Attic/CommandLineTest.java,v
retrieving revision 1.1.2.7
retrieving revision 1.1.2.8
diff -u -r1.1.2.7 -r1.1.2.8
--- CommandLineTest.java 17 Feb 2004 21:36:39 -0000 1.1.2.7
+++ CommandLineTest.java 24 Feb 2004 23:59:44 -0000 1.1.2.8
@@ -15,18 +15,228 @@
*/
package org.apache.commons.cli2;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
-import org.apache.commons.cli2.builders.*;
+import org.apache.commons.cli2.builders.DefaultOptionBuilder;
+import org.apache.commons.cli2.builders.GroupBuilder;
import org.apache.commons.cli2.commandline.Parser;
-import org.apache.commons.cli2.impl.*;
+import org.apache.commons.cli2.impl.ArgumentTest;
+import org.apache.commons.cli2.impl.CommandTest;
+import org.apache.commons.cli2.impl.DefaultOptionTest;
+import org.apache.commons.cli2.impl.OptionTestCase;
+import org.apache.commons.cli2.impl.PropertyOption;
+import org.apache.commons.cli2.impl.SwitchTest;
+
+public abstract class CommandLineTest extends CLITestCase {
+
+ protected abstract CommandLine createCommandLine();
+
+ public final Option present = new DefaultOptionBuilder().withLongName("present").withLongName("alsopresent").create();
+ public final Option missing = new DefaultOptionBuilder().withLongName("missing").create();
+ public final Option multiple = new DefaultOptionBuilder().withLongName("multiple").create();
+
+ public final Option root = new GroupBuilder().withOption(present).withOption(missing).withOption(multiple).create();
+
+ private CommandLine commandLine;
+
+ /*
+ * @see TestCase#setUp()
+ */
+ public void setUp() throws Exception {
+ super.setUp();
+ commandLine = createCommandLine();
+ }
+ /*
+ * Class to test for boolean hasOption(String)
+ */
+ public final void testHasOptionString() {
+ System.out.println(commandLine);
+ assertTrue(commandLine.hasOption("--present"));
+ assertTrue(commandLine.hasOption("--alsopresent"));
+ assertFalse(commandLine.hasOption("--missing"));
+ }
+
+ /*
+ * Class to test for boolean hasOption(Option)
+ */
+ public final void testHasOptionOption() {
+ assertTrue(commandLine.hasOption(present));
+ assertFalse(commandLine.hasOption(missing));
+ }
+ public final void testGetOption() {
+ assertSame(present,commandLine.getOption("--present"));
+ assertSame(present,commandLine.getOption("--alsopresent"));
+ //TODO decide whether the following assertion is valid
+ //assertSame(missing,commandLine.getOption("--missing"));
+ }
+ /*
+ * Class to test for List getValues(String)
+ */
+ public final void testGetValuesString() {
+ assertListContentsEqual(list("present value"),commandLine.getValues("--present"));
+ assertTrue(commandLine.getValues("--missing").isEmpty());
+ }
+ /*
+ * Class to test for List getValues(String, List)
+ */
+ public final void testGetValuesStringList() {
+ assertListContentsEqual(list("present value"),commandLine.getValues("--present",null));
+ assertListContentsEqual(list("present value"),commandLine.getValues("--alsopresent",null));
+ assertTrue(commandLine.getValues("--missing",null).isEmpty());
+ final List def = Collections.singletonList("default value");
+ assertSame(def,commandLine.getValues("--missing",def));
+ }
+ /*
+ * Class to test for List getValues(Option)
+ */
+ public final void testGetValuesOption() {
+ assertListContentsEqual(list("present value"),commandLine.getValues(present));
+ assertTrue(commandLine.getValues(missing).isEmpty());
+ }
+ /*
+ * Class to test for List getValues(Option, List)
+ */
+ public final void testGetValuesOptionList() {
+ assertListContentsEqual(list("present value"),commandLine.getValues(present));
+ assertTrue(commandLine.getValues(missing,null).isEmpty());
+ final List defs = Collections.singletonList("custom default");
+ assertSame(defs,commandLine.getValues(missing,defs));
+ }
+ /*
+ * Class to test for Object getValue(String)
+ */
+ public final void testGetValueString() {
+ assertEquals("present value",commandLine.getValue("--present"));
+ assertEquals("present value",commandLine.getValue("--alsopresent"));
+ assertNull(commandLine.getValue("--missing"));
+ try{
+ commandLine.getValue("--multiple");
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Object getValue(String, Object)
+ */
+ public final void testGetValueStringObject() {
+ assertEquals("present value",commandLine.getValue("--present","default value"));
+ assertEquals("present value",commandLine.getValue("--alsopresent","default value"));
+ assertEquals("default value",commandLine.getValue("--missing","default value"));
+ try{
+ commandLine.getValue("--multiple");
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Object getValue(Option)
+ */
+ public final void testGetValueOption() {
+ assertEquals("present value",commandLine.getValue(present));
+ assertNull(commandLine.getValue(missing));
+ try{
+ commandLine.getValue(multiple);
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Object getValue(Option, Object)
+ */
+ public final void testGetValueOptionObject() {
+ assertEquals("present value",commandLine.getValue(present,"default value"));
+ assertEquals("default value",commandLine.getValue(missing,"default value"));
+ try{
+ commandLine.getValue(multiple);
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Boolean getSwitch(String)
+ */
+ public final void testGetSwitchString() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--present"));
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--alsopresent"));
+ assertNull(commandLine.getSwitch("--missing"));
+ }
+ /*
+ * Class to test for Boolean getSwitch(String, Boolean)
+ */
+ public final void testGetSwitchStringBoolean() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--present",Boolean.FALSE));
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--alsopresent",Boolean.FALSE));
+ assertEquals(Boolean.FALSE,commandLine.getSwitch("--missing",Boolean.FALSE));
+ }
+ /*
+ * Class to test for Boolean getSwitch(Option)
+ */
+ public final void testGetSwitchOption() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch(present));
+ assertNull(commandLine.getSwitch(missing));
+ }
+ /*
+ * Class to test for Boolean getSwitch(Option, Boolean)
+ */
+ public final void testGetSwitchOptionBoolean() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch(present,Boolean.FALSE));
+ assertEquals(Boolean.FALSE,commandLine.getSwitch(missing,Boolean.FALSE));
+ }
+ /*
+ * Class to test for String getProperty(String)
+ */
+ public final void testGetPropertyString() {
+ assertEquals("present property",commandLine.getProperty("present"));
+ assertNull(commandLine.getProperty("missing"));
+ }
+ /*
+ * Class to test for String getProperty(String, String)
+ */
+ public final void testGetPropertyStringString() {
+ assertEquals("present property",commandLine.getProperty("present","default property"));
+ assertEquals("default property",commandLine.getProperty("missing","default property"));
+ }
+ public final void testGetProperties() {
+ assertContentsEqual(list("present"),commandLine.getProperties());
+ }
+ /*
+ * Class to test for int getOptionCount(String)
+ */
+ public final void testGetOptionCountString() {
+ // one option, one switch
+ assertEquals(2,commandLine.getOptionCount("--present"));
+ assertEquals(2,commandLine.getOptionCount("--alsopresent"));
+ assertEquals(0,commandLine.getOptionCount("--missing"));
+ }
+ /*
+ * Class to test for int getOptionCount(Option)
+ */
+ public final void testGetOptionCountOption() {
+ // one option, one switch
+ assertEquals(2,commandLine.getOptionCount(present));
+ assertEquals(0,commandLine.getOptionCount(missing));
+ }
+ public final void testGetOptions() {
+ //TODO Implement getOptions().
+ }
+ public final void testGetOptionTriggers() {
+ //TODO Implement getOptionTriggers().
+ }
+
+
+ // OLD TESTS FOLLOW
-import junit.framework.TestCase;
-
-public class CommandLineTest extends TestCase {
-
- public void testProperties() {
+ public final void testProperties() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -47,7 +257,7 @@
assertEquals("myval3", commandLine.getProperty("myprop2"));
}
- public void testOptions() {
+ public final void testOptions() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -66,7 +276,7 @@
assertTrue(commandLine.hasOption("go"));
}
- public void testValues() {
+ public final void testValues() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -114,7 +324,7 @@
commandLine.getValues(start).add("file3");
}
- public void testSwitches() {
+ public final void testSwitches() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -136,7 +346,7 @@
}
}
- public void testSwitches_True() {
+ public final void testSwitches_True() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -148,7 +358,7 @@
assertSame(Boolean.TRUE, commandLine.getSwitch(start));
}
- public void testSwitches_False() {
+ public final void testSwitches_False() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -160,23 +370,23 @@
assertSame(Boolean.FALSE, commandLine.getSwitch(start));
}
- public void testLooksLikeOption() {
- final Option option = new PropertyOption();
- final List args = OptionTestCase.list();
- final WriteableCommandLine commandLine =
- OptionTestCase.commandLine(option, args);
-
- assertTrue(commandLine.looksLikeOption("-D"));
- assertFalse(commandLine.looksLikeOption("--help"));
- assertFalse(commandLine.looksLikeOption("+display"));
- assertFalse(commandLine.looksLikeOption("myprefix"));
- assertFalse(commandLine.looksLikeOption("myprefix2"));
- assertFalse(commandLine.looksLikeOption("myprefference"));
- assertFalse(commandLine.looksLikeOption("/SCANDISK"));
- assertFalse(commandLine.looksLikeOption("update"));
- }
+// public final void testLooksLikeOption() {
+// final Option option = new PropertyOption();
+// final List args = OptionTestCase.list();
+// final WriteableCommandLine commandLine =
+// OptionTestCase.commandLine(option, args);
+//
+// assertTrue(commandLine.looksLikeOption("-D"));
+// assertFalse(commandLine.looksLikeOption("--help"));
+// assertFalse(commandLine.looksLikeOption("+display"));
+// assertFalse(commandLine.looksLikeOption("myprefix"));
+// assertFalse(commandLine.looksLikeOption("myprefix2"));
+// assertFalse(commandLine.looksLikeOption("myprefference"));
+// assertFalse(commandLine.looksLikeOption("/SCANDISK"));
+// assertFalse(commandLine.looksLikeOption("update"));
+// }
- public void testGetOptions_Order() throws OptionException {
+ public final void testGetOptions_Order() throws OptionException {
final Option help = DefaultOptionTest.buildHelpOption();
final Option login = CommandTest.buildLoginCommand();
final Option targets = ArgumentTest.buildTargetsArgument();
@@ -208,7 +418,7 @@
assertFalse(i.hasNext());
}
- public void testGetOptionCount() throws OptionException {
+ public final void testGetOptionCount() throws OptionException {
final Option help = DefaultOptionTest.buildHelpOption();
final Option login = CommandTest.buildLoginCommand();
final Option targets = ArgumentTest.buildTargetsArgument();
@@ -241,7 +451,7 @@
assertEquals(0, cl.getOptionCount(display));
}
- public void testGetOptionCount_Strings() throws OptionException {
+ public final void testGetOptionCount_Strings() throws OptionException {
final Option help = DefaultOptionTest.buildHelpOption();
final Option login = CommandTest.buildLoginCommand();
final Option targets = ArgumentTest.buildTargetsArgument();
No revision
Index: CommandLineTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli2/Attic/CommandLineTest.java,v
retrieving revision 1.1.2.7
retrieving revision 1.1.2.8
diff -u -r1.1.2.7 -r1.1.2.8
--- CommandLineTest.java 17 Feb 2004 21:36:39 -0000 1.1.2.7
+++ CommandLineTest.java 24 Feb 2004 23:59:44 -0000 1.1.2.8
@@ -15,18 +15,228 @@
*/
package org.apache.commons.cli2;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
-import org.apache.commons.cli2.builders.*;
+import org.apache.commons.cli2.builders.DefaultOptionBuilder;
+import org.apache.commons.cli2.builders.GroupBuilder;
import org.apache.commons.cli2.commandline.Parser;
-import org.apache.commons.cli2.impl.*;
+import org.apache.commons.cli2.impl.ArgumentTest;
+import org.apache.commons.cli2.impl.CommandTest;
+import org.apache.commons.cli2.impl.DefaultOptionTest;
+import org.apache.commons.cli2.impl.OptionTestCase;
+import org.apache.commons.cli2.impl.PropertyOption;
+import org.apache.commons.cli2.impl.SwitchTest;
+
+public abstract class CommandLineTest extends CLITestCase {
+
+ protected abstract CommandLine createCommandLine();
+
+ public final Option present = new DefaultOptionBuilder().withLongName("present").withLongName("alsopresent").create();
+ public final Option missing = new DefaultOptionBuilder().withLongName("missing").create();
+ public final Option multiple = new DefaultOptionBuilder().withLongName("multiple").create();
+
+ public final Option root = new GroupBuilder().withOption(present).withOption(missing).withOption(multiple).create();
+
+ private CommandLine commandLine;
+
+ /*
+ * @see TestCase#setUp()
+ */
+ public void setUp() throws Exception {
+ super.setUp();
+ commandLine = createCommandLine();
+ }
+ /*
+ * Class to test for boolean hasOption(String)
+ */
+ public final void testHasOptionString() {
+ System.out.println(commandLine);
+ assertTrue(commandLine.hasOption("--present"));
+ assertTrue(commandLine.hasOption("--alsopresent"));
+ assertFalse(commandLine.hasOption("--missing"));
+ }
+
+ /*
+ * Class to test for boolean hasOption(Option)
+ */
+ public final void testHasOptionOption() {
+ assertTrue(commandLine.hasOption(present));
+ assertFalse(commandLine.hasOption(missing));
+ }
+ public final void testGetOption() {
+ assertSame(present,commandLine.getOption("--present"));
+ assertSame(present,commandLine.getOption("--alsopresent"));
+ //TODO decide whether the following assertion is valid
+ //assertSame(missing,commandLine.getOption("--missing"));
+ }
+ /*
+ * Class to test for List getValues(String)
+ */
+ public final void testGetValuesString() {
+ assertListContentsEqual(list("present value"),commandLine.getValues("--present"));
+ assertTrue(commandLine.getValues("--missing").isEmpty());
+ }
+ /*
+ * Class to test for List getValues(String, List)
+ */
+ public final void testGetValuesStringList() {
+ assertListContentsEqual(list("present value"),commandLine.getValues("--present",null));
+ assertListContentsEqual(list("present value"),commandLine.getValues("--alsopresent",null));
+ assertTrue(commandLine.getValues("--missing",null).isEmpty());
+ final List def = Collections.singletonList("default value");
+ assertSame(def,commandLine.getValues("--missing",def));
+ }
+ /*
+ * Class to test for List getValues(Option)
+ */
+ public final void testGetValuesOption() {
+ assertListContentsEqual(list("present value"),commandLine.getValues(present));
+ assertTrue(commandLine.getValues(missing).isEmpty());
+ }
+ /*
+ * Class to test for List getValues(Option, List)
+ */
+ public final void testGetValuesOptionList() {
+ assertListContentsEqual(list("present value"),commandLine.getValues(present));
+ assertTrue(commandLine.getValues(missing,null).isEmpty());
+ final List defs = Collections.singletonList("custom default");
+ assertSame(defs,commandLine.getValues(missing,defs));
+ }
+ /*
+ * Class to test for Object getValue(String)
+ */
+ public final void testGetValueString() {
+ assertEquals("present value",commandLine.getValue("--present"));
+ assertEquals("present value",commandLine.getValue("--alsopresent"));
+ assertNull(commandLine.getValue("--missing"));
+ try{
+ commandLine.getValue("--multiple");
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Object getValue(String, Object)
+ */
+ public final void testGetValueStringObject() {
+ assertEquals("present value",commandLine.getValue("--present","default value"));
+ assertEquals("present value",commandLine.getValue("--alsopresent","default value"));
+ assertEquals("default value",commandLine.getValue("--missing","default value"));
+ try{
+ commandLine.getValue("--multiple");
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Object getValue(Option)
+ */
+ public final void testGetValueOption() {
+ assertEquals("present value",commandLine.getValue(present));
+ assertNull(commandLine.getValue(missing));
+ try{
+ commandLine.getValue(multiple);
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Object getValue(Option, Object)
+ */
+ public final void testGetValueOptionObject() {
+ assertEquals("present value",commandLine.getValue(present,"default value"));
+ assertEquals("default value",commandLine.getValue(missing,"default value"));
+ try{
+ commandLine.getValue(multiple);
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e){
+ assertEquals("More than one value was supplied",e.getMessage());
+ }
+ }
+ /*
+ * Class to test for Boolean getSwitch(String)
+ */
+ public final void testGetSwitchString() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--present"));
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--alsopresent"));
+ assertNull(commandLine.getSwitch("--missing"));
+ }
+ /*
+ * Class to test for Boolean getSwitch(String, Boolean)
+ */
+ public final void testGetSwitchStringBoolean() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--present",Boolean.FALSE));
+ assertEquals(Boolean.TRUE,commandLine.getSwitch("--alsopresent",Boolean.FALSE));
+ assertEquals(Boolean.FALSE,commandLine.getSwitch("--missing",Boolean.FALSE));
+ }
+ /*
+ * Class to test for Boolean getSwitch(Option)
+ */
+ public final void testGetSwitchOption() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch(present));
+ assertNull(commandLine.getSwitch(missing));
+ }
+ /*
+ * Class to test for Boolean getSwitch(Option, Boolean)
+ */
+ public final void testGetSwitchOptionBoolean() {
+ assertEquals(Boolean.TRUE,commandLine.getSwitch(present,Boolean.FALSE));
+ assertEquals(Boolean.FALSE,commandLine.getSwitch(missing,Boolean.FALSE));
+ }
+ /*
+ * Class to test for String getProperty(String)
+ */
+ public final void testGetPropertyString() {
+ assertEquals("present property",commandLine.getProperty("present"));
+ assertNull(commandLine.getProperty("missing"));
+ }
+ /*
+ * Class to test for String getProperty(String, String)
+ */
+ public final void testGetPropertyStringString() {
+ assertEquals("present property",commandLine.getProperty("present","default property"));
+ assertEquals("default property",commandLine.getProperty("missing","default property"));
+ }
+ public final void testGetProperties() {
+ assertContentsEqual(list("present"),commandLine.getProperties());
+ }
+ /*
+ * Class to test for int getOptionCount(String)
+ */
+ public final void testGetOptionCountString() {
+ // one option, one switch
+ assertEquals(2,commandLine.getOptionCount("--present"));
+ assertEquals(2,commandLine.getOptionCount("--alsopresent"));
+ assertEquals(0,commandLine.getOptionCount("--missing"));
+ }
+ /*
+ * Class to test for int getOptionCount(Option)
+ */
+ public final void testGetOptionCountOption() {
+ // one option, one switch
+ assertEquals(2,commandLine.getOptionCount(present));
+ assertEquals(0,commandLine.getOptionCount(missing));
+ }
+ public final void testGetOptions() {
+ //TODO Implement getOptions().
+ }
+ public final void testGetOptionTriggers() {
+ //TODO Implement getOptionTriggers().
+ }
+
+
+ // OLD TESTS FOLLOW
-import junit.framework.TestCase;
-
-public class CommandLineTest extends TestCase {
-
- public void testProperties() {
+ public final void testProperties() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -47,7 +257,7 @@
assertEquals("myval3", commandLine.getProperty("myprop2"));
}
- public void testOptions() {
+ public final void testOptions() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -66,7 +276,7 @@
assertTrue(commandLine.hasOption("go"));
}
- public void testValues() {
+ public final void testValues() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -114,7 +324,7 @@
commandLine.getValues(start).add("file3");
}
- public void testSwitches() {
+ public final void testSwitches() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -136,7 +346,7 @@
}
}
- public void testSwitches_True() {
+ public final void testSwitches_True() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -148,7 +358,7 @@
assertSame(Boolean.TRUE, commandLine.getSwitch(start));
}
- public void testSwitches_False() {
+ public final void testSwitches_False() {
final Option option = new PropertyOption();
final List args = OptionTestCase.list();
final WriteableCommandLine commandLine =
@@ -160,23 +370,23 @@
assertSame(Boolean.FALSE, commandLine.getSwitch(start));
}
- public void testLooksLikeOption() {
- final Option option = new PropertyOption();
- final List args = OptionTestCase.list();
- final WriteableCommandLine commandLine =
- OptionTestCase.commandLine(option, args);
-
- assertTrue(commandLine.looksLikeOption("-D"));
- assertFalse(commandLine.looksLikeOption("--help"));
- assertFalse(commandLine.looksLikeOption("+display"));
- assertFalse(commandLine.looksLikeOption("myprefix"));
- assertFalse(commandLine.looksLikeOption("myprefix2"));
- assertFalse(commandLine.looksLikeOption("myprefference"));
- assertFalse(commandLine.looksLikeOption("/SCANDISK"));
- assertFalse(commandLine.looksLikeOption("update"));
- }
+// public final void testLooksLikeOption() {
+// final Option option = new PropertyOption();
+// final List args = OptionTestCase.list();
+// final WriteableCommandLine commandLine =
+// OptionTestCase.commandLine(option, args);
+//
+// assertTrue(commandLine.looksLikeOption("-D"));
+// assertFalse(commandLine.looksLikeOption("--help"));
+// assertFalse(commandLine.looksLikeOption("+display"));
+// assertFalse(commandLine.looksLikeOption("myprefix"));
+// assertFalse(commandLine.looksLikeOption("myprefix2"));
+// assertFalse(commandLine.looksLikeOption("myprefference"));
+// assertFalse(commandLine.looksLikeOption("/SCANDISK"));
+// assertFalse(commandLine.looksLikeOption("update"));
+// }
- public void testGetOptions_Order() throws OptionException {
+ public final void testGetOptions_Order() throws OptionException {
final Option help = DefaultOptionTest.buildHelpOption();
final Option login = CommandTest.buildLoginCommand();
final Option targets = ArgumentTest.buildTargetsArgument();
@@ -208,7 +418,7 @@
assertFalse(i.hasNext());
}
- public void testGetOptionCount() throws OptionException {
+ public final void testGetOptionCount() throws OptionException {
final Option help = DefaultOptionTest.buildHelpOption();
final Option login = CommandTest.buildLoginCommand();
final Option targets = ArgumentTest.buildTargetsArgument();
@@ -241,7 +451,7 @@
assertEquals(0, cl.getOptionCount(display));
}
- public void testGetOptionCount_Strings() throws OptionException {
+ public final void testGetOptionCount_Strings() throws OptionException {
final Option help = DefaultOptionTest.buildHelpOption();
final Option login = CommandTest.buildLoginCommand();
final Option targets = ArgumentTest.buildTargetsArgument();
1.1.2.1 +94 -0 jakarta-commons/cli/src/test/org/apache/commons/cli2/Attic/WriteableCommandLineTest.java
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org