use of org.simpleflatmapper.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.
the class DynamicCsvMapperImplTest method testMultipleThread.
@Test
public void testMultipleThread() throws InterruptedException, ExecutionException {
final CsvMapper<DbObject> mapper = CsvMapperFactory.newInstance().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();
}
};
final String str = buildCsvContent();
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 StringReader(str), 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(nbRow.get(), NBFUTURE * NBROW);
int sum = 0;
for (i = 0; i < NBROW; i++) {
sum += i;
}
assertEquals(sumOfAllIds.get(), NBFUTURE * sum);
}
use of org.simpleflatmapper.util.CheckedConsumer 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.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.
the class PostgresqlCrudTest method testManualAutogeneratedFromSeq.
@Test
public void testManualAutogeneratedFromSeq() 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_seq");
st.execute("DROP SEQUENCE IF EXISTS test_seq_seq");
st.execute("CREATE SEQUENCE test_seq_seq");
st.execute("CREATE TABLE test_db_object_seq( id bigint 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().addColumnProperty("id", KeyProperty.DEFAULT, AutoGeneratedProperty.of("nextval('test_seq_seq')")).<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "test_db_object_seq");
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));
object = DbObject.newInstance();
object.setId(-22225);
// create batch
key = objectCrud.create(connection, Arrays.asList(object), new CheckedConsumer<Long>() {
Long key;
@Override
public void accept(Long aLong) throws Exception {
if (key != null)
throw new IllegalArgumentException();
key = aLong;
}
}).key;
assertFalse(key.equals(object.getId()));
object.setId(key);
// read
assertEquals(object, objectCrud.read(connection, key));
object = DbObject.newInstance();
object.setId(-22225);
try {
// createOrUpdate
key = objectCrud.createOrUpdate(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));
// createOrUpdate batch
key = objectCrud.createOrUpdate(connection, Arrays.asList(object), new CheckedConsumer<Long>() {
Long key;
@Override
public void accept(Long aLong) throws Exception {
if (key != null)
throw new IllegalArgumentException();
key = aLong;
}
}).key;
assertFalse(key.equals(object.getId()));
object.setId(key);
// read
assertEquals(object, objectCrud.read(connection, key));
} catch (UnsupportedOperationException e) {
// wrong postgres version
}
} finally {
connection.close();
}
}
use of org.simpleflatmapper.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.
the class CrudTest method testDbObjectCrudAutoInc.
@Test
public void testDbObjectCrudAutoInc() 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_AUTOINC");
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));
object.setName("Udpdated");
// update
objectCrud.update(connection, object);
assertEquals(object, objectCrud.read(connection, key));
// delete
objectCrud.delete(connection, key);
assertNull(objectCrud.read(connection, key));
objectCrud.create(connection, DbObject.newInstance());
// batch
final List<Long> keys = new ArrayList<Long>();
final List<DbObject> values = Arrays.asList(DbObject.newInstance(), DbObject.newInstance());
objectCrud.create(connection, values, new CheckedConsumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
values.get(keys.size()).setId(aLong);
keys.add(aLong);
}
});
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 JdbcTemplateCrudTest method testCrudBatch.
@Test
public void testCrudBatch() throws SQLException {
JdbcTemplateCrud<DbObject, Long> objectCrud = JdbcTemplateMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).to(template, "TEST_DB_OBJECT");
DbObject object = DbObject.newInstance();
assertNull(objectCrud.read(object.getId()));
// create
final List<DbObject> objects = Arrays.asList(object);
Long key = objectCrud.create(objects, new CheckedConsumer<Long>() {
Long key;
@Override
public void accept(Long aLong) throws Exception {
key = aLong;
}
}).key;
assertNull(key);
List<Long> keys = Arrays.asList(object.getId());
// read
assertEquals(objects, objectCrud.read(keys, new ListCollector<DbObject>()).getList());
object.setName("Udpdated");
// update
objectCrud.update(objects);
assertEquals(object, objectCrud.read(object.getId()));
// delete
objectCrud.delete(keys);
assertNull(objectCrud.read(key));
objectCrud.create(objects);
assertEquals(objects, objectCrud.read(keys, new ListCollector<DbObject>()).getList());
objectCrud.delete(keys);
try {
objectCrud.createOrUpdate(objects);
assertEquals(objects, objectCrud.read(keys, new ListCollector<DbObject>()).getList());
} catch (UnsupportedOperationException e) {
}
}
Aggregations