Skip to content

Commit c17d0ff

Browse files
committed
CLI-277: Add generics to TypeHandler
1 parent 0b45395 commit c17d0ff

2 files changed

Lines changed: 170 additions & 10 deletions

File tree

src/main/java/org/apache/commons/cli/TypeHandler.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,43 +61,44 @@ public static Object createValue(final String str, final Object obj) throws Pars
6161
* the value of <code>str</code>.
6262
* @throws ParseException if the value creation for the given class failed
6363
*/
64-
public static Object createValue(final String str, final Class<?> clazz) throws ParseException
64+
@SuppressWarnings("unchecked") // returned value will have type T because it is fixed by clazz
65+
public static <T> T createValue(final String str, final Class<T> clazz) throws ParseException
6566
{
6667
if (PatternOptionBuilder.STRING_VALUE == clazz)
6768
{
68-
return str;
69+
return (T) str;
6970
}
7071
else if (PatternOptionBuilder.OBJECT_VALUE == clazz)
7172
{
72-
return createObject(str);
73+
return (T) createObject(str);
7374
}
7475
else if (PatternOptionBuilder.NUMBER_VALUE == clazz)
7576
{
76-
return createNumber(str);
77+
return (T) createNumber(str);
7778
}
7879
else if (PatternOptionBuilder.DATE_VALUE == clazz)
7980
{
80-
return createDate(str);
81+
return (T) createDate(str);
8182
}
8283
else if (PatternOptionBuilder.CLASS_VALUE == clazz)
8384
{
84-
return createClass(str);
85+
return (T) createClass(str);
8586
}
8687
else if (PatternOptionBuilder.FILE_VALUE == clazz)
8788
{
88-
return createFile(str);
89+
return (T) createFile(str);
8990
}
9091
else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)
9192
{
92-
return openFile(str);
93+
return (T) openFile(str);
9394
}
9495
else if (PatternOptionBuilder.FILES_VALUE == clazz)
9596
{
96-
return createFiles(str);
97+
return (T) createFiles(str);
9798
}
9899
else if (PatternOptionBuilder.URL_VALUE == clazz)
99100
{
100-
return createURL(str);
101+
return (T) createURL(str);
101102
}
102103
else
103104
{
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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 org.junit.Test;
21+
22+
import java.io.File;
23+
import java.io.FileInputStream;
24+
import java.net.URL;
25+
26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.Assert.assertTrue;
29+
30+
public class TypeHandlerTest
31+
{
32+
33+
@Test
34+
public void testCreateValueString()
35+
throws Exception
36+
{
37+
assertEquals("String", TypeHandler.createValue("String", PatternOptionBuilder.STRING_VALUE));
38+
}
39+
40+
@Test(expected = ParseException.class)
41+
public void testCreateValueObject_unknownClass()
42+
throws Exception
43+
{
44+
TypeHandler.createValue("unknown", PatternOptionBuilder.OBJECT_VALUE);
45+
}
46+
47+
@Test(expected = ParseException.class)
48+
public void testCreateValueObject_notInstantiableClass()
49+
throws Exception
50+
{
51+
TypeHandler.createValue(NotInstantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE);
52+
}
53+
54+
@Test
55+
public void testCreateValueObject_InstantiableClass()
56+
throws Exception
57+
{
58+
Object result = TypeHandler.createValue(Instantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE);
59+
assertTrue(result instanceof Instantiable);
60+
}
61+
62+
@Test(expected = ParseException.class)
63+
public void testCreateValueNumber_noNumber()
64+
throws Exception
65+
{
66+
TypeHandler.createValue("not a number", PatternOptionBuilder.NUMBER_VALUE);
67+
}
68+
69+
@Test
70+
public void testCreateValueNumber_Double()
71+
throws Exception
72+
{
73+
assertEquals(1.5d, TypeHandler.createValue("1.5", PatternOptionBuilder.NUMBER_VALUE));
74+
}
75+
76+
@Test
77+
public void testCreateValueNumber_Long()
78+
throws Exception
79+
{
80+
assertEquals(Long.valueOf(15), TypeHandler.createValue("15", PatternOptionBuilder.NUMBER_VALUE));
81+
}
82+
83+
@Test(expected = UnsupportedOperationException.class)
84+
public void testCreateValueDate()
85+
throws Exception
86+
{
87+
TypeHandler.createValue("what ever", PatternOptionBuilder.DATE_VALUE);
88+
}
89+
90+
@Test(expected = ParseException.class)
91+
public void testCreateValueClass_notFound()
92+
throws Exception
93+
{
94+
TypeHandler.createValue("what ever", PatternOptionBuilder.CLASS_VALUE);
95+
}
96+
97+
@Test
98+
public void testCreateValueClass()
99+
throws Exception
100+
{
101+
Object clazz = TypeHandler.createValue(Instantiable.class.getName(), PatternOptionBuilder.CLASS_VALUE);
102+
assertEquals(Instantiable.class, clazz);
103+
}
104+
105+
@Test
106+
public void testCreateValueFile()
107+
throws Exception
108+
{
109+
File result = TypeHandler.createValue("some-file.txt", PatternOptionBuilder.FILE_VALUE);
110+
assertEquals("some-file.txt", result.getName());
111+
}
112+
113+
@Test
114+
public void testCreateValueExistingFile()
115+
throws Exception
116+
{
117+
FileInputStream result = TypeHandler.createValue("src/test/resources/existing-readable.file", PatternOptionBuilder.EXISTING_FILE_VALUE);
118+
assertNotNull(result);
119+
}
120+
121+
@Test(expected = ParseException.class)
122+
public void testCreateValueExistingFile_nonExistingFile()
123+
throws Exception
124+
{
125+
TypeHandler.createValue("non-existing.file", PatternOptionBuilder.EXISTING_FILE_VALUE);
126+
}
127+
128+
@Test(expected = UnsupportedOperationException.class)
129+
public void testCreateValueFiles()
130+
throws Exception
131+
{
132+
TypeHandler.createValue("some.files", PatternOptionBuilder.FILES_VALUE);
133+
}
134+
135+
@Test
136+
public void testCreateValueURL()
137+
throws Exception
138+
{
139+
String urlString = "http://commons.apache.org";
140+
URL result = TypeHandler.createValue(urlString, PatternOptionBuilder.URL_VALUE);
141+
assertEquals(urlString, result.toString());
142+
}
143+
144+
@Test(expected = ParseException.class)
145+
public void testCreateValueURL_malformed()
146+
throws Exception
147+
{
148+
TypeHandler.createValue("malformed-url", PatternOptionBuilder.URL_VALUE);
149+
}
150+
151+
public static class Instantiable
152+
{
153+
}
154+
155+
public static class NotInstantiable
156+
{
157+
private NotInstantiable() {}
158+
}
159+
}

0 commit comments

Comments
 (0)