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