View Javadoc
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  
18  package org.apache.commons.cli;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.net.URL;
30  import java.util.Calendar;
31  import java.util.Date;
32  import java.util.Vector;
33  
34  import org.junit.Test;
35  
36  /**
37   * Test case for the PatternOptionBuilder class.
38   */
39  @SuppressWarnings("deprecation") // tests some deprecated classes
40  public class PatternOptionBuilderTest {
41      @Test
42      public void testClassPattern() throws Exception {
43          final Options options = PatternOptionBuilder.parsePattern("c+d+");
44          final CommandLineParser parser = new PosixParser();
45          final CommandLine line = parser.parse(options, new String[] {"-c", "java.util.Calendar", "-d", "System.DateTime"});
46  
47          assertEquals("c value", Calendar.class, line.getOptionObject("c"));
48          assertNull("d value", line.getOptionObject("d"));
49      }
50  
51      @Test
52      public void testEmptyPattern() {
53          final Options options = PatternOptionBuilder.parsePattern("");
54          assertTrue(options.getOptions().isEmpty());
55      }
56  
57      @Test
58      public void testExistingFilePattern() throws Exception {
59          final Options options = PatternOptionBuilder.parsePattern("g<");
60          final CommandLineParser parser = new PosixParser();
61          final CommandLine line = parser.parse(options, new String[] {"-g", "src/test/resources/org/apache/commons/cli/existing-readable.file"});
62  
63          final Object parsedReadableFileStream = line.getOptionObject("g");
64  
65          assertNotNull("option g not parsed", parsedReadableFileStream);
66          assertTrue("option g not FileInputStream", parsedReadableFileStream instanceof FileInputStream);
67      }
68  
69      @Test
70      public void testExistingFilePatternFileNotExist() throws Exception {
71          final Options options = PatternOptionBuilder.parsePattern("f<");
72          final CommandLineParser parser = new PosixParser();
73          final CommandLine line = parser.parse(options, new String[] {"-f", "non-existing.file"});
74  
75          assertNull("option f parsed", line.getOptionObject("f"));
76      }
77  
78      @Test
79      public void testNumberPattern() throws Exception {
80          final Options options = PatternOptionBuilder.parsePattern("n%d%x%");
81          final CommandLineParser parser = new PosixParser();
82          final CommandLine line = parser.parse(options, new String[] {"-n", "1", "-d", "2.1", "-x", "3,5"});
83  
84          assertEquals("n object class", Long.class, line.getOptionObject("n").getClass());
85          assertEquals("n value", Long.valueOf(1), line.getOptionObject("n"));
86  
87          assertEquals("d object class", Double.class, line.getOptionObject("d").getClass());
88          assertEquals("d value", Double.valueOf(2.1), line.getOptionObject("d"));
89  
90          assertNull("x object", line.getOptionObject("x"));
91      }
92  
93      @Test
94      public void testObjectPattern() throws Exception {
95          final Options options = PatternOptionBuilder.parsePattern("o@i@n@");
96          final CommandLineParser parser = new PosixParser();
97          final CommandLine line = parser.parse(options, new String[] {"-o", "java.lang.String", "-i", "java.util.Calendar", "-n", "System.DateTime"});
98  
99          assertEquals("o value", "", line.getOptionObject("o"));
100         assertNull("i value", line.getOptionObject("i"));
101         assertNull("n value", line.getOptionObject("n"));
102     }
103 
104     @Test
105     public void testRequiredOption() throws Exception {
106         final Options options = PatternOptionBuilder.parsePattern("!n%m%");
107         final CommandLineParser parser = new PosixParser();
108 
109         try {
110             parser.parse(options, new String[] {""});
111             fail("MissingOptionException wasn't thrown");
112         } catch (final MissingOptionException e) {
113             assertEquals(1, e.getMissingOptions().size());
114             assertTrue(e.getMissingOptions().contains("n"));
115         }
116     }
117 
118     @Test
119     public void testSimplePattern() throws Exception {
120         final Options options = PatternOptionBuilder.parsePattern("a:b@cde>f+n%t/m*z#");
121         final String[] args = {"-c", "-a", "foo", "-b", "java.util.Vector", "-e", "build.xml", "-f", "java.util.Calendar", "-n", "4.5", "-t",
122             "https://commons.apache.org", "-z", "Thu Jun 06 17:48:57 EDT 2002", "-m", "test*"};
123 
124         final CommandLineParser parser = new PosixParser();
125         final CommandLine line = parser.parse(options, args);
126 
127         assertEquals("flag a", "foo", line.getOptionValue("a"));
128         assertEquals("string flag a", "foo", line.getOptionObject("a"));
129         assertEquals("object flag b", new Vector<>(), line.getOptionObject("b"));
130         assertTrue("boolean true flag c", line.hasOption("c"));
131         assertFalse("boolean false flag d", line.hasOption("d"));
132         assertEquals("file flag e", new File("build.xml"), line.getOptionObject("e"));
133         assertEquals("class flag f", Calendar.class, line.getOptionObject("f"));
134         assertEquals("number flag n", Double.valueOf(4.5), line.getOptionObject("n"));
135         assertEquals("url flag t", new URL("https://commons.apache.org"), line.getOptionObject("t"));
136 
137         // tests the char methods of CommandLine that delegate to the String methods
138         assertEquals("flag a", "foo", line.getOptionValue('a'));
139         assertEquals("string flag a", "foo", line.getOptionObject('a'));
140         assertEquals("object flag b", new Vector<>(), line.getOptionObject('b'));
141         assertTrue("boolean true flag c", line.hasOption('c'));
142         assertFalse("boolean false flag d", line.hasOption('d'));
143         assertEquals("file flag e", new File("build.xml"), line.getOptionObject('e'));
144         assertEquals("class flag f", Calendar.class, line.getOptionObject('f'));
145         assertEquals("number flag n", Double.valueOf(4.5), line.getOptionObject('n'));
146         assertEquals("url flag t", new URL("https://commons.apache.org"), line.getOptionObject('t'));
147 
148         // FILES NOT SUPPORTED YET
149         try {
150             assertEquals("files flag m", new File[0], line.getOptionObject('m'));
151             fail("Multiple files are not supported yet, should have failed");
152         } catch (final UnsupportedOperationException uoe) {
153             // expected
154         }
155 
156         // DATES NOT SUPPORTED YET
157         try {
158             assertEquals("date flag z", new Date(1023400137276L), line.getOptionObject('z'));
159             fail("Date is not supported yet, should have failed");
160         } catch (final UnsupportedOperationException uoe) {
161             // expected
162         }
163     }
164 
165     @Test
166     public void testUntypedPattern() throws Exception {
167         final Options options = PatternOptionBuilder.parsePattern("abc");
168         final CommandLineParser parser = new PosixParser();
169         final CommandLine line = parser.parse(options, new String[] {"-abc"});
170 
171         assertTrue(line.hasOption('a'));
172         assertNull("value a", line.getOptionObject('a'));
173         assertTrue(line.hasOption('b'));
174         assertNull("value b", line.getOptionObject('b'));
175         assertTrue(line.hasOption('c'));
176         assertNull("value c", line.getOptionObject('c'));
177     }
178 
179     @Test
180     public void testURLPattern() throws Exception {
181         final Options options = PatternOptionBuilder.parsePattern("u/v/");
182         final CommandLineParser parser = new PosixParser();
183         final CommandLine line = parser.parse(options, new String[] {"-u", "https://commons.apache.org", "-v", "foo://commons.apache.org"});
184 
185         assertEquals("u value", new URL("https://commons.apache.org"), line.getOptionObject("u"));
186         assertNull("v value", line.getOptionObject("v"));
187     }
188 }