KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > PatternOptionBuilderTest


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  *
8  * $Id: PatternOptionBuilderTest.java,v 1.1 2002/06/06 22:09:25 bayard Exp $
9  */

10 package org.apache.commons.cli;
11
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 import java.io.StringWriter JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17
18 /**
19  * Test case for the PatternOptionBuilder class
20  *
21  * @author Henri Yandell
22  **/

23 public class PatternOptionBuilderTest
24 extends TestCase
25 {
26     public static void main( String JavaDoc[] args )
27    {
28       String JavaDoc[] testName = { PatternOptionBuilderTest.class.getName() };
29       junit.textui.TestRunner.main(testName);
30    }
31
32    public static TestSuite suite()
33    {
34       return new TestSuite(PatternOptionBuilderTest.class);
35    }
36
37    public PatternOptionBuilderTest( String JavaDoc s )
38    {
39       super( s );
40    }
41
42    public void testSimplePattern()
43    {
44        try {
45            Options options = PatternOptionBuilder.parsePattern("a:b@cde>f+n%t/");
46            String JavaDoc[] args = new String JavaDoc[] { "-c", "-a", "foo", "-b", "java.util.Vector", "-e", "build.xml", "-f", "java.util.Calendar", "-n", "4.5", "-t", "http://jakarta.apache.org/" };
47       
48            CommandLineParser parser = new PosixParser();
49            CommandLine line = parser.parse(options,args);
50
51            // tests the char methods of CommandLine that delegate to
52
// the String methods
53
assertEquals("flag a", "foo", line.getOptionValue("a"));
54            assertEquals("flag a", "foo", line.getOptionValue('a'));
55            assertEquals("string flag a", "foo", line.getOptionObject("a"));
56            assertEquals("string flag a", "foo", line.getOptionObject('a'));
57            assertEquals("object flag b", new java.util.Vector JavaDoc(), line.getOptionObject("b"));
58            assertEquals("object flag b", new java.util.Vector JavaDoc(), line.getOptionObject('b'));
59            assertEquals("boolean true flag c", true, line.hasOption("c"));
60            assertEquals("boolean true flag c", true, line.hasOption('c'));
61            assertEquals("boolean false flag d", false, line.hasOption("d"));
62            assertEquals("boolean false flag d", false, line.hasOption('d'));
63            assertEquals("file flag e", new java.io.File JavaDoc("build.xml"), line.getOptionObject("e"));
64            assertEquals("file flag e", new java.io.File JavaDoc("build.xml"), line.getOptionObject('e'));
65            assertEquals("class flag f", java.util.Calendar JavaDoc.class, line.getOptionObject("f"));
66            assertEquals("class flag f", java.util.Calendar JavaDoc.class, line.getOptionObject('f'));
67            assertEquals("number flag n", new Float JavaDoc(4.5), line.getOptionObject("n"));
68            assertEquals("number flag n", new Float JavaDoc(4.5), line.getOptionObject('n'));
69            assertEquals("url flag t", new java.net.URL JavaDoc("http://jakarta.apache.org/"), line.getOptionObject("t"));
70            assertEquals("url flag t", new java.net.URL JavaDoc("http://jakarta.apache.org/"), line.getOptionObject('t'));
71            /// DATES NOT SUPPORTED YET.
72
// assertEquals("number flag t", new java.util.Date(1023400137276L), line.getOptionObject('z'));
73
// input is: "Thu Jun 06 17:48:57 EDT 2002"
74
}
75        catch( ParseException exp ) {
76            fail( exp.getMessage() );
77        }
78        catch( java.net.MalformedURLException JavaDoc exp ) {
79            fail( exp.getMessage() );
80        }
81    }
82
83 }
84
Popular Tags