Source Code Cross Referenced for BugsTest.java in  » Open-Source-Library » Apache-command-line » org » apache » commons » cli » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. JDK Core
2. JDK Modules
3. JDK Modules com.sun
4. JDK Modules com.sun.java
5. JDK Modules Platform
6. JDK Modules sun
7. Open Source Build
8. Open Source Graphic Library
9. Open Source IDE Eclipse
10. Open Source J2EE
11. Open Source JDBC Driver
12. Open Source Library
13. Open Source Library Database
14. Open Source Net
15. Open Source Script
16. Science
17. Security
18. Sevlet Container
19. SUN GlassFish
20. Swing Library
21. Web Services apache cxf 2.0.1
22. Web Services AXIS2
23. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Source Code / Java Documentation » Open Source Library » Apache command line » org.apache.commons.cli 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */package org.apache.commons.cli;
017:
018:        import java.io.ByteArrayOutputStream;
019:        import java.io.PrintStream;
020:        import java.io.PrintWriter;
021:        import java.io.StringWriter;
022:        import java.util.Properties;
023:
024:        import junit.framework.Test;
025:        import junit.framework.TestCase;
026:        import junit.framework.TestSuite;
027:
028:        public class BugsTest extends TestCase {
029:            /** CommandLine instance */
030:            private CommandLine _cmdline = null;
031:            private Option _option = null;
032:
033:            public static Test suite() {
034:                return new TestSuite(BugsTest.class);
035:            }
036:
037:            public BugsTest(String name) {
038:                super (name);
039:            }
040:
041:            public void setUp() {
042:            }
043:
044:            public void tearDown() {
045:            }
046:
047:            public void test11457() {
048:                Options options = new Options();
049:                options
050:                        .addOption(OptionBuilder.withLongOpt("verbose")
051:                                .create());
052:                String[] args = new String[] { "--verbose" };
053:
054:                CommandLineParser parser = new PosixParser();
055:
056:                try {
057:                    CommandLine cmd = parser.parse(options, args);
058:                    assertTrue(cmd.hasOption("verbose"));
059:                } catch (ParseException exp) {
060:                    exp.printStackTrace();
061:                    fail("Unexpected Exception: " + exp.getMessage());
062:                }
063:            }
064:
065:            public void test11458() {
066:                Options options = new Options();
067:                options.addOption(OptionBuilder.withValueSeparator('=')
068:                        .hasArgs().create('D'));
069:                options.addOption(OptionBuilder.withValueSeparator(':')
070:                        .hasArgs().create('p'));
071:                String[] args = new String[] { "-DJAVA_HOME=/opt/java",
072:                        "-pfile1:file2:file3" };
073:
074:                CommandLineParser parser = new PosixParser();
075:
076:                try {
077:                    CommandLine cmd = parser.parse(options, args);
078:
079:                    String[] values = cmd.getOptionValues('D');
080:
081:                    assertEquals(values[0], "JAVA_HOME");
082:                    assertEquals(values[1], "/opt/java");
083:
084:                    values = cmd.getOptionValues('p');
085:
086:                    assertEquals(values[0], "file1");
087:                    assertEquals(values[1], "file2");
088:                    assertEquals(values[2], "file3");
089:
090:                    java.util.Iterator iter = cmd.iterator();
091:                    while (iter.hasNext()) {
092:                        Option opt = (Option) iter.next();
093:                        switch (opt.getId()) {
094:                        case 'D':
095:                            assertEquals(opt.getValue(0), "JAVA_HOME");
096:                            assertEquals(opt.getValue(1), "/opt/java");
097:                            break;
098:                        case 'p':
099:                            assertEquals(opt.getValue(0), "file1");
100:                            assertEquals(opt.getValue(1), "file2");
101:                            assertEquals(opt.getValue(2), "file3");
102:                            break;
103:                        default:
104:                            fail("-D option not found");
105:                        }
106:                    }
107:                } catch (ParseException exp) {
108:                    fail("Unexpected Exception:\nMessage:" + exp.getMessage()
109:                            + "Type: " + exp.getClass().getName());
110:                }
111:            }
112:
113:            public void test11680() {
114:                Options options = new Options();
115:                options.addOption("f", true, "foobar");
116:                options.addOption("m", true, "missing");
117:                String[] args = new String[] { "-f", "foo" };
118:
119:                CommandLineParser parser = new PosixParser();
120:
121:                try {
122:                    CommandLine cmd = parser.parse(options, args);
123:
124:                    try {
125:                        cmd.getOptionValue("f", "default f");
126:                        cmd.getOptionValue("m", "default m");
127:                    } catch (NullPointerException exp) {
128:                        fail("NullPointer caught: " + exp.getMessage());
129:                    }
130:                } catch (ParseException exp) {
131:                    fail("Unexpected Exception: " + exp.getMessage());
132:                }
133:            }
134:
135:            public void test11456() {
136:                // Posix 
137:                Options options = new Options();
138:                options.addOption(OptionBuilder.hasOptionalArg().create('a'));
139:                options.addOption(OptionBuilder.hasArg().create('b'));
140:                String[] args = new String[] { "-a", "-bvalue" };
141:
142:                CommandLineParser parser = new PosixParser();
143:
144:                try {
145:                    CommandLine cmd = parser.parse(options, args);
146:                    assertEquals(cmd.getOptionValue('b'), "value");
147:                } catch (ParseException exp) {
148:                    fail("Unexpected Exception: " + exp.getMessage());
149:                }
150:
151:                // GNU
152:                options = new Options();
153:                options.addOption(OptionBuilder.hasOptionalArg().create('a'));
154:                options.addOption(OptionBuilder.hasArg().create('b'));
155:                args = new String[] { "-a", "-b", "value" };
156:
157:                parser = new GnuParser();
158:
159:                try {
160:                    CommandLine cmd = parser.parse(options, args);
161:                    assertEquals(cmd.getOptionValue('b'), "value");
162:                } catch (ParseException exp) {
163:                    fail("Unexpected Exception: " + exp.getMessage());
164:                }
165:
166:            }
167:
168:            public void test12210() {
169:                // create the main options object which will handle the first parameter
170:                Options mainOptions = new Options();
171:                // There can be 2 main exclusive options:  -exec|-rep
172:
173:                // Therefore, place them in an option group
174:
175:                String[] argv = new String[] { "-exec", "-exec_opt1",
176:                        "-exec_opt2" };
177:                OptionGroup grp = new OptionGroup();
178:
179:                grp.addOption(new Option("exec", false,
180:                        "description for this option"));
181:
182:                grp.addOption(new Option("rep", false,
183:                        "description for this option"));
184:
185:                mainOptions.addOptionGroup(grp);
186:
187:                // for the exec option, there are 2 options...
188:                Options execOptions = new Options();
189:                execOptions.addOption("exec_opt1", false, " desc");
190:                execOptions.addOption("exec_opt2", false, " desc");
191:
192:                // similarly, for rep there are 2 options...
193:                Options repOptions = new Options();
194:                repOptions.addOption("repopto", false, "desc");
195:                repOptions.addOption("repoptt", false, "desc");
196:
197:                // create the parser
198:                GnuParser parser = new GnuParser();
199:
200:                // finally, parse the arguments:
201:
202:                // first parse the main options to see what the user has specified
203:                // We set stopAtNonOption to true so it does not touch the remaining
204:                // options
205:                try {
206:                    CommandLine cmd = parser.parse(mainOptions, argv, true);
207:                    // get the remaining options...
208:                    argv = cmd.getArgs();
209:
210:                    if (cmd.hasOption("exec")) {
211:                        cmd = parser.parse(execOptions, argv, false);
212:                        // process the exec_op1 and exec_opt2...
213:                        assertTrue(cmd.hasOption("exec_opt1"));
214:                        assertTrue(cmd.hasOption("exec_opt2"));
215:                    } else if (cmd.hasOption("rep")) {
216:                        cmd = parser.parse(repOptions, argv, false);
217:                        // process the rep_op1 and rep_opt2...
218:                    } else {
219:                        fail("exec option not found");
220:                    }
221:                } catch (ParseException exp) {
222:                    fail("Unexpected exception: " + exp.getMessage());
223:                }
224:            }
225:
226:            public void test13425() {
227:                Options options = new Options();
228:                Option oldpass = OptionBuilder.withLongOpt("old-password")
229:                        .withDescription(
230:                                "Use this option to specify the old password")
231:                        .hasArg().create('o');
232:                Option newpass = OptionBuilder.withLongOpt("new-password")
233:                        .withDescription(
234:                                "Use this option to specify the new password")
235:                        .hasArg().create('n');
236:
237:                String[] args = { "-o", "-n", "newpassword" };
238:
239:                options.addOption(oldpass);
240:                options.addOption(newpass);
241:
242:                Parser parser = new PosixParser();
243:
244:                try {
245:                    CommandLine line = parser.parse(options, args);
246:                }
247:                // catch the exception and leave the method
248:                catch (Exception exp) {
249:                    assertTrue(exp != null);
250:                    return;
251:                }
252:                fail("MissingArgumentException not caught.");
253:            }
254:
255:            public void test13666() {
256:                Options options = new Options();
257:                Option dir = OptionBuilder.withDescription("dir").hasArg()
258:                        .create('d');
259:                options.addOption(dir);
260:
261:                final PrintStream oldSystemOut = System.out;
262:                try {
263:                    final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
264:                    final PrintStream print = new PrintStream(bytes);
265:
266:                    // capture this platform's eol symbol
267:                    print.println();
268:                    final String eol = bytes.toString();
269:                    bytes.reset();
270:
271:                    System.setOut(new PrintStream(bytes));
272:                    try {
273:                        HelpFormatter formatter = new HelpFormatter();
274:                        formatter.printHelp("dir", options);
275:                    } catch (Exception exp) {
276:                        fail("Unexpected Exception: " + exp.getMessage());
277:                    }
278:                    assertEquals("usage: dir" + eol + " -d <arg>   dir" + eol,
279:                            bytes.toString());
280:                } finally {
281:                    System.setOut(oldSystemOut);
282:                }
283:            }
284:
285:            public void test13935() {
286:                OptionGroup directions = new OptionGroup();
287:
288:                Option left = new Option("l", "left", false, "go left");
289:                Option right = new Option("r", "right", false, "go right");
290:                Option straight = new Option("s", "straight", false,
291:                        "go straight");
292:                Option forward = new Option("f", "forward", false, "go forward");
293:                forward.setRequired(true);
294:
295:                directions.addOption(left);
296:                directions.addOption(right);
297:                directions.setRequired(true);
298:
299:                Options opts = new Options();
300:                opts.addOptionGroup(directions);
301:                opts.addOption(straight);
302:
303:                CommandLineParser parser = new PosixParser();
304:                boolean exception = false;
305:
306:                String[] args = new String[] {};
307:                try {
308:                    CommandLine line = parser.parse(opts, args);
309:                } catch (ParseException exp) {
310:                    exception = true;
311:                }
312:
313:                if (!exception) {
314:                    fail("Expected exception not caught.");
315:                }
316:
317:                exception = false;
318:
319:                args = new String[] { "-s" };
320:                try {
321:                    CommandLine line = parser.parse(opts, args);
322:                } catch (ParseException exp) {
323:                    exception = true;
324:                }
325:
326:                if (!exception) {
327:                    fail("Expected exception not caught.");
328:                }
329:
330:                exception = false;
331:
332:                args = new String[] { "-s", "-l" };
333:                try {
334:                    CommandLine line = parser.parse(opts, args);
335:                } catch (ParseException exp) {
336:                    fail("Unexpected exception: " + exp.getClass().getName()
337:                            + ":" + exp.getMessage());
338:                }
339:
340:                opts.addOption(forward);
341:                args = new String[] { "-s", "-l", "-f" };
342:                try {
343:                    CommandLine line = parser.parse(opts, args);
344:                } catch (ParseException exp) {
345:                    fail("Unexpected exception: " + exp.getClass().getName()
346:                            + ":" + exp.getMessage());
347:                }
348:            }
349:
350:            public void test14786() throws Exception {
351:                Option o = OptionBuilder.isRequired().withDescription("test")
352:                        .create("test");
353:                Options opts = new Options();
354:                opts.addOption(o);
355:                opts.addOption(o);
356:
357:                CommandLineParser parser = new GnuParser();
358:
359:                String[] args = new String[] { "-test" };
360:
361:                CommandLine line = parser.parse(opts, args);
362:                assertTrue(line.hasOption("test"));
363:            }
364:
365:            public void test15046() throws Exception {
366:                CommandLineParser parser = new PosixParser();
367:                final String[] CLI_ARGS = new String[] { "-z", "c" };
368:                Option option = new Option("z", "timezone", true,
369:                        "affected option");
370:                Options cliOptions = new Options();
371:                cliOptions.addOption(option);
372:                parser.parse(cliOptions, CLI_ARGS);
373:
374:                //now add conflicting option
375:                cliOptions.addOption("c", "conflict", true, "conflict option");
376:                CommandLine line = parser.parse(cliOptions, CLI_ARGS);
377:                assertEquals(option.getValue(), "c");
378:                assertTrue(!line.hasOption("c"));
379:            }
380:
381:            public void test15648() throws Exception {
382:                CommandLineParser parser = new PosixParser();
383:                final String[] args = new String[] { "-m", "\"Two Words\"" };
384:                Option m = OptionBuilder.hasArgs().create("m");
385:                Options options = new Options();
386:                options.addOption(m);
387:                CommandLine line = parser.parse(options, args);
388:                assertEquals("Two Words", line.getOptionValue("m"));
389:            }
390:
391:            public void test27635() {
392:                Option help = new Option("h", "help", false,
393:                        "print this message");
394:                Option version = new Option("v", "version", false,
395:                        "print version information");
396:                Option newRun = new Option("n", "new", false,
397:                        "Create NLT cache entries only for new items");
398:                Option trackerRun = new Option("t", "tracker", false,
399:                        "Create NLT cache entries only for tracker items");
400:
401:                Option timeLimit = OptionBuilder.withLongOpt("limit").hasArg()
402:                        .withValueSeparator().withDescription(
403:                                "Set time limit for execution, in mintues")
404:                        .create("l");
405:
406:                Option age = OptionBuilder
407:                        .withLongOpt("age")
408:                        .hasArg()
409:                        .withValueSeparator()
410:                        .withDescription(
411:                                "Age (in days) of cache item before being recomputed")
412:                        .create("a");
413:
414:                Option server = OptionBuilder.withLongOpt("server").hasArg()
415:                        .withValueSeparator().withDescription(
416:                                "The NLT server address").create("s");
417:
418:                Option numResults = OptionBuilder.withLongOpt("results")
419:                        .hasArg().withValueSeparator().withDescription(
420:                                "Number of results per item").create("r");
421:
422:                Option configFile = OptionBuilder.withLongOpt("config")
423:                        .hasArg().withValueSeparator().withDescription(
424:                                "Use the specified configuration file")
425:                        .create();
426:
427:                Options mOptions = new Options();
428:                mOptions.addOption(help);
429:                mOptions.addOption(version);
430:                mOptions.addOption(newRun);
431:                mOptions.addOption(trackerRun);
432:                mOptions.addOption(timeLimit);
433:                mOptions.addOption(age);
434:                mOptions.addOption(server);
435:                mOptions.addOption(numResults);
436:                mOptions.addOption(configFile);
437:
438:                HelpFormatter formatter = new HelpFormatter();
439:                final String EOL = System.getProperty("line.separator");
440:                StringWriter out = new StringWriter();
441:                formatter.printHelp(new PrintWriter(out), 80, "commandline",
442:                        "header", mOptions, 2, 2, "footer", true);
443:                assertEquals(
444:                        "usage: commandline [-a <arg>] [--config <arg>] [-h] [-l <arg>] [-n] [-r <arg>]"
445:                                + EOL
446:                                + "       [-s <arg>] [-t] [-v]"
447:                                + EOL
448:                                + "header"
449:                                + EOL
450:                                + "  -a,--age <arg>      Age (in days) of cache item before being recomputed"
451:                                + EOL
452:                                + "     --config <arg>   Use the specified configuration file"
453:                                + EOL
454:                                + "  -h,--help           print this message"
455:                                + EOL
456:                                + "  -l,--limit <arg>    Set time limit for execution, in mintues"
457:                                + EOL
458:                                + "  -n,--new            Create NLT cache entries only for new items"
459:                                + EOL
460:                                + "  -r,--results <arg>  Number of results per item"
461:                                + EOL
462:                                + "  -s,--server <arg>   The NLT server address"
463:                                + EOL
464:                                + "  -t,--tracker        Create NLT cache entries only for tracker items"
465:                                + EOL
466:                                + "  -v,--version        print version information"
467:                                + EOL + "footer" + EOL, out.toString());
468:            }
469:
470:            public void test31148() throws ParseException {
471:                Option multiArgOption = new Option("o",
472:                        "option with multiple args");
473:                multiArgOption.setArgs(1);
474:
475:                Options options = new Options();
476:                options.addOption(multiArgOption);
477:
478:                Parser parser = new PosixParser();
479:                String[] args = new String[] {};
480:                Properties props = new Properties();
481:                props.setProperty("o", "ovalue");
482:                CommandLine cl = parser.parse(options, args, props);
483:
484:                assertTrue(cl.hasOption('o'));
485:                assertEquals("ovalue", cl.getOptionValue('o'));
486:            }
487:
488:            public void test21215() {
489:                Options options = new Options();
490:                HelpFormatter formatter = new HelpFormatter();
491:                String SEP = System.getProperty("line.separator");
492:                String header = SEP + "Header";
493:                String footer = "Footer";
494:                StringWriter out = new StringWriter();
495:                formatter.printHelp(new PrintWriter(out), 80, "foobar", header,
496:                        options, 2, 2, footer, true);
497:                assertEquals("usage: foobar" + SEP + "" + SEP + "Header" + SEP
498:                        + "" + SEP + "Footer" + SEP, out.toString());
499:            }
500:
501:            public void test19383() {
502:                Options options = new Options();
503:                options.addOption(new Option("a", "aaa", false, "aaaaaaa"));
504:                options.addOption(new Option(null, "bbb", false, "bbbbbbb"));
505:                options.addOption(new Option("c", null, false, "ccccccc"));
506:
507:                HelpFormatter formatter = new HelpFormatter();
508:                String SEP = System.getProperty("line.separator");
509:                StringWriter out = new StringWriter();
510:                formatter.printHelp(new PrintWriter(out), 80, "foobar", "",
511:                        options, 2, 2, "", true);
512:                assertEquals("usage: foobar [-a] [--bbb] [-c]" + SEP
513:                        + "  -a,--aaa  aaaaaaa" + SEP + "     --bbb  bbbbbbb"
514:                        + SEP + "  -c        ccccccc" + SEP, out.toString());
515:            }
516:
517:        }
w__w___w.java_2_s_.c___om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.