001: /*
002: * Copyright (C) 2007 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017: package com.Ostermiller.util;
018:
019: /**
020: * Regression tests for the command line options.
021: *
022: * More information about this class and code samples for suggested use are
023: * available from <a target="_top" href=
024: * "http://ostermiller.org/utils/CmdLn.html">ostermiller.org</a>.
025: *
026: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
027: * @since ostermillerutils 1.07.00
028: */
029: class CmdLnTests {
030:
031: /**
032: * Main method for tests
033: * @param args command line options (ignored)
034: *
035: * @since ostermillerutils 1.07.00
036: */
037: public static void main(String[] args) {
038: try {
039:
040: CmdLnOption option;
041:
042: option = new CmdLnOption("help", 'h')
043: .setDescription("this is the description, it can be long");
044: if (!" -h --help this is the description, it can be long"
045: .equals(option.getHelp("--", "-", 0, 80))) {
046: throw new Exception(
047: "80 char help string should be: ' -h --help this is the description, it can be long'");
048: }
049: if (!" -h --help this is the description,\n it can be long"
050: .equals(option.getHelp("--", "-", 0, 37))) {
051: throw new Exception(
052: "37 char help string should be: ' -h --help this is the description,\n it can be long'");
053: }
054: if (!" -h --help this is the description,\n it can be long"
055: .equals(option.getHelp("--", "-", 0, 38))) {
056: throw new Exception(
057: "38 char help string should be: ' -h --help this is the description,\n it can be long'");
058: }
059: if (!" -h --help this is the description,\n it can be long"
060: .equals(option.getHelp("--", "-", 0, 39))) {
061: throw new Exception(
062: "39 char help string should be: ' -h --help this is the description,\n it can be long'");
063: }
064:
065: option = new CmdLnOption('h').setDescription("description");
066: if (!" -h description".equals(option.getHelp("--", "-",
067: 0, 10))) {
068: throw new Exception(
069: "10 char help string should be: ' -h description'");
070: }
071: option = new CmdLnOption('h').setDescription("description")
072: .setOptionalArgument();
073: if (!" -h <?> description".equals(option.getHelp("--",
074: "-", 0, 60))) {
075: throw new Exception(
076: "60 char help string should be: ' -h <?> description'");
077: }
078:
079: CmdLn clo;
080:
081: clo = new CmdLn(new String[] { "--help" })
082: .addOption(new CmdLnOption("help", 'h'));
083: if (clo.getResult('h') == null) {
084: throw new Exception("h option should have been present");
085: }
086: if (clo.getResult("help") == null) {
087: throw new Exception(
088: "help option should have been present");
089: }
090: if (clo.present('H')) {
091: throw new Exception(
092: "H option should not have been present");
093: }
094: if (clo.present("HELP")) {
095: throw new Exception(
096: "HELP option should not have been present");
097: }
098: if (clo.present("h")) {
099: throw new Exception(
100: "h long option should not have been present");
101: }
102: if (clo.getNonOptionArguments().size() != 0) {
103: throw new Exception(
104: "there should not have been left over arguments");
105: }
106:
107: clo = new CmdLn(new String[] { "-h" }).addOption(
108: new CmdLnOption("help", 'h')).addOption(
109: new CmdLnOption("argument").setOptionalArgument());
110: if (clo.getResult('h') == null) {
111: throw new Exception("h option should have been present");
112: }
113: if (clo.getResult("help") == null) {
114: throw new Exception(
115: "help option should have been present");
116: }
117: if (clo.getResult("argument") != null) {
118: throw new Exception(
119: "argument option should not have been present");
120: }
121: if (clo.getNonOptionArguments().size() != 0) {
122: throw new Exception(
123: "there should not have been left over arguments");
124: }
125:
126: clo = new CmdLn(new String[] { "-f", "file", "one" })
127: .addOption(new CmdLnOption('f')
128: .setRequiredArgument());
129: if (clo.getResult('f') == null) {
130: throw new Exception("f option should have been present");
131: }
132: if (clo.getResult('f').getArgumentCount() != 1) {
133: throw new Exception("f should have had one argument");
134: }
135: if (!"file".equals(clo.getResult('f').getArgument())) {
136: throw new Exception(
137: "f should have had an argument 'file'");
138: }
139: if (clo.getNonOptionArguments().size() != 1) {
140: throw new Exception(
141: "there should have been one left over argument");
142: }
143: if (!"one".equals(clo.getNonOptionArguments().get(0))) {
144: throw new Exception(
145: "left over argument should have been 'one'");
146: }
147:
148: clo = new CmdLn(new String[] { "-f", "-", "2", "3",
149: "-it=hello", "--car:thirty", "-p " }).addOption(
150: new CmdLnOption('f').setUnlimitedArguments())
151: .addOption(
152: new CmdLnOption('i')
153: .setUnlimitedArguments())
154: .addOption(
155: new CmdLnOption('t').setOptionalArgument())
156: .addOption(
157: new CmdLnOption("car")
158: .setRequiredArgument()).addOption(
159: new CmdLnOption('p').setRequiredArgument());
160: if (clo.getResult('f') == null) {
161: throw new Exception("f option should have been present");
162: }
163: if (clo.getResult('f').getArgumentCount() != 3) {
164: throw new Exception("f should have had three arguments");
165: }
166: if (!"-".equals(clo.getResult('f').getArgument())) {
167: throw new Exception("f should have had an argument '-'");
168: }
169: if (!"-".equals(clo.getResult('f').getArguments().get(0))) {
170: throw new Exception("f should have had an argument '-'");
171: }
172: if (!"2".equals(clo.getResult('f').getArguments().get(1))) {
173: throw new Exception("f should have had an argument '1'");
174: }
175: if (!"3".equals(clo.getResult('f').getArguments().get(2))) {
176: throw new Exception("f should have had an argument '2'");
177: }
178: if (clo.getNonOptionArguments().size() != 0) {
179: throw new Exception(
180: "there should have been no left over arguments");
181: }
182: if (!clo.present('t')) {
183: throw new Exception("t option should have been present");
184: }
185: if (!"hello".equals(clo.getResult('t').getArgument())) {
186: throw new Exception(
187: "t option should have had argument 'hello'");
188: }
189: if (!clo.present("car")) {
190: throw new Exception("t option should have been present");
191: }
192: if (!"thirty".equals(clo.getResult("car").getArgument())) {
193: throw new Exception(
194: "car option should have had argument 'thirty'");
195: }
196: if (!clo.present('p')) {
197: throw new Exception("p option should have been present");
198: }
199: if (!"".equals(clo.getResult('p').getArgument())) {
200: throw new Exception(
201: "p option should have had argument ''");
202: }
203:
204: clo = new CmdLn(new String[] { "-help" }).addOption(
205: new CmdLnOption("help")).setOptionStarts("-", null);
206: if (!clo.present("help")) {
207: throw new Exception(
208: "help option should have been present");
209: }
210: if (clo.present('h')) {
211: throw new Exception(
212: "h option should not have been present");
213: }
214:
215: clo = new CmdLn(new String[] { "!!!air=wall",
216: "@@@bed=soft", "###fog", "$$$hum" }).addOptions(
217: new CmdLnOption[] {
218: new CmdLnOption('a'),
219: new CmdLnOption('i'),
220: new CmdLnOption('r').setOptionalArgument(),
221: new CmdLnOption("bed")
222: .setOptionalArgument(),
223: new CmdLnOption('f'), new CmdLnOption('o'),
224: new CmdLnOption('g'),
225: new CmdLnOption("hum"), }).setOptionStarts(
226: new String[] { "@@@", "$$$" },
227: new String[] { "!!!", "###" });
228: if (!clo.present('a')) {
229: throw new Exception("a option should have been present");
230: }
231: if (!clo.present('i')) {
232: throw new Exception("i option should have been present");
233: }
234: if (!clo.present('r')) {
235: throw new Exception("r option should have been present");
236: }
237: if (!"wall".equals(clo.getResult('r').getArgument())) {
238: throw new Exception("r should have had arument 'wall'");
239: }
240: if (!clo.present("bed")) {
241: throw new Exception(
242: "bed option should have been present");
243: }
244: if (!"soft".equals(clo.getResult("bed").getArgument())) {
245: throw new Exception(
246: "bed should have had arument 'soft'");
247: }
248: if (!clo.present('f')) {
249: throw new Exception("f option should have been present");
250: }
251: if (!clo.present('o')) {
252: throw new Exception("o option should have been present");
253: }
254: if (!clo.present('g')) {
255: throw new Exception("g option should have been present");
256: }
257: if (!clo.present("hum")) {
258: throw new Exception(
259: "hum option should have been present");
260: }
261:
262: clo = new CmdLn(new String[] { "-i", "1", "-t", "2", "3",
263: "4", "-i", "5", "-s", "6", "7", "8", "9", "10",
264: "11", "12", "-p" }).addOptions(new CmdLnOption[] {
265: new CmdLnOption('i').setRequiredArgument(),
266: new CmdLnOption('s').setArgumentBounds(1, 4),
267: new CmdLnOption('t').setOptionalArgument(),
268: new CmdLnOption('p').setOptionalArgument(),
269: new CmdLnOption('r').setRequiredArgument(), });
270: if (clo.getNonOptionArguments().size() != 5) {
271: throw new Exception(
272: "there should have been five left over arguments");
273: }
274: if (clo.getResult('r') != null) {
275: throw new Exception("r should not have been present");
276: }
277: if (clo.getResult('p') == null) {
278: throw new Exception("p should have been present");
279: }
280: if (clo.getResult('p').getArgument() != null) {
281: throw new Exception("p should not have had an argument");
282: }
283: if (!"2".equals(clo.getResult('t').getArgument())) {
284: throw new Exception("t should have had an argument '2'");
285: }
286: if (!"3".equals(clo.getNonOptionArguments().get(0))) {
287: throw new Exception(
288: "'3' should have been a left over argument");
289: }
290: if (!"4".equals(clo.getNonOptionArguments().get(1))) {
291: throw new Exception(
292: "'4' should have been a left over argument");
293: }
294: if (!"5".equals(clo.getResult('i').getArgument())) {
295: throw new Exception("i should have had an argument '5'");
296: }
297: if (!"6".equals(clo.getResult('s').getArgument())) {
298: throw new Exception("i should have had an argument '6'");
299: }
300: if (clo.getResult('s').getArgumentCount() != 4) {
301: throw new Exception("s should have had 4 arguments");
302: }
303: if (!"10".equals(clo.getNonOptionArguments().get(2))) {
304: throw new Exception(
305: "'10' should have been a left over argument");
306: }
307:
308: clo = new CmdLn(new String[] { "-f", "--", "-t" })
309: .addOption(new CmdLnOption('f'));
310: if (!clo.present('f')) {
311: throw new Exception("f option should have been present");
312: }
313: if (clo.getResult('f').getArgumentCount() != 0) {
314: throw new Exception("f should have had no arguments");
315: }
316: if (clo.present('t')) {
317: throw new Exception(
318: "t option should not have been present");
319: }
320: if (clo.getNonOptionArguments().size() != 1) {
321: throw new Exception(
322: "should have had one non optional argument");
323: }
324: if (!"-t".equals(clo.getNonOptionArguments().get(0))) {
325: throw new Exception(
326: "'-t' should have been non-optional argument");
327: }
328:
329: clo = new CmdLn(new String[] { "-f" });
330: UnknownCmdLnOptionException uclox = null;
331: try {
332: clo.parse();
333: } catch (UnknownCmdLnOptionException x) {
334: uclox = x;
335: }
336: if (uclox == null) {
337: throw new Exception(
338: "f option should have thrown exception");
339: }
340: if (!"f".equals(uclox.getOption())) {
341: throw new Exception(
342: "exception should have been for option f");
343: }
344:
345: clo = new CmdLn(new String[] { "-f=oops" })
346: .addOption(new CmdLnOption('f'));
347: ExtraCmdLnArgumentException eclax = null;
348: try {
349: clo.parse();
350: } catch (ExtraCmdLnArgumentException x) {
351: eclax = x;
352: }
353: if (eclax == null) {
354: throw new Exception(
355: "f option should have thrown exception");
356: }
357: if (!"f".equals(eclax.getOption().toString())) {
358: throw new Exception(
359: "exception should have been for option f");
360: }
361:
362: clo = new CmdLn(new String[] { "-f" })
363: .addOption(new CmdLnOption('f')
364: .setRequiredArgument());
365: MissingCmdLnArgumentException mclax = null;
366: try {
367: clo.parse();
368: } catch (MissingCmdLnArgumentException x) {
369: mclax = x;
370: }
371: if (mclax == null) {
372: throw new Exception(
373: "f option should have thrown exception");
374: }
375: if (!"f".equals(mclax.getOption().toString())) {
376: throw new Exception(
377: "exception should have been for option f");
378: }
379:
380: } catch (Exception x) {
381: x.printStackTrace();
382: System.exit(1);
383: }
384: System.exit(0);
385: }
386: }
|