diff --git a/common/source/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactory.java b/common/source/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactory.java index c9c464a26128202fb425889bdb325f97b73360e9..4b38e3056419f92b642064783fb67e4178371974 100644 --- a/common/source/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactory.java +++ b/common/source/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactory.java @@ -62,6 +62,8 @@ public abstract class AbstractParserObjectFactory<E> implements IParserObjectFac protected AbstractParserObjectFactory(Class<E> beanClass, IPropertyMapper propertyMapper) { + assert beanClass != null; + assert propertyMapper != null; propertyDescriptors = BeanUtils.getPropertyDescriptors(beanClass); mandatoryFields = createMandatoryFields(beanClass); checkPropertyMapper(beanClass, propertyMapper); @@ -75,10 +77,21 @@ public abstract class AbstractParserObjectFactory<E> implements IParserObjectFac return new ConverterPool(); } - /** Registers given <var>converter</var> for given class type. */ + /** + * Registers given <var>converter</var> for given class type. + * <p> + * If given <var>converter</var>, then it will unregister it. + * </p> + */ protected final <T> void registerConverter(Class<T> clazz, Converter<T> converter) { - converterPool.registerConverter(clazz, converter); + if (converter == null) + { + converterPool.unregisterConverter(clazz); + } else + { + converterPool.registerConverter(clazz, converter); + } } private final <T> T convert(String value, Class<T> type) @@ -107,8 +120,6 @@ public abstract class AbstractParserObjectFactory<E> implements IParserObjectFac */ private final void checkPropertyMapper(Class<E> clazz, IPropertyMapper propMapper) throws UserFailureException { - assert propMapper != null; - assert clazz != null; assert propertyDescriptors != null; Set<String> propertyNames = new HashSet<String>(propMapper.getAllPropertyNames()); diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactoryTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactoryTest.java index f2ca242f0e901ca39f8dfa32512cfc978f547b51..ca8b98d32667893a6d0794863a0a75dce0fc7348 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactoryTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactoryTest.java @@ -16,6 +16,15 @@ package ch.systemsx.cisd.common.parser; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.fail; + +import org.apache.commons.lang.ArrayUtils; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.annotation.Mandatory; +import ch.systemsx.cisd.common.exceptions.UserFailureException; + /** * Test cases for corresponding {@link AbstractParserObjectFactory} class. * @@ -24,4 +33,159 @@ package ch.systemsx.cisd.common.parser; public final class AbstractParserObjectFactoryTest { + private final static IPropertyMapper createPropertyMapper() + { + return new HeaderFilePropertyMapper(new String[] + { "Name", "Description", "Number" }); + } + + private final static String[] createDefaultLineTokens() + { + return new String[] + { "Bean Name", "Bean Description", "1" }; + } + + private final void checkBean(Bean bean) + { + assertEquals("Bean Name", bean.name); + assertEquals("Bean Description", bean.description); + assertEquals(1, bean.number); + } + + @Test(expectedExceptions = AssertionError.class) + public final void testConstructorWithNull() + { + new BeanFactory(null, null); + } + + @Test + public final void testPropertyMapperWithNoExperimentProperties() + { + IPropertyMapper propertyMapper = new HeaderFilePropertyMapper(new String[] + { "Name", "Description", "IsNotIn" }); + try + { + new BeanFactory(Bean.class, propertyMapper); + fail("Following properties '[isnotin]' are not part of 'Bean'."); + } catch (UserFailureException ex) + { + assertEquals("Following properties '[isnotin]' are not part of 'Bean'.", ex.getMessage()); + } + } + + @Test + public final void testMandatoryFields() + { + HeaderFilePropertyMapper propertyMapper = new HeaderFilePropertyMapper(new String[] + { "description" }); + try + { + BeanFactory beanFactory = new BeanFactory(Bean.class, propertyMapper); + String[] lineTokens = new String[] + { "1. experiment" }; + beanFactory.createObject(lineTokens); + fail("Field/Property name 'name' is mandatory."); + } catch (UserFailureException ex) + { + assertEquals("Field/Property name 'name' is mandatory.", ex.getMessage()); + } + } + + @Test + public final void testTooManyDataColumns() + { + IPropertyMapper propertyMapper = createPropertyMapper(); + BeanFactory beanFactory = new BeanFactory(Bean.class, propertyMapper); + String[] lineTokens = (String[]) ArrayUtils.add(createDefaultLineTokens(), "notUsed"); + Bean bean = beanFactory.createObject(lineTokens); + checkBean(bean); + } + + @Test + public final void testNotEnoughDataColumns() + { + IPropertyMapper propertyMapper = createPropertyMapper(); + BeanFactory beanFactory = new BeanFactory(Bean.class, propertyMapper); + String[] defaultTokens = createDefaultLineTokens(); + String[] lineTokens = (String[]) ArrayUtils.remove(defaultTokens, defaultTokens.length - 1); + String msg = String.format("Not enough tokens are available (index: 2, available: 2)", 5, lineTokens.length); + try + { + beanFactory.createObject(lineTokens); + fail(msg); + } catch (UserFailureException ex) + { + assertEquals(msg, ex.getMessage()); + } + } + + @Test + public final void testRegisterConverterWithNull() + { + IPropertyMapper propertyMapper = createPropertyMapper(); + BeanFactory beanFactory = new BeanFactory(Bean.class, propertyMapper); + try + { + beanFactory.registerConverter(null, null); + fail("Null type is not allowed."); + } catch (AssertionError ex) + { + // Nothing to do here. + } + beanFactory.registerConverter(String.class, null); + } + + // + // Helper Classes + // + + private final static class BeanFactory extends AbstractParserObjectFactory<Bean> + { + + BeanFactory(Class<Bean> clazz, IPropertyMapper propertyMapper) + { + super(clazz, propertyMapper); + } + + } + + public final static class Bean + { + @Mandatory + private String name; + + private String description; + + private int number; + + public final String getName() + { + return name; + } + + public final void setName(String name) + { + this.name = name; + } + + public final int getNumber() + { + return number; + } + + public final void setNumber(int number) + { + this.number = number; + } + + public final String getDescription() + { + return description; + } + + public final void setDescription(String description) + { + this.description = description; + } + } } \ No newline at end of file diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/HeaderFilePropertyMapperTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/parser/HeaderFilePropertyMapperTest.java index c79659897f36fa2aa8800a6a5517436d76598b17..ae31bd4a0d12fd87c0ff1068c4b343038c3fa0d5 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/parser/HeaderFilePropertyMapperTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/parser/HeaderFilePropertyMapperTest.java @@ -17,6 +17,10 @@ package ch.systemsx.cisd.common.parser; import static org.testng.AssertJUnit.*; + +import java.util.ArrayList; +import java.util.List; + import org.testng.annotations.Test; /** @@ -27,12 +31,12 @@ import org.testng.annotations.Test; public final class HeaderFilePropertyMapperTest { - @Test(expectedExceptions=AssertionError.class) + @Test(expectedExceptions = AssertionError.class) public final void testNullHeaders() { new HeaderFilePropertyMapper(null); } - + @Test public final void testGetProperty() { @@ -44,4 +48,18 @@ public final class HeaderFilePropertyMapperTest assertTrue(propertyMapper.getProperty("firstName").getColumn() == 0); assertTrue(propertyMapper.getProperty("address").getColumn() == 2); } + + @Test + public final void testCasePropertyMapper() + { + HeaderFilePropertyMapper propertyMapper = new HeaderFilePropertyMapper(new String[] + { "Code", "Description", "RegistrationTimestamp" }); + List<String> properties = new ArrayList<String>(propertyMapper.getAllPropertyNames()); + assertTrue(properties.indexOf("Code") < 0); + assertTrue(properties.indexOf("code") > -1); + assertTrue(properties.indexOf("Description") < 0); + assertTrue(properties.indexOf("description") > -1); + assertTrue(properties.indexOf("RegistrationTimestamp") < 0); + assertTrue(properties.indexOf("registrationtimestamp") > -1); + } }