Search in sources :

Example 51 with DbObject

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);
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) ArrayList(java.util.ArrayList)

Example 52 with DbObject

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();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) Test(org.junit.Test)

Example 53 with DbObject

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();
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 54 with DbObject

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();
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 55 with DbObject

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());
}
Also used : CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) DbObject(org.simpleflatmapper.test.beans.DbObject) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) NoSuchElementException(java.util.NoSuchElementException) AtomicLong(java.util.concurrent.atomic.AtomicLong) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Aggregations

DbObject (org.simpleflatmapper.test.beans.DbObject)83 Test (org.junit.Test)75 Connection (java.sql.Connection)27 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)10 CheckedConsumer (org.simpleflatmapper.util.CheckedConsumer)10 ArrayList (java.util.ArrayList)9 MappingException (org.simpleflatmapper.map.MappingException)6 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)6 Type (java.lang.reflect.Type)5 ParseException (java.text.ParseException)5 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)5 StringReader (java.io.StringReader)4 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)4 DbPartialFinalObject (org.simpleflatmapper.test.beans.DbPartialFinalObject)4 Statement (java.sql.Statement)3 MapperBuilderErrorHandler (org.simpleflatmapper.map.MapperBuilderErrorHandler)3 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)3 ListCollector (org.simpleflatmapper.util.ListCollector)3