1   /***
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli;
18  
19  import junit.framework.TestCase;
20  import junit.framework.TestSuite;
21  
22  /*** 
23   * Test case for the PatternOptionBuilder class 
24   *
25   * @author Henri Yandell
26   **/
27  public class PatternOptionBuilderTest
28  extends TestCase
29  {
30  
31     public static TestSuite suite()
32     {
33        return new TestSuite(PatternOptionBuilderTest.class);
34     }
35  
36     public void testSimplePattern()
37     {
38         try {
39             Options options = PatternOptionBuilder.parsePattern("a:b@cde>f+n%t/");
40             String[] args = new String[] { "-c", "-a", "foo", "-b", "java.util.Vector", "-e", "build.xml", "-f", "java.util.Calendar", "-n", "4.5", "-t", "http://jakarta.apache.org/" };
41        
42             CommandLineParser parser = new PosixParser();
43             CommandLine line = parser.parse(options,args);
44  
45             assertEquals("flag a", "foo", line.getOptionValue("a"));
46             assertEquals("string flag a", "foo", line.getOptionObject("a"));
47             assertEquals("object flag b", new java.util.Vector(), line.getOptionObject("b"));
48             assertTrue("boolean true flag c", line.hasOption("c"));
49             assertFalse("boolean false flag d", line.hasOption("d"));
50             assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject("e"));
51             assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject("f"));
52             assertEquals("number flag n", new Double(4.5), line.getOptionObject("n"));
53             assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject("t"));
54  
55             // tests the char methods of CommandLine that delegate to the String methods
56             assertEquals("flag a", "foo", line.getOptionValue('a'));
57             assertEquals("string flag a", "foo", line.getOptionObject('a'));
58             assertEquals("object flag b", new java.util.Vector(), line.getOptionObject('b'));
59             assertTrue("boolean true flag c", line.hasOption('c'));
60             assertFalse("boolean false flag d", line.hasOption('d'));
61             assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject('e'));
62             assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject('f'));
63             assertEquals("number flag n", new Double(4.5), line.getOptionObject('n'));
64             assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject('t'));
65  
66             /// DATES NOT SUPPORTED YET.
67             //      assertEquals("number flag t", new java.util.Date(1023400137276L), line.getOptionObject('z'));
68             //     input is:  "Thu Jun 06 17:48:57 EDT 2002"
69         }
70         catch( ParseException exp ) {
71             fail( exp.getMessage() );
72         }
73         catch( java.net.MalformedURLException exp ) {
74             fail( exp.getMessage() );
75         }
76     }
77  
78  }