Search in sources :

Example 11 with ResultHandler

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

the class IssueMapperTest method scrollClosedByComponentUuid_return_one_row_per_status_diff_to_CLOSED_sorted_by_most_recent_creation_date_first.

@Test
@UseDataProvider("closedIssuesSupportedRuleTypes")
public void scrollClosedByComponentUuid_return_one_row_per_status_diff_to_CLOSED_sorted_by_most_recent_creation_date_first(RuleType ruleType) {
    ComponentDto component = randomComponent();
    IssueDto issue = insertNewClosedIssue(component, ruleType);
    Date date = new Date();
    IssueChangeDto[] changes = new IssueChangeDto[] { insertToClosedDiff(issue, DateUtils.addDays(date, -10)), 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(4).extracting(IssueDto::getKey, t -> t.getClosedChangeData().get()).containsExactly(tuple(issue.getKey(), changes[2].getChangeData()), tuple(issue.getKey(), changes[3].getChangeData()), tuple(issue.getKey(), changes[0].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 12 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_non_existing_rule.

@Test
@UseDataProvider("closedIssuesSupportedRuleTypes")
public void scrollClosedByComponentUuid_does_not_return_closed_issues_of_non_existing_rule(RuleType ruleType) {
    ComponentDto component = randomComponent();
    IssueDto issueWithRule = insertNewClosedIssue(component, ruleType);
    IssueChangeDto issueChange = insertToClosedDiff(issueWithRule);
    IssueDto issueWithoutRule = insertNewClosedIssue(component, new RuleDefinitionDto().setType(ruleType).setUuid("uuid-50"));
    insertToClosedDiff(issueWithoutRule);
    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(issueWithRule.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) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 13 with ResultHandler

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

the class DBSessionsImplTest method openSession_with_caching_returns_wrapper_of_MyBatis_DbSession_which_delegates_all_methods_but_close.

@Test
public void openSession_with_caching_returns_wrapper_of_MyBatis_DbSession_which_delegates_all_methods_but_close() {
    boolean batchOrRegular = random.nextBoolean();
    underTest.enableCaching();
    verifyFirstDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        dbSession.rollback();
        verify(myBatisDbSession).rollback();
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        boolean flag = random.nextBoolean();
        dbSession.rollback(flag);
        verify(myBatisDbSession).rollback(flag);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        dbSession.commit();
        verify(myBatisDbSession).commit();
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        boolean flag = random.nextBoolean();
        dbSession.commit(flag);
        verify(myBatisDbSession).commit(flag);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        dbSession.selectOne(str);
        verify(myBatisDbSession).selectOne(str);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object object = new Object();
        dbSession.selectOne(str, object);
        verify(myBatisDbSession).selectOne(str, object);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        dbSession.selectList(str);
        verify(myBatisDbSession).selectList(str);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object object = new Object();
        dbSession.selectList(str, object);
        verify(myBatisDbSession).selectList(str, object);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object parameter = new Object();
        RowBounds rowBounds = new RowBounds();
        dbSession.selectList(str, parameter, rowBounds);
        verify(myBatisDbSession).selectList(str, parameter, rowBounds);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        String mapKey = randomAlphabetic(10);
        dbSession.selectMap(str, mapKey);
        verify(myBatisDbSession).selectMap(str, mapKey);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object parameter = new Object();
        String mapKey = randomAlphabetic(10);
        dbSession.selectMap(str, parameter, mapKey);
        verify(myBatisDbSession).selectMap(str, parameter, mapKey);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object parameter = new Object();
        String mapKey = randomAlphabetic(10);
        RowBounds rowBounds = new RowBounds();
        dbSession.selectMap(str, parameter, mapKey, rowBounds);
        verify(myBatisDbSession).selectMap(str, parameter, mapKey, rowBounds);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        ResultHandler handler = mock(ResultHandler.class);
        dbSession.select(str, handler);
        verify(myBatisDbSession).select(str, handler);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object parameter = new Object();
        ResultHandler handler = mock(ResultHandler.class);
        dbSession.select(str, parameter, handler);
        verify(myBatisDbSession).select(str, parameter, handler);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object parameter = new Object();
        ResultHandler handler = mock(ResultHandler.class);
        RowBounds rowBounds = new RowBounds();
        dbSession.select(str, parameter, rowBounds, handler);
        verify(myBatisDbSession).select(str, parameter, rowBounds, handler);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        dbSession.insert(str);
        verify(myBatisDbSession).insert(str);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object object = new Object();
        dbSession.insert(str, object);
        verify(myBatisDbSession).insert(str, object);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        dbSession.update(str);
        verify(myBatisDbSession).update(str);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object object = new Object();
        dbSession.update(str, object);
        verify(myBatisDbSession).update(str, object);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        dbSession.delete(str);
        verify(myBatisDbSession).delete(str);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        String str = randomAlphabetic(10);
        Object object = new Object();
        dbSession.delete(str, object);
        verify(myBatisDbSession).delete(str, object);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        dbSession.flushStatements();
        verify(myBatisDbSession).flushStatements();
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        dbSession.clearCache();
        verify(myBatisDbSession).clearCache();
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        Configuration expected = mock(Configuration.class);
        when(myBatisDbSession.getConfiguration()).thenReturn(expected);
        assertThat(dbSession.getConfiguration()).isSameAs(expected);
        verify(myBatisDbSession).getConfiguration();
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        Class<Object> clazz = Object.class;
        Object expected = new Object();
        when(myBatisDbSession.getMapper(clazz)).thenReturn(expected);
        assertThat(dbSession.getMapper(clazz)).isSameAs(expected);
        verify(myBatisDbSession).getMapper(clazz);
    });
    verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
        Connection connection = mock(Connection.class);
        when(myBatisDbSession.getConnection()).thenReturn(connection);
        assertThat(dbSession.getConnection()).isSameAs(connection);
        verify(myBatisDbSession).getConnection();
    });
}
Also used : Configuration(org.apache.ibatis.session.Configuration) Connection(java.sql.Connection) RowBounds(org.apache.ibatis.session.RowBounds) ResultHandler(org.apache.ibatis.session.ResultHandler) Test(org.junit.Test)

Example 14 with ResultHandler

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

the class ComponentDaoTest method assertSelectForIndexing.

private ListAssert<String> assertSelectForIndexing(@Nullable String projectUuid) {
    db.prepareDbUnit(getClass(), "selectForIndexing.xml");
    List<ComponentDto> components = new ArrayList<>();
    underTest.selectForIndexing(dbSession, projectUuid, new ResultHandler() {

        @Override
        public void handleResult(ResultContext context) {
            components.add((ComponentDto) context.getResultObject());
        }
    });
    return assertThat(components).extracting(ComponentDto::uuid);
}
Also used : ResultContext(org.apache.ibatis.session.ResultContext) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ResultHandler(org.apache.ibatis.session.ResultHandler)

Example 15 with ResultHandler

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

the class CommonPropertyDeferLoadError method testDeferLoadAfterResultHandlerWithLazyLoad.

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

            List<Child> children = new ArrayList<Child>();

            @Override
            public void handleResult(ResultContext context) {
                Child child = (Child) context.getResultObject();
                children.add(child);
            }
        }
        ;
        MyResultHandler myResultHandler = new MyResultHandler();
        sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", myResultHandler);
        for (Child child : myResultHandler.children) {
            assertNotNull(child.getFather());
        }
    } finally {
        sqlSession.close();
    }
}
Also used : ResultContext(org.apache.ibatis.session.ResultContext) SqlSession(org.apache.ibatis.session.SqlSession) ArrayList(java.util.ArrayList) List(java.util.List) ResultHandler(org.apache.ibatis.session.ResultHandler) 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