1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.cli;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.net.URL;
28
29 import org.junit.Test;
30
31 public class TypeHandlerTest {
32
33 public static class Instantiable {
34 }
35
36 public static final class NotInstantiable {
37 private NotInstantiable() {
38 }
39 }
40
41 @Test
42 public void testCreateValueClass() throws Exception {
43 final Object clazz = TypeHandler.createValue(Instantiable.class.getName(), PatternOptionBuilder.CLASS_VALUE);
44 assertEquals(Instantiable.class, clazz);
45 }
46
47 @Test
48 public void testCreateValueClass_notFound() {
49 assertThrows(ParseException.class, () ->
50 TypeHandler.createValue("what ever", PatternOptionBuilder.CLASS_VALUE));
51 }
52
53 @Test
54 public void testCreateValueDate() {
55 assertThrows(UnsupportedOperationException.class, () ->
56 TypeHandler.createValue("what ever", PatternOptionBuilder.DATE_VALUE));
57 }
58
59 @Test
60 public void testCreateValueExistingFile() throws Exception {
61 try (FileInputStream result = TypeHandler.createValue("src/test/resources/org/apache/commons/cli/existing-readable.file",
62 PatternOptionBuilder.EXISTING_FILE_VALUE)) {
63 assertNotNull(result);
64 }
65 }
66
67 @Test
68 public void testCreateValueExistingFile_nonExistingFile() {
69 assertThrows(ParseException.class, () ->
70 TypeHandler.createValue("non-existing.file", PatternOptionBuilder.EXISTING_FILE_VALUE));
71 }
72
73 @Test
74 public void testCreateValueFile() throws Exception {
75 final File result = TypeHandler.createValue("some-file.txt", PatternOptionBuilder.FILE_VALUE);
76 assertEquals("some-file.txt", result.getName());
77 }
78
79 @Test
80 public void testCreateValueFiles() {
81 assertThrows(UnsupportedOperationException.class, () ->
82 TypeHandler.createValue("some.files", PatternOptionBuilder.FILES_VALUE));
83 }
84
85 @Test
86 public void testCreateValueInteger_failure() {
87 assertThrows(ParseException.class, () ->
88 TypeHandler.createValue("just-a-string", Integer.class));
89 }
90
91 @Test
92 public void testCreateValueNumber_Double() throws Exception {
93 assertEquals(1.5d, TypeHandler.createValue("1.5", PatternOptionBuilder.NUMBER_VALUE));
94 }
95
96 @Test
97 public void testCreateValueNumber_Long() throws Exception {
98 assertEquals(Long.valueOf(15), TypeHandler.createValue("15", PatternOptionBuilder.NUMBER_VALUE));
99 }
100
101 @Test
102 public void testCreateValueNumber_noNumber() {
103 assertThrows(ParseException.class, () ->
104 TypeHandler.createValue("not a number", PatternOptionBuilder.NUMBER_VALUE));
105 }
106
107 @Test
108 public void testCreateValueObject_InstantiableClass() throws Exception {
109 final Object result = TypeHandler.createValue(Instantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE);
110 assertTrue(result instanceof Instantiable);
111 }
112
113 @Test
114 public void testCreateValueObject_notInstantiableClass() {
115 assertThrows(ParseException.class, () ->
116 TypeHandler.createValue(NotInstantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE));
117 }
118
119 @Test
120 public void testCreateValueObject_unknownClass() {
121 assertThrows(ParseException.class, () ->
122 TypeHandler.createValue("unknown", PatternOptionBuilder.OBJECT_VALUE));
123 }
124
125 @Test
126 public void testCreateValueString() throws Exception {
127 assertEquals("String", TypeHandler.createValue("String", PatternOptionBuilder.STRING_VALUE));
128 }
129
130 @Test
131 public void testCreateValueURL() throws Exception {
132 final String urlString = "https://commons.apache.org";
133 final URL result = TypeHandler.createValue(urlString, PatternOptionBuilder.URL_VALUE);
134 assertEquals(urlString, result.toString());
135 }
136
137 @Test
138 public void testCreateValueURL_malformed() {
139 assertThrows(ParseException.class, () ->
140 TypeHandler.createValue("malformed-url", PatternOptionBuilder.URL_VALUE));
141 }
142
143 }