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