Search in sources :

Example 1 with MockDataSource

use of org.seasar.doma.internal.jdbc.mock.MockDataSource in project doma by domaframework.

the class BuiltinTableIdGeneratorTest method test.

@Test
public void test() {
    MockConfig config = new MockConfig();
    config.setDialect(new PostgresDialect());
    MockConnection connection = new MockConnection();
    MockConnection connection2 = new MockConnection();
    MockResultSet resultSet2 = connection2.preparedStatement.resultSet;
    resultSet2.rows.add(new RowData(11L));
    final LinkedList<MockConnection> connections = new LinkedList<>();
    connections.add(connection);
    connections.add(connection2);
    config.dataSource = new MockDataSource() {

        @Override
        public Connection getConnection() {
            return connections.pop();
        }
    };
    BuiltinTableIdGenerator idGenerator = new BuiltinTableIdGenerator();
    idGenerator.setQualifiedTableName("aaa");
    idGenerator.setPkColumnName("PK");
    idGenerator.setPkColumnValue("EMP_ID");
    idGenerator.setValueColumnName("VALUE");
    idGenerator.setInitialValue(1);
    idGenerator.setAllocationSize(1);
    idGenerator.initialize();
    IdGenerationConfig idGenerationConfig = new IdGenerationConfig(config, _IdGeneratedEmp.getSingletonInternal());
    Long value = idGenerator.generatePreInsert(idGenerationConfig);
    assertEquals(new Long(10), value);
    assertEquals("update aaa set VALUE = VALUE + ? where PK = ?", connection.preparedStatement.sql);
    assertEquals(2, connection.preparedStatement.bindValues.size());
    assertEquals("select VALUE from aaa where PK = ?", connection2.preparedStatement.sql);
    assertEquals(1, connection2.preparedStatement.bindValues.size());
}
Also used : Connection(java.sql.Connection) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) LinkedList(java.util.LinkedList) RowData(org.seasar.doma.internal.jdbc.mock.RowData) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) PostgresDialect(org.seasar.doma.jdbc.dialect.PostgresDialect) Test(org.junit.jupiter.api.Test)

Example 2 with MockDataSource

use of org.seasar.doma.internal.jdbc.mock.MockDataSource in project doma by domaframework.

the class KeepAliveLocalTransactionTest method testBegin_failedToSetTransactionIsolation.

@Test
public void testBegin_failedToSetTransactionIsolation() {
    final SQLException exception = new SQLException();
    MockConnection connection = new MockConnection() {

        @Override
        public void setTransactionIsolation(int level) throws SQLException {
            throw exception;
        }
    };
    LocalTransactionDataSource dataSource = new LocalTransactionDataSource(new MockDataSource(connection));
    LocalTransaction transaction = dataSource.getKeepAliveLocalTransaction(jdbcLogger);
    try {
        transaction.begin(TransactionIsolationLevel.READ_COMMITTED);
        dataSource.getConnection();
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(exception, expected.getCause());
    }
}
Also used : SQLException(java.sql.SQLException) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 3 with MockDataSource

use of org.seasar.doma.internal.jdbc.mock.MockDataSource in project doma by domaframework.

the class LocalTransactionDataSourceTest method testUnwrap.

@Test
public void testUnwrap() throws SQLException {
    DataSource dataSource = new LocalTransactionDataSource(new MockDataSource());
    assertNotNull(dataSource.unwrap(LocalTransactionDataSource.class));
    assertNotNull(dataSource.unwrap(MockDataSource.class));
    try {
        dataSource.unwrap(Runnable.class);
        fail();
    } catch (SQLException ignored) {
    }
}
Also used : SQLException(java.sql.SQLException) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) DataSource(javax.sql.DataSource) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) Test(org.junit.jupiter.api.Test)

Example 4 with MockDataSource

use of org.seasar.doma.internal.jdbc.mock.MockDataSource in project doma by domaframework.

the class LocalTransactionTest method testBegin_failedToSetTransactionIsolation.

@Test
public void testBegin_failedToSetTransactionIsolation() {
    final SQLException exception = new SQLException();
    MockConnection connection = new MockConnection() {

        @Override
        public void setTransactionIsolation(int level) throws SQLException {
            throw exception;
        }
    };
    LocalTransactionDataSource dataSource = new LocalTransactionDataSource(new MockDataSource(connection));
    LocalTransaction transaction = dataSource.getLocalTransaction(jdbcLogger);
    try {
        transaction.begin(TransactionIsolationLevel.READ_COMMITTED);
        dataSource.getConnection();
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(exception, expected.getCause());
    }
}
Also used : SQLException(java.sql.SQLException) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 5 with MockDataSource

use of org.seasar.doma.internal.jdbc.mock.MockDataSource in project doma by domaframework.

the class KeepAliveLocalTransactionTest method testBegin_failedToBegin.

@Test
public void testBegin_failedToBegin() {
    final SQLException exception = new SQLException();
    MockConnection connection = new MockConnection() {

        @Override
        public void setAutoCommit(boolean autoCommit) throws SQLException {
            throw exception;
        }
    };
    LocalTransactionDataSource dataSource = new LocalTransactionDataSource(new MockDataSource(connection));
    LocalTransaction transaction = dataSource.getKeepAliveLocalTransaction(jdbcLogger);
    try {
        transaction.begin();
        dataSource.getConnection();
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(exception, expected.getCause());
    }
}
Also used : SQLException(java.sql.SQLException) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)9 MockDataSource (org.seasar.doma.internal.jdbc.mock.MockDataSource)9 SQLException (java.sql.SQLException)5 MockConnection (org.seasar.doma.internal.jdbc.mock.MockConnection)5 JdbcException (org.seasar.doma.jdbc.JdbcException)4 DataSource (javax.sql.DataSource)2 Connection (java.sql.Connection)1 LinkedList (java.util.LinkedList)1 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)1 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)1 RowData (org.seasar.doma.internal.jdbc.mock.RowData)1 UtilLoggingJdbcLogger (org.seasar.doma.jdbc.UtilLoggingJdbcLogger)1 PostgresDialect (org.seasar.doma.jdbc.dialect.PostgresDialect)1