Search in sources :

Example 1 with MockPreparedStatement

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

the class SelectCommandTest method testExecute_NoResultException.

@Test
public void testExecute_NoResultException() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    metaData.columns.add(new ColumnMetaData("salary"));
    metaData.columns.add(new ColumnMetaData("version"));
    MockResultSet resultSet = new MockResultSet(metaData);
    runtimeConfig.dataSource.connection = new MockConnection(new MockPreparedStatement(resultSet));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.addParameter("name", String.class, "hoge");
    query.addParameter("salary", BigDecimal.class, new BigDecimal(10000));
    query.setMethod(getClass().getMethod(method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setResultEnsured(true);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    SelectCommand<Emp> command = new SelectCommand<>(query, new EntitySingleResultHandler<>(_Emp.getSingletonInternal()));
    try {
        command.execute();
        fail();
    } catch (Exception expected) {
    }
}
Also used : SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) MockPreparedStatement(org.seasar.doma.internal.jdbc.mock.MockPreparedStatement) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 2 with MockPreparedStatement

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

the class SelectCommandTest method testExecute_singleResult.

@Test
public void testExecute_singleResult() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    metaData.columns.add(new ColumnMetaData("salary"));
    metaData.columns.add(new ColumnMetaData("version"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "hoge", new BigDecimal(10000), 100));
    runtimeConfig.dataSource.connection = new MockConnection(new MockPreparedStatement(resultSet));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.addParameter("name", String.class, "hoge");
    query.addParameter("salary", BigDecimal.class, new BigDecimal(10000));
    query.setMethod(getClass().getMethod(method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    SelectCommand<Emp> command = new SelectCommand<>(query, new EntitySingleResultHandler<>(_Emp.getSingletonInternal()));
    Emp entity = command.execute();
    query.complete();
    assertNotNull(entity);
    assertEquals(new Integer(1), entity.getId());
    assertEquals("hoge", entity.getName());
    assertEquals(new BigDecimal(10000), entity.getSalary());
    assertEquals(new Integer(100), entity.getVersion());
    List<BindValue> bindValues = runtimeConfig.dataSource.connection.preparedStatement.bindValues;
    BindValue bindValue = bindValues.get(0);
    assertEquals("hoge", bindValue.getValue());
    assertEquals(1, bindValue.getIndex());
    bindValue = bindValues.get(1);
    assertEquals(new BigDecimal(10000), bindValue.getValue());
    assertEquals(2, bindValue.getIndex());
}
Also used : SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) MockPreparedStatement(org.seasar.doma.internal.jdbc.mock.MockPreparedStatement) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) BigDecimal(java.math.BigDecimal) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) RowData(org.seasar.doma.internal.jdbc.mock.RowData) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 3 with MockPreparedStatement

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

the class PreparedSqlParameterBinderTest method testBind.

@Test
public void testBind() throws Exception {
    MockPreparedStatement preparedStatement = new MockPreparedStatement();
    List<BasicInParameter<?>> parameters = new ArrayList<>();
    parameters.add(new BasicInParameter<>(() -> new org.seasar.doma.wrapper.StringWrapper("aaa")));
    parameters.add(new BasicInParameter<>(() -> new org.seasar.doma.wrapper.BigDecimalWrapper(new BigDecimal(10))));
    PreparedSqlParameterBinder binder = new PreparedSqlParameterBinder(new MyQuery());
    binder.bind(preparedStatement, parameters);
    List<BindValue> bindValues = preparedStatement.bindValues;
    assertEquals(2, bindValues.size());
    BindValue bindValue = bindValues.get(0);
    assertEquals(1, bindValue.getIndex());
    assertEquals("aaa", bindValue.getValue());
    bindValue = bindValues.get(1);
    assertEquals(2, bindValue.getIndex());
    assertEquals(new BigDecimal(10), bindValue.getValue());
}
Also used : ArrayList(java.util.ArrayList) MockPreparedStatement(org.seasar.doma.internal.jdbc.mock.MockPreparedStatement) BigDecimal(java.math.BigDecimal) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) BasicInParameter(org.seasar.doma.internal.jdbc.sql.BasicInParameter) Test(org.junit.jupiter.api.Test)

Example 4 with MockPreparedStatement

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

the class DeleteCommandTest method testExecute_throwsOptimisticLockException.

