You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/01/29 13:51:16 UTC
(commons-cli) 03/04: Sort members
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git
commit 6a647049ba6d66fa2547001ebb5e43d8aed60bfa
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jan 29 08:48:28 2024 -0500
Sort members
---
.../java/org/apache/commons/cli/CommandLine.java | 46 +++----
src/main/java/org/apache/commons/cli/Option.java | 62 +++++-----
.../org/apache/commons/cli/ParseException.java | 16 +--
.../apache/commons/cli/PatternOptionBuilder.java | 20 ++--
.../java/org/apache/commons/cli/TypeHandler.java | 132 ++++++++++-----------
.../org/apache/commons/cli/ConverterTests.java | 56 ++++-----
.../java/org/apache/commons/cli/OptionTest.java | 34 +++---
.../org/apache/commons/cli/TypeHandlerTest.java | 128 ++++++++++----------
8 files changed, 247 insertions(+), 247 deletions(-)
diff --git a/src/main/java/org/apache/commons/cli/CommandLine.java b/src/main/java/org/apache/commons/cli/CommandLine.java
index 6570b82..35e33c3 100644
--- a/src/main/java/org/apache/commons/cli/CommandLine.java
+++ b/src/main/java/org/apache/commons/cli/CommandLine.java
@@ -400,46 +400,32 @@ public class CommandLine implements Serializable {
/**
* Gets a version of this {@code Option} converted to a particular type.
*
- * @param option the name of the option.
+ * @param opt the name of the option.
+ * @param defaultValue the default value to return if opt is not set.
* @param <T> The return type for the method.
* @return the value parsed into a particular object.
* @throws ParseException if there are problems turning the option value into the desired type
* @see PatternOptionBuilder
- * @since 1.5.0
+ * @since 1.7.0
*/
- public <T> T getParsedOptionValue(final Option option) throws ParseException {
- return getParsedOptionValue(option, null);
+ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws ParseException {
+ return getParsedOptionValue(String.valueOf(opt), defaultValue);
}
/**
* Gets a version of this {@code Option} converted to a particular type.
*
- * @param opt the name of the option.
+ * @param option the name of the option.
* @param <T> The return type for the method.
* @return the value parsed into a particular object.
* @throws ParseException if there are problems turning the option value into the desired type
* @see PatternOptionBuilder
- * @since 1.2
+ * @since 1.5.0
*/
- public <T> T getParsedOptionValue(final String opt) throws ParseException {
- return getParsedOptionValue(resolveOption(opt));
+ public <T> T getParsedOptionValue(final Option option) throws ParseException {
+ return getParsedOptionValue(option, null);
}
- /**
- * Gets a version of this {@code Option} converted to a particular type.
- *
- * @param opt the name of the option.
- * @param defaultValue the default value to return if opt is not set.
- * @param <T> The return type for the method.
- * @return the value parsed into a particular object.
- * @throws ParseException if there are problems turning the option value into the desired type
- * @see PatternOptionBuilder
- * @since 1.7.0
- */
- public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws ParseException {
- return getParsedOptionValue(String.valueOf(opt), defaultValue);
- }
-
/**
* Gets a version of this {@code Option} converted to a particular type.
*
@@ -465,6 +451,20 @@ public class CommandLine implements Serializable {
}
}
+ /**
+ * Gets a version of this {@code Option} converted to a particular type.
+ *
+ * @param opt the name of the option.
+ * @param <T> The return type for the method.
+ * @return the value parsed into a particular object.
+ * @throws ParseException if there are problems turning the option value into the desired type
+ * @see PatternOptionBuilder
+ * @since 1.2
+ */
+ public <T> T getParsedOptionValue(final String opt) throws ParseException {
+ return getParsedOptionValue(resolveOption(opt));
+ }
+
/**
* Gets a version of this {@code Option} converted to a particular type.
*
diff --git a/src/main/java/org/apache/commons/cli/Option.java b/src/main/java/org/apache/commons/cli/Option.java
index 6ffc0c5..3367356 100644
--- a/src/main/java/org/apache/commons/cli/Option.java
+++ b/src/main/java/org/apache/commons/cli/Option.java
@@ -120,6 +120,18 @@ public class Option implements Cloneable, Serializable {
return new Option(this);
}
+ /**
+ * Sets the converter for the option.
+ * <p>Note: see {@link TypeHandler} for serialization discussion.</p>
+ * @param converter the Converter to use.
+ * @return this builder, to allow method chaining.
+ * @since 1.7.0
+ */
+ public Builder converter(final Converter<?> converter) {
+ this.converter = converter;
+ return this;
+ }
+
/**
* Sets the description for this option.
*
@@ -273,18 +285,6 @@ public class Option implements Cloneable, Serializable {
this.valueSeparator = valueSeparator;
return this;
}
-
- /**
- * Sets the converter for the option.
- * <p>Note: see {@link TypeHandler} for serialization discussion.</p>
- * @param converter the Converter to use.
- * @return this builder, to allow method chaining.
- * @since 1.7.0
- */
- public Builder converter(final Converter<?> converter) {
- this.converter = converter;
- return this;
- }
}
@@ -542,6 +542,15 @@ public class Option implements Cloneable, Serializable {
return argCount;
}
+ /**
+ * Gets the value to type converter.
+ * @return the value to type converter
+ * @since 1.7.0
+ */
+ public Converter<?> getConverter() {
+ return converter == null ? TypeHandler.getConverter(type) : converter;
+ }
+
/**
* Gets the self-documenting description of this Option
*
@@ -816,6 +825,15 @@ public class Option implements Cloneable, Serializable {
this.argCount = num;
}
+ /**
+ * Sets the value to type converter.
+ * @param converter The converter to convert the string value to the type.
+ * @since 1.7.0
+ */
+ public void setConverter(final Converter<?> converter) {
+ this.converter = converter;
+ }
+
/**
* Sets the self-documenting description of this Option
*
@@ -862,7 +880,7 @@ public class Option implements Cloneable, Serializable {
public void setType(final Class<?> type) {
this.type = type;
}
-
+
/**
* Sets the type of this Option.
* <p>
@@ -886,24 +904,6 @@ public class Option implements Cloneable, Serializable {
public void setValueSeparator(final char sep) {
this.valuesep = sep;
}
-
- /**
- * Gets the value to type converter.
- * @return the value to type converter
- * @since 1.7.0
- */
- public Converter<?> getConverter() {
- return converter == null ? TypeHandler.getConverter(type) : converter;
- }
-
- /**
- * Sets the value to type converter.
- * @param converter The converter to convert the string value to the type.
- * @since 1.7.0
- */
- public void setConverter(final Converter<?> converter) {
- this.converter = converter;
- }
/**
* Dump state, suitable for debugging.
diff --git a/src/main/java/org/apache/commons/cli/ParseException.java b/src/main/java/org/apache/commons/cli/ParseException.java
index f023779..8b7e08c 100644
--- a/src/main/java/org/apache/commons/cli/ParseException.java
+++ b/src/main/java/org/apache/commons/cli/ParseException.java
@@ -49,6 +49,14 @@ public class ParseException extends Exception {
}
return new ParseException(e);
}
+ /**
+ * Constructs a new {@code ParseException} wrapping the specified exception.
+ * @param e the Exception to wrap.
+ */
+ public ParseException(final Exception e) {
+ super(e);
+ }
+
/**
* Constructs a new {@code ParseException} with the specified detail message.
*
@@ -57,12 +65,4 @@ public class ParseException extends Exception {
public ParseException(final String message) {
super(message);
}
-
- /**
- * Constructs a new {@code ParseException} wrapping the specified exception.
- * @param e the Exception to wrap.
- */
- public ParseException(final Exception e) {
- super(e);
- }
}
diff --git a/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java b/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
index de3168e..4a337a5 100644
--- a/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
+++ b/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
@@ -112,14 +112,6 @@ public class PatternOptionBuilder {
registerTypes();
}
- /**
- * Registers custom {@code Converter}s with the {@code TypeHandler}.
- * @since 1.7.0
- */
- public static void registerTypes() {
- TypeHandler.register(PatternOptionBuilder.FILES_VALUE, NOT_IMPLEMENTED);
- }
-
/**
* Retrieve the class that {@code ch} represents.
*
@@ -131,7 +123,7 @@ public class PatternOptionBuilder {
public static Object getValueClass(final char ch) {
return getValueType(ch);
}
-
+
/**
* Retrieve the class that {@code ch} represents.
*
@@ -163,7 +155,7 @@ public class PatternOptionBuilder {
return null;
}
-
+
/**
* Returns whether {@code ch} is a value code, i.e. whether it represents a class in a pattern.
*
@@ -223,4 +215,12 @@ public class PatternOptionBuilder {
return options;
}
+
+ /**
+ * Registers custom {@code Converter}s with the {@code TypeHandler}.
+ * @since 1.7.0
+ */
+ public static void registerTypes() {
+ TypeHandler.register(PatternOptionBuilder.FILES_VALUE, NOT_IMPLEMENTED);
+ }
}
diff --git a/src/main/java/org/apache/commons/cli/TypeHandler.java b/src/main/java/org/apache/commons/cli/TypeHandler.java
index 602942a..7f17912 100644
--- a/src/main/java/org/apache/commons/cli/TypeHandler.java
+++ b/src/main/java/org/apache/commons/cli/TypeHandler.java
@@ -51,71 +51,6 @@ public class TypeHandler {
resetConverters();
}
- /**
- * Resets the registered Converters to the default state.
- * @since 1.7.0
- */
- public static void resetConverters() {
- converterMap.clear();
- converterMap.put(Object.class, Converter.OBJECT);
- converterMap.put(Class.class, Converter.CLASS);
- converterMap.put(Date.class, Converter.DATE);
- converterMap.put(File.class, Converter.FILE);
- converterMap.put(Path.class, Converter.PATH);
- converterMap.put(Number.class, Converter.NUMBER);
- converterMap.put(URL.class, Converter.URL);
- converterMap.put(FileInputStream.class, s -> new FileInputStream(s));
- converterMap.put(Long.class, Long::parseLong);
- converterMap.put(Integer.class, Integer::parseInt);
- converterMap.put(Short.class, Short::parseShort);
- converterMap.put(Byte.class, Byte::parseByte);
- converterMap.put(Character.class, s -> {
- if (s.startsWith("\\u")) {
- return Character.toChars(Integer.parseInt(s.substring(2), HEX_RADIX))[0];
- } else {
- return s.charAt(0);
- } });
- converterMap.put(Double.class, Double::parseDouble);
- converterMap.put(Float.class, Float::parseFloat);
- converterMap.put(BigInteger.class, s -> new BigInteger(s));
- converterMap.put(BigDecimal.class, s -> new BigDecimal(s));
- }
-
- /**
- * Unregisters all Converters.
- * @since 1.7.0
- */
- public static void noConverters() {
- converterMap.clear();
- }
-
- /**
- * Registers a Converter for a Class. If @code converter} is null registration is cleared for {@code clazz}, and
- * no converter will be used in processing.
- *
- * @param clazz the Class to register the Converter and Verifier to.
- * @param converter The Converter to associate with Class. May be null.
- * @since 1.7.0
- */
- public static void register(final Class<?> clazz, final Converter<?> converter) {
- if (converter == null) {
- converterMap.remove(clazz);
- } else {
- converterMap.put(clazz, converter);
- }
- }
-
- /**
- * Gets the converter for the the Class. Never null.
- * @param clazz The Class to get the Converter for.
- * @return the registered converter if any, {@link Converter#DEFAULT} otherwise.
- * @since 1.7.0
- */
- public static Converter<?> getConverter(final Class<?> clazz) {
- Converter<?> converter = converterMap.get(clazz);
- return converter == null ? Converter.DEFAULT : converter;
- }
-
/**
* Returns the class whose name is {@code className}.
*
@@ -128,7 +63,7 @@ public class TypeHandler {
public static Class<?> createClass(final String className) throws ParseException {
return createValue(className, Class.class);
}
-
+
/**
* Returns the date represented by {@code str}.
* <p>
@@ -250,6 +185,25 @@ public class TypeHandler {
return createValue(str, (Class<?>) obj);
}
+ /**
+ * Gets the converter for the the Class. Never null.
+ * @param clazz The Class to get the Converter for.
+ * @return the registered converter if any, {@link Converter#DEFAULT} otherwise.
+ * @since 1.7.0
+ */
+ public static Converter<?> getConverter(final Class<?> clazz) {
+ Converter<?> converter = converterMap.get(clazz);
+ return converter == null ? Converter.DEFAULT : converter;
+ }
+
+ /**
+ * Unregisters all Converters.
+ * @since 1.7.0
+ */
+ public static void noConverters() {
+ converterMap.clear();
+ }
+
/**
* Returns the opened FileInputStream represented by {@code str}.
*
@@ -262,4 +216,50 @@ public class TypeHandler {
public static FileInputStream openFile(final String str) throws ParseException {
return createValue(str, FileInputStream.class);
}
+
+ /**
+ * Registers a Converter for a Class. If @code converter} is null registration is cleared for {@code clazz}, and
+ * no converter will be used in processing.
+ *
+ * @param clazz the Class to register the Converter and Verifier to.
+ * @param converter The Converter to associate with Class. May be null.
+ * @since 1.7.0
+ */
+ public static void register(final Class<?> clazz, final Converter<?> converter) {
+ if (converter == null) {
+ converterMap.remove(clazz);
+ } else {
+ converterMap.put(clazz, converter);
+ }
+ }
+
+ /**
+ * Resets the registered Converters to the default state.
+ * @since 1.7.0
+ */
+ public static void resetConverters() {
+ converterMap.clear();
+ converterMap.put(Object.class, Converter.OBJECT);
+ converterMap.put(Class.class, Converter.CLASS);
+ converterMap.put(Date.class, Converter.DATE);
+ converterMap.put(File.class, Converter.FILE);
+ converterMap.put(Path.class, Converter.PATH);
+ converterMap.put(Number.class, Converter.NUMBER);
+ converterMap.put(URL.class, Converter.URL);
+ converterMap.put(FileInputStream.class, s -> new FileInputStream(s));
+ converterMap.put(Long.class, Long::parseLong);
+ converterMap.put(Integer.class, Integer::parseInt);
+ converterMap.put(Short.class, Short::parseShort);
+ converterMap.put(Byte.class, Byte::parseByte);
+ converterMap.put(Character.class, s -> {
+ if (s.startsWith("\\u")) {
+ return Character.toChars(Integer.parseInt(s.substring(2), HEX_RADIX))[0];
+ } else {
+ return s.charAt(0);
+ } });
+ converterMap.put(Double.class, Double::parseDouble);
+ converterMap.put(Float.class, Float::parseFloat);
+ converterMap.put(BigInteger.class, s -> new BigInteger(s));
+ converterMap.put(BigDecimal.class, s -> new BigDecimal(s));
+ }
}
diff --git a/src/test/java/org/apache/commons/cli/ConverterTests.java b/src/test/java/org/apache/commons/cli/ConverterTests.java
index a647f70..2354601 100644
--- a/src/test/java/org/apache/commons/cli/ConverterTests.java
+++ b/src/test/java/org/apache/commons/cli/ConverterTests.java
@@ -36,13 +36,9 @@ import org.junit.jupiter.params.provider.MethodSource;
*/
public class ConverterTests {
- @ParameterizedTest
- @MethodSource("numberTestParameters")
- public void numberTests(final String str, final Number expected) throws Exception {
- if (expected != null) {
- assertEquals(expected, Converter.NUMBER.apply(str));
- } else {
- assertThrows(NumberFormatException.class, () -> Converter.NUMBER.apply(str));
+ // A class without a default constructor.
+ public class AClassWithoutADefaultConstructor {
+ public AClassWithoutADefaultConstructor(final int i) {
}
}
@@ -75,18 +71,6 @@ public class ConverterTests {
assertNotNull(Converter.CLASS.apply(AClassWithoutADefaultConstructor.class.getName()));
}
- @Test
- public void objectTests() throws Exception {
- assertNotNull(Converter.OBJECT.apply(this.getClass().getName()), this.getClass().getName());
- assertNotNull(Converter.OBJECT.apply(this.getClass().getCanonicalName()), this.getClass().getCanonicalName());
- assertThrows(ClassNotFoundException.class, () -> Converter.OBJECT.apply(this.getClass().getSimpleName()),
- this.getClass().getSimpleName());
- assertNotNull(Converter.OBJECT.apply(this.getClass().getTypeName()), this.getClass().getTypeName());
-
- assertThrows(ClassNotFoundException.class, () -> Converter.OBJECT.apply("foo.bar"));
- assertThrows(NoSuchMethodException.class, () -> Converter.OBJECT.apply(AClassWithoutADefaultConstructor.class.getName()));
- }
-
@Test
public void dateTests() throws Exception {
assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("whatever"));
@@ -97,12 +81,6 @@ public class ConverterTests {
assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("Jun 06 17:48:57 EDT 2002"));
}
- @Test
- public void urlTests() throws Exception {
- assertEquals(new URL("http://apache.org"), Converter.URL.apply("http://apache.org"));
- assertThrows(java.net.MalformedURLException.class, () -> Converter.URL.apply("foo.bar"));
- }
-
@Test
public void fileTests() throws Exception {
URL url = this.getClass().getClassLoader().getResource("./org/apache/commons/cli/existing-readable.file");
@@ -110,9 +88,31 @@ public class ConverterTests {
assertNotNull(Converter.FILE.apply(fileName));
}
- // A class without a default constructor.
- public class AClassWithoutADefaultConstructor {
- public AClassWithoutADefaultConstructor(final int i) {
+ @ParameterizedTest
+ @MethodSource("numberTestParameters")
+ public void numberTests(final String str, final Number expected) throws Exception {
+ if (expected != null) {
+ assertEquals(expected, Converter.NUMBER.apply(str));
+ } else {
+ assertThrows(NumberFormatException.class, () -> Converter.NUMBER.apply(str));
}
}
+
+ @Test
+ public void objectTests() throws Exception {
+ assertNotNull(Converter.OBJECT.apply(this.getClass().getName()), this.getClass().getName());
+ assertNotNull(Converter.OBJECT.apply(this.getClass().getCanonicalName()), this.getClass().getCanonicalName());
+ assertThrows(ClassNotFoundException.class, () -> Converter.OBJECT.apply(this.getClass().getSimpleName()),
+ this.getClass().getSimpleName());
+ assertNotNull(Converter.OBJECT.apply(this.getClass().getTypeName()), this.getClass().getTypeName());
+
+ assertThrows(ClassNotFoundException.class, () -> Converter.OBJECT.apply("foo.bar"));
+ assertThrows(NoSuchMethodException.class, () -> Converter.OBJECT.apply(AClassWithoutADefaultConstructor.class.getName()));
+ }
+
+ @Test
+ public void urlTests() throws Exception {
+ assertEquals(new URL("http://apache.org"), Converter.URL.apply("http://apache.org"));
+ assertThrows(java.net.MalformedURLException.class, () -> Converter.URL.apply("foo.bar"));
+ }
}
diff --git a/src/test/java/org/apache/commons/cli/OptionTest.java b/src/test/java/org/apache/commons/cli/OptionTest.java
index a49dbaa..43df6b4 100644
--- a/src/test/java/org/apache/commons/cli/OptionTest.java
+++ b/src/test/java/org/apache/commons/cli/OptionTest.java
@@ -79,6 +79,15 @@ public class OptionTest {
assertEquals(cls, option.getType());
}
+ private Option roundTrip(final Option o) throws IOException, ClassNotFoundException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(o);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ ObjectInputStream ois = new ObjectInputStream(bais);
+ return (Option) ois.readObject();
+ }
+
@Test
public void testBuilderInsufficientParams1() {
assertThrows(IllegalArgumentException.class, () ->
@@ -230,23 +239,6 @@ public class OptionTest {
assertNotEquals(Option.builder("test").build().hashCode(), Option.builder("test").longOpt("long test").build().hashCode());
}
- @Test
- public void testSubclass() {
- final Option option = new DefaultOption("f", "file", "myfile.txt");
- final Option clone = (Option) option.clone();
- assertEquals("myfile.txt", clone.getValue());
- assertEquals(DefaultOption.class, clone.getClass());
- }
-
-
- private Option roundTrip(final Option o) throws IOException, ClassNotFoundException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(baos);
- oos.writeObject(o);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
- ObjectInputStream ois = new ObjectInputStream(bais);
- return (Option) ois.readObject();
- }
@Test
public void testSerialization() throws IOException, ClassNotFoundException {
@@ -273,4 +265,12 @@ public class OptionTest {
TypeHandler.register(TypeHandlerTest.Instantiable.class, null);
}
}
+
+ @Test
+ public void testSubclass() {
+ final Option option = new DefaultOption("f", "file", "myfile.txt");
+ final Option clone = (Option) option.clone();
+ assertEquals("myfile.txt", clone.getValue());
+ assertEquals(DefaultOption.class, clone.getClass());
+ }
}
diff --git a/src/test/java/org/apache/commons/cli/TypeHandlerTest.java b/src/test/java/org/apache/commons/cli/TypeHandlerTest.java
index 26c051d..545347c 100644
--- a/src/test/java/org/apache/commons/cli/TypeHandlerTest.java
+++ b/src/test/java/org/apache/commons/cli/TypeHandlerTest.java
@@ -54,12 +54,6 @@ public class TypeHandlerTest {
}
}
- /* proof of equality for later tests */
- @Test
- public void testnstantiableEquals() {
- assertEquals(new Instantiable(), new Instantiable());
- }
-
/** Used for Class and Object negative creation tests */
public static final class NotInstantiable {
private NotInstantiable() {
@@ -67,64 +61,6 @@ public class TypeHandlerTest {
}
- @Test
- public void testRegister() {
- assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
- try {
- TypeHandler.register(NotInstantiable.class, Converter.DATE);
- assertEquals(Converter.DATE, TypeHandler.getConverter(NotInstantiable.class));
- } finally {
- TypeHandler.register(NotInstantiable.class, null);
- assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
- }
- }
-
- @Test
- public void testResetConverters() {
- assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
- try {
- TypeHandler.register(NotInstantiable.class, Converter.DATE);
- assertEquals(Converter.DATE, TypeHandler.getConverter(NotInstantiable.class));
- TypeHandler.resetConverters();
- assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
- assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
- } finally {
- TypeHandler.register(NotInstantiable.class, null);
- }
- }
-
- @Test
- public void testNoConverters() {
- assertEquals(Converter.NUMBER, TypeHandler.getConverter(Number.class));
- try {
- TypeHandler.noConverters();
- assertEquals(Converter.DEFAULT, TypeHandler.getConverter(Number.class));
- } finally {
- TypeHandler.resetConverters();
- assertEquals(Converter.NUMBER, TypeHandler.getConverter(Number.class));
- }
- }
-
- @Test
- public void testCreateValueExistingFile() throws Exception {
- try (FileInputStream result = TypeHandler.createValue(
- "src/test/resources/org/apache/commons/cli/existing-readable.file",
- PatternOptionBuilder.EXISTING_FILE_VALUE)) {
- assertNotNull(result);
- }
- }
-
- @SuppressWarnings("unchecked")
- @ParameterizedTest(name = "{0} as {1}")
- @MethodSource("createValueTestParameters")
- public void createValueTests(final String str, final Class<?> type, final Object expected) throws Exception {
- if (expected instanceof Class<?> && Throwable.class.isAssignableFrom((Class<?>) expected)) {
- assertThrows((Class<Throwable>) expected, () -> TypeHandler.createValue(str, type));
- } else {
- assertEquals(expected, TypeHandler.createValue(str, type));
- }
- }
-
private static Stream<Arguments> createValueTestParameters() {
// forse the PatternOptionBuilder to load / modify the TypeHandler table.
Class<?> ignore = PatternOptionBuilder.FILES_VALUE;
@@ -213,4 +149,68 @@ public class TypeHandlerTest {
}
}
+
+ @SuppressWarnings("unchecked")
+ @ParameterizedTest(name = "{0} as {1}")
+ @MethodSource("createValueTestParameters")
+ public void createValueTests(final String str, final Class<?> type, final Object expected) throws Exception {
+ if (expected instanceof Class<?> && Throwable.class.isAssignableFrom((Class<?>) expected)) {
+ assertThrows((Class<Throwable>) expected, () -> TypeHandler.createValue(str, type));
+ } else {
+ assertEquals(expected, TypeHandler.createValue(str, type));
+ }
+ }
+
+ @Test
+ public void testCreateValueExistingFile() throws Exception {
+ try (FileInputStream result = TypeHandler.createValue(
+ "src/test/resources/org/apache/commons/cli/existing-readable.file",
+ PatternOptionBuilder.EXISTING_FILE_VALUE)) {
+ assertNotNull(result);
+ }
+ }
+
+ @Test
+ public void testNoConverters() {
+ assertEquals(Converter.NUMBER, TypeHandler.getConverter(Number.class));
+ try {
+ TypeHandler.noConverters();
+ assertEquals(Converter.DEFAULT, TypeHandler.getConverter(Number.class));
+ } finally {
+ TypeHandler.resetConverters();
+ assertEquals(Converter.NUMBER, TypeHandler.getConverter(Number.class));
+ }
+ }
+
+ /* proof of equality for later tests */
+ @Test
+ public void testnstantiableEquals() {
+ assertEquals(new Instantiable(), new Instantiable());
+ }
+
+ @Test
+ public void testRegister() {
+ assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
+ try {
+ TypeHandler.register(NotInstantiable.class, Converter.DATE);
+ assertEquals(Converter.DATE, TypeHandler.getConverter(NotInstantiable.class));
+ } finally {
+ TypeHandler.register(NotInstantiable.class, null);
+ assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
+ }
+ }
+
+ @Test
+ public void testResetConverters() {
+ assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
+ try {
+ TypeHandler.register(NotInstantiable.class, Converter.DATE);
+ assertEquals(Converter.DATE, TypeHandler.getConverter(NotInstantiable.class));
+ TypeHandler.resetConverters();
+ assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
+ assertEquals(Converter.DEFAULT, TypeHandler.getConverter(NotInstantiable.class));
+ } finally {
+ TypeHandler.register(NotInstantiable.class, null);
+ }
+ }
}