diff options
Diffstat (limited to 'sonar-db')
4 files changed, 73 insertions, 0 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java b/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java index c5885901385..a2e02898891 100644 --- a/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java +++ b/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java @@ -23,6 +23,7 @@ import com.google.common.base.Function; import com.google.common.base.Optional; import java.util.List; import javax.annotation.Nonnull; +import org.apache.ibatis.session.ResultHandler; import org.sonar.api.rule.RuleKey; import org.sonar.db.Dao; import org.sonar.db.DbSession; @@ -56,6 +57,14 @@ public class RuleDao implements Dao { return mapper(session).selectEnabledAndNonManual(); } + public void selectEnabledAndNonManual(DbSession session, ResultHandler resultHandler) { + mapper(session).selectEnabledAndNonManual(resultHandler); + } + + public void insert(DbSession session, RuleDto dto) { + mapper(session).insert(dto); + } + private RuleMapper mapper(DbSession session) { return session.getMapper(RuleMapper.class); } diff --git a/sonar-db/src/main/java/org/sonar/db/rule/RuleMapper.java b/sonar-db/src/main/java/org/sonar/db/rule/RuleMapper.java index 9e957944c2b..84d2859b911 100644 --- a/sonar-db/src/main/java/org/sonar/db/rule/RuleMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/rule/RuleMapper.java @@ -23,6 +23,7 @@ import java.sql.Timestamp; import java.util.List; import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.session.ResultHandler; import org.sonar.api.rule.RuleKey; public interface RuleMapper { @@ -31,6 +32,8 @@ public interface RuleMapper { List<RuleDto> selectEnabledAndNonManual(); + void selectEnabledAndNonManual(ResultHandler resultHandler); + List<RuleDto> selectNonManual(); List<RuleDto> selectBySubCharacteristicId(int characteristicId); diff --git a/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java b/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java index c6265510d76..96bd40611b9 100644 --- a/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java @@ -20,8 +20,12 @@ package org.sonar.db.rule; import com.google.common.base.Optional; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; +import org.apache.ibatis.session.ResultContext; +import org.apache.ibatis.session.ResultHandler; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -113,4 +117,34 @@ public class RuleDaoTest { assertThat(ruleDto.getEffortToFixDescription()).isEqualTo("squid.S115.effortToFix"); } + @Test + public void insert() throws Exception { + dbTester.getDbClient().ruleDao().insert(dbTester.getSession(), RuleTesting.newDto(RuleKey.of("java", "S001")).setConfigKey(null)); + dbTester.getDbClient().ruleDao().insert(dbTester.getSession(), RuleTesting.newDto(RuleKey.of("java", "S002")).setConfigKey("I002")); + dbTester.getSession().commit(); + + List<Map<String, Object>> rows = dbTester.select("select plugin_rule_key as \"ruleKey\" from rules order by plugin_rule_key"); + assertThat(rows).hasSize(2); + assertThat(rows.get(0).get("ruleKey")).isEqualTo("S001"); + assertThat(rows.get(1).get("ruleKey")).isEqualTo("S002"); + + } + + @Test + public void selectEnabledAndNonManual_with_ResultHandler() { + dbTester.prepareDbUnit(getClass(), "selectEnabledAndNonManual.xml"); + + final List<RuleDto> rules = new ArrayList<>(); + ResultHandler resultHandler = new ResultHandler() { + @Override + public void handleResult(ResultContext resultContext) { + rules.add((RuleDto) resultContext.getResultObject()); + } + }; + underTest.selectEnabledAndNonManual(dbTester.getSession(), resultHandler); + + assertThat(rules.size()).isEqualTo(1); + RuleDto ruleDto = rules.get(0); + assertThat(ruleDto.getId()).isEqualTo(1); + } } diff --git a/sonar-db/src/test/resources/org/sonar/db/rule/RuleDaoTest/selectEnabledAndNonManual.xml b/sonar-db/src/test/resources/org/sonar/db/rule/RuleDaoTest/selectEnabledAndNonManual.xml new file mode 100644 index 00000000000..fe85e591078 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/rule/RuleDaoTest/selectEnabledAndNonManual.xml @@ -0,0 +1,27 @@ +<dataset> + + <rules tags="[null]" system_tags="[null]" id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY" + note_data="Rule note with accents éèà" note_user_login="polop.palap" note_created_at="2013-12-25" description_format="HTML" + characteristic_id="100" default_characteristic_id="101" + remediation_function="LINEAR" default_remediation_function="LINEAR_OFFSET" + remediation_coeff="1h" default_remediation_coeff="5d" + remediation_offset="5min" default_remediation_offset="10h" + effort_to_fix_description="squid.S115.effortToFix"/> + + <rules tags="[null]" system_tags="[null]" id="2" plugin_rule_key="AvoidNull" plugin_name="squid" name="Avoid Null" description="Should avoid NULL" status="REMOVED" + note_data="[null]" note_user_login="[null]" note_created_at="[null]" description_format="HTML" + characteristic_id="[null]" default_characteristic_id="[null]" + remediation_function="[null]" default_remediation_function="[null]" + remediation_coeff="[null]" default_remediation_coeff="[null]" + remediation_offset="[null]" default_remediation_offset="[null]" + effort_to_fix_description="[null]"/> + + <rules tags="[null]" system_tags="[null]" id="3" plugin_rule_key="AvoidNull" plugin_name="manual" name="Manual Rule" description="Should not appear" status="READY" + note_data="[null]" note_user_login="[null]" note_created_at="[null]" description_format="HTML" + characteristic_id="[null]" default_characteristic_id="[null]" + remediation_function="[null]" default_remediation_function="[null]" + remediation_coeff="[null]" default_remediation_coeff="[null]" + remediation_offset="[null]" default_remediation_offset="[null]" + effort_to_fix_description="[null]"/> + +</dataset> |