Search in sources :

Example 1 with ConnectedCrud

use of org.simpleflatmapper.jdbc.ConnectedCrud 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);
}
Also used : ConnectedCrud(org.simpleflatmapper.jdbc.ConnectedCrud) CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) Connection(java.sql.Connection) DbObject(org.simpleflatmapper.test.beans.DbObject) DataSourceTransactionTemplate(org.simpleflatmapper.jdbc.impl.DataSourceTransactionTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 2 with ConnectedCrud

use of org.simpleflatmapper.jdbc.ConnectedCrud 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);
    }
}
Also used : ConnectedCrud(org.simpleflatmapper.jdbc.ConnectedCrud) CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) Connection(java.sql.Connection) DbObject(org.simpleflatmapper.test.beans.DbObject) DataSourceTransactionTemplate(org.simpleflatmapper.jdbc.impl.DataSourceTransactionTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)2 DataSource (javax.sql.DataSource)2 Test (org.junit.Test)2 ConnectedCrud (org.simpleflatmapper.jdbc.ConnectedCrud)2 DataSourceTransactionTemplate (org.simpleflatmapper.jdbc.impl.DataSourceTransactionTemplate)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 CheckedConsumer (org.simpleflatmapper.util.CheckedConsumer)2