Search in sources :

Example 41 with DbObject

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

the class CsvMapperBuilderTest method testMapDbObjectWithColumnIndex.

@Test
public void testMapDbObjectWithColumnIndex() throws Exception {
    CsvMapperBuilder<DbObject> builder = csvMapperFactory.newBuilder(DbObject.class);
    builder.addMapping("email", 2);
    CsvMapper<DbObject> mapper = builder.mapper();
    List<DbObject> list = mapper.forEach(CsvMapperImplTest.dbObjectCsvReader(), new ListCollector<DbObject>()).getList();
    assertEquals(1, list.size());
    DbObject o = list.get(0);
    assertEquals(0, o.getId());
    assertNull(o.getName());
    assertEquals("name1@mail.com", o.getEmail());
    assertNull(o.getCreationTime());
    assertNull(o.getTypeName());
    assertNull(o.getTypeOrdinal());
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 42 with DbObject

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

the class CsvMapperBuilderTest method testMapDbObjectWithWrongColumnStillMapGoodOne.

@Test
public void testMapDbObjectWithWrongColumnStillMapGoodOne() throws Exception {
    CsvMapperBuilder<DbObject> builder = csvMapperFactory.mapperBuilderErrorHandler(MapperBuilderErrorHandler.NULL).newBuilder(DbObject.class);
    builder.addMapping("no_id");
    builder.addMapping("no_name");
    builder.addMapping("email");
    CsvMapper<DbObject> mapper = builder.mapper();
    List<DbObject> list = mapper.forEach(CsvMapperImplTest.dbObjectCsvReader(), new ListCollector<DbObject>()).getList();
    assertEquals(1, list.size());
    DbObject o = list.get(0);
    assertEquals(0, o.getId());
    assertNull(o.getName());
    assertEquals("name1@mail.com", o.getEmail());
    assertNull(o.getCreationTime());
    assertNull(o.getTypeName());
    assertNull(o.getTypeOrdinal());
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 43 with DbObject

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

the class CsvMapperCustomReaderFactoryTest method testCustomReaderFactory.

@Test
public void testCustomReaderFactory() throws IOException {
    CsvMapper<DbObject> mapper = CsvMapperFactory.newInstance().failOnAsm(true).cellValueReaderFactory(new CellValueReaderFactory() {

        @Override
        public <P> CellValueReader<P> getReader(Type propertyType, int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder builder) {
            return new CellValueReader<P>() {

                @SuppressWarnings("unchecked")
                @Override
                public P read(char[] chars, int offset, int length, ParsingContext parsingContext) {
                    return (P) "Hello!";
                }
            };
        }
    }).newBuilder(DbObject.class).addMapping("name").mapper();
    DbObject bop = mapper.iterator(new StringReader("bop")).next();
    assertEquals("Hello!", bop.getName());
}
Also used : Type(java.lang.reflect.Type) ParsingContext(org.simpleflatmapper.csv.ParsingContext) ParsingContextFactoryBuilder(org.simpleflatmapper.csv.ParsingContextFactoryBuilder) DbObject(org.simpleflatmapper.test.beans.DbObject) CellValueReader(org.simpleflatmapper.csv.CellValueReader) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) StringReader(java.io.StringReader) CsvColumnDefinition(org.simpleflatmapper.csv.CsvColumnDefinition) Test(org.junit.Test)

Example 44 with DbObject

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

the class PostgresqlCrudTest method testBatchUpsertOnDb.

@Test
public void testBatchUpsertOnDb() throws SQLException {
    Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
    if (connection == null) {
        System.err.println("Db Postgresql not available");
        return;
    }
    System.out.println("connection = " + connection.getMetaData().getDatabaseMajorVersion() + "." + connection.getMetaData().getDatabaseMinorVersion());
    if (connection.getMetaData().getDatabaseMajorVersion() != 9 || connection.getMetaData().getDatabaseMinorVersion() < 5) {
        System.err.println("Db Postgresql 9.5 not available");
        return;
    }
    try {
        Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT");
        DbObject object1 = DbObject.newInstance();
        DbObject object2 = DbObject.newInstance();
        objectCrud.create(connection, object1);
        object1.setName("BatchUpdate");
        object2.setName("BatchUpdate");
        objectCrud.createOrUpdate(connection, Arrays.<DbObject>asList(object1, object2));
        ResultSet resultSet = connection.createStatement().executeQuery("SELECT count(*) FROM TEST_DB_OBJECT WHERE name = 'BatchUpdate'");
        assertTrue(resultSet.next());
        assertEquals(2, resultSet.getInt(1));
        assertEquals(object1, objectCrud.read(connection, object1.getId()));
        assertEquals(object2, objectCrud.read(connection, object2.getId()));
    } finally {
        connection.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 45 with DbObject

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

the class QueryPreparerBuilderTest method testMapDbObjectToStatement.

@Test
public void testMapDbObjectToStatement() throws Exception {
    Mapper<DbObject, PreparedStatement> mapper = JdbcMapperFactory.newInstance().buildFrom(DbObject.class).addColumn("id").addColumn("name").addColumn("email").addColumn("creation_time").mapper();
    PreparedStatement ps = mock(PreparedStatement.class);
    DbObject dbObject = new DbObject();
    dbObject.setId(123);
    dbObject.setName("name");
    dbObject.setEmail("email");
    dbObject.setCreationTime(new Date());
    mapper.mapTo(dbObject, ps, null);
    verify(ps).setLong(1, 123);
    verify(ps).setString(2, "name");
    verify(ps).setString(3, "email");
    verify(ps).setTimestamp(4, new Timestamp(dbObject.getCreationTime().getTime()));
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date) 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