/** * Create a connected crud validating it against the specified datasource. * The table name is derived from the jpa annotation or from the class name. * @param dataSource the datasource * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> to(DataSource dataSource) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), to(connection)); } finally { connection.close(); } }
ConnectedCrud<Object, Object> connectedCrud = new ConnectedCrud<Object, Object>(new DataSourceTransactionTemplate(dataSource), crud); 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); connectedCrud.read(value); verify(crud).read(connection, value); verifyCloseCommitAndReset(crud, connection); connectedCrud.read(values, consumer); verify(crud).read(connection, values, consumer); verifyCloseCommitAndReset(crud, connection); connectedCrud.update(value); verify(crud).update(connection, value); verifyCloseCommitAndReset(crud, connection);
private <T extends DbObject> void checkCrudDbObject(ConnectedCrud<T, Long> objectCrud, T object) throws SQLException { 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)); object.setName("Udpdated"); // update objectCrud.update(object); assertEquals(object, objectCrud.read(key)); // delete objectCrud.delete(key); assertNull(objectCrud.read(key)); objectCrud.create(object); assertEquals(object, objectCrud.where("id = :id", Long.class).readFirst(object.getId())); assertEquals(Arrays.asList(object), objectCrud.where("name = :name and id = :id", DbObject.class).read(object, new ListCollector<T>()).getList()); }
@Test public void testDbObjectCrud() throws Exception { DataSource dataSource = DbHelper.getHsqlDataSource(); ConnectedCrud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(dataSource, "TEST_DB_OBJECT"); checkCrudDbObject(objectCrud, DbObject.newInstance()); Connection connection = dataSource.getConnection(); try { CrudTest.checkCrudDbObject(connection, objectCrud.crud(), DbObject.newInstance()); } finally { connection.close(); } }
crud.createOrUpdate(objects); fail(); } catch(UnsupportedOperationException e) { crud.createOrUpdate(DbObject.newInstance()); fail(); } catch(UnsupportedOperationException e) { crud.createOrUpdate(objects, new CheckedConsumer<Long>() { @Override public void accept(Long aLong) throws Exception { crud.createOrUpdate(DbObject.newInstance(), new CheckedConsumer<Long>() { @Override public void accept(Long aLong) throws Exception {
ConnectedCrud<Object, Object> connectedCrud = new ConnectedCrud<Object, Object>(new DataSourceTransactionTemplate(dataSource), crud); connectedCrud.create(values); fail(); } catch (RuntimeException e) { connectedCrud.create(value); fail(); } catch (RuntimeException e) { connectedCrud.create(values, consumer); fail(); } catch (RuntimeException e) { connectedCrud.create(value, consumer); fail(); } catch (RuntimeException e) { connectedCrud.read(value); fail(); } catch (RuntimeException e) { connectedCrud.read(values, consumer); fail(); } catch (RuntimeException e) { connectedCrud.update(value); fail(); } catch (RuntimeException e) {
@Test public void testDbObjectCrudTable() throws Exception { DataSource dataSource = DbHelper.getHsqlDataSource(); ConnectedCrud<CrudTest.DbObjectTable, Long> objectCrud = JdbcMapperFactory.newInstance().<CrudTest.DbObjectTable, Long>crud(CrudTest.DbObjectTable.class, Long.class).to(dataSource); checkCrudDbObject(objectCrud, DbObject.newInstance(new CrudTest.DbObjectTable())); Connection connection = dataSource.getConnection(); try { CrudTest.checkCrudDbObject(connection, objectCrud.crud(), DbObject.newInstance(new CrudTest.DbObjectTable())); } finally { connection.close(); } }
/** * Create a connected crud against the specified table validating it against the specified datasource. * @param dataSource the datasource * @param table the table * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> table(DataSource dataSource, String table) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), table(connection, table)); } finally { connection.close(); } }
/** * Create a connected crud validating it against the specified datasource. * The table name is derived from the jpa annotation or from the class name. * @param dataSource the datasource * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> to(DataSource dataSource) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), to(connection)); } finally { connection.close(); } }
/** * Create a connected crud against the specified table validating it against the specified datasource. * @param dataSource the datasource * @param table the table * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> table(DataSource dataSource, String table) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), table(connection, table)); } finally { connection.close(); } }