[Japi-cvs] SF.net SVN: japi: [243] libs/argparser/trunk/src/test/net/sf/japi/io/args/ converter
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-12-02 19:01:38
|
Revision: 243 http://svn.sourceforge.net/japi/?rev=243&view=rev Author: christianhujer Date: 2006-12-02 11:01:29 -0800 (Sat, 02 Dec 2006) Log Message: ----------- Added more tests, improved existing tests. Now all non-deprecated converter classes have 100% method coverage in unit test. Modified Paths: -------------- 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 Added Paths: ----------- libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/ConverterRegistryTest.java libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/NoConverterFoundExceptionTest.java Modified: 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 2006-12-02 16:15:10 UTC (rev 242) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/AbstractConverterTest.java 2006-12-02 19:01:29 UTC (rev 243) @@ -31,15 +31,29 @@ } /** + * Tests whether creating a Converter works. + * @throws Exception (unexpected) + */ + public void testAbstractConverter() throws Exception { + // Tested by the constructor. + } + + /** * 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); + public void testConvertThrowsNPE() throws Exception { + try { + converter.convert(null); + Assert.fail("Expected exception IllegalArgumentException or NullPointerException but got no exception when invoking convert(null)."); + } catch (final IllegalArgumentException ignore) { + // expected Exception if compiled with @NotNull support + } catch (final NullPointerException ignore) { + // expected Exception if compiled without @NotNull support + } } + /** * Tests whether getting the target class works. * @throws Exception In case of test problems. @@ -54,7 +68,7 @@ * @throws Exception In case of unexpected errors. */ @Test - public void testDescription() throws Exception { + public void testGetDescription() throws Exception { final String description = converter.getDescription(); Assert.assertNotNull(description); Assert.assertTrue(description.length() > 0); @@ -66,7 +80,7 @@ * @throws Exception In case of unexpected errors. */ @Test - public void testDisplayName() throws Exception { + public void testGetDisplayName() throws Exception { final String displayName = converter.getDisplayName(); Assert.assertNotNull(displayName); Assert.assertTrue(displayName.length() > 0); Modified: 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 2006-12-02 16:15:10 UTC (rev 242) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/BooleanConverterTest.java 2006-12-02 19:01:29 UTC (rev 243) @@ -19,6 +19,15 @@ } /** + * Tests whether instanciating a BooleanConverter works. + * @throws Exception (unexpected) + */ + @Test + public void testBooleanConverter() throws Exception { + // Tested by this test class's superclass constructor. + } + + /** * Tests whether convert works for true. * @throws Exception In case of unexpected errors. */ Added: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/ConverterRegistryTest.java =================================================================== --- libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/ConverterRegistryTest.java (rev 0) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/ConverterRegistryTest.java 2006-12-02 19:01:29 UTC (rev 243) @@ -0,0 +1,129 @@ +package test.net.sf.japi.io.args.converter; + +import net.sf.japi.io.args.converter.ConverterRegistry; +import net.sf.japi.io.args.converter.Converter; +import org.junit.Test; +import org.junit.Assert; +import org.jetbrains.annotations.NotNull; +import java.util.Locale; + +/** + * Test for {@link ConverterRegistry}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class ConverterRegistryTest { + ConverterRegistry converterRegistry; + + /** + * Tests that creating a new ConverterRegistry works. + * @throws Exception (unexpected) + */ + @Test + public void testConverterRegistry() throws Exception { + final ConverterRegistry converterRegistry = new ConverterRegistry(); + } + + /** + * Tests whether {@link ConverterRegistry#getInstance()} works. + * @throws Exception (unexpected) + */ + @Test + public void testGetInstance() throws Exception { + final ConverterRegistry converterRegistry = ConverterRegistry.getInstance(); + Assert.assertNotNull("ConverterRegistry.getInstance() must not return null.", converterRegistry); + } + + /** + * Tests whether {@link ConverterRegistry#getConverter(Class)} works. + * @throws Exception (unexpected) + */ + @Test + public void testGetConverter() throws Exception { + final ConverterRegistry converterRegistry = new ConverterRegistry(); + final Converter<String> converter = new DummyConverter<String>(String.class); + converterRegistry.register(converter); + Assert.assertSame(converterRegistry.getConverter(String.class), converter); + } + + /** + * Tests whether {@link ConverterRegistry#register(Converter)} works. + * @throws Exception (unexpected) + */ + @Test + public void testRegister() throws Exception { + // see #testGetConverter(); + } + + /** + * Tests whether {@link ConverterRegistry#convert(Class, String)} and {@link ConverterRegistry#convert(Class, Locale, String)} work. + * @throws Exception (unexpected) + */ + @Test + public void testConvert() throws Exception { + Assert.assertEquals("foo", ConverterRegistry.convert(String.class, "foo")); + Assert.assertEquals("foo", ConverterRegistry.convert(String.class, Locale.GERMANY, "foo")); + } + + /** + * Dummy Converter. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ + @SuppressWarnings({"ConstantConditions"}) + private static class DummyConverter<T> implements Converter<T> { + + /** Class for this converter. */ + private final Class<T> targetClass; + + /** + * Create a DummyConverter. + * @param targetClass Target class. + */ + private DummyConverter(final Class<T> targetClass) { + this.targetClass = targetClass; + } + + /** {@inheritDoc} */ + @NotNull + public T convert(@NotNull String arg) throws Exception { + return null; + } + + /** {@inheritDoc} */ + @NotNull + public T convert(@NotNull Locale locale, @NotNull String arg) throws Exception { + return null; + } + + /** {@inheritDoc} */ + @NotNull + public Class<T> getTargetClass() { + return targetClass; + } + + /** {@inheritDoc} */ + @NotNull + public String getDisplayName() { + return null; + } + + /** {@inheritDoc} */ + @NotNull + public String getDisplayName(@NotNull Locale locale) { + return null; + } + + /** {@inheritDoc} */ + @NotNull + public String getDescription() { + return null; + } + + /** {@inheritDoc} */ + @NotNull + public String getDescription(@NotNull Locale locale) { + return null; + } + + } // class DummyConverter + +} // class ConverterRegistryTest \ No newline at end of file Property changes on: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/ConverterRegistryTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Modified: 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 2006-12-02 16:15:10 UTC (rev 242) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/IntegerConverterTest.java 2006-12-02 19:01:29 UTC (rev 243) @@ -19,11 +19,20 @@ } /** + * Tests whether instanciating a IntegerConverter works. + * @throws Exception (unexpected) + */ + @Test + public void testIntegerConverter() throws Exception { + // Tested by this test class's superclass constructor. + } + + /** * 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 { + public void testConvertWithText() throws Exception { converter.convert("foo"); } @@ -32,7 +41,7 @@ * @throws Exception In case of unexpected errors. */ @Test - public void convertDecimalNumbers() throws Exception { + public void testConvertDecimalNumbers() 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))); } @@ -43,7 +52,7 @@ * @throws Exception In case of unexpected errors. */ @Test - public void convertHexadecimalNumbers() throws Exception { + public void testConvertHexadecimalNumbers() throws Exception { Assert.assertEquals(0x50000, converter.convert("0x50000")); Assert.assertEquals(0x50000, converter.convert("0X50000")); Assert.assertEquals(0x50000, converter.convert("#50000")); @@ -54,7 +63,7 @@ * @throws Exception In case of unexpected errors. */ @Test - public void convertOctalNumbers() throws Exception { + public void testConvertOctalNumbers() throws Exception { Assert.assertEquals(0x1ff, converter.convert("0777")); } Added: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/NoConverterFoundExceptionTest.java =================================================================== --- libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/NoConverterFoundExceptionTest.java (rev 0) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/NoConverterFoundExceptionTest.java 2006-12-02 19:01:29 UTC (rev 243) @@ -0,0 +1,31 @@ +package test.net.sf.japi.io.args.converter; + +import net.sf.japi.io.args.converter.NoConverterFoundException; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for {@link NoConverterFoundException}. + */ +public class NoConverterFoundExceptionTest { + + /** + * Tests whether the target type is stored correctly. + * @throws Exception (unexpected) + */ + @Test + public void testGetTargetType() throws Exception { + final NoConverterFoundException exception = new NoConverterFoundException(Object.class); + Assert.assertSame(exception.getTargetType(), Object.class); + } + + /** + * Tests whether the target type is stored correctly. + * @throws Exception (unexpected) + */ + @Test + public void testNoConverterFoundException() throws Exception { + testGetTargetType(); + } + +} // class NoConverterFoundExceptionTest \ No newline at end of file Property changes on: libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/NoConverterFoundExceptionTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Modified: 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 2006-12-02 16:15:10 UTC (rev 242) +++ libs/argparser/trunk/src/test/net/sf/japi/io/args/converter/StringConverterTest.java 2006-12-02 19:01:29 UTC (rev 243) @@ -19,11 +19,20 @@ } /** + * Tests whether instanciating a StringConverter works. + * @throws Exception (unexpected) + */ + @Test + public void testStringConverter() throws Exception { + // Tested by this test class's superclass constructor. + } + + /** * Tests whether {@link StringConverter} has a public default constructor. * @throws Exception In case of unexpected errors. */ @Test - public void testOperation() throws Exception { + public void testConvert() throws Exception { final String foo = converter.convert("foo"); Assert.assertEquals("StringConverter.convert(\"foo\") must return \"foo\".", "foo", foo); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |