diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-12-23 22:36:03 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-12-23 22:36:03 +0100 |
commit | 5e87ef770b0195d7abfbcf071232940ae14588b5 (patch) | |
tree | ac2bf56f28eaba4538792c8a40cb59cd91c752e8 /sonar-core | |
parent | 5e34fbd623c274fbed43151ef3fb561ae92c99ca (diff) | |
download | sonarqube-5e87ef770b0195d7abfbcf071232940ae14588b5.tar.gz sonarqube-5e87ef770b0195d7abfbcf071232940ae14588b5.zip |
SONAR-4535 Add some check when creating new rule
Diffstat (limited to 'sonar-core')
5 files changed, 33 insertions, 1 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java index c768b85e0ad..482835a57b6 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java @@ -25,7 +25,6 @@ import org.sonar.api.ServerComponent; import org.sonar.core.persistence.MyBatis; import javax.annotation.CheckForNull; - import java.util.Collection; import java.util.List; @@ -64,6 +63,15 @@ public class RuleDao implements BatchComponent, ServerComponent { } } + public RuleDto selectByName(String name) { + SqlSession session = mybatis.openSession(); + try { + return getMapper(session).selectByName(name); + } finally { + MyBatis.closeQuietly(session); + } + } + public void update(RuleDto rule) { SqlSession session = mybatis.openSession(); try { diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java index 405220f7fab..9b2698d9453 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java @@ -30,6 +30,8 @@ public interface RuleMapper { RuleDto selectById(Integer id); + RuleDto selectByName(String name); + void update(RuleDto rule); void batchInsert(RuleDto rule); diff --git a/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml b/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml index 96243fe2028..efa9a1f7290 100644 --- a/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml @@ -31,6 +31,10 @@ select <include refid="selectColumns"/> from rules WHERE id=#{id} </select> + <select id="selectByName" parameterType="String" resultType="Rule"> + select <include refid="selectColumns"/> from rules WHERE name=#{name} + </select> + <select id="selectNonManual" resultType="Rule"> select <include refid="selectColumns"/> from rules where plugin_name != 'manual' diff --git a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java index 825ec610de8..f58e4790a15 100644 --- a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java @@ -69,6 +69,18 @@ public class RuleDaoTest extends AbstractDaoTestCase { } @Test + public void select_by_name() throws Exception { + setupData("select_by_name"); + RuleDto ruleDto = dao.selectByName("Avoid Null"); + + assertThat(ruleDto.getId()).isEqualTo(2); + assertThat(ruleDto.getName()).isEqualTo("Avoid Null"); + assertThat(ruleDto.getDescription()).isEqualTo("Should avoid NULL"); + assertThat(ruleDto.getStatus()).isEqualTo(Rule.STATUS_READY); + assertThat(ruleDto.getRepositoryKey()).isEqualTo("checkstyle"); + } + + @Test public void testSelectNonManual() throws Exception { setupData("selectNonManual"); List<RuleDto> ruleDtos = dao.selectNonManual(); diff --git a/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_by_name.xml b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_by_name.xml new file mode 100644 index 00000000000..dc8fe1e2615 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_by_name.xml @@ -0,0 +1,6 @@ +<dataset> + + <rules id="1" plugin_rule_key="AvoidComparison" plugin_name="checkstyle" name="Avoid Comparison" description="Should avoid ==" status="READY"/> + <rules id="2" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY"/> + +</dataset>
\ No newline at end of file |