Search in sources :

Example 1 with MappingException

use of org.simpleflatmapper.map.MappingException in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilderTest method testFieldMapperErrorHandler.

@Test
public void testFieldMapperErrorHandler() throws Exception {
    ConstantSourceMapperBuilder<Object, MyObjectWithInner, SampleFieldKey> constantSourceMapperBuilder = new ConstantSourceMapperBuilder<Object, MyObjectWithInner, SampleFieldKey>(mapperSource, classMeta, MapperConfig.config(new IdentityFieldMapperColumnDefinitionProvider<SampleFieldKey>()).fieldMapperErrorHandler(new FieldMapperErrorHandler<SampleFieldKey>() {

        @Override
        public void errorMappingField(SampleFieldKey key, Object source, Object target, Exception error) throws MappingException {
        }
    }), new MappingContextFactoryBuilder<Object, SampleFieldKey>(null), SampleFieldKey.KEY_FACTORY);
    constantSourceMapperBuilder.addMapping(new SampleFieldKey("prop_date", 16), FieldMapperColumnDefinition.<SampleFieldKey>identity());
    Mapper<Object, MyObjectWithInner> mapper = constantSourceMapperBuilder.mapper();
    MyObjectWithInner o = mapper.map(null);
    System.out.println("mapper = " + mapper);
    assertNull(o.prop);
}
Also used : SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) FieldMapperErrorHandler(org.simpleflatmapper.map.FieldMapperErrorHandler) ConstantSourceMapperBuilder(org.simpleflatmapper.map.mapper.ConstantSourceMapperBuilder) MappingException(org.simpleflatmapper.map.MappingException) Test(org.junit.Test)

Example 2 with MappingException

use of org.simpleflatmapper.map.MappingException in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperFactoryTest method testErrorHandlers.

@Test
public void testErrorHandlers() {
    MapperFactory mapperFactory = new MapperFactory();
    FieldMapperErrorHandler<SampleFieldKey> fieldMapperErrorHandler = new FieldMapperErrorHandler<SampleFieldKey>() {

        @Override
        public void errorMappingField(SampleFieldKey key, Object source, Object target, Exception error) throws MappingException {
        }
    };
    MapperBuilderErrorHandler mapperBuilderErrorHandler = new MapperBuilderErrorHandler() {

        @Override
        public void accessorNotFound(String msg) {
        }

        @Override
        public void propertyNotFound(Type target, String property) {
        }

        @Override
        public void customFieldError(FieldKey<?> key, String message) {
        }
    };
    ConsumerErrorHandler consumerErrorHandler = new ConsumerErrorHandler() {

        @Override
        public void handlerError(Throwable error, Object target) {
        }
    };
    mapperFactory.fieldMapperErrorHandler(fieldMapperErrorHandler);
    mapperFactory.mapperBuilderErrorHandler(mapperBuilderErrorHandler);
    mapperFactory.consumerErrorHandler(consumerErrorHandler);
    MapperConfig<SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> mapperConfig = mapperFactory.mapperConfig();
    assertEquals(fieldMapperErrorHandler, mapperConfig.fieldMapperErrorHandler());
    assertEquals(mapperBuilderErrorHandler, mapperConfig.mapperBuilderErrorHandler());
    assertEquals(consumerErrorHandler, mapperConfig.consumerErrorHandler());
    Assert.assertEquals(consumerErrorHandler, mapperFactory.consumerErrorHandler());
    mapperFactory = new MapperFactory();
    assertTrue(mapperFactory.ignorePropertyNotFound().mapperConfig().mapperBuilderErrorHandler() instanceof IgnoreMapperBuilderErrorHandler);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) RethrowMapperBuilderErrorHandler(org.simpleflatmapper.map.error.RethrowMapperBuilderErrorHandler) IgnoreMapperBuilderErrorHandler(org.simpleflatmapper.map.IgnoreMapperBuilderErrorHandler) MapperBuilderErrorHandler(org.simpleflatmapper.map.MapperBuilderErrorHandler) IgnoreMapperBuilderErrorHandler(org.simpleflatmapper.map.IgnoreMapperBuilderErrorHandler) MappingException(org.simpleflatmapper.map.MappingException) Type(java.lang.reflect.Type) RethrowConsumerErrorHandler(org.simpleflatmapper.map.error.RethrowConsumerErrorHandler) ConsumerErrorHandler(org.simpleflatmapper.map.ConsumerErrorHandler) AbstractMapperFactory(org.simpleflatmapper.map.mapper.AbstractMapperFactory) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) FieldMapperErrorHandler(org.simpleflatmapper.map.FieldMapperErrorHandler) FieldKey(org.simpleflatmapper.map.FieldKey) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) Test(org.junit.Test)

Example 3 with MappingException

use of org.simpleflatmapper.map.MappingException in project SimpleFlatMapper by arnaudroger.

the class DiscriminatorMapperTest method setUp.

