use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class QueryPreparerTest method testQueryBinderClosePsOnException.
@Test
public void testQueryBinderClosePsOnException() throws SQLException {
NamedSqlQuery query = NamedSqlQuery.parse("select id, name, email, creation_time, type_ordinal, type_name from TEST_DB_OBJECT where id = ? ");
QueryPreparer<DbObject> queryPreparer = jdbcMapperFactory.from(DbObject.class).to(query);
Connection conn = mock(Connection.class);
PreparedStatement ps = mock(PreparedStatement.class);
when(conn.prepareStatement(anyString())).thenReturn(ps);
doThrow(new IllegalStateException()).when(ps).setLong(anyInt(), anyLong());
try {
queryPreparer.prepare(conn).bind(DbObject.newInstance());
fail();
} catch (IllegalStateException e) {
// expected
}
verify(ps).close();
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class QueryPreparerTest method testInsertAndSelectWithPreparedStatementMapper.
@Test
public void testInsertAndSelectWithPreparedStatementMapper() throws SQLException {
NamedSqlQuery insertQuery = NamedSqlQuery.parse("INSERT INTO test_db_object(id, name, email, creation_time, type_ordinal, type_name) values(?, ?, ?, ?, ?, ?) ");
NamedSqlQuery selectQuery = NamedSqlQuery.parse("select id, name, email, creation_time, type_ordinal, type_name from TEST_DB_OBJECT where id = ? ");
QueryPreparer<DbObject> insertQueryPreparer = jdbcMapperFactory.from(DbObject.class).to(insertQuery);
QueryPreparer<DbObject> selectQueryPreparer = jdbcMapperFactory.from(DbObject.class).to(selectQuery);
DbObject dbObject = DbObject.newInstance();
Connection connection = DbHelper.objectDb();
try {
PreparedStatement ps = insertQueryPreparer.prepare(connection).bind(dbObject);
try {
ps.execute();
} finally {
ps.close();
}
final QueryBinder<DbObject> queryBinder = selectQueryPreparer.prepare(connection);
ps = selectQueryPreparer.prepareStatement(connection);
try {
queryBinder.bindTo(dbObject, ps);
ResultSet resultSet = ps.executeQuery();
assertTrue(resultSet.next());
assertEquals(dbObject, JdbcMapperFactory.newInstance().newMapper(DbObject.class).map(resultSet));
} finally {
ps.close();
}
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class ConnectedCrudTest method testDbObjectCrud.
@Test
public void testDbObjectCrud() throws Exception {
DataSource dataSource = DbHelper.getHsqlDataSource();
ConnectedCrud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(dataSource, "TEST_DB_OBJECT");
checkCrudDbObject(objectCrud, DbObject.newInstance());
Connection connection = dataSource.getConnection();
try {
CrudTest.checkCrudDbObject(connection, objectCrud.crud(), DbObject.newInstance());
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class CrudTest method testCompositeKey.
@Test
public void testCompositeKey() throws SQLException {
Connection connection = DbHelper.getDbConnection(targetDB);
if (connection == null) {
System.err.println("Db " + targetDB + " not available");
return;
}
try {
Crud<DbObject, CKEY> objectCrud = JdbcMapperFactory.newInstance().<DbObject, CKEY>crud(DbObject.class, CKEY.class).table(connection, "TEST_DB_OBJECT_CKEY");
DbObject object = DbObject.newInstance();
CKEY ckey = new CKEY();
ckey.id = object.getId();
ckey.name = object.getName();
assertNull(objectCrud.read(connection, ckey));
// create
CKEY key = objectCrud.create(connection, object, new CheckedConsumer<CKEY>() {
CKEY key;
@Override
public void accept(CKEY aLong) throws Exception {
key = aLong;
}
}).key;
assertNull(key);
key = ckey;
// read
assertEquals(object, objectCrud.read(connection, key));
object.setEmail("Udpdated");
// update
objectCrud.update(connection, object);
assertEquals(object, objectCrud.read(connection, key));
// delete
objectCrud.delete(connection, key);
assertNull(objectCrud.read(connection, key));
List<DbObject> values = Arrays.asList(DbObject.newInstance(), DbObject.newInstance());
final List<CKEY> keys = new ArrayList<CKEY>();
for (DbObject value : values) {
CKEY k = new CKEY();
k.id = value.getId();
k.name = value.getName();
keys.add(k);
}
objectCrud.create(connection, values);
assertCollectionEquals(values, objectCrud.read(connection, keys, new ListCollector<DbObject>()).getList());
int i = 333;
for (DbObject value : values) {
value.setEmail(Integer.toHexString(i));
i++;
}
objectCrud.update(connection, values);
assertCollectionEquals(values, objectCrud.read(connection, keys, new ListCollector<DbObject>()).getList());
objectCrud.delete(connection, keys);
assertCollectionEquals(Collections.<DbObject>emptyList(), objectCrud.read(connection, keys, new ListCollector<DbObject>()).getList());
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class CrudTest method testDbObjectCrud.
@Test
public void testDbObjectCrud() throws SQLException {
Connection connection = DbHelper.getDbConnection(targetDB);
if (connection == null) {
System.err.println("Db " + targetDB + " not available");
return;
}
try {
Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT");
checkCrudDbObject(connection, objectCrud, DbObject.newInstance());
} finally {
connection.close();
}
}
Aggregations