001: package org.incava.jagol;
002:
003: import java.io.*;
004: import java.util.*;
005: import junit.framework.TestCase;
006:
007: public class TestOptionSet extends TestCase {
008: OptionSet optSet = new OptionSet("app",
009: "this application does wonderful things");
010:
011: IntegerOption intOpt = new IntegerOption("intopt",
012: "this option takes an integer argument");
013: StringOption stringOpt = new StringOption("stringopt",
014: "this option takes a string argument");
015: FloatOption floatOpt = new FloatOption("floatopt",
016: "this option takes a float argument");
017: DoubleOption doubleOpt = new DoubleOption("doubleopt",
018: "this option takes a double argument");
019: BooleanOption booleanOpt = new BooleanOption("booleanopt",
020: "this option takes a boolean argument");
021:
022: public TestOptionSet(String name) {
023: super (name);
024:
025: tr.Ace.log("running");
026: }
027:
028: public void setUp() {
029: tr.Ace.log("setting up");
030:
031: floatOpt.setShortName('f');
032:
033: optSet.add(intOpt);
034: optSet.add(stringOpt);
035: optSet.add(floatOpt);
036: optSet.add(doubleOpt);
037: optSet.add(booleanOpt);
038: }
039:
040: public void testCommandLine() {
041: tr.Ace.log("testing command line");
042:
043: tr.Ace.log("done adding");
044:
045: String[] args = new String[] { "--intopt", "1",
046: "--stringopt=two", "-f", "3.1415", "--no-booleanopt",
047: "--doubleopt", "4.14" };
048:
049: args = optSet.process(args);
050:
051: assertEquals(new Integer(1), intOpt.getValue());
052: assertEquals("two", stringOpt.getValue());
053: assertEquals(new Float(3.1415F), floatOpt.getValue());
054: assertEquals(new Double(4.14), doubleOpt.getValue());
055: assertEquals(Boolean.FALSE, booleanOpt.getValue());
056: }
057:
058: public void testCommandLineRemainingArgs() {
059: tr.Ace.log("testing command line");
060:
061: tr.Ace.log("done adding");
062:
063: String[] args = new String[] { "--intopt", "1",
064: "--stringopt=two", "-f", "3.1415", "--no-booleanopt",
065: "--doubleopt", "4.14", "foo", "bar", "baz" };
066:
067: args = optSet.process(args);
068:
069: assertEquals(new Integer(1), intOpt.getValue());
070: assertEquals("two", stringOpt.getValue());
071: assertEquals(new Float(3.1415F), floatOpt.getValue());
072: assertEquals(new Double(4.14), doubleOpt.getValue());
073: assertEquals(Boolean.FALSE, booleanOpt.getValue());
074:
075: assertEquals(3, args.length);
076: assertEquals("foo", args[0]);
077: assertEquals("bar", args[1]);
078: assertEquals("baz", args[2]);
079: }
080:
081: public void testUsage() {
082: tr.Ace.log("testing usage");
083:
084: String[] args = new String[] { "--help" };
085: optSet.addRunControlFile("/etc/TestOptionSet.conf");
086: optSet.addRunControlFile("~/.TestOptionSet");
087:
088: optSet.process(args);
089: }
090:
091: public void testConfig() {
092: tr.Ace.log("testing config help");
093:
094: String[] args = new String[] { "--help-config" };
095: optSet.addRunControlFile("/etc/TestOptionSet.conf");
096: optSet.addRunControlFile("~/.TestOptionSet");
097:
098: optSet.process(args);
099: }
100:
101: public void testRunControlFile() {
102: tr.Ace.log("testing command line");
103:
104: tr.Ace.log("done adding");
105:
106: try {
107: String userHome = System.getProperty("user.home");
108: String rcFileName = userHome + "/.TestOptionSet";
109: File rcFile = new File(rcFileName);
110:
111: Writer out = new BufferedWriter(new FileWriter(rcFile));
112: out.write("intopt: 999\n");
113: out.write("stringopt: april\n");
114: out.write("floatopt: 8.41\n");
115: out.write("booleanopt: false\n");
116: out.write("doubleopt: 66.938432\n");
117: out.close();
118:
119: optSet.addRunControlFile("~/.TestOptionSet");
120:
121: String[] args = new String[] { "app" };
122:
123: args = optSet.process(args);
124:
125: assertEquals(new Integer(999), intOpt.getValue());
126: assertEquals("april", stringOpt.getValue());
127: assertEquals(new Float(8.41F), floatOpt.getValue());
128: assertEquals(new Double(66.938432), doubleOpt.getValue());
129: assertEquals(Boolean.FALSE, booleanOpt.getValue());
130:
131: rcFile.delete();
132: } catch (IOException ioe) {
133: fail("exception not expected");
134: }
135: }
136:
137: public void testRunControlFileAndCommandLine() {
138: tr.Ace.log("testing command line");
139:
140: tr.Ace.log("done adding");
141:
142: try {
143: String userHome = System.getProperty("user.home");
144: String rcFileName = userHome + "/.TestOptionSet";
145: File rcFile = new File(rcFileName);
146:
147: Writer out = new BufferedWriter(new FileWriter(rcFile));
148: out.write("intopt: 999\n");
149: out.write("stringopt: april\n");
150: out.write("floatopt: 8.41\n");
151: out.write("booleanopt: false\n");
152: out.write("doubleopt: 66.938432\n");
153: out.close();
154:
155: optSet.addRunControlFile("~/.TestOptionSet");
156:
157: String[] args = new String[] { "--doubleopt=4.38",
158: "--booleanopt" };
159:
160: args = optSet.process(args);
161:
162: assertEquals(new Integer(999), intOpt.getValue());
163: assertEquals("april", stringOpt.getValue());
164: assertEquals(new Float(8.41F), floatOpt.getValue());
165: assertEquals(new Double(4.38), doubleOpt.getValue());
166: assertEquals(Boolean.TRUE, booleanOpt.getValue());
167:
168: rcFile.delete();
169: } catch (IOException ioe) {
170: fail("exception not expected");
171: }
172: }
173:
174: }
|