CLI-277: Add generics to TypeHandler
diff --git a/src/main/java/org/apache/commons/cli/TypeHandler.java b/src/main/java/org/apache/commons/cli/TypeHandler.java
index 68b4f02..5b97b22 100644
--- a/src/main/java/org/apache/commons/cli/TypeHandler.java
+++ b/src/main/java/org/apache/commons/cli/TypeHandler.java
@@ -61,43 +61,44 @@
* the value of <code>str</code>.
* @throws ParseException if the value creation for the given class failed
*/
- public static Object createValue(final String str, final Class<?> clazz) throws ParseException
+ @SuppressWarnings("unchecked") // returned value will have type T because it is fixed by clazz
+ public static <T> T createValue(final String str, final Class<T> clazz) throws ParseException
{
if (PatternOptionBuilder.STRING_VALUE == clazz)
{
- return str;
+ return (T) str;
}
else if (PatternOptionBuilder.OBJECT_VALUE == clazz)
{
- return createObject(str);
+ return (T) createObject(str);
}
else if (PatternOptionBuilder.NUMBER_VALUE == clazz)
{
- return createNumber(str);
+ return (T) createNumber(str);
}
else if (PatternOptionBuilder.DATE_VALUE == clazz)
{
- return createDate(str);
+ return (T) createDate(str);
}
else if (PatternOptionBuilder.CLASS_VALUE == clazz)
{
- return createClass(str);
+ return (T) createClass(str);
}
else if (PatternOptionBuilder.FILE_VALUE == clazz)
{
- return createFile(str);
+ return (T) createFile(str);
}
else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)
{
- return openFile(str);
+ return (T) openFile(str);
}
else if (PatternOptionBuilder.FILES_VALUE == clazz)
{
- return createFiles(str);
+ return (T) createFiles(str);
}
else if (PatternOptionBuilder.URL_VALUE == clazz)
{
- return createURL(str);
+ return (T) createURL(str);
}
else
{
diff --git a/src/test/java/org/apache/commons/cli/TypeHandlerTest.java b/src/test/java/org/apache/commons/cli/TypeHandlerTest.java
new file mode 100644
index 0000000..1d7f8cf
--- /dev/null
+++ b/src/test/java/org/apache/commons/cli/TypeHandlerTest.java
@@ -0,0 +1,159 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.cli;
+
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.net.URL;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class TypeHandlerTest
+{
+
+ @Test
+ public void testCreateValueString()
+ throws Exception
+ {
+ assertEquals("String", TypeHandler.createValue("String", PatternOptionBuilder.STRING_VALUE));
+ }
+
+ @Test(expected = ParseException.class)
+ public void testCreateValueObject_unknownClass()
+ throws Exception
+ {
+ TypeHandler.createValue("unknown", PatternOptionBuilder.OBJECT_VALUE);
+ }
+
+ @Test(expected = ParseException.class)
+ public void testCreateValueObject_notInstantiableClass()
+ throws Exception
+ {
+ TypeHandler.createValue(NotInstantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE);
+ }
+
+ @Test
+ public void testCreateValueObject_InstantiableClass()
+ throws Exception
+ {
+ Object result = TypeHandler.createValue(Instantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE);
+ assertTrue(result instanceof Instantiable);
+ }
+
+ @Test(expected = ParseException.class)
+ public void testCreateValueNumber_noNumber()
+ throws Exception
+ {
+ TypeHandler.createValue("not a number", PatternOptionBuilder.NUMBER_VALUE);
+ }
+
+ @Test
+ public void testCreateValueNumber_Double()
+ throws Exception
+ {
+ assertEquals(1.5d, TypeHandler.createValue("1.5", PatternOptionBuilder.NUMBER_VALUE));
+ }
+
+ @Test
+ public void testCreateValueNumber_Long()
+ throws Exception
+ {
+ assertEquals(Long.valueOf(15), TypeHandler.createValue("15", PatternOptionBuilder.NUMBER_VALUE));
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testCreateValueDate()
+ throws Exception
+ {
+ TypeHandler.createValue("what ever", PatternOptionBuilder.DATE_VALUE);
+ }
+
+ @Test(expected = ParseException.class)
+ public void testCreateValueClass_notFound()
+ throws Exception
+ {
+ TypeHandler.createValue("what ever", PatternOptionBuilder.CLASS_VALUE);
+ }
+
+ @Test
+ public void testCreateValueClass()
+ throws Exception
+ {
+ Object clazz = TypeHandler.createValue(Instantiable.class.getName(), PatternOptionBuilder.CLASS_VALUE);
+ assertEquals(Instantiable.class, clazz);
+ }
+
+ @Test
+ public void testCreateValueFile()
+ throws Exception
+ {
+ File result = TypeHandler.createValue("some-file.txt", PatternOptionBuilder.FILE_VALUE);
+ assertEquals("some-file.txt", result.getName());
+ }
+
+ @Test
+ public void testCreateValueExistingFile()
+ throws Exception
+ {
+ FileInputStream result = TypeHandler.createValue("src/test/resources/existing-readable.file", PatternOptionBuilder.EXISTING_FILE_VALUE);
+ assertNotNull(result);
+ }
+
+ @Test(expected = ParseException.class)
+ public void testCreateValueExistingFile_nonExistingFile()
+ throws Exception
+ {
+ TypeHandler.createValue("non-existing.file", PatternOptionBuilder.EXISTING_FILE_VALUE);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testCreateValueFiles()
+ throws Exception
+ {
+ TypeHandler.createValue("some.files", PatternOptionBuilder.FILES_VALUE);
+ }
+
+ @Test
+ public void testCreateValueURL()
+ throws Exception
+ {
+ String urlString = "http://commons.apache.org";
+ URL result = TypeHandler.createValue(urlString, PatternOptionBuilder.URL_VALUE);
+ assertEquals(urlString, result.toString());
+ }
+
+ @Test(expected = ParseException.class)
+ public void testCreateValueURL_malformed()
+ throws Exception
+ {
+ TypeHandler.createValue("malformed-url", PatternOptionBuilder.URL_VALUE);
+ }
+
+ public static class Instantiable
+ {
+ }
+
+ public static class NotInstantiable
+ {
+ private NotInstantiable() {}
+ }
+}