001: /*
002: Copyright 2004-2007 Paul R. Holser, Jr. All rights reserved.
003: Licensed under the Academic Free License version 3.0
004: */
005:
006: package joptsimple;
007:
008: import java.math.BigInteger;
009: import java.sql.Time;
010: import java.sql.Timestamp;
011: import java.util.Collections;
012:
013: /**
014: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
015: * @version $Id: OptionParserNewDeclarationTest.java,v 1.17 2007/04/10 20:06:26 pholser Exp $
016: */
017: public class OptionParserNewDeclarationTest extends
018: AbstractOptionParserFixture {
019: public void testAcceptsNull() {
020: try {
021: parser.accepts(null);
022: fail();
023: } catch (NullPointerException expected) {
024: assertTrue(expected.getMessage(), true);
025: }
026: }
027:
028: public void testAcceptsIllegalCharacters() {
029: try {
030: parser.accepts("!");
031: fail();
032: } catch (IllegalOptionSpecificationException expected) {
033: assertTrue(expected.getMessage(), true);
034: }
035: }
036:
037: public void testBooleanArgumentType() {
038: parser.accepts("a").withRequiredArg().ofType(Boolean.class);
039: parser.accepts("b").withOptionalArg().ofType(Boolean.class);
040:
041: OptionSet options = parser.parse(new String[] { "-a", "true",
042: "-b", "false" });
043:
044: assertOptionDetected(options, "a");
045: assertOptionDetected(options, "b");
046: assertEquals(Boolean.TRUE, options.valueOf("a"));
047: assertEquals(Collections.singletonList(Boolean.TRUE), options
048: .valuesOf("a"));
049: assertEquals(Boolean.FALSE, options.valueOf("b"));
050: assertEquals(Collections.singletonList(Boolean.FALSE), options
051: .valuesOf("b"));
052: assertEquals(Collections.EMPTY_LIST, options
053: .nonOptionArguments());
054: }
055:
056: public void testByteArgumentType() {
057: parser.accepts("a").withRequiredArg().ofType(Byte.class);
058: parser.accepts("b").withOptionalArg().ofType(Byte.class);
059:
060: OptionSet options = parser.parse(new String[] { "-a", "-1",
061: "-b", "-2" });
062:
063: assertOptionDetected(options, "a");
064: assertOptionDetected(options, "b");
065: assertEquals(Byte.valueOf("-1"), options.valueOf("a"));
066: assertEquals(Collections.singletonList(Byte.valueOf("-1")),
067: options.valuesOf("a"));
068: assertEquals(Byte.valueOf("-2"), options.valueOf("b"));
069: assertEquals(Collections.singletonList(Byte.valueOf("-2")),
070: options.valuesOf("b"));
071: assertEquals(Collections.EMPTY_LIST, options
072: .nonOptionArguments());
073: }
074:
075: public void testDoubleArgumentType() {
076: parser.accepts("a").withRequiredArg().ofType(Double.class);
077: parser.accepts("b").withOptionalArg().ofType(Double.class);
078:
079: OptionSet options = parser.parse(new String[] { "-a",
080: "3.1415926D", "-b", "6.02E23" });
081:
082: assertOptionDetected(options, "a");
083: assertOptionDetected(options, "b");
084: assertEquals(Double.valueOf("3.1415926D"), options.valueOf("a"));
085: assertEquals(Collections.singletonList(Double
086: .valueOf("3.1415926D")), options.valuesOf("a"));
087: assertEquals(Double.valueOf("6.02E23"), options.valueOf("b"));
088: assertEquals(Collections.singletonList(Double
089: .valueOf("6.02E23")), options.valuesOf("b"));
090: assertEquals(Collections.EMPTY_LIST, options
091: .nonOptionArguments());
092: }
093:
094: public void testFloatArgumentType() {
095: parser.accepts("a").withRequiredArg().ofType(Float.class);
096: parser.accepts("b").withOptionalArg().ofType(Float.class);
097:
098: OptionSet options = parser.parse(new String[] { "-a",
099: "3.1415926F", "-b", "6.02E23F" });
100:
101: assertOptionDetected(options, "a");
102: assertOptionDetected(options, "b");
103: assertEquals(Float.valueOf("3.1415926F"), options.valueOf("a"));
104: assertEquals(Collections.singletonList(Float
105: .valueOf("3.1415926F")), options.valuesOf("a"));
106: assertEquals(Float.valueOf("6.02E23F"), options.valueOf("b"));
107: assertEquals(Collections.singletonList(Float
108: .valueOf("6.02E23F")), options.valuesOf("b"));
109: assertEquals(Collections.EMPTY_LIST, options
110: .nonOptionArguments());
111: }
112:
113: public void testIntegerArgumentType() {
114: parser.accepts("a").withRequiredArg().ofType(Integer.class);
115: parser.accepts("b").withOptionalArg().ofType(Integer.class);
116:
117: OptionSet options = parser.parse(new String[] { "-a", "12",
118: "-b", "34" });
119:
120: assertOptionDetected(options, "a");
121: assertOptionDetected(options, "b");
122: assertEquals(Integer.valueOf("12"), options.valueOf("a"));
123: assertEquals(Collections.singletonList(Integer.valueOf("12")),
124: options.valuesOf("a"));
125: assertEquals(Integer.valueOf("34"), options.valueOf("b"));
126: assertEquals(Collections.singletonList(Integer.valueOf("34")),
127: options.valuesOf("b"));
128: assertEquals(Collections.EMPTY_LIST, options
129: .nonOptionArguments());
130: }
131:
132: public void testLongArgumentType() {
133: parser.accepts("a").withRequiredArg().ofType(Long.class);
134: parser.accepts("b").withOptionalArg().ofType(Long.class);
135:
136: OptionSet options = parser.parse(new String[] { "-a",
137: "123454678901234", "-b", "98765432109876" });
138:
139: assertOptionDetected(options, "a");
140: assertOptionDetected(options, "b");
141: assertEquals(Long.valueOf("123454678901234"), options
142: .valueOf("a"));
143: assertEquals(Collections.singletonList(Long
144: .valueOf("123454678901234")), options.valuesOf("a"));
145: assertEquals(Long.valueOf("98765432109876"), options
146: .valueOf("b"));
147: assertEquals(Collections.singletonList(Long
148: .valueOf("98765432109876")), options.valuesOf("b"));
149: assertEquals(Collections.EMPTY_LIST, options
150: .nonOptionArguments());
151: }
152:
153: public void testShortArgumentType() {
154: parser.accepts("a").withRequiredArg().ofType(Short.class);
155: parser.accepts("b").withOptionalArg().ofType(Short.class);
156:
157: OptionSet options = parser.parse(new String[] { "-a", "5675",
158: "-b", "345" });
159:
160: assertOptionDetected(options, "a");
161: assertOptionDetected(options, "b");
162: assertEquals(Short.valueOf("5675"), options.valueOf("a"));
163: assertEquals(Collections.singletonList(Short.valueOf("5675")),
164: options.valuesOf("a"));
165: assertEquals(Short.valueOf("345"), options.valueOf("b"));
166: assertEquals(Collections.singletonList(Short.valueOf("345")),
167: options.valuesOf("b"));
168: assertEquals(Collections.EMPTY_LIST, options
169: .nonOptionArguments());
170: }
171:
172: public void testSqlDateArgumentType() {
173: parser.accepts("a").withRequiredArg().ofType(
174: java.sql.Date.class);
175: parser.accepts("b").withOptionalArg().ofType(
176: java.sql.Date.class);
177:
178: OptionSet options = parser.parse(new String[] { "-a",
179: "2001-09-11", "-b", "1941-12-07" });
180:
181: assertOptionDetected(options, "a");
182: assertOptionDetected(options, "b");
183: assertEquals(java.sql.Date.valueOf("2001-09-11"), options
184: .valueOf("a"));
185: assertEquals(Collections.singletonList(java.sql.Date
186: .valueOf("2001-09-11")), options.valuesOf("a"));
187: assertEquals(java.sql.Date.valueOf("1941-12-07"), options
188: .valueOf("b"));
189: assertEquals(Collections.singletonList(java.sql.Date
190: .valueOf("1941-12-07")), options.valuesOf("b"));
191: assertEquals(Collections.EMPTY_LIST, options
192: .nonOptionArguments());
193: }
194:
195: public void testSqlTimeArgumentType() {
196: parser.accepts("a").withRequiredArg().ofType(Time.class);
197: parser.accepts("b").withOptionalArg().ofType(Time.class);
198:
199: OptionSet options = parser.parse(new String[] { "-a",
200: "08:57:39", "-b", "23:59:59" });
201:
202: assertOptionDetected(options, "a");
203: assertOptionDetected(options, "b");
204: assertEquals(Time.valueOf("08:57:39"), options.valueOf("a"));
205: assertEquals(Collections
206: .singletonList(Time.valueOf("08:57:39")), options
207: .valuesOf("a"));
208: assertEquals(Time.valueOf("23:59:59"), options.valueOf("b"));
209: assertEquals(Collections
210: .singletonList(Time.valueOf("23:59:59")), options
211: .valuesOf("b"));
212: assertEquals(Collections.EMPTY_LIST, options
213: .nonOptionArguments());
214: }
215:
216: public void testSqlTimestampArgumentType() {
217: parser.accepts("a").withRequiredArg().ofType(Timestamp.class);
218: parser.accepts("b").withOptionalArg().ofType(Timestamp.class);
219:
220: OptionSet options = parser.parse(new String[] { "-a",
221: "1970-01-01 00:00:00", "-b",
222: "1979-12-31 23:59:59.0123456" });
223:
224: assertOptionDetected(options, "a");
225: assertOptionDetected(options, "b");
226: assertEquals(Timestamp.valueOf("1970-01-01 00:00:00"), options
227: .valueOf("a"));
228: assertEquals(Collections.singletonList(Timestamp
229: .valueOf("1970-01-01 00:00:00")), options.valuesOf("a"));
230: assertEquals(Timestamp.valueOf("1979-12-31 23:59:59.0123456"),
231: options.valueOf("b"));
232: assertEquals(Collections.singletonList(Timestamp
233: .valueOf("1979-12-31 23:59:59.0123456")), options
234: .valuesOf("b"));
235: assertEquals(Collections.EMPTY_LIST, options
236: .nonOptionArguments());
237: }
238:
239: public void testIllegalOptionArgumentMethodConversion() {
240: parser.accepts("a").withRequiredArg().ofType(Integer.class);
241:
242: try {
243: parser.parse(new String[] { "-a", "foo" });
244: fail();
245: } catch (OptionException expected) {
246: assertEquals(
247: "cannot convert argument 'foo' of option 'a' to class java.lang.Integer",
248: expected.getMessage());
249: }
250: }
251:
252: public void testIllegalOptionArgumentConstructorConversion() {
253: parser.accepts("a").withRequiredArg().ofType(BigInteger.class);
254:
255: try {
256: parser.parse(new String[] { "-a", "foo" });
257: fail();
258: } catch (OptionException expected) {
259: assertTrue(expected.getMessage(), true);
260: }
261: }
262:
263: public void testOptionsWithOptionalNegativeNumberArguments() {
264: parser.accepts("a").withOptionalArg().ofType(Integer.class);
265: parser.accepts("b").withOptionalArg().ofType(Integer.class);
266: parser.accepts("1");
267: parser.accepts("2");
268:
269: OptionSet options = parser.parse(new String[] { "-a", "-1",
270: "-b", "-2" });
271:
272: assertOptionDetected(options, "a");
273: assertOptionDetected(options, "b");
274: assertEquals(Integer.valueOf("-1"), options.valueOf("a"));
275: assertEquals(Collections.singletonList(Integer.valueOf("-1")),
276: options.valuesOf("a"));
277: assertEquals(Integer.valueOf("-2"), options.valueOf("b"));
278: assertEquals(Collections.singletonList(Integer.valueOf("-2")),
279: options.valuesOf("b"));
280: assertEquals(Collections.EMPTY_LIST, options
281: .nonOptionArguments());
282: }
283:
284: public void testOptionsWithOptionalNegativeNumberArgumentsAndNumberOptions() {
285: parser.accepts("a").withOptionalArg().ofType(Integer.class);
286: parser.accepts("b").withOptionalArg().ofType(Integer.class);
287: parser.accepts("1");
288: parser.accepts("2");
289:
290: OptionSet options = parser.parse(new String[] { "-1", "-2",
291: "-a", "-b" });
292:
293: assertOptionDetected(options, "1");
294: assertOptionDetected(options, "a");
295: assertOptionDetected(options, "2");
296: assertOptionDetected(options, "b");
297: assertNull(options.valueOf("1"));
298: assertNull(options.valueOf("a"));
299: assertNull(options.valueOf("2"));
300: assertNull(options.valueOf("b"));
301: assertEquals(Collections.EMPTY_LIST, options.valuesOf("1"));
302: assertEquals(Collections.EMPTY_LIST, options.valuesOf("a"));
303: assertEquals(Collections.EMPTY_LIST, options.valuesOf("2"));
304: assertEquals(Collections.EMPTY_LIST, options.valuesOf("b"));
305: assertEquals(Collections.EMPTY_LIST, options
306: .nonOptionArguments());
307: }
308:
309: public void testOptionsWithNegativeNumberArgumentsAndNonNumberOptions() {
310: parser.accepts("a").withOptionalArg().ofType(Integer.class);
311: parser.accepts("b").withOptionalArg().ofType(Integer.class);
312: parser.accepts("1");
313: parser.accepts("2");
314:
315: OptionSet options = parser.parse(new String[] { "-1", "-a",
316: "-b", "-2" });
317:
318: assertOptionDetected(options, "1");
319: assertOptionDetected(options, "a");
320: assertOptionDetected(options, "b");
321: assertOptionNotDetected(options, "2");
322: assertNull(options.valueOf("1"));
323: assertNull(options.valueOf("a"));
324: assertEquals(Integer.valueOf("-2"), options.valueOf("b"));
325: assertEquals(Collections.EMPTY_LIST, options.valuesOf("1"));
326: assertEquals(Collections.EMPTY_LIST, options.valuesOf("a"));
327: assertEquals(Collections.singletonList(Integer.valueOf("-2")),
328: options.valuesOf("b"));
329: assertEquals(Collections.EMPTY_LIST, options
330: .nonOptionArguments());
331: }
332: }
|