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  package org.apache.commons.cli;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.net.URL;
24  import java.text.DateFormat;
25  import java.text.SimpleDateFormat;
26  import java.util.ArrayList;
27  import java.util.Date;
28  import java.util.List;
29  import java.util.stream.Stream;
30  
31  import org.junit.jupiter.api.Test;
32  import org.junit.jupiter.params.ParameterizedTest;
33  import org.junit.jupiter.params.provider.Arguments;
34  import org.junit.jupiter.params.provider.MethodSource;
35  
36  /**
37   * Tests for standard Converters.
38   */
39  public class ConverterTests {
40  
41      // A class without a default constructor.
42      public class AClassWithoutADefaultConstructor {
43          public AClassWithoutADefaultConstructor(final int i) {
44          }
45      }
46  
47      private static Stream<Arguments> numberTestParameters() {
48          final List<Arguments> lst = new ArrayList<>();
49  
50          lst.add(Arguments.of("123", Long.valueOf("123")));
51          lst.add(Arguments.of("12.3", Double.valueOf("12.3")));
52          lst.add(Arguments.of("-123", Long.valueOf("-123")));
53          lst.add(Arguments.of("-12.3", Double.valueOf("-12.3")));
54          lst.add(Arguments.of(".3", Double.valueOf("0.3")));
55          lst.add(Arguments.of("-.3", Double.valueOf("-0.3")));
56          lst.add(Arguments.of("0x5F", null));
57          lst.add(Arguments.of("2,3", null));
58          lst.add(Arguments.of("1.2.3", null));
59  
60          return lst.stream();
61      }
62  
63      @Test
64      public void classTests() throws Exception {
65  
66          assertNotNull(Converter.CLASS.apply(this.getClass().getName()), this.getClass().getName());
67          assertNotNull(Converter.CLASS.apply(this.getClass().getCanonicalName()), this.getClass().getCanonicalName());
68          assertThrows(ClassNotFoundException.class, () -> Converter.CLASS.apply(this.getClass().getSimpleName()),
69                  this.getClass().getSimpleName());
70          assertNotNull(Converter.CLASS.apply(this.getClass().getTypeName()), this.getClass().getTypeName());
71  
72          assertThrows(ClassNotFoundException.class, () -> Converter.CLASS.apply("foo.bar"));
73          assertNotNull(Converter.CLASS.apply(AClassWithoutADefaultConstructor.class.getName()));
74      }
75  
76      @Test
77      public void dateTests() throws Exception {
78          assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("whatever"));
79  
80          /*
81           * Dates calculated from strings are dependent upon configuration and environment settings for the
82           * machine on which the test is running.  To avoid this problem, convert the time into a string
83           * and then unparse that using the converter.  This produces strings that always match the correct
84           * time zone.
85           */
86          final Date expected = new Date(1023400137000L);
87          final DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
88          final String formatted = dateFormat.format(expected);
89          assertEquals(expected, Converter.DATE.apply(formatted));
90  
91          assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("Jun 06 17:48:57 EDT 2002"));
92      }
93  
94      @Test
95      public void fileTests() throws Exception {
96          final URL url = this.getClass().getClassLoader().getResource("./org/apache/commons/cli/existing-readable.file");
97          final String fileName = url.toString().substring("file:".length());
98          assertNotNull(Converter.FILE.apply(fileName));
99      }
100 
101     @ParameterizedTest
102     @MethodSource("numberTestParameters")
103     public void numberTests(final String str, final Number expected) throws Exception {
104         if (expected != null) {
105             assertEquals(expected, Converter.NUMBER.apply(str));
106         } else {
107             assertThrows(NumberFormatException.class, () -> Converter.NUMBER.apply(str));
108         }
109     }
110 
111     @Test
112     public void objectTests() throws Exception {
113         assertNotNull(Converter.OBJECT.apply(this.getClass().getName()), this.getClass().getName());
114         assertNotNull(Converter.OBJECT.apply(this.getClass().getCanonicalName()), this.getClass().getCanonicalName());
115         assertThrows(ClassNotFoundException.class, () -> Converter.OBJECT.apply(this.getClass().getSimpleName()),
116                 this.getClass().getSimpleName());
117         assertNotNull(Converter.OBJECT.apply(this.getClass().getTypeName()), this.getClass().getTypeName());
118 
119         assertThrows(ClassNotFoundException.class, () -> Converter.OBJECT.apply("foo.bar"));
120         assertThrows(NoSuchMethodException.class, () -> Converter.OBJECT.apply(AClassWithoutADefaultConstructor.class.getName()));
121     }
122 
123     @Test
124     public void urlTests() throws Exception {
125         assertEquals(new URL("http://apache.org"), Converter.URL.apply("http://apache.org"));
126         assertThrows(java.net.MalformedURLException.class, () -> Converter.URL.apply("foo.bar"));
127     }
128 }