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());
}
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());
}
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());
}
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();
}
}
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()));
}
Aggregations