1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
38
39 public class ConverterTests {
40
41
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
82
83
84
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 }