[Japi-cvs] SF.net SVN: japi: [242] libs/argparser/trunk/src
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-12-02 16:15:13
|
Revision: 242 http://svn.sourceforge.net/japi/?rev=242&view=rev Author: christianhujer Date: 2006-12-02 08:15:10 -0800 (Sat, 02 Dec 2006) Log Message: ----------- Improved converters, added some test cases. Modified Paths: -------------- libs/argparser/trunk/src/net/sf/japi/io/args/Option.java libs/argparser/trunk/src/net/sf/japi/io/args/converter/AbstractConverter.java libs/argparser/trunk/src/net/sf/japi/io/args/converter/BooleanConverter.java libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter.java libs/argparser/trunk/src/net/sf/japi/io/args/converter/ConverterRegistry.java libs/argparser/trunk/src/net/sf/japi/io/args/converter/InputStreamConverter.java libs/argparser/trunk/src/net/sf/japi/io/args/converter/IntegerConverter.java libs/argparser/trunk/src/net/sf/japi/io/args/converter/StringConverter.java libs/argparser/trunk/src/test/net/sf/japi/io/args/ArgParserTest.java Added Paths: ----------- libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter.properties libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter_de.properties libs/argparser/trunk/src/net/sf/japi/io/args/converter/NoConverterFoundException.java libs/argparser/trunk/src/net/sf/japi/io/args/converter/OututStreamConverter.java libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/AbstractConverterTest.java libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/BooleanConverterTest.java libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/IntegerConverterTest.java libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/StringConverterTest.java Modified: libs/argparser/trunk/src/net/sf/japi/io/args/Option.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/Option.java 2006-11-29 23:09:22 UTC (rev 241) +++ libs/argparser/trunk/src/net/sf/japi/io/args/Option.java 2006-12-02 16:15:10 UTC (rev 242) @@ -37,6 +37,7 @@ /** * The option type. * Default is {@link OptionType#OPTIONAL}. + * Normally you wouldn't change this. * @return option type */ OptionType type() default OptionType.OPTIONAL; Modified: libs/argparser/trunk/src/net/sf/japi/io/args/converter/AbstractConverter.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/AbstractConverter.java 2006-11-29 23:09:22 UTC (rev 241) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/AbstractConverter.java 2006-12-02 16:15:10 UTC (rev 242) @@ -2,37 +2,53 @@ import java.util.Locale; import java.util.ResourceBundle; +import org.jetbrains.annotations.NotNull; /** * Base class for the default converters. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> */ -abstract class AbstractConverter<T> implements Converter<T> { +public abstract class AbstractConverter<T> implements Converter<T> { /** The target class. */ - private final Class<T> targetClass; + @NotNull private final Class<T> targetClass; /** * Create an AbstractConverter. * @param targetClass TargetClass */ - AbstractConverter(final Class<T> targetClass) { + AbstractConverter(@NotNull final Class<T> targetClass) { this.targetClass = targetClass; } /** {@inheritDoc} */ - public final Class<T> getTargetClass() { + @NotNull public final Class<T> getTargetClass() { return targetClass; } /** {@inheritDoc} */ - public final String getDisplayName() { - return ResourceBundle.getBundle("net.sf.japi.io.args.converter.names").getString(targetClass.getName()); + @NotNull public final T convert(@NotNull final String arg) throws Exception { + return convert(Locale.getDefault(), arg); } /** {@inheritDoc} */ - public final String getDisplayName(final Locale locale) { - return ResourceBundle.getBundle("net.sf.japi.io.args.converter.names", locale).getString(targetClass.getName()); + @NotNull public final String getDisplayName() { + return getDisplayName(Locale.getDefault()); } + /** {@inheritDoc} */ + @NotNull public final String getDisplayName(@NotNull final Locale locale) { + return ResourceBundle.getBundle("net.sf.japi.io.args.converter.Converter", locale).getString(targetClass.getName() + ".displayName"); + } + + /** {@inheritDoc} */ + @NotNull public final String getDescription() { + return getDescription(Locale.getDefault()); + } + + /** {@inheritDoc} */ + @NotNull public final String getDescription(@NotNull final Locale locale) { + return ResourceBundle.getBundle("net.sf.japi.io.args.converter.Converter", locale).getString(targetClass.getName() + ".description"); + } + } // class AbstractConverter Modified: libs/argparser/trunk/src/net/sf/japi/io/args/converter/BooleanConverter.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/BooleanConverter.java 2006-11-29 23:09:22 UTC (rev 241) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/BooleanConverter.java 2006-12-02 16:15:10 UTC (rev 242) @@ -1,11 +1,21 @@ package net.sf.japi.io.args.converter; +import org.jetbrains.annotations.NotNull; +import java.util.Locale; +import java.util.ResourceBundle; + /** * Converter which converts a String into a Boolean. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> */ public class BooleanConverter extends AbstractConverter<Boolean> { + /** Strings for true. */ + private static final String[] TRUE_STRINGS = { "true", "yes", "on", "1" }; + + /** Strings for true. */ + private static final String[] FALSE_STRINGS = { "false", "no", "off", "0" }; + /** * Create a BooleanConverter. */ @@ -14,8 +24,29 @@ } /** {@inheritDoc} */ - public Boolean convert(final String arg) throws Exception { - return Boolean.valueOf(arg); + @NotNull + public Boolean convert(@NotNull final Locale locale, @NotNull final String arg) throws Exception { + for (final String s : TRUE_STRINGS) { + if (s.equalsIgnoreCase(arg)) { + return Boolean.TRUE; + } + } + for (final String s : FALSE_STRINGS) { + if (s.equalsIgnoreCase(arg)) { + return Boolean.FALSE; + } + } + for (final String s : ResourceBundle.getBundle("net.sf.japi.io.args.converter.Converter", locale).getString("java.lang.Boolean.true").split("\\s+")) { + if (s.equalsIgnoreCase(arg)) { + return Boolean.TRUE; + } + } + for (final String s : ResourceBundle.getBundle("net.sf.japi.io.args.converter.Converter", locale).getString("java.lang.Boolean.false").split("\\s+")) { + if (s.equalsIgnoreCase(arg)) { + return Boolean.TRUE; + } + } + throw new IllegalArgumentException(arg + " is not a valid String for a boolean."); } } // class BooleanConverter Modified: libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter.java 2006-11-29 23:09:22 UTC (rev 241) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter.java 2006-12-02 16:15:10 UTC (rev 242) @@ -1,6 +1,7 @@ package net.sf.japi.io.args.converter; import java.util.Locale; +import org.jetbrains.annotations.NotNull; /** * The Converter interface is used for converters that convert Strings into other types. @@ -13,26 +14,50 @@ * @param arg Argument to convert * @return Argument converted to T. * @throws Exception In case of conversion failure. + * @throws NullPointerException In case <code><var>arg</var> == null</code>. */ - T convert(final String arg) throws Exception; + @NotNull T convert(@NotNull final String arg) throws Exception; /** + * Convert the given argument to the desired return type. + * @param arg Argument to convert + * @param locale Locale to get perform conversion for. + * @return Argument converted to T. + * @throws Exception In case of conversion failure. + * @throws NullPointerException In case <code><var>arg</var> == null</code>. + */ + @NotNull T convert(@NotNull final Locale locale, @NotNull final String arg) throws Exception; + + /** * Returns the Class this Converter is for. * @return The Class this Converter is for. */ - Class<T> getTargetClass(); + @NotNull Class<T> getTargetClass(); /** * Returns a display name for the type of this Converter. * @return A display name for the type of this Converter. */ - String getDisplayName(); + @NotNull String getDisplayName(); /** * Returns a display name for the type of this Converter. * @param locale Locale to get display name for. * @return A display name for the type of this Converter in the specified locale. */ - String getDisplayName(final Locale locale); + @NotNull String getDisplayName(@NotNull final Locale locale); + /** + * Returns a description for this Converter. + * @return A description for this Converter. + */ + @NotNull String getDescription(); + + /** + * Returns a description for this Converter. + * @param locale Locale to get the description for. + * @return A description for this Converter in the specified locale. + */ + @NotNull String getDescription(@NotNull final Locale locale); + } // interface Convert Added: libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter.properties =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter.properties (rev 0) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter.properties 2006-12-02 16:15:10 UTC (rev 242) @@ -0,0 +1,12 @@ +java.io.InputStream.displayName=uri|filename|- +java.io.InputStream.description=Input file (URI, filename or - for STDIN). +java.io.OutputStream.displayName=filename|- +java.io.OutputStream.description=Output file (filename or - for STDOUT). +java.lang.Boolean.displayName=boolean +java.lang.Boolean.description=Valid values are: {0} for true and {1} for false. +java.lang.Boolean.true= +java.lang.Boolean.false= +java.lang.Integer.displayName=integer +java.lang.Integer.description=Integer number (decimal, 0... octal, 0x... 0X... #... hexadecimal) +java.lang.String.displayName=string +java.lang.String.description=Simple text \ No newline at end of file Property changes on: libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter.properties ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Modified: libs/argparser/trunk/src/net/sf/japi/io/args/converter/ConverterRegistry.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/ConverterRegistry.java 2006-11-29 23:09:22 UTC (rev 241) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/ConverterRegistry.java 2006-12-02 16:15:10 UTC (rev 242) @@ -3,7 +3,10 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import java.util.Locale; import sun.misc.Service; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Registry for Converters. @@ -11,15 +14,15 @@ */ public class ConverterRegistry { - /** Singleton instance. */ - private static final ConverterRegistry instance = createSingletonInstance(); + /** Singleton INSTANCE. */ + private static final ConverterRegistry INSTANCE = createSingletonInstance(); /** Map for converters. */ private final Map<Class<?>, Converter<?>> converters = new HashMap<Class<?>, Converter<?>>(); /** * Creates a ConverterRegistry. - * Usually you want a shared global ConverterRegistry and use {@link #getInstance()} instead of creating your own ConverterRegistry instance. + * Usually you want a shared global ConverterRegistry and use {@link #getInstance()} instead of creating your own ConverterRegistry INSTANCE. * @see #getInstance() */ public ConverterRegistry() { @@ -29,6 +32,7 @@ * Creates the global shared instance of ConverterRegistry. * @return The global shared instance of ConverterRegistry. */ + @SuppressWarnings({"unchecked"}) private static ConverterRegistry createSingletonInstance() { final ConverterRegistry instance = new ConverterRegistry(); for (final Iterator<Converter<?>> converters = Service.providers(Converter.class); converters.hasNext();) { @@ -42,7 +46,7 @@ * @return The global shared instance of ConverterRegistry. */ public static ConverterRegistry getInstance() { - return instance; + return INSTANCE; } /** @@ -50,7 +54,8 @@ * @param clazz Class to get Converter for. * @return <code>null</code> if no suited converter was found. */ - public <T> Converter<T> getConverter(final Class<T> clazz) { + @Nullable public <T> Converter<T> getConverter(@NotNull final Class<T> clazz) { + //noinspection unchecked return (Converter<T>) converters.get(clazz); } @@ -58,7 +63,7 @@ * Register a Converter for a specific class. * @param converter Converter to register */ - public <T> void register(final Converter<T> converter) { + public <T> void register(@NotNull final Converter<T> converter) { converters.put(converter.getTargetClass(), converter); for (Class<?> superClass = converter.getTargetClass(); (superClass = superClass.getSuperclass()) != null;) { if (!converters.containsKey(superClass)) { @@ -79,8 +84,30 @@ * @return Converted String in the desired target type. * @throws Exception in case the conversion failed. */ - public static <T> T convert(final Class<T> targetType, final String s) throws Exception { - return getInstance().getConverter(targetType).convert(s); + @NotNull public static <T> T convert(@NotNull final Class<T> targetType, @NotNull final String s) throws Exception { + final Converter<T> converter = getInstance().getConverter(targetType); + if (converter != null) { + return converter.convert(s); + } else { + throw new NoConverterFoundException(targetType); + } } + /** + * Convenience method to convert a String to the desired target type using the default ConverterRegistry. + * @param targetType target type to convert to. + * @param locale Locale to perform the conversion in. + * @param s String to convert. + * @return Converted String in the desired target type. + * @throws Exception in case the conversion failed. + */ + @NotNull public static <T> T convert(@NotNull final Class<T> targetType, @NotNull final Locale locale, @NotNull final String s) throws Exception { + final Converter<T> converter = getInstance().getConverter(targetType); + if (converter != null) { + return converter.convert(locale, s); + } else { + throw new NoConverterFoundException(targetType); + } + } + } // class ConverterRegistry Added: libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter_de.properties =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter_de.properties (rev 0) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter_de.properties 2006-12-02 16:15:10 UTC (rev 242) @@ -0,0 +1,7 @@ +java.io.InputStream.description=Eingabedatei (als URI, Dateiname oder - f\xFCr STDIN). +java.io.OutputStream.description=Ausgabedatei (Dateiname oder - f\xFCr STDOUT). +java.lang.Boolean.description=G\xFCltige Werte: {0} f\xFCr wahr und {1} f\xFCr falsch. +java.lang.Boolean.true=wahr ja an +java.lang.Boolean.false=falsch nein aus +java.lang.Integer.description=Ganzzahl (dezimal, 0... oktal, 0x... 0X... #... hexadezimal) +java.lang.String.description=Einfacher Text. Property changes on: libs/argparser/trunk/src/net/sf/japi/io/args/converter/Converter_de.properties ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Modified: libs/argparser/trunk/src/net/sf/japi/io/args/converter/InputStreamConverter.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/InputStreamConverter.java 2006-11-29 23:09:22 UTC (rev 241) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/InputStreamConverter.java 2006-12-02 16:15:10 UTC (rev 242) @@ -5,12 +5,16 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.Locale; +import org.jetbrains.annotations.NotNull; /** * Converter that converts a String into an InputStream. + * @note This converter always behaves the same independently of the {@link Locale}. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @deprecated The concept of this class is not yet mature. Please don't use it. */ -public class InputStreamConverter extends AbstractConverter<InputStream> { +@Deprecated public class InputStreamConverter extends AbstractConverter<InputStream> { /** * Create an InputStreamConverter. @@ -20,11 +24,19 @@ } /** {@inheritDoc} */ - public InputStream convert(final String arg) throws FileNotFoundException { + @NotNull public InputStream convert(@NotNull final Locale locale, @NotNull final String arg) throws FileNotFoundException { try { return new URL(arg).openStream(); } catch (final IOException ignore) { - return new FileInputStream(arg); + try { + return new FileInputStream(arg); + } catch (final FileNotFoundException e) { + if ("-".equals(arg)) { + return System.in; + } else { + throw e; + } + } } } Modified: libs/argparser/trunk/src/net/sf/japi/io/args/converter/IntegerConverter.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/IntegerConverter.java 2006-11-29 23:09:22 UTC (rev 241) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/IntegerConverter.java 2006-12-02 16:15:10 UTC (rev 242) @@ -1,8 +1,15 @@ package net.sf.japi.io.args.converter; +import org.jetbrains.annotations.NotNull; +import java.util.Locale; + /** * Converter which converts a String into a an Integer. + * The Converter uses a method that at minimum supports the same conversions as {@link Integer#decode(String)}. + * That means the following formats are supported: + * @note This converter always behaves the same independently of the {@link Locale}. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @see Integer#decode(String) Minimum grammar supported. */ public class IntegerConverter extends AbstractConverter<Integer> { @@ -14,8 +21,8 @@ } /** {@inheritDoc} */ - public Integer convert(final String arg) throws Exception { - return Integer.valueOf(arg); + @NotNull public Integer convert(@NotNull final Locale locale, @NotNull final String arg) throws NumberFormatException { + return Integer.decode(arg); } } // class IntegerConverter Added: libs/argparser/trunk/src/net/sf/japi/io/args/converter/NoConverterFoundException.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/NoConverterFoundException.java (rev 0) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/NoConverterFoundException.java 2006-12-02 16:15:10 UTC (rev 242) @@ -0,0 +1,30 @@ +package net.sf.japi.io.args.converter; + +/** + * Exception that is thrown in case no matching converter for a conversion was found. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class NoConverterFoundException extends Exception { + + /** + * The type for that no Converter was found. + */ + private final Class<?> targetType; + + /** + * Create a NoConverterFoundException. + * @param targetType Type for that no Converter was found. + */ + public NoConverterFoundException(final Class<?> targetType) { + this.targetType = targetType; + } + + /** + * Returns the type for that no Converter was found. + * @return The type for that no Converter was found. + */ + public Class<?> getTargetType() { + return targetType; + } + +} // class NoConverterFoundException Property changes on: libs/argparser/trunk/src/net/sf/japi/io/args/converter/NoConverterFoundException.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/argparser/trunk/src/net/sf/japi/io/args/converter/OututStreamConverter.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/OututStreamConverter.java (rev 0) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/OututStreamConverter.java 2006-12-02 16:15:10 UTC (rev 242) @@ -0,0 +1,34 @@ +package net.sf.japi.io.args.converter; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.Locale; +import org.jetbrains.annotations.NotNull; + +/** + * Converter that converts a String into an InputStream. + * @note This converter always behaves the same independently of the {@link Locale}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @deprecated The concept of this class is not yet mature. Please don't use it. + */ +@Deprecated public class OututStreamConverter extends AbstractConverter<OutputStream> { + + /** + * Create an InputStreamConverter. + */ + public OututStreamConverter() { + super(OutputStream.class); + } + + /** {@inheritDoc} */ + @NotNull public OutputStream convert(@NotNull final Locale locale, @NotNull final String arg) throws FileNotFoundException { + if ("-".equals(arg) && !new File("-").exists()) { + return System.out; + } else { + return new FileOutputStream(arg); + } + } + +} // class InputStreamConverter Property changes on: libs/argparser/trunk/src/net/sf/japi/io/args/converter/OututStreamConverter.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Modified: libs/argparser/trunk/src/net/sf/japi/io/args/converter/StringConverter.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/converter/StringConverter.java 2006-11-29 23:09:22 UTC (rev 241) +++ libs/argparser/trunk/src/net/sf/japi/io/args/converter/StringConverter.java 2006-12-02 16:15:10 UTC (rev 242) @@ -1,7 +1,12 @@ package net.sf.japi.io.args.converter; +import org.jetbrains.annotations.NotNull; +import java.util.Locale; + /** - * Dummy Converter which "converts" a String into a String by simply returning it. + * Simple Converter which "converts" a String into a String by simply returning it. + * It exists to be able to apply the same conversion pattern for all types including Strings. + * @note This converter always behaves the same independently of the {@link Locale}. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> */ public class StringConverter extends AbstractConverter<String> { @@ -14,7 +19,7 @@ } /** {@inheritDoc} */ - public String convert(final String arg) throws Exception { + @NotNull public String convert(@NotNull final Locale locale, @NotNull final String arg) throws Exception { return arg; } Modified: libs/argparser/trunk/src/test/net/sf/japi/io/args/ArgParserTest.java =================================================================== --- libs/argparser/trunk/src/test/net/sf/japi/io/args/ArgParserTest.java 2006-11-29 23:09:22 UTC (rev 241) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/ArgParserTest.java 2006-12-02 16:15:10 UTC (rev 242) @@ -54,7 +54,7 @@ * Set the value of the input option. * @param input Value of the input option. */ - @Option(type = OptionType.REQUIRED, value = {"i", "input"}) + @Option(value = {"i", "input"}, type = OptionType.REQUIRED) public void setInput(final String input) { this.input = input; } @@ -63,7 +63,7 @@ * Set the value of the foo option. * @param foo Value of the foo option. */ - @Option(value = {"f", "b", "foo", "bar", "buzz"}) + @Option({"f", "b", "foo", "bar", "buzz"}) public void setFoo(final String foo) { // ignored } Added: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/AbstractConverterTest.java =================================================================== --- libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/AbstractConverterTest.java (rev 0) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/AbstractConverterTest.java 2006-12-02 16:15:10 UTC (rev 242) @@ -0,0 +1,76 @@ +package test.net.sf.japi.io.args.converter; + +import java.util.Locale; +import net.sf.japi.io.args.converter.Converter; +import org.jetbrains.annotations.NotNull; +import org.junit.Assert; +import org.junit.Test; + +/** + * Base class for tests for Converters. + * Provides some basic testing methods. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public abstract class AbstractConverterTest<V, T extends Converter<V>> { + + /** The target class. */ + private Class<V> targetClass; + + /** The converter testling. */ + protected T converter; + + /** + * Create an AbstractConverterTest. + * @param targetClass Class of the Converter's target to test. + * @param converterClass Class of the Converter to test. + * @throws Exception in case of setup problems. + */ + protected AbstractConverterTest(@NotNull final Class<V> targetClass, @NotNull final Class<T> converterClass) throws Exception { + this.targetClass = targetClass; + converter = converterClass.newInstance(); + } + + /** + * Tests whether convert throws a NullPointerException if invoked with <code>null</code> + * @throws NullPointerException Expected exception that's thrown if the test case is successful. + * @throws Exception In case of test problems. + */ + @SuppressWarnings({"ConstantConditions"}) + @Test(expected=NullPointerException.class) + public void testThrowsNPE() throws Exception { + converter.convert(null); + } + /** + * Tests whether getting the target class works. + * @throws Exception In case of test problems. + */ + @Test + public void testGetTargetClass() throws Exception { + Assert.assertSame(targetClass, converter.getTargetClass()); + } + + /** + * Tests whether the testling has a proper description for the default locale. + * @throws Exception In case of unexpected errors. + */ + @Test + public void testDescription() throws Exception { + final String description = converter.getDescription(); + Assert.assertNotNull(description); + Assert.assertTrue(description.length() > 0); + Assert.assertEquals(description, converter.getDescription(Locale.getDefault())); + } + + /** + * Tests whether the testling has a proper display name. + * @throws Exception In case of unexpected errors. + */ + @Test + public void testDisplayName() throws Exception { + final String displayName = converter.getDisplayName(); + Assert.assertNotNull(displayName); + Assert.assertTrue(displayName.length() > 0); + Assert.assertEquals(displayName, converter.getDisplayName(Locale.getDefault())); + } + +} // class AbstractConverterTest \ No newline at end of file Property changes on: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/AbstractConverterTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/BooleanConverterTest.java =================================================================== --- libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/BooleanConverterTest.java (rev 0) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/BooleanConverterTest.java 2006-12-02 16:15:10 UTC (rev 242) @@ -0,0 +1,55 @@ +package test.net.sf.japi.io.args.converter; + +import net.sf.japi.io.args.converter.BooleanConverter; +import org.junit.Test; +import org.junit.Assert; + +/** + * Tests for {@link BooleanConverter}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class BooleanConverterTest extends AbstractConverterTest<Boolean, BooleanConverter> { + + /* + * Create an BooleanConverterTest. + * @throws Exception in case of setup problems. + */ + public BooleanConverterTest() throws Exception { + super(Boolean.class, BooleanConverter.class); + } + + /** + * Tests whether convert works for true. + * @throws Exception In case of unexpected errors. + */ + @Test + public void testConvertTrue() throws Exception { + Assert.assertTrue(converter.convert("true")); + Assert.assertTrue(converter.convert("TRUE")); + Assert.assertTrue(converter.convert("True")); + Assert.assertTrue(converter.convert("1")); + } + + /** + * Tests whether convert works for true. + * @throws Exception In case of unexpected errors. + */ + @Test + public void testConvertFalse() throws Exception { + Assert.assertFalse(converter.convert("false")); + Assert.assertFalse(converter.convert("FALSE")); + Assert.assertFalse(converter.convert("False")); + Assert.assertFalse(converter.convert("0")); + } + + /** + * Tests whether convert works for other Strings (IllegalArgumentException must be thrown). + * @throws IllegalArgumentException Expected exception that's thrown if the test case is successful. + * @throws Exception In case of unexpected errors. + */ + @Test(expected=IllegalArgumentException.class) + public void testConvertOther() throws Exception { + converter.convert("foobarbuzz"); + } + +} // class BooleanConverterTest Property changes on: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/BooleanConverterTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/IntegerConverterTest.java =================================================================== --- libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/IntegerConverterTest.java (rev 0) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/IntegerConverterTest.java 2006-12-02 16:15:10 UTC (rev 242) @@ -0,0 +1,61 @@ +package test.net.sf.japi.io.args.converter; + +import net.sf.japi.io.args.converter.IntegerConverter; +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link IntegerConverter}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class IntegerConverterTest extends AbstractConverterTest<Integer, IntegerConverter> { + + /** + * Create an IntegerConverterTest. + * @throws Exception in case of setup problems. + */ + public IntegerConverterTest() throws Exception { + super(Integer.class, IntegerConverter.class); + } + + /** + * Tests whether converting an arbitrary text throws a NumberFormatException. + * @throws NumberFormatException Expected exception that's thrown if the test case is successful. + */ + @Test(expected=NumberFormatException.class) + public void convertWithText() throws Exception { + converter.convert("foo"); + } + + /** + * Tests whether converting decimal (10-base) integer numbers works correclty. + * @throws Exception In case of unexpected errors. + */ + @Test + public void convertDecimalNumbers() throws Exception { + for (final int number : new int[] { Integer.MIN_VALUE, -100, -1, 0, 1, 100, Integer.MAX_VALUE }) { + Assert.assertEquals(number, converter.convert(Integer.toString(number))); + } + } + + /** + * Tests whether converting hexadecimal (16-base) integer numbers works correctly. + * @throws Exception In case of unexpected errors. + */ + @Test + public void convertHexadecimalNumbers() throws Exception { + Assert.assertEquals(0x50000, converter.convert("0x50000")); + Assert.assertEquals(0x50000, converter.convert("0X50000")); + Assert.assertEquals(0x50000, converter.convert("#50000")); + } + + /** + * Tests whether converting octal (8-base) integer numbers works correctly. + * @throws Exception In case of unexpected errors. + */ + @Test + public void convertOctalNumbers() throws Exception { + Assert.assertEquals(0x1ff, converter.convert("0777")); + } + +} // class IntegerConverterTest \ No newline at end of file Property changes on: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/IntegerConverterTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/StringConverterTest.java =================================================================== --- libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/StringConverterTest.java (rev 0) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/StringConverterTest.java 2006-12-02 16:15:10 UTC (rev 242) @@ -0,0 +1,31 @@ +package test.net.sf.japi.io.args.converter; + +import net.sf.japi.io.args.converter.StringConverter; +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link StringConverter}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class StringConverterTest extends AbstractConverterTest<String, StringConverter> { + + /** + * Create a StringConverterTest. + * @throws Exception in case of setup problems. + */ + public StringConverterTest() throws Exception { + super(String.class, StringConverter.class); + } + + /** + * Tests whether {@link StringConverter} has a public default constructor. + * @throws Exception In case of unexpected errors. + */ + @Test + public void testOperation() throws Exception { + final String foo = converter.convert("foo"); + Assert.assertEquals("StringConverter.convert(\"foo\") must return \"foo\".", "foo", foo); + } + +} // class StringConverterTest Property changes on: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/StringConverterTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |