Search in sources :

Example 21 with ColumnMetaData

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

the class EntitySingleResultHandlerTest method testHandle_NonUniqueResultException.

@Test
public void testHandle_NonUniqueResultException() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "aaa"));
    resultSet.rows.add(new RowData(2, "bbb"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    EntitySingleResultHandler<Emp> handler = new EntitySingleResultHandler<>(_Emp.getSingletonInternal());
    try {
        handler.handle(resultSet, query, (i, next) -> {
        });
        fail();
    } catch (NonUniqueResultException expected) {
    }
}
Also used : NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) RowData(org.seasar.doma.internal.jdbc.mock.RowData) 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) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 22 with ColumnMetaData

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

the class ScalarProviderTest method testOptionalDomain.

@Test
public void testOptionalDomain() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("aaa"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("hoge"));
    resultSet.next();
    DomainType<String, PhoneNumber> domainType = _PhoneNumber.getSingletonInternal();
    ScalarProvider<String, Optional<PhoneNumber>> provider = new ScalarProvider<>(domainType::createOptionalScalar, new MySelectQuery());
    Optional<PhoneNumber> result = provider.get(resultSet);
    assertEquals("hoge", result.get().getValue());
}
Also used : Optional(java.util.Optional) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) RowData(org.seasar.doma.internal.jdbc.mock.RowData) PhoneNumber(example.domain.PhoneNumber) example.domain._PhoneNumber(example.domain._PhoneNumber) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 23 with ColumnMetaData

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

Example 24 with ColumnMetaData

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

the class SelectCommandTest method testExecute_resultStream.

@Test
public void testExecute_resultStream() 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.setFetchType(FetchType.LAZY);
    query.setResultStream(true);
    query.prepare();
    SelectCommand<Stream<Emp>> command = new SelectCommand<>(query, new EntityStreamHandler<>(_Emp.getSingletonInternal(), Function.identity()));
    try (Stream<Emp> stream = command.execute()) {
        query.complete();
        List<Emp> entities = stream.collect(Collectors.toList());
        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());
        assertFalse(runtimeConfig.dataSource.connection.preparedStatement.resultSet.isClosed());
        assertFalse(runtimeConfig.dataSource.connection.preparedStatement.isClosed());
        assertFalse(runtimeConfig.dataSource.connection.isClosed());
    }
    assertTrue(runtimeConfig.dataSource.connection.preparedStatement.resultSet.isClosed());
    assertTrue(runtimeConfig.dataSource.connection.preparedStatement.isClosed());
    assertTrue(runtimeConfig.dataSource.connection.isClosed());
}
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) Stream(java.util.stream.Stream) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)24 ColumnMetaData (org.seasar.doma.internal.jdbc.mock.ColumnMetaData)24 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)24 MockResultSetMetaData (org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData)24 RowData (org.seasar.doma.internal.jdbc.mock.RowData)23 SqlFileSelectQuery (org.seasar.doma.jdbc.query.SqlFileSelectQuery)17 Emp (example.entity.Emp)12 example.entity._Emp (example.entity._Emp)12 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)10 Method (java.lang.reflect.Method)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 TestInfo (org.junit.jupiter.api.TestInfo)8 SqlFileUtil (org.seasar.doma.internal.jdbc.util.SqlFileUtil)8 SqlLogType (org.seasar.doma.jdbc.SqlLogType)8 BigDecimal (java.math.BigDecimal)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 NonUniqueResultException (org.seasar.doma.jdbc.NonUniqueResultException)6 PhoneNumber (example.domain.PhoneNumber)5 example.domain._PhoneNumber (example.domain._PhoneNumber)5 StringWrapper (org.seasar.doma.wrapper.StringWrapper)5