Repository: commons-cli Updated Branches: refs/heads/master 9a845a2a3 -> c246bd419
CLI-277: Add generics to TypeHandler Project: http://git-wip-us.apache.org/repos/asf/commons-cli/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-cli/commit/c17d0ff5 Tree: http://git-wip-us.apache.org/repos/asf/commons-cli/tree/c17d0ff5 Diff: http://git-wip-us.apache.org/repos/asf/commons-cli/diff/c17d0ff5 Branch: refs/heads/master Commit: c17d0ff553fd34e2e53e0446083ee284ee97f6cb Parents: 0b45395 Author: Benedikt Ritter <brit...@apache.org> Authored: Wed Jun 14 10:25:32 2017 +0200 Committer: Benedikt Ritter <brit...@apache.org> Committed: Wed Jun 14 10:25:32 2017 +0200 ---------------------------------------------------------------------- .../org/apache/commons/cli/TypeHandler.java | 21 +-- .../org/apache/commons/cli/TypeHandlerTest.java | 159 +++++++++++++++++++ 2 files changed, 170 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-cli/blob/c17d0ff5/src/main/java/org/apache/commons/cli/TypeHandler.java ---------------------------------------------------------------------- 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 @@ public class TypeHandler * 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 { http://git-wip-us.apache.org/repos/asf/commons-cli/blob/c17d0ff5/src/test/java/org/apache/commons/cli/TypeHandlerTest.java ---------------------------------------------------------------------- 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() {} + } +}