Skip to content

Commit

Permalink
Merge c17d0ff into 0b45395
Browse files Browse the repository at this point in the history
  • Loading branch information
britter committed Jun 14, 2017
2 parents 0b45395 + c17d0ff commit 1385978
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/main/java/org/apache/commons/cli/TypeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,44 @@ public static Object createValue(final String str, final Object obj) throws Pars
* 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
{
Expand Down
159 changes: 159 additions & 0 deletions src/test/java/org/apache/commons/cli/TypeHandlerTest.java
Original file line number Diff line number Diff line change
@@ -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() {}
}
}

0 comments on commit 1385978

Please sign in to comment.