Search in sources :

Example 21 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperFactoryTest method testSetCheckedConsumerError.

@Test
public void testSetCheckedConsumerError() throws SQLException {
    ConsumerErrorHandler errorHandler = mock(ConsumerErrorHandler.class);
    ResultSet rs = mock(ResultSet.class);
    when(rs.next()).thenReturn(true, true, false);
    when(rs.getLong(1)).thenReturn(1l);
    final Exception exception = new SQLException("Error!");
    JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().consumerErrorHandler(errorHandler).newBuilder(DbObject.class).addMapping("id").mapper();
    mapper.forEach(rs, new CheckedConsumer<DbObject>() {

        @Override
        public void accept(DbObject dbObject) throws Exception {
            throw exception;
        }
    });
    verify(errorHandler, times(2)).handlerError(same(exception), any(DbObject.class));
}
Also used : ConsumerErrorHandler(org.simpleflatmapper.map.ConsumerErrorHandler) DbObject(org.simpleflatmapper.test.beans.DbObject) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 22 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperToStringTest method testDynamicJdbcMapperNoAsm.

@Test
public void testDynamicJdbcMapperNoAsm() throws SQLException {
    JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.noAsm().newMapper(DbObject.class);
    ResultSet rs = mock(ResultSet.class);
    ResultSetMetaData metaData = mock(ResultSetMetaData.class);
    when(rs.getMetaData()).thenReturn(metaData);
    when(metaData.getColumnCount()).thenReturn(1);
    when(metaData.getColumnLabel(1)).thenReturn("id");
    mapper.iterator(rs);
    assertNotNull(mapper.toString());
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) DbObject(org.simpleflatmapper.test.beans.DbObject) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 23 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperCustomMappingTest method testColumnAlias.

@Test
public void testColumnAlias() throws Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactoryHelper.asm();
    mapperFactory.addAlias("not_id_column", "id");
    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,", "id as not_id_column,"));
}
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) Test(org.junit.Test)

Example 24 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperErrorTest method testHandleMapperErrorSetterNotFound.

@Test
public void testHandleMapperErrorSetterNotFound() throws NoSuchMethodException, SecurityException, IOException {
    MapperBuilderErrorHandler errorHandler = mock(MapperBuilderErrorHandler.class);
    JdbcMapperBuilder<DbObject> builder = JdbcMapperFactoryHelper.asm().mapperBuilderErrorHandler(errorHandler).newBuilder(DbObject.class);
    builder.addMapping("id", 1);
    builder.addMapping("notthere1", 2);
    verify(errorHandler).propertyNotFound(DbObject.class, "notthere1");
    builder.addMapping("notthere3");
    verify(errorHandler).propertyNotFound(DbObject.class, "notthere3");
}
Also used : MapperBuilderErrorHandler(org.simpleflatmapper.map.MapperBuilderErrorHandler) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 25 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperErrorTest method testInstantiatorError.

@Test
public void testInstantiatorError() {
    MapperImpl<ResultSet, DbObject> mapper = new MapperImpl<ResultSet, DbObject>(null, null, new BiInstantiator<ResultSet, MappingContext<? super ResultSet>, DbObject>() {

        @Override
        public DbObject newInstance(ResultSet s, MappingContext<? super ResultSet> context) throws Exception {
            throw new IOException();
        }
    });
    try {
        mapper.map(null);
        fail("Expected error");
    } catch (Exception e) {
        assertTrue(e instanceof IOException);
    }
}
Also used : MappingContext(org.simpleflatmapper.map.MappingContext) MapperImpl(org.simpleflatmapper.map.mapper.MapperImpl) DbObject(org.simpleflatmapper.test.beans.DbObject) ResultSet(java.sql.ResultSet) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

DbObject (org.simpleflatmapper.test.beans.DbObject)83 Test (org.junit.Test)75 Connection (java.sql.Connection)27 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)10 CheckedConsumer (org.simpleflatmapper.util.CheckedConsumer)10 ArrayList (java.util.ArrayList)9 MappingException (org.simpleflatmapper.map.MappingException)6 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)6 Type (java.lang.reflect.Type)5 ParseException (java.text.ParseException)5 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)5 StringReader (java.io.StringReader)4 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)4 DbPartialFinalObject (org.simpleflatmapper.test.beans.DbPartialFinalObject)4 Statement (java.sql.Statement)3 MapperBuilderErrorHandler (org.simpleflatmapper.map.MapperBuilderErrorHandler)3 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)3 ListCollector (org.simpleflatmapper.util.ListCollector)3