@Test
public void testExecute_throwsOptimisticLockException() {
    Emp emp = new Emp();
    emp.setId(10);
    emp.setName("aaa");
    emp.setVersion(100);
    MockPreparedStatement ps = new MockPreparedStatement();
    ps.updatedRows = 0;
    runtimeConfig.dataSource.connection = new MockConnection(ps);
    AutoDeleteQuery<Emp> query = new AutoDeleteQuery<>(_Emp.getSingletonInternal());
    query.setMethod(method);
    query.setConfig(runtimeConfig);
    query.setEntity(emp);
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    DeleteCommand command = new DeleteCommand(query);
    try {
        command.execute();
        fail();
    } catch (OptimisticLockException expected) {
    }
}
Also used : AutoDeleteQuery(org.seasar.doma.jdbc.query.AutoDeleteQuery) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) MockPreparedStatement(org.seasar.doma.internal.jdbc.mock.MockPreparedStatement) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) Test(org.junit.jupiter.api.Test)

Example 5 with MockPreparedStatement

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

the class SelectCommandTest method testExecute_resultList.

@Test
public void testExecute_resultList() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    metaData.columns.add(new ColumnMetaData("salary"));
    metaData.columns.add(new ColumnMetaData("version"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "hoge", new BigDecimal(10000), 100));
    resultSet.rows.add(new RowData(2, "foo", new BigDecimal(20000), 200));
    resultSet.rows.add(new RowData(3, "bar", new BigDecimal(30000), 300));
    runtimeConfig.dataSource.connection = new MockConnection(new MockPreparedStatement(resultSet));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.addParameter("salary", BigDecimal.class, new BigDecimal(5000));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(getClass().getMethod(method.getName()));
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    SelectCommand<List<Emp>> command = new SelectCommand<>(query, new EntityResultListHandler<>(_Emp.getSingletonInternal()));
    List<Emp> entities = command.execute();
    query.complete();
    assertNotNull(entities);
    assertEquals(3, entities.size());
    Emp entity = entities.get(0);
    assertEquals(new Integer(1), entity.getId());
    assertEquals("hoge", entity.getName());
    assertEquals(new BigDecimal(10000), entity.getSalary());
    assertEquals(new Integer(100), entity.getVersion());
    entity = entities.get(1);
    assertEquals(new Integer(2), entity.getId());
    assertEquals("foo", entity.getName());
    assertEquals(new BigDecimal(20000), entity.getSalary());
    assertEquals(new Integer(200), entity.getVersion());
    entity = entities.get(2);
    assertEquals(new Integer(3), entity.getId());
    assertEquals("bar", entity.getName());
    assertEquals(new BigDecimal(30000), entity.getSalary());
    assertEquals(new Integer(300), entity.getVersion());
    List<BindValue> bindValues = runtimeConfig.dataSource.connection.preparedStatement.bindValues;
    BindValue bindValue = bindValues.get(0);
    assertEquals(new BigDecimal(5000), bindValue.getValue());
    assertEquals(1, bindValue.getIndex());
}
Also used : SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) MockPreparedStatement(org.seasar.doma.internal.jdbc.mock.MockPreparedStatement) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) BigDecimal(java.math.BigDecimal) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) RowData(org.seasar.doma.internal.jdbc.mock.RowData) List(java.util.List) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 MockPreparedStatement (org.seasar.doma.internal.jdbc.mock.MockPreparedStatement)6 Emp (example.entity.Emp)5 example.entity._Emp (example.entity._Emp)5 BigDecimal (java.math.BigDecimal)5 MockConnection (org.seasar.doma.internal.jdbc.mock.MockConnection)5 BindValue (org.seasar.doma.internal.jdbc.mock.BindValue)4 ColumnMetaData (org.seasar.doma.internal.jdbc.mock.ColumnMetaData)4 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)4 MockResultSetMetaData (org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData)4 SqlFileSelectQuery (org.seasar.doma.jdbc.query.SqlFileSelectQuery)4 RowData (org.seasar.doma.internal.jdbc.mock.RowData)3 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Stream (java.util.stream.Stream)1 BasicInParameter (org.seasar.doma.internal.jdbc.sql.BasicInParameter)1 OptimisticLockException (org.seasar.doma.jdbc.OptimisticLockException)1 AutoDeleteQuery (org.seasar.doma.jdbc.query.AutoDeleteQuery)1