use of org.simpleflatmapper.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.
the class JdbcTemplateCrudTest method testLazyCrud.
@Test
public void testLazyCrud() throws SQLException {
// drop table if exist
try {
template.execute("DROP TABLE TEST_DB_OBJECT_LAZY");
} catch (Exception e) {
}
JdbcTemplateCrud<DbObject, Long> objectCrud = JdbcTemplateMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).lazilyTo(template, "TEST_DB_OBJECT_LAZY");
template.execute("create table TEST_DB_OBJECT_LAZY(" + " id bigint primary key," + " name varchar(100), " + " email varchar(100)," + " creation_Time timestamp, type_ordinal int, type_name varchar(10) )");
DbObject object = DbObject.newInstance();
assertNull(objectCrud.read(object.getId()));
// create
Long key = objectCrud.create(object, new CheckedConsumer<Long>() {
Long key;
@Override
public void accept(Long aLong) throws Exception {
key = aLong;
}
}).key;
assertNull(key);
key = object.getId();
// read
assertEquals(object, objectCrud.read(key));
}
use of org.simpleflatmapper.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.
the class ConnectedCrudTest method testDelegateSuccess.
@Test
public void testDelegateSuccess() throws SQLException {
Crud<Object, Object> crud = mock(Crud.class);
DataSource dataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
when(dataSource.getConnection()).thenReturn(connection);
ConnectedCrud<Object, Object> connectedCrud = new ConnectedCrud<Object, Object>(new DataSourceTransactionTemplate(dataSource), crud);
Collection<Object> values = new ArrayList<Object>();
Object value = new Object();
CheckedConsumer consumer = new CheckedConsumer() {
@Override
public void accept(Object o) throws Exception {
}
};
connectedCrud.create(values);
verify(crud).create(connection, values);
verifyCloseCommitAndReset(crud, connection);
connectedCrud.create(value);
verify(crud).create(connection, value);
verifyCloseCommitAndReset(crud, connection);
connectedCrud.create(values, consumer);
verify(crud).create(connection, values, consumer);
verifyCloseCommitAndReset(crud, connection);
connectedCrud.create(value, consumer);
verify(crud).create(connection, value, consumer);
verifyCloseCommitAndReset(crud, connection);
// read
connectedCrud.read(value);
verify(crud).read(connection, value);
verifyCloseCommitAndReset(crud, connection);
connectedCrud.read(values, consumer);
verify(crud).read(connection, values, consumer);
verifyCloseCommitAndReset(crud, connection);
// update
connectedCrud.update(value);
verify(crud).update(connection, value);
verifyCloseCommitAndReset(crud, connection);
connectedCrud.update(values);
verify(crud).update(connection, values);
verifyCloseCommitAndReset(crud, connection);
// delete
connectedCrud.delete(value);
verify(crud).delete(connection, value);
verifyCloseCommitAndReset(crud, connection);
connectedCrud.delete(values);
verify(crud).delete(connection, values);
verifyCloseCommitAndReset(crud, connection);
// create or update
connectedCrud.createOrUpdate(value);
verify(crud).createOrUpdate(connection, value);
verifyCloseCommitAndReset(crud, connection);
connectedCrud.createOrUpdate(values);
verify(crud).createOrUpdate(connection, values);
verifyCloseCommitAndReset(crud, connection);
connectedCrud.createOrUpdate(value, consumer);
verify(crud).createOrUpdate(connection, value, consumer);
verifyCloseCommitAndReset(crud, connection);
connectedCrud.createOrUpdate(values, consumer);
verify(crud).createOrUpdate(connection, values, consumer);
verifyCloseCommitAndReset(crud, connection);
}
use of org.simpleflatmapper.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.
the class ConnectedCrudTest method testDelegateFail.
@Test
public void testDelegateFail() throws SQLException {
Crud<Object, Object> crud = mock(Crud.class);
DataSource dataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
when(dataSource.getConnection()).thenReturn(connection);
ConnectedCrud<Object, Object> connectedCrud = new ConnectedCrud<Object, Object>(new DataSourceTransactionTemplate(dataSource), crud);
Collection<Object> values = new ArrayList<Object>();
Object value = new Object();
CheckedConsumer consumer = new CheckedConsumer() {
@Override
public void accept(Object o) throws Exception {
}
};
RuntimeException exception = new RuntimeException();
doThrow(exception).when(connection).commit();
try {
connectedCrud.create(values);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).create(connection, values);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.create(value);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).create(connection, value);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.create(values, consumer);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).create(connection, values, consumer);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.create(value, consumer);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).create(connection, value, consumer);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
// read
try {
connectedCrud.read(value);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).read(connection, value);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.read(values, consumer);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).read(connection, values, consumer);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.update(value);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).update(connection, value);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.update(values);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).update(connection, values);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
// delete
try {
connectedCrud.delete(value);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).delete(connection, value);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.delete(values);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).delete(connection, values);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.createOrUpdate(value);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).createOrUpdate(connection, value);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.createOrUpdate(values);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).createOrUpdate(connection, values);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.createOrUpdate(value, consumer);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).createOrUpdate(connection, value, consumer);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
try {
connectedCrud.createOrUpdate(values, consumer);
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(crud).createOrUpdate(connection, values, consumer);
verifyCloseCommitFailAndReset(exception, crud, connection);
}
}
use of org.simpleflatmapper.util.CheckedConsumer 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.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.
the class DynamicJdbcMapperTest method testMultipleThread.
@Test
public void testMultipleThread() throws InterruptedException, ExecutionException {
final JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().newMapper(DbObject.class);
ExecutorService service = Executors.newFixedThreadPool(4);
final AtomicLong sumOfAllIds = new AtomicLong();
final AtomicLong nbRow = new AtomicLong();
final CheckedConsumer<DbObject> handler = new CheckedConsumer<DbObject>() {
@Override
public void accept(DbObject t) throws Exception {
long id = t.getId();
assertEquals("name" + Long.toHexString(id), t.getName());
assertEquals("email" + Long.toHexString(id), t.getEmail());
assertEquals(Type.values()[(int) (id) % 4], t.getTypeName());
assertEquals(Type.values()[(int) (id) % 4], t.getTypeOrdinal());
assertEquals(id, t.getCreationTime().getTime() / 1000);
sumOfAllIds.addAndGet(id);
nbRow.incrementAndGet();
}
};
List<Future<Object>> futures = new ArrayList<Future<Object>>();
for (int i = 0; i < NBFUTURE; i++) {
futures.add(service.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
mapper.forEach(new MockDbObjectResultSet(NBROW), handler);
return null;
}
}));
}
int i = 0;
for (Future<Object> future : futures) {
try {
future.get();
} catch (Exception e) {
System.out.println("Future " + i + " fail " + e);
}
i++;
}
assertEquals(NBFUTURE, i);
assertEquals(NBFUTURE * NBROW, nbRow.get());
int sum = 0;
for (i = 1; i <= NBROW; i++) {
sum += i;
}
assertEquals(NBFUTURE * sum, sumOfAllIds.get());
}
Aggregations