diff --git a/common/source/java/ch/systemsx/cisd/common/collections/ToStringDefaultConverter.java b/common/source/java/ch/systemsx/cisd/common/collections/ToStringDefaultConverter.java index 4d0eed326aeaf0de75538317d0cc5f62d44cd76b..04579c54ef5bff95cc2b741abd7895db819500aa 100644 --- a/common/source/java/ch/systemsx/cisd/common/collections/ToStringDefaultConverter.java +++ b/common/source/java/ch/systemsx/cisd/common/collections/ToStringDefaultConverter.java @@ -45,7 +45,7 @@ public final class ToStringDefaultConverter implements IToStringConverter<Object public final String toString(final Object value) { - return value.toString(); + return String.valueOf(value); } } 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 12a48b661731f8f3ca8bab9c32547ccaee030f2e..9f1190f7ca81c4e3450ee2530a3fff808e742dcd 100644 --- a/common/source/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactory.java +++ b/common/source/java/ch/systemsx/cisd/common/parser/AbstractParserObjectFactory.java @@ -174,6 +174,7 @@ public abstract class AbstractParserObjectFactory<E> implements IParserObjectFac public E createObject(final String[] lineTokens) throws ParserException { + assert lineTokens != null : "Unspecified line tokens"; final E object = ClassUtils.createInstance(beanClass); for (final Map.Entry<String, Method> entry : beanAnalyzer.getLabelToWriteMethods() .entrySet()) diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverter.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverter.java index 9d6cb9078871d019fe973fd0a1cca71d7a735b52..d3244d65649dec619da597e6c365c2e2e4e77ac9 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverter.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverter.java @@ -91,7 +91,7 @@ public final class EntityPropertiesConverter implements IEntityPropertiesConvert .listEntityTypes(), KeyExtractorFactory .getEntityTypeByCodeKeyExtractor()); } - final EntityTypePE entityType = entityTypesByCode.tryGet(entityTypeCode); + final EntityTypePE entityType = entityTypesByCode.tryGet(entityTypeCode.toUpperCase()); if (entityType == null) { throw UserFailureException.fromTemplate("Entity type with code '%s' does not exist!", diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverterTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverterTest.java index 1fb456e5888b5f4d277ec03db99b33b636f32499..d8606a18e235efcf15bcc764a76a7be13b1af04e 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverterTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverterTest.java @@ -127,14 +127,19 @@ public final class EntityPropertiesConverterTest return sampleType; } - private final static SampleProperty createVarcharSampleProperty() + private final static SampleProperty createVarcharSampleProperty(final boolean lowerCase) { final SampleProperty sampleProperty = new SampleProperty(); sampleProperty.setValue("blue"); final SampleTypePropertyType sampleTypePropertyType = new SampleTypePropertyType(); final PropertyType propertyType = new PropertyType(); - propertyType.setLabel(VARCHAR_PROPERTY_TYPE_CODE); - propertyType.setCode(VARCHAR_PROPERTY_TYPE_CODE); + String code = VARCHAR_PROPERTY_TYPE_CODE; + if (lowerCase) + { + code = code.toLowerCase(); + } + propertyType.setLabel(code); + propertyType.setCode(code); final DataType dataType = new DataType(); dataType.setCode(DataTypeCode.VARCHAR); propertyType.setDataType(dataType); @@ -143,10 +148,10 @@ public final class EntityPropertiesConverterTest return sampleProperty; } - private final SampleProperty[] createSampleProperties() + private final SampleProperty[] createSampleProperties(final boolean lowerCase) { return new SampleProperty[] - { createVarcharSampleProperty() }; + { createVarcharSampleProperty(lowerCase) }; } @Test @@ -196,10 +201,35 @@ public final class EntityPropertiesConverterTest one(propertyValueValidator).validatePropertyValue(propertyTypePE, "blue"); } }); - final SampleProperty[] properties = createSampleProperties(); + final SampleProperty[] properties = createSampleProperties(false); final List<EntityPropertyPE> convertedProperties = entityPropertiesConverter.convertProperties(properties, SAMPLE_TYPE_CODE, ManagerTestTool.EXAMPLE_PERSON); assertEquals(1, convertedProperties.size()); } + + @Test + public final void testConvertPropertiesWithLowerCase() + { + final IEntityPropertiesConverter entityPropertiesConverter = + createEntityPropertiesConverter(EntityKind.SAMPLE); + final PropertyTypePE propertyTypePE = new PropertyTypePE(); + propertyTypePE.setCode(VARCHAR_PROPERTY_TYPE_CODE.toLowerCase()); + context.checking(new Expectations() + { + { + prepareForConvertion(this); + + one(propertyTypeDAO).tryFindPropertyTypeByCode(VARCHAR_PROPERTY_TYPE_CODE); + will(returnValue(propertyTypePE)); + + one(propertyValueValidator).validatePropertyValue(propertyTypePE, "blue"); + } + }); + final SampleProperty[] properties = createSampleProperties(true); + final List<EntityPropertyPE> convertedProperties = + entityPropertiesConverter.convertProperties(properties, SAMPLE_TYPE_CODE + .toLowerCase(), ManagerTestTool.EXAMPLE_PERSON); + assertEquals(1, convertedProperties.size()); + } } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/NewSampleParserObjectFactoryTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/NewSampleParserObjectFactoryTest.java index d00c2ec26e51c5e5a6c03fefa0d156488b95ac67..12e761c55c40238f004fdec6da740fc242953e3c 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/NewSampleParserObjectFactoryTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/NewSampleParserObjectFactoryTest.java @@ -16,6 +16,21 @@ package ch.systemsx.cisd.openbis.plugin.generic.client.web.server; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.fail; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.parser.DefaultPropertyMapper; +import ch.systemsx.cisd.common.parser.IPropertyMapper; +import ch.systemsx.cisd.openbis.generic.client.web.client.dto.NewSample; +import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SampleProperty; +import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SampleType; + /** * Test cases for corresponding {@link NewSampleParserObjectFactory} class. * @@ -24,4 +39,82 @@ package ch.systemsx.cisd.openbis.plugin.generic.client.web.server; public final class NewSampleParserObjectFactoryTest { + private final static NewSampleParserObjectFactory createNewSampleParserObjectFactory() + { + final SampleType sampleType = new SampleType(); + sampleType.setCode("SAMPLE_TYPE"); + final NewSampleParserObjectFactory parserObjectFactory = + new NewSampleParserObjectFactory(sampleType, createPropertyMapper()); + return parserObjectFactory; + } + + private final static IPropertyMapper createPropertyMapper() + { + final String[] properties = new String[] + { "identifier", "container", "parent", "prop1", "prop2" }; + final DefaultPropertyMapper propertyMapper = new DefaultPropertyMapper(properties); + return propertyMapper; + } + + @Test + public final void testCreateObjectWithNullOrNotEnoughColumns() + { + final NewSampleParserObjectFactory parserObjectFactory = + createNewSampleParserObjectFactory(); + boolean fail = true; + try + { + parserObjectFactory.createObject(null); + } catch (AssertionError e) + { + fail = false; + } + assertFalse(fail); + try + { + parserObjectFactory.createObject(ArrayUtils.EMPTY_STRING_ARRAY); + fail(String.format("'%s' expected.", + ch.systemsx.cisd.common.parser.IndexOutOfBoundsException.class)); + } catch (final ch.systemsx.cisd.common.parser.IndexOutOfBoundsException ex) + { + // Nothing to do here. + } + } + + @SuppressWarnings("unused") + @DataProvider + private final static Object[][] getLineTokens() + { + return new Object[][] + { + { new String[] + { "", "", "", "", "" }, 0 }, + { new String[] + { null, null, null, null, null }, 0 }, + + { new String[] + { "id1", "cont1", "par1", "1", "hello" }, 2 }, + + }; + } + + @Test(dataProvider = "getLineTokens") + public final void testCreateObject(final String[] lineTokens, final int numberOfProperties) + { + final NewSampleParserObjectFactory parserObjectFactory = + createNewSampleParserObjectFactory(); + final NewSample objectCreated = parserObjectFactory.createObject(lineTokens); + assertEquals(objectCreated.getIdentifier(), lineTokens[0]); + assertEquals(objectCreated.getContainerIdentifier(), + StringUtils.isEmpty(lineTokens[1]) ? null : lineTokens[1]); + assertEquals(objectCreated.getParentIdentifier(), StringUtils.isEmpty(lineTokens[2]) ? null + : lineTokens[2]); + final SampleProperty[] properties = objectCreated.getProperties(); + assertEquals(numberOfProperties, properties.length); + int index = 3; + for (final SampleProperty sampleProperty : properties) + { + sampleProperty.getValue().equals(lineTokens[index++]); + } + } }