@Before
public void setUp() {
    Mapper<Object[], Person> studentMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            StudentGS studentGS = new StudentGS();
            try {
                mapTo(source, studentGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return studentGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            StudentGS studentGS = (StudentGS) target;
            studentGS.setName((String) source[1]);
            studentGS.setId((Integer) source[2]);
        }
    };
    Mapper<Object[], Person> professorMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            ProfessorGS professorGS = new ProfessorGS();
            try {
                mapTo(source, professorGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return professorGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            ProfessorGS professorGS = (ProfessorGS) target;
            professorGS.setName((String) source[1]);
            professorGS.setId((Integer) source[2]);
        }
    };
    List<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>> mappers = new ArrayList<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>>();
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "student".equals(objects[0]);
        }
    }, studentMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "professor".equals(objects[0]);
        }
    }, professorMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mapper = new DiscriminatorMapper<Object[], Object[][], Person, RuntimeException>(mappers, new UnaryFactory<Object[][], Enumarable<Object[]>>() {

        @Override
        public Enumarable<Object[]> newInstance(Object[][] objects) {
            return new ArrayEnumarable<Object[]>(objects);
        }
    }, new UncheckedConverter<Object[], String>() {

        @Override
        public String convert(Object[] in) {
            return Arrays.toString(in);
        }
    }, RethrowConsumerErrorHandler.INSTANCE);
}
Also used : ArrayList(java.util.ArrayList) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) MappingContextFactoryFromRows(org.simpleflatmapper.map.context.MappingContextFactoryFromRows) MappingException(org.simpleflatmapper.map.MappingException) Predicate(org.simpleflatmapper.util.Predicate) MappingContext(org.simpleflatmapper.map.MappingContext) ProfessorGS(org.simpleflatmapper.test.beans.ProfessorGS) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) Mapper(org.simpleflatmapper.map.Mapper) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) UncheckedConverter(org.simpleflatmapper.converter.UncheckedConverter) Person(org.simpleflatmapper.test.beans.Person) ArrayEnumarable(org.simpleflatmapper.util.ArrayEnumarable) StudentGS(org.simpleflatmapper.test.beans.StudentGS) Before(org.junit.Before)

Example 4 with MappingException

use of org.simpleflatmapper.map.MappingException in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperCustomMappingTest method testCustomMapping.

@Test
public void testCustomMapping() throws SQLException, Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactoryHelper.asm();
    mapperFactory.addCustomFieldMapper("id", new FieldMapper<ResultSet, DbObject>() {

        @Override
        public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> mappingContext) throws MappingException {
            target.setId(1);
        }
    });
    final JdbcMapper<DbObject> mapper = mapperFactory.newMapper(DbObject.class);
    DbHelper.testQuery(new TestRowHandler<PreparedStatement>() {

        @Override
        public void handle(PreparedStatement t) throws Exception {
            ResultSet r = t.executeQuery();
            r.next();
            DbHelper.assertDbObjectMapping(mapper.map(r));
        }
    }, DbHelper.TEST_DB_OBJECT_QUERY.replace("id,", "33 as id,"));
}
Also used : JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) DbObject(org.simpleflatmapper.test.beans.DbObject) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MappingException(org.simpleflatmapper.map.MappingException) SQLException(java.sql.SQLException) MappingException(org.simpleflatmapper.map.MappingException) Test(org.junit.Test)

Example 5 with MappingException

use of org.simpleflatmapper.map.MappingException in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperCustomMappingTest method testCustomMappingStatic.

@Test
public void testCustomMappingStatic() throws SQLException, Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactoryHelper.asm();
    mapperFactory.addCustomFieldMapper("id", new FieldMapper<ResultSet, DbObject>() {

        @Override
        public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> mappingContext) throws MappingException {
            target.setId(1);
        }
    });
    final JdbcMapper<DbObject> mapper = JdbcMapperDbObjectTest.addColumn(mapperFactory.newBuilder(DbObject.class)).mapper();
    DbHelper.testQuery(new TestRowHandler<PreparedStatement>() {

        @Override
        public void handle(PreparedStatement t) throws Exception {
            ResultSet r = t.executeQuery();
            r.next();
            DbHelper.assertDbObjectMapping(mapper.map(r));
        }
    }, DbHelper.TEST_DB_OBJECT_QUERY.replace("id,", "33 as id,"));
}
Also used : JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) DbObject(org.simpleflatmapper.test.beans.DbObject) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MappingException(org.simpleflatmapper.map.MappingException) SQLException(java.sql.SQLException) MappingException(org.simpleflatmapper.map.MappingException) Test(org.junit.Test)

Aggregations

MappingException (org.simpleflatmapper.map.MappingException)6 Test (org.junit.Test)5 ResultSet (java.sql.ResultSet)3 DbObject (org.simpleflatmapper.test.beans.DbObject)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)2 FieldMapperErrorHandler (org.simpleflatmapper.map.FieldMapperErrorHandler)2 Mapper (org.simpleflatmapper.map.Mapper)2 MappingContext (org.simpleflatmapper.map.MappingContext)2 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)2 UnaryFactory (org.simpleflatmapper.util.UnaryFactory)2 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 UncheckedConverter (org.simpleflatmapper.converter.UncheckedConverter)1 SfmResultSetMapperFactory (org.simpleflatmapper.jdbi.SfmResultSetMapperFactory)1 ConsumerErrorHandler (org.simpleflatmapper.map.ConsumerErrorHandler)1 FieldKey (org.simpleflatmapper.map.FieldKey)1 IgnoreMapperBuilderErrorHandler (org.simpleflatmapper.map.IgnoreMapperBuilderErrorHandler)1