use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class PostgresqlCrudTest method testManualAutogenerated.
@Test
public void testManualAutogenerated() 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("DROP TABLE IF EXISTS test_db_object_dv");
st.execute("CREATE TABLE test_db_object_dv( id bigint DEFAULT 3," + " 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().addColumnProperty("id", KeyProperty.DEFAULT, AutoGeneratedProperty.DEFAULT).<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "test_db_object_dv");
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();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class ResultSetFieldMapperFactoryTest method testPrimitiveField.
@Test
public void testPrimitiveField() {
ClassMeta<DbObject> classMeta = ReflectionService.newInstance(false).getClassMeta(DbObject.class);
PropertyMeta<DbObject, Long> id = classMeta.newPropertyFinder(ConstantPredicate.<PropertyMeta<?, ?>>truePredicate()).<Long>findProperty(new DefaultPropertyNameMatcher("id", 0, false, false), new Object[0]);
FieldMapperColumnDefinition<JdbcColumnKey> identity = FieldMapperColumnDefinition.identity();
PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 1), identity);
FieldMapper<ResultSet, DbObject> fieldMapper = factory.newFieldMapper(propertyMapping, null, RethrowMapperBuilderErrorHandler.INSTANCE);
assertTrue(fieldMapper instanceof LongFieldMapper);
PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping1 = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 0), identity);
fieldMapper = factory.newFieldMapper(propertyMapping1, null, RethrowMapperBuilderErrorHandler.INSTANCE);
assertTrue(fieldMapper instanceof LongFieldMapper);
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class MysqlCrudTest method testSplitBatch.
@Test
public void testSplitBatch() throws SQLException {
Connection connection = mock(Connection.class);
PreparedStatement ps = mock(PreparedStatement.class);
PreparedStatement ps2 = mock(PreparedStatement.class);
when(connection.prepareStatement("INSERT INTO TEST(id) VALUES(?), (?), (?), (?), (?), (?), (?), (?), (?), (?)")).thenReturn(ps);
when(connection.prepareStatement("INSERT INTO TEST(id) VALUES(?), (?), (?), (?), (?)")).thenReturn(ps2);
when(ps.executeUpdate()).thenThrow(getPacketTooBigException());
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 int batchsize = 10;
final List<DbObject> values = new ArrayList<DbObject>();
for (int i = 0; i < batchsize; i++) {
values.add(DbObject.newInstance());
}
objectCrud.create(connection, values);
for (int i = 0; i < batchsize; i++) {
verify(ps).setLong(i + 1, values.get(i).getId());
}
verify(ps).executeUpdate();
for (int i = 0; i < batchsize / 2; i++) {
ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class);
verify(ps2, times(2)).setLong(eq(i + 1), captor.capture());
final List<Long> allValues = captor.getAllValues();
assertEquals(values.get(i).getId(), allValues.get(0).longValue());
assertEquals(values.get(i + batchsize / 2).getId(), allValues.get(1).longValue());
}
verify(ps).executeUpdate();
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class MysqlMultiRowsBatchInsertCrudTest method testUpsert.
@Test
public void testUpsert() 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");
final DbObject object = DbObject.newInstance();
objectCrud.createOrUpdate(connection, object);
assertEquals(object, objectCrud.read(connection, object.getId()));
object.setEmail("Updated Email");
objectCrud.createOrUpdate(connection, object);
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 testBatchInsertSizeReducer.
@Test
public void testBatchInsertSizeReducer() 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 preparedStatementFail = mock(PreparedStatement.class);
PreparedStatement preparedStatementSucceed = mock(PreparedStatement.class);
List<DbObject> values = new ArrayList<DbObject>();
for (int i = 0; i < 101; i++) {
values.add(DbObject.newInstance());
}
when(mockConnection.prepareStatement(anyString())).thenReturn(preparedStatementFail, preparedStatementFail, preparedStatementSucceed);
when(preparedStatementFail.executeUpdate()).thenThrow(MysqlCrudTest.getPacketTooBigException());
objectCrud.create(mockConnection, values);
verify(preparedStatementFail, times(2)).executeUpdate();
verify(preparedStatementSucceed, times(5)).executeUpdate();
} finally {
connection.close();
}
}
Aggregations