Skip to content

Commit c246bd4

Browse files
committed
Merge branch 'CLI-277'
2 parents 9a845a2 + c17d0ff commit c246bd4

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
@@ -59,43 +59,44 @@ public static Object createValue(final String str, final Object obj) throws Pars
5959
* the value of <code>str</code>.
6060
* @throws ParseException if the value creation for the given class failed
6161
*/
62-
public static Object createValue(final String str, final Class<?> clazz) throws ParseException
62+
@SuppressWarnings("unchecked") // returned value will have type T because it is fixed by clazz
63+
public static <T> T createValue(final String str, final Class<T> clazz) throws ParseException
6364
{
6465
if (PatternOptionBuilder.STRING_VALUE == clazz)
6566
{
66-
return str;
67+
return (T) str;
6768
}
6869
else if (PatternOptionBuilder.OBJECT_VALUE == clazz)
6970
{
70-
return createObject(str);
71+
return (T) createObject(str);
7172
}
7273
else if (PatternOptionBuilder.NUMBER_VALUE == clazz)
7374
{
74-
return createNumber(str);
75+
return (T) createNumber(str);
7576
}
7677
else if (PatternOptionBuilder.DATE_VALUE == clazz)
7778
{
78-
return createDate(str);
79+
return (T) createDate(str);
7980
}
8081
else if (PatternOptionBuilder.CLASS_VALUE == clazz)
8182
{
82-
return createClass(str);
83+
return (T) createClass(str);
8384
}
8485
else if (PatternOptionBuilder.FILE_VALUE == clazz)
8586
{
86-
return createFile(str);
87+
return (T) createFile(str);
8788
}
8889
else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)
8990
{
90-
return openFile(str);
91+
return (T) openFile(str);
9192
}
9293
else if (PatternOptionBuilder.FILES_VALUE == clazz)
9394
{
94-
return createFiles(str);
95+
return (T) createFiles(str);
9596
}
9697
else if (PatternOptionBuilder.URL_VALUE == clazz)
9798
{
98-
return createURL(str);
99+
return (T) createURL(str);
99100
}
100101
else
101102
{
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)