Search in sources :

Example 1 with MockConnection

use of org.seasar.doma.internal.jdbc.mock.MockConnection 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 MockConnection

use of org.seasar.doma.internal.jdbc.mock.MockConnection 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 MockConnection

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

the class LocalTransactionConnectionTest method testIsWrapperFor.

@Test
public void testIsWrapperFor() throws Exception {
    try (Connection connection = new LocalTransactionConnection(new MockConnection(), Connection.TRANSACTION_READ_COMMITTED, true)) {
        assertTrue(connection.isWrapperFor(LocalTransactionConnection.class));
        assertTrue(connection.isWrapperFor(MockConnection.class));
        assertFalse(connection.isWrapperFor(Runnable.class));
    }
}
Also used : Connection(java.sql.Connection) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) Test(org.junit.jupiter.api.Test)

Example 4 with MockConnection

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

the class LocalTransactionConnectionTest method testUnwrap.

@Test
public void testUnwrap() throws Exception {
    try (Connection connection = new LocalTransactionConnection(new MockConnection(), Connection.TRANSACTION_READ_COMMITTED, true)) {
        assertNotNull(connection.unwrap(LocalTransactionConnection.class));
        assertNotNull(connection.unwrap(LocalTransactionConnection.class));
        try {
            connection.unwrap(Runnable.class);
            fail();
        } catch (SQLException ignored) {
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) Test(org.junit.jupiter.api.Test)

Example 5 with MockConnection

use of org.seasar.doma.internal.jdbc.mock.MockConnection 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)

Aggregations

Test (org.junit.jupiter.api.Test)13 MockConnection (org.seasar.doma.internal.jdbc.mock.MockConnection)13 Emp (example.entity.Emp)5 example.entity._Emp (example.entity._Emp)5 SQLException (java.sql.SQLException)5 MockDataSource (org.seasar.doma.internal.jdbc.mock.MockDataSource)5 MockPreparedStatement (org.seasar.doma.internal.jdbc.mock.MockPreparedStatement)5 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)5 BigDecimal (java.math.BigDecimal)4 ColumnMetaData (org.seasar.doma.internal.jdbc.mock.ColumnMetaData)4 MockResultSetMetaData (org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData)4 RowData (org.seasar.doma.internal.jdbc.mock.RowData)4 JdbcException (org.seasar.doma.jdbc.JdbcException)4 SqlFileSelectQuery (org.seasar.doma.jdbc.query.SqlFileSelectQuery)4 Connection (java.sql.Connection)3 BindValue (org.seasar.doma.internal.jdbc.mock.BindValue)3 Savepoint (java.sql.Savepoint)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Stream (java.util.stream.Stream)1