From aafdc9b3165d26d14e8dc030331696fd43b9300f Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Tue, 7 Aug 2007 10:59:15 +0000 Subject: [PATCH] add: - Unit test for 'ConverterPool' - default conversion for predefined primitive resp. java types remove: - 'IdentityStringConverter' no longer needed SVN: 1254 --- .../cisd/common/converter/ConverterPool.java | 75 ++++++-- .../converter/IdentityStringConverter.java | 57 ------ .../common/converter/ConverterPoolTest.java | 162 ++++++++++++++++++ 3 files changed, 226 insertions(+), 68 deletions(-) delete mode 100644 common/source/java/ch/systemsx/cisd/common/converter/IdentityStringConverter.java create mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/converter/ConverterPoolTest.java diff --git a/common/source/java/ch/systemsx/cisd/common/converter/ConverterPool.java b/common/source/java/ch/systemsx/cisd/common/converter/ConverterPool.java index 05a659b2a8b..0b63d2a97ca 100644 --- a/common/source/java/ch/systemsx/cisd/common/converter/ConverterPool.java +++ b/common/source/java/ch/systemsx/cisd/common/converter/ConverterPool.java @@ -42,9 +42,7 @@ public final class ConverterPool private final static Map<Class<?>, Converter<?>> createConverters() { - final Map<Class<?>, Converter<?>> converterMap = new HashMap<Class<?>, Converter<?>>(); - converterMap.put(String.class, new IdentityStringConverter()); - return converterMap; + return new HashMap<Class<?>, Converter<?>>(); } /** Returns <code>instance</code>. */ @@ -53,11 +51,60 @@ public final class ConverterPool return instance; } + /** + * Default conversion if no specific <code>Converter</code> has been defined. + */ + @SuppressWarnings("unchecked") + private final static <T> T defaultConvert(String value, Class<T> type) + { + if (value == null) + { + return null; + } + if (type.equals(Integer.class) || type.equals(Integer.TYPE)) + { + return (T) Integer.valueOf(value); + } else if (type.equals(Long.class) || type.equals(Long.TYPE)) + { + return (T) Long.valueOf(value); + } else if (type.equals(Float.class) || type.equals(Float.TYPE)) + { + return (T) Float.valueOf(value); + } else if (type.equals(Double.class) || type.equals(Double.TYPE)) + { + return (T) Double.valueOf(value); + } else if (type.equals(Byte.class) || type.equals(Byte.TYPE)) + { + return (T) Byte.valueOf(value); + } else if (type.equals(Short.class) || type.equals(Short.TYPE)) + { + return (T) Short.valueOf(value); + } else if (type.equals(Character.class) || type.equals(Character.TYPE)) + { + if (value.length() > 0) + { + return (T) Character.valueOf(value.charAt(0)); + } else + { + return (T) new Character('\0'); + } + } else if (type.equals(String.class)) + { + return (T) value; + } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) + { + return (T) Boolean.valueOf(value); + } + throw new IllegalArgumentException("No converter for type '" + type.getCanonicalName() + "'."); + } + /** * Registers given <code>Converter</code> for given <code>Class</code>. */ public final <T> void registerConverter(Class<T> type, Converter<T> converter) { + assert type != null; + assert converter != null; converters.put(type, converter); } @@ -66,31 +113,37 @@ public final class ConverterPool */ public final <T> void unregisterConverter(Class<T> type) { + assert type != null; converters.remove(type); } - /** + /** * Performs the conversion. * - * @throws IllegalArgumentException If there is no converter for <var>type</var>. + * @throws IllegalArgumentException If there is no converter for <var>type</var>. */ public final <T> T convert(String value, Class<T> type) { + assert type != null; final Converter<T> converter = getConverter(type); + T returned = null; if (converter == null) { - throw new IllegalArgumentException("No converter for type '" + type.getCanonicalName() + "'."); + returned = defaultConvert(value, type); + } else + { + returned = converter.convert(value); } - if (value == null) + if (returned == null && converter != null) { - converter.getDefaultValue(); + return converter.getDefaultValue(); } - return converter.convert(value); + return returned; } - // The setter registerConverter() will ensure that no converter can be entered that is of the wrong type. + // The setter registerConverter() will ensure that no converter can be entered that is of the wrong type. @SuppressWarnings("unchecked") - private final <T> Converter<T> getConverter(Class<T> type) + final <T> Converter<T> getConverter(Class<T> type) { return (Converter<T>) converters.get(type); } diff --git a/common/source/java/ch/systemsx/cisd/common/converter/IdentityStringConverter.java b/common/source/java/ch/systemsx/cisd/common/converter/IdentityStringConverter.java deleted file mode 100644 index 7617d7891ba..00000000000 --- a/common/source/java/ch/systemsx/cisd/common/converter/IdentityStringConverter.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2007 ETH Zuerich, CISD - * - * Licensed 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 ch.systemsx.cisd.common.converter; - -/** - * The identity converter for <code>String</code>s. - * - * @author Bernd Rinn - */ -public final class IdentityStringConverter implements Converter<String> -{ - - private final String defaultValue; - - public IdentityStringConverter() - { - this(null); - } - - public IdentityStringConverter(final String defaultValue) - { - this.defaultValue = defaultValue; - } - - // - // Converter - // - - public String convert(String value) - { - return value; - } - - /** - * This converter does not have a default value. - * - * @return <code>null</code> - */ - public String getDefaultValue() - { - return defaultValue; - } -} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/converter/ConverterPoolTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/converter/ConverterPoolTest.java new file mode 100644 index 00000000000..57cd517ff0b --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/converter/ConverterPoolTest.java @@ -0,0 +1,162 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * Licensed 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 ch.systemsx.cisd.common.converter; + +import java.util.Date; + +import org.testng.annotations.Test; +import static org.testng.AssertJUnit.*; + +/** + * Test cases for the {@link ConverterPool}. + * + * @author Christian Ribeaud + */ +public final class ConverterPoolTest +{ + + private static char NULL; + + @Test + public final void testRegisterConverter() + { + try + { + ConverterPool.getInstance().registerConverter(null, null); + fail("Null type is not allowed."); + } catch (AssertionError ex) + { + // Nothing to do here. + } + try + { + ConverterPool.getInstance().registerConverter(String.class, null); + fail("Null converter is not allowed."); + } catch (AssertionError ex) + { + // Nothing to do here. + } + assertNull(ConverterPool.getInstance().getConverter(String.class)); + assertNull(ConverterPool.getInstance().getConverter(Date.class)); + ConverterPool.getInstance().registerConverter(Date.class, new DateConverter("dd.MM.yyyy")); + assertNotNull(ConverterPool.getInstance().getConverter(Date.class)); + assertNull(ConverterPool.getInstance().getConverter(String.class)); + } + + @Test(dependsOnMethods = "testRegisterConverter") + public final void testUnRegisterConverter() + { + assertNotNull(ConverterPool.getInstance().getConverter(Date.class)); + try + { + ConverterPool.getInstance().unregisterConverter(null); + fail("Null type is not allowed."); + } catch (AssertionError ex) + { + // Nothing to do here. + } + ConverterPool.getInstance().unregisterConverter(Date.class); + assertNull(ConverterPool.getInstance().getConverter(Date.class)); + } + + @Test + public final void testConvert() + { + try + { + ConverterPool.getInstance().convert(null, null); + fail("Null type is not allowed."); + } catch (AssertionError ex) + { + // Nothing to do here. + } + ConverterPool pool = ConverterPool.getInstance(); + // String + assertNull(pool.convert(null, String.class)); + assertEquals("", pool.convert("", String.class)); + // Integer + assertEquals(new Integer(1), pool.convert("1", Integer.class)); + assertEquals(new Integer(1), pool.convert("1", Integer.TYPE)); + try + { + pool.convert("notParsable", Integer.class); + fail("Could not be parsed."); + } catch (NumberFormatException ex) + { + // Nothing to do here. + } + String msg = "No converter for type 'java.util.Date'."; + try + { + pool.convert("", Date.class); + fail(msg); + } catch (IllegalArgumentException ex) + { + assertEquals(msg, ex.getMessage()); + } + // Boolean + assertEquals(Boolean.FALSE, pool.convert("1", Boolean.class)); + assertEquals(Boolean.FALSE, pool.convert("1", Boolean.TYPE)); + assertEquals(Boolean.TRUE, pool.convert("TrUe", Boolean.TYPE)); + // Character + assertEquals(new Character('c'), pool.convert("c", Character.class)); + assertEquals(new Character('c'), pool.convert("c", Character.TYPE)); + assertEquals(new Character('c'), pool.convert("choubidou", Character.class)); + assertEquals(new Character(NULL), pool.convert("", Character.class)); + // Customized Integer converter + pool.registerConverter(Integer.class, new IntegerConverter()); + assertEquals(new Integer(13), pool.convert("1", Integer.class)); + assertEquals(new Integer(12), pool.convert("", Integer.class)); + try + { + assertEquals(new Integer(12), pool.convert("", Integer.TYPE)); + fail("Converter only registered for 'Integer.class' and not for 'Integer.TYPE'"); + } catch (NumberFormatException ex) + { + // Nothing to do here. + } + } + + // + // Helper Classes + // + + private final static class IntegerConverter implements Converter<Integer> + { + + // + // Converter + // + + public final Integer convert(String value) + { + try + { + Integer.parseInt(value); + return new Integer(13); + } catch (NumberFormatException ex) + { + return null; + } + } + + public final Integer getDefaultValue() + { + return new Integer(12); + } + } +} \ No newline at end of file -- GitLab