use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class MysqlCrudTest method testBatch.
@Test
public void testBatch() throws SQLException {
Connection connection = mock(Connection.class);
PreparedStatement ps = mock(PreparedStatement.class);
when(connection.prepareStatement("INSERT INTO TEST(id) VALUES(?), (?), (?), (?), (?), (?), (?), (?), (?), (?)")).thenReturn(ps);
Crud<DbObject, Long> objectCrud = CrudFactory.<DbObject, Long>newInstance(ReflectionService.newInstance().getClassMeta(DbObject.class), ReflectionService.newInstance().getClassMeta(Long.class), new CrudMeta(new DatabaseMeta("MySQL", 5, 5), "TEST", new ColumnMeta[] { new ColumnMeta("id", Types.INTEGER, true, null) }), JdbcMapperFactory.newInstance());
final List<DbObject> values = new ArrayList<DbObject>();
for (int i = 0; i < 10; i++) {
values.add(DbObject.newInstance());
}
objectCrud.create(connection, values);
for (int i = 0; i < 10; i++) {
verify(ps).setLong(i + 1, values.get(i).getId());
}
verify(ps).executeUpdate();
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class MysqlMultiRowsBatchInsertCrudTest method testUpsertAutoIncUniqueIndexOnIndex.
@Test
public void testUpsertAutoIncUniqueIndexOnIndex() throws SQLException {
Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.MYSQL);
if (connection == null) {
System.err.println("Db MySQL not available");
return;
}
try {
Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT_AUTOINC_NAMEINDEX");
final DbObject object = DbObject.newInstance();
Long key = objectCrud.createOrUpdate(connection, object, new KeyCapture<Long>()).getKey();
assertNotNull(key);
object.setId(key);
assertEquals(object, objectCrud.read(connection, object.getId()));
object.setEmail("Updated Email " + key);
key = objectCrud.createOrUpdate(connection, object, new KeyCapture<Long>()).getKey();
System.out.println("key = " + key + " current id " + object.getId());
ResultSet resultSet = connection.createStatement().executeQuery("SELECT count(*) FROM TEST_DB_OBJECT_AUTOINC_NAMEINDEX WHERE email = '" + object.getEmail() + "'");
assertTrue(resultSet.next());
assertEquals(1, resultSet.getInt(1));
assertEquals(object, objectCrud.read(connection, object.getId()));
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class MysqlMultiRowsBatchInsertCrudTest method testBatchInsertCreateOneQuery.
@Test
public void testBatchInsertCreateOneQuery() throws SQLException {
Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.MYSQL);
if (connection == null) {
System.err.println("Db MySQL not available");
return;
}
try {
Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT");
Connection mockConnection = mock(Connection.class);
PreparedStatement preparedStatement = mock(PreparedStatement.class);
ArgumentCaptor<String> queryCapture = ArgumentCaptor.forClass(String.class);
when(mockConnection.prepareStatement(queryCapture.capture())).thenReturn(preparedStatement);
objectCrud.create(mockConnection, Arrays.asList(DbObject.newInstance(), DbObject.newInstance()));
assertEquals("INSERT INTO test_db_object(id, name, email, creation_Time, type_ordinal, type_name) VALUES(?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)".toLowerCase(), queryCapture.getValue().toLowerCase());
verify(preparedStatement, never()).addBatch();
verify(preparedStatement, never()).executeBatch();
verify(preparedStatement).executeUpdate();
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class MysqlMultiRowsBatchInsertCrudTest method testBatchUpsertCreateOneQuery.
@Test
public void testBatchUpsertCreateOneQuery() throws SQLException {
Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.MYSQL);
if (connection == null) {
System.err.println("Db MySQL not available");
return;
}
try {
Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT");
Connection mockConnection = mock(Connection.class);
PreparedStatement preparedStatement = mock(PreparedStatement.class);
ArgumentCaptor<String> queryCapture = ArgumentCaptor.forClass(String.class);
when(mockConnection.prepareStatement(queryCapture.capture())).thenReturn(preparedStatement);
objectCrud.createOrUpdate(mockConnection, Arrays.asList(DbObject.newInstance(), DbObject.newInstance()));
assertEquals(("INSERT INTO test_db_object(id, name, email, creation_Time, type_ordinal, type_name) " + "VALUES(?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?) " + "ON DUPLICATE KEY UPDATE name = VALUES(name), " + "email = VALUES(email)," + " creation_Time = VALUES(creation_Time), " + "type_ordinal = VALUES(type_ordinal), " + "type_name = VALUES(type_name)").toLowerCase(), queryCapture.getValue().toLowerCase());
verify(preparedStatement, never()).addBatch();
verify(preparedStatement, never()).executeBatch();
verify(preparedStatement).executeUpdate();
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class PostgresqlCrudTest method testSerial.
@Test
public void testSerial() throws SQLException {
Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
if (connection == null) {
System.err.println("Db POSTGRESQL not available");
return;
}
try {
Statement st = connection.createStatement();
try {
st.execute("CREATE TABLE IF NOT EXISTS test_db_object_serial( id bigserial primary key," + " name varchar(100), " + " email varchar(100)," + " creation_Time timestamp, type_ordinal int, type_name varchar(10) )");
} finally {
st.close();
}
Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().addKeys("id").<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "test_db_object_serial");
DbObject object = DbObject.newInstance();
object.setId(-22225);
// create
Long key = objectCrud.create(connection, object, new CheckedConsumer<Long>() {
Long key;
@Override
public void accept(Long aLong) throws Exception {
key = aLong;
}
}).key;
assertFalse(key.equals(object.getId()));
object.setId(key);
// read
assertEquals(object, objectCrud.read(connection, key));
} finally {
connection.close();
}
}
Aggregations