Search in sources :

Example 21 with ResultHandler

use of org.apache.ibatis.session.ResultHandler in project mybatis-3 by mybatis.

the class CommonPropertyDeferLoadTest method testDeferLoadDuringResultHandlerWithLazyLoad.

@Test
void testDeferLoadDuringResultHandlerWithLazyLoad() {
    try (SqlSession sqlSession = lazyLoadSqlSessionFactory.openSession()) {
        class MyResultHandler implements ResultHandler {

            @Override
            public void handleResult(ResultContext context) {
                Child child = (Child) context.getResultObject();
                assertNotNull(child.getFather());
            }
        }
        sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", new MyResultHandler());
    }
}
Also used : ResultContext(org.apache.ibatis.session.ResultContext) SqlSession(org.apache.ibatis.session.SqlSession) ResultHandler(org.apache.ibatis.session.ResultHandler) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 22 with ResultHandler

use of org.apache.ibatis.session.ResultHandler in project sonarqube by SonarSource.

the class IssueMapperTest method scrollClosedByComponentUuid_does_not_return_closed_issues_without_any_status_diff_to_CLOSED.

@Test
@UseDataProvider("closedIssuesSupportedRuleTypes")
public void scrollClosedByComponentUuid_does_not_return_closed_issues_without_any_status_diff_to_CLOSED(RuleType ruleType) {
    ComponentDto component = randomComponent();
    IssueDto issueWithLineDiff = insertNewClosedIssue(component, ruleType);
    IssueChangeDto issueChange = insertToClosedDiff(issueWithLineDiff);
    insertNewClosedIssue(component, ruleType);
    RecorderResultHandler resultHandler = new RecorderResultHandler();
    underTest.scrollClosedByComponentUuid(component.uuid(), NO_FILTERING_ON_CLOSE_DATE, resultHandler);
    assertThat(resultHandler.issues).extracting(IssueDto::getKey, t -> t.getClosedChangeData().get()).containsOnly(tuple(issueWithLineDiff.getKey(), issueChange.getChangeData()));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DbSession(org.sonar.db.DbSession) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) Uuids(org.sonar.core.util.Uuids) DateUtils(org.sonar.api.utils.DateUtils) ComponentTesting(org.sonar.db.component.ComponentTesting) UuidFactoryFast(org.sonar.core.util.UuidFactoryFast) ResultContext(org.apache.ibatis.session.ResultContext) Before(org.junit.Before) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Assertions.tuple(org.assertj.core.api.Assertions.tuple) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) AlwaysIncreasingSystem2(org.sonar.api.impl.utils.AlwaysIncreasingSystem2) ResultHandler(org.apache.ibatis.session.ResultHandler) Test(org.junit.Test) Consumer(java.util.function.Consumer) FieldDiffs(org.sonar.core.issue.FieldDiffs) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) Issue(org.sonar.api.issue.Issue) RuleDto(org.sonar.db.rule.RuleDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 23 with ResultHandler

use of org.apache.ibatis.session.ResultHandler in project sonarqube by SonarSource.

the class IssueMapperTest method scrollClosedByComponentUuid_does_not_return_closed_issues_of_orphan_component.

@Test
@UseDataProvider("closedIssuesSupportedRuleTypes")
public void scrollClosedByComponentUuid_does_not_return_closed_issues_of_orphan_component(RuleType ruleType) {
    ComponentDto component = randomComponent();
    IssueDto issue = insertNewClosedIssue(component, ruleType);
    IssueChangeDto issueChange = insertToClosedDiff(issue);
    IssueDto issueMissingComponent = insertNewClosedIssue(component, ruleType, t -> t.setComponentUuid("does_not_exist"));
    insertToClosedDiff(issueMissingComponent);
    IssueDto issueMissingProject = insertNewClosedIssue(component, ruleType, t -> t.setProjectUuid("does_not_exist"));
    insertToClosedDiff(issueMissingProject);
    RecorderResultHandler resultHandler = new RecorderResultHandler();
    underTest.scrollClosedByComponentUuid(component.uuid(), NO_FILTERING_ON_CLOSE_DATE, resultHandler);
    assertThat(resultHandler.issues).extracting(IssueDto::getKey, t -> t.getClosedChangeData().get()).containsOnly(tuple(issue.getKey(), issueChange.getChangeData()));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DbSession(org.sonar.db.DbSession) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) Uuids(org.sonar.core.util.Uuids) DateUtils(org.sonar.api.utils.DateUtils) ComponentTesting(org.sonar.db.component.ComponentTesting) UuidFactoryFast(org.sonar.core.util.UuidFactoryFast) ResultContext(org.apache.ibatis.session.ResultContext) Before(org.junit.Before) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Assertions.tuple(org.assertj.core.api.Assertions.tuple) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) AlwaysIncreasingSystem2(org.sonar.api.impl.utils.AlwaysIncreasingSystem2) ResultHandler(org.apache.ibatis.session.ResultHandler) Test(org.junit.Test) Consumer(java.util.function.Consumer) FieldDiffs(org.sonar.core.issue.FieldDiffs) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) Issue(org.sonar.api.issue.Issue) RuleDto(org.sonar.db.rule.RuleDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 24 with ResultHandler

use of org.apache.ibatis.session.ResultHandler in project sonarqube by SonarSource.

the class IssueMapperTest method scrollClosedByComponentUuid_does_not_return_row_for_status_change_from_close.

@Test
@UseDataProvider("closedIssuesSupportedRuleTypes")
public void scrollClosedByComponentUuid_does_not_return_row_for_status_change_from_close(RuleType ruleType) {
    ComponentDto component = randomComponent();
    IssueDto issue = insertNewClosedIssue(component, ruleType);
    Date date = new Date();
    IssueChangeDto[] changes = new IssueChangeDto[] { insertToClosedDiff(issue, DateUtils.addDays(date, -10), Issue.STATUS_CLOSED, Issue.STATUS_REOPENED), insertToClosedDiff(issue, DateUtils.addDays(date, -60)), insertToClosedDiff(issue, date), insertToClosedDiff(issue, DateUtils.addDays(date, -5)) };
    RecorderResultHandler resultHandler = new RecorderResultHandler();
    underTest.scrollClosedByComponentUuid(component.uuid(), NO_FILTERING_ON_CLOSE_DATE, resultHandler);
    assertThat(resultHandler.issues).hasSize(3).extracting(IssueDto::getKey, t -> t.getClosedChangeData().get()).containsExactly(tuple(issue.getKey(), changes[2].getChangeData()), tuple(issue.getKey(), changes[3].getChangeData()), tuple(issue.getKey(), changes[1].getChangeData()));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DbSession(org.sonar.db.DbSession) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) Uuids(org.sonar.core.util.Uuids) DateUtils(org.sonar.api.utils.DateUtils) ComponentTesting(org.sonar.db.component.ComponentTesting) UuidFactoryFast(org.sonar.core.util.UuidFactoryFast) ResultContext(org.apache.ibatis.session.ResultContext) Before(org.junit.Before) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Assertions.tuple(org.assertj.core.api.Assertions.tuple) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) AlwaysIncreasingSystem2(org.sonar.api.impl.utils.AlwaysIncreasingSystem2) ResultHandler(org.apache.ibatis.session.ResultHandler) Test(org.junit.Test) Consumer(java.util.function.Consumer) FieldDiffs(org.sonar.core.issue.FieldDiffs) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) Issue(org.sonar.api.issue.Issue) RuleDto(org.sonar.db.rule.RuleDto) ComponentDto(org.sonar.db.component.ComponentDto) Date(java.util.Date) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 25 with ResultHandler

use of org.apache.ibatis.session.ResultHandler in project sonarqube by SonarSource.

the class DbSessionImplTest method all_methods_to_wrapped_SqlSession.

@Test
public void all_methods_to_wrapped_SqlSession() {
    Random random = new Random();
    boolean randomBoolean = random.nextBoolean();
    int randomInt = random.nextInt(200);
    String randomStatement = randomAlphabetic(10);
    Object randomParameter = new Object();
    Cursor<Object> mockCursor = mock(Cursor.class);
    RowBounds rowBounds = new RowBounds();
    Object randomObject = new Object();
    List<Object> randomList = new ArrayList<>();
    Map<Object, Object> randomMap = new HashMap<>();
    String randomMapKey = randomAlphabetic(10);
    ResultHandler randomResultHandler = resultContext -> {
    // don't care
    };
    List<BatchResult> randomBatchResults = new ArrayList<>();
    Configuration randomConfiguration = new Configuration();
    verifyDelegation(DbSessionImpl::commit, s -> verify(s).commit());
    verifyDelegation(t -> t.commit(randomBoolean), s -> verify(s).commit(randomBoolean));
    verifyDelegation(sqlSession -> when(sqlSession.selectCursor(randomStatement)).thenReturn(mockCursor), dbSession -> dbSession.selectCursor(randomStatement), sqlSession -> {
        verify(sqlSession).selectCursor(randomStatement);
        return mockCursor;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectCursor(randomStatement, randomParameter)).thenReturn(mockCursor), dbSession -> dbSession.selectCursor(randomStatement, randomParameter), sqlSession -> {
        verify(sqlSessionMock).selectCursor(randomStatement, randomParameter);
        return mockCursor;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectCursor(randomStatement, randomParameter, rowBounds)).thenReturn(mockCursor), dbSession -> dbSession.selectCursor(randomStatement, randomParameter, rowBounds), sqlSession -> {
        verify(sqlSessionMock).selectCursor(randomStatement, randomParameter, rowBounds);
        return mockCursor;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectOne(randomStatement)).thenReturn(randomObject), dbSession -> dbSession.selectOne(randomStatement), sqlSession -> {
        verify(sqlSession).selectOne(randomStatement);
        return randomObject;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectOne(randomStatement, randomParameter)).thenReturn(randomObject), dbSession -> dbSession.selectOne(randomStatement, randomParameter), sqlSession -> {
        verify(sqlSessionMock).selectOne(randomStatement, randomParameter);
        return randomObject;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectList(randomStatement)).thenReturn(randomList), dbSession -> dbSession.selectList(randomStatement), sqlSession -> {
        verify(sqlSession).selectList(randomStatement);
        return randomList;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectList(randomStatement, randomParameter)).thenReturn(randomList), dbSession -> dbSession.selectList(randomStatement, randomParameter), sqlSession -> {
        verify(sqlSessionMock).selectList(randomStatement, randomParameter);
        return randomList;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectList(randomStatement, randomParameter, rowBounds)).thenReturn(randomList), dbSession -> dbSession.selectList(randomStatement, randomParameter, rowBounds), sqlSession -> {
        verify(sqlSessionMock).selectList(randomStatement, randomParameter, rowBounds);
        return randomList;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectMap(randomStatement, randomMapKey)).thenReturn(randomMap), dbSession -> dbSession.selectMap(randomStatement, randomMapKey), sqlSession -> {
        verify(sqlSession).selectMap(randomStatement, randomMapKey);
        return randomMap;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectMap(randomStatement, randomParameter, randomMapKey)).thenReturn(randomMap), dbSession -> dbSession.selectMap(randomStatement, randomParameter, randomMapKey), sqlSession -> {
        verify(sqlSessionMock).selectMap(randomStatement, randomParameter, randomMapKey);
        return randomMap;
    });
    verifyDelegation(sqlSession -> when(sqlSession.selectMap(randomStatement, randomParameter, randomMapKey, rowBounds)).thenReturn(randomMap), dbSession -> dbSession.selectMap(randomStatement, randomParameter, randomMapKey, rowBounds), sqlSession -> {
        verify(sqlSessionMock).selectMap(randomStatement, randomParameter, randomMapKey, rowBounds);
        return randomMap;
    });
    verifyDelegation(dbSession -> dbSession.select(randomStatement, randomResultHandler), sqlSession -> verify(sqlSessionMock).select(randomStatement, randomResultHandler));
    verifyDelegation(dbSession -> dbSession.select(randomStatement, randomParameter, randomResultHandler), sqlSession -> verify(sqlSession).select(randomStatement, randomParameter, randomResultHandler));
    verifyDelegation(dbSession -> dbSession.select(randomStatement, randomParameter, rowBounds, randomResultHandler), sqlSession -> verify(sqlSessionMock).select(randomStatement, randomParameter, rowBounds, randomResultHandler));
    verifyDelegation(sqlSession -> when(sqlSession.insert(randomStatement)).thenReturn(randomInt), dbSession -> dbSession.insert(randomStatement), sqlSession -> {
        verify(sqlSession).insert(randomStatement);
        return randomInt;
    });
    verifyDelegation(sqlSession -> when(sqlSession.insert(randomStatement, randomParameter)).thenReturn(randomInt), dbSession -> dbSession.insert(randomStatement, randomParameter), sqlSession -> {
        verify(sqlSessionMock).insert(randomStatement, randomParameter);
        return randomInt;
    });
    verifyDelegation(sqlSession -> when(sqlSession.update(randomStatement)).thenReturn(randomInt), dbSession -> dbSession.update(randomStatement), sqlSession -> {
        verify(sqlSession).update(randomStatement);
        return randomInt;
    });
    verifyDelegation(sqlSession -> when(sqlSession.update(randomStatement, randomParameter)).thenReturn(randomInt), dbSession -> dbSession.update(randomStatement, randomParameter), sqlSession -> {
        verify(sqlSessionMock).update(randomStatement, randomParameter);
        return randomInt;
    });
    verifyDelegation(sqlSession -> when(sqlSession.delete(randomStatement)).thenReturn(randomInt), dbSession -> dbSession.delete(randomStatement), sqlSession -> {
        verify(sqlSession).delete(randomStatement);
        return randomInt;
    });
    verifyDelegation(sqlSession -> when(sqlSession.delete(randomStatement, randomParameter)).thenReturn(randomInt), dbSession -> dbSession.delete(randomStatement, randomParameter), sqlSession -> {
        verify(sqlSessionMock).delete(randomStatement, randomParameter);
        return randomInt;
    });
    verifyDelegation(DbSessionImpl::rollback, s -> verify(s).rollback());
    verifyDelegation(t -> t.rollback(randomBoolean), s -> verify(s).rollback(randomBoolean));
    verifyDelegation(sqlSession -> when(sqlSession.flushStatements()).thenReturn(randomBatchResults), DbSessionImpl::flushStatements, sqlSession -> {
        verify(sqlSession).flushStatements();
        return randomBatchResults;
    });
    verifyDelegation(DbSessionImpl::close, s -> verify(s).close());
    verifyDelegation(DbSessionImpl::clearCache, s -> verify(s).clearCache());
    verifyDelegation(sqlSession -> when(sqlSession.getConfiguration()).thenReturn(randomConfiguration), DbSessionImpl::getConfiguration, sqlSession -> {
        verify(sqlSession).getConfiguration();
        return randomConfiguration;
    });
    verifyDelegation(sqlSession -> when(sqlSession.getMapper(DbSessionImplTest.class)).thenReturn(DbSessionImplTest.this), dbSession -> dbSession.getMapper(DbSessionImplTest.class), sqlSession -> {
        verify(sqlSession).getMapper(DbSessionImplTest.class);
        return DbSessionImplTest.this;
    });
    verifyDelegation(DbSessionImpl::getConnection, s -> verify(s).getConnection());
}
Also used : RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Random(java.util.Random) ResultHandler(org.apache.ibatis.session.ResultHandler) Test(org.junit.Test) BatchResult(org.apache.ibatis.executor.BatchResult) Mockito.when(org.mockito.Mockito.when) RowBounds(org.apache.ibatis.session.RowBounds) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) List(java.util.List) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) Cursor(org.apache.ibatis.cursor.Cursor) Configuration(org.apache.ibatis.session.Configuration) SqlSession(org.apache.ibatis.session.SqlSession) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) Configuration(org.apache.ibatis.session.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RowBounds(org.apache.ibatis.session.RowBounds) ResultHandler(org.apache.ibatis.session.ResultHandler) BatchResult(org.apache.ibatis.executor.BatchResult) Random(java.util.Random) Test(org.junit.Test)

Aggregations

ResultHandler (org.apache.ibatis.session.ResultHandler)25 ResultContext (org.apache.ibatis.session.ResultContext)17 ArrayList (java.util.ArrayList)16 List (java.util.List)14 Test (org.junit.Test)13 SqlSession (org.apache.ibatis.session.SqlSession)10 RowBounds (org.apache.ibatis.session.RowBounds)8 Random (java.util.Random)7 Consumer (java.util.function.Consumer)7 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)6 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)6 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)6 Arrays (java.util.Arrays)6 Date (java.util.Date)6 IntStream (java.util.stream.IntStream)6 Executor (org.apache.ibatis.executor.Executor)6 BoundSql (org.apache.ibatis.mapping.BoundSql)6 MappedStatement (org.apache.ibatis.mapping.MappedStatement)6