use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class CrudTest method main.
public static void main(String[] args) throws SQLException {
Connection connection = MysqlDbHelper.objectDb();
Crud<DbObject, Long> crud = JdbcMapperFactory.newInstance().crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT");
connection.createStatement().execute("TRUNCATE TABLE TEST_DB_OBJECT");
List<DbObject> objects = new ArrayList<DbObject>(10000);
for (int i = 0; i < 65001; i++) {
DbObject e = DbObject.newInstance();
e.setId(i + 1);
objects.add(e);
}
crud.create(connection, objects);
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class CrudTest method testDbObjectLazyCrud.
@Test
public void testDbObjectLazyCrud() 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("TEST_DB_OBJECT");
checkCrudDbObject(connection, objectCrud, DbObject.newInstance());
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class CrudUpsertTest method testUpsertDefaultCrud.
@Test
public void testUpsertDefaultCrud() throws SQLException {
Connection connection = mock(Connection.class);
PreparedStatement ps = mock(PreparedStatement.class);
when(connection.prepareStatement("INSERT INTO TEST(id, name) " + "VALUES(?, ?) " + "ON CONFLICT (id) " + "DO UPDATE " + "SET name = EXCLUDED.name", new String[0])).thenReturn(ps);
DbObject o = DbObject.newInstance();
postGresqlCrud.createOrUpdate(connection, o);
verify(ps).setLong(1, o.getId());
verify(ps).setString(2, o.getName());
verify(ps).executeUpdate();
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class CrudUpsertTest method testUpsertDefaultCrudBatch.
@Test
public void testUpsertDefaultCrudBatch() throws SQLException {
Connection connection = mock(Connection.class);
PreparedStatement ps = mock(PreparedStatement.class);
when(connection.prepareStatement("INSERT INTO TEST(id, name) " + "VALUES(?, ?), (?, ?) " + "ON CONFLICT (id) " + "DO UPDATE " + "SET name = EXCLUDED.name")).thenReturn(ps);
List<DbObject> objects = Arrays.asList(DbObject.newInstance(), DbObject.newInstance());
postGresqlCrud.createOrUpdate(connection, objects);
verify(ps).setLong(1, objects.get(0).getId());
verify(ps).setString(2, objects.get(0).getName());
verify(ps).setLong(3, objects.get(1).getId());
verify(ps).setString(4, objects.get(1).getName());
verify(ps).executeUpdate();
}
use of org.simpleflatmapper.test.beans.DbObject 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