summaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2013-12-23 19:32:38 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2013-12-23 19:32:38 +0100
commite1e0a28e63dcb4a4ccfc86f97cbd5d88c3d7d757 (patch)
treeb3d35669978a13754842b0b0c70bb01acf9f72ff /sonar-core/src
parent7dc3fd6a131f853640764ce488e5ce431d4d3624 (diff)
downloadsonarqube-e1e0a28e63dcb4a4ccfc86f97cbd5d88c3d7d757.tar.gz
sonarqube-e1e0a28e63dcb4a4ccfc86f97cbd5d88c3d7d757.zip
SONAR-4535 Create copy rule from Java facade
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java22
-rw-r--r--sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java21
-rw-r--r--sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java4
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml19
-rw-r--r--sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java33
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/insert_parameter-result.xml3
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/insert_parameter.xml3
7 files changed, 74 insertions, 31 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 2446eb97f54..c768b85e0ad 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
@@ -74,10 +74,14 @@ public class RuleDao implements BatchComponent, ServerComponent {
}
}
+ public void insert(RuleDto ruleToInsert, SqlSession session) {
+ getMapper(session).insert(ruleToInsert);
+ }
+
public void insert(RuleDto ruleToInsert) {
SqlSession session = mybatis.openSession();
try {
- getMapper(session).insert(ruleToInsert);
+ insert(ruleToInsert, session);
session.commit();
} finally {
MyBatis.closeQuietly(session);
@@ -88,7 +92,7 @@ public class RuleDao implements BatchComponent, ServerComponent {
SqlSession session = mybatis.openBatchSession();
try {
for (RuleDto rule : rules) {
- getMapper(session).insert(rule);
+ getMapper(session).batchInsert(rule);
}
session.commit();
} finally {
@@ -118,6 +122,20 @@ public class RuleDao implements BatchComponent, ServerComponent {
return getMapper(session).selectParamsForRule(ruleId);
}
+ public void insert(RuleParamDto param, SqlSession session) {
+ getMapper(session).insertParameter(param);
+ }
+
+ public void insert(RuleParamDto param) {
+ SqlSession session = mybatis.openSession();
+ try {
+ insert(param, session);
+ session.commit();
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
@CheckForNull
public RuleParamDto selectParamByRuleAndKey(Integer ruleId, String key, SqlSession session) {
return getMapper(session).selectParamByRuleAndKey(ruleId, key);
diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java
index 7af9e56ff6f..35065011f6e 100644
--- a/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java
+++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java
@@ -20,7 +20,6 @@
package org.sonar.core.rule;
import org.sonar.check.Cardinality;
-import org.sonar.check.Priority;
import java.util.Date;
@@ -32,7 +31,7 @@ public final class RuleDto {
private String status;
private String name;
private String configKey;
- private Priority priority;
+ private Integer severity;
private Cardinality cardinality;
private String language;
private Integer parentId;
@@ -106,22 +105,12 @@ public final class RuleDto {
return this;
}
- public Priority getPriority() {
- return priority;
+ public Integer getSeverity() {
+ return severity;
}
- public int getPriorityOrdinal() {
- return priority.ordinal();
- }
-
- // TODO do not use Priority
- public RuleDto setPriority(Priority priority) {
- this.priority = priority;
- return this;
- }
-
- public RuleDto setPriorityOrdinal(int ordinal) {
- this.priority = Priority.values()[ordinal];
+ public RuleDto setSeverity(Integer severity) {
+ this.severity = severity;
return this;
}
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 23c93e672c8..405220f7fab 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
@@ -32,6 +32,8 @@ public interface RuleMapper {
void update(RuleDto rule);
+ void batchInsert(RuleDto rule);
+
void insert(RuleDto rule);
List<RuleParamDto> selectAllParams();
@@ -39,4 +41,6 @@ public interface RuleMapper {
List<RuleParamDto> selectParamsForRule(Integer id);
RuleParamDto selectParamByRuleAndKey(@Param("ruleId") Integer ruleId, @Param("key") String key);
+
+ void insertParameter(RuleParamDto param);
}
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 0979713ca73..e86859e542b 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
@@ -11,7 +11,7 @@
status,
name,
plugin_config_key as "configKey",
- priority as "priorityOrdinal",
+ priority as "severity",
cardinality,
language as "language",
parent_id as "parentId",
@@ -44,7 +44,7 @@
status=#{status},
name=#{name},
plugin_config_key=#{configKey},
- priority=#{priorityOrdinal},
+ priority=#{severity},
cardinality=#{cardinality},
language=#{language},
parent_id=#{parentId},
@@ -61,10 +61,16 @@
parent_id, created_at, updated_at)
</sql>
- <insert id="insert" parameterType="Rule" useGeneratedKeys="false">
+ <insert id="insert" parameterType="Rule" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
insert into rules <include refid="insertColumns"/>
values (#{ruleKey}, #{repositoryKey}, #{description}, #{status}, #{name}, #{configKey},
- #{priorityOrdinal}, #{cardinality}, #{language}, #{parentId}, #{createdAt}, #{updatedAt})
+ #{severity}, #{cardinality}, #{language}, #{parentId}, #{createdAt}, #{updatedAt})
+ </insert>
+
+ <insert id="batchInsert" parameterType="Rule" useGeneratedKeys="false">
+ insert into rules <include refid="insertColumns"/>
+ values (#{ruleKey}, #{repositoryKey}, #{description}, #{status}, #{name}, #{configKey},
+ #{severity}, #{cardinality}, #{language}, #{parentId}, #{createdAt}, #{updatedAt})
</insert>
<delete id="deleteParams" parameterType="Integer" >
@@ -95,5 +101,10 @@
</where>
</select>
+ <insert id="insertParameter" parameterType="Rule" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+ INSERT INTO RULES_PARAMETERS (rule_id, name, param_type, default_value, description)
+ VALUES (#{ruleId}, #{name}, #{type}, #{defaultValue}, #{description})
+ </insert>
+
</mapper>
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 7844e2a7e2f..825ec610de8 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
@@ -25,7 +25,6 @@ import org.junit.Test;
import org.sonar.api.rules.Rule;
import org.sonar.api.utils.DateUtils;
import org.sonar.check.Cardinality;
-import org.sonar.check.Priority;
import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.Date;
@@ -95,7 +94,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
.setDescription("new description")
.setStatus(Rule.STATUS_DEPRECATED)
.setConfigKey("NewConfigKey")
- .setPriority(Priority.INFO)
+ .setSeverity(0)
.setCardinality(Cardinality.MULTIPLE)
.setLanguage("dart")
.setParentId(3)
@@ -120,7 +119,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
String newDescription = "new description";
String newStatus = Rule.STATUS_DEPRECATED;
String newConfigKey = "NewConfigKey";
- Priority newPriority = Priority.INFO;
+ Integer newSeverity = 0;
Cardinality newCardinality = Cardinality.MULTIPLE;
String newLanguage = "dart";
Date updatedAt = new Date();
@@ -132,7 +131,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
ruleToInsert.setDescription(newDescription);
ruleToInsert.setStatus(newStatus);
ruleToInsert.setConfigKey(newConfigKey);
- ruleToInsert.setPriority(newPriority);
+ ruleToInsert.setSeverity(0);
ruleToInsert.setCardinality(newCardinality);
ruleToInsert.setLanguage(newLanguage);
ruleToInsert.setUpdatedAt(updatedAt);
@@ -150,7 +149,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
assertThat(insertedRule.getDescription()).isEqualTo(newDescription);
assertThat(insertedRule.getStatus()).isEqualTo(newStatus);
assertThat(insertedRule.getConfigKey()).isEqualTo(newConfigKey);
- assertThat(insertedRule.getPriority()).isEqualTo(newPriority);
+ assertThat(insertedRule.getSeverity()).isEqualTo(newSeverity);
assertThat(insertedRule.getCardinality()).isEqualTo(newCardinality);
assertThat(insertedRule.getLanguage()).isEqualTo(newLanguage);
assertThat(insertedRule.getParentId()).isEqualTo(newParentId);
@@ -166,7 +165,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
String newDescription = "new description1";
String newStatus = Rule.STATUS_DEPRECATED;
String newConfigKey = "NewConfigKey1";
- Priority newPriority = Priority.INFO;
+ Integer newSeverity = 0;
Cardinality newCardinality = Cardinality.MULTIPLE;
String newLanguage = "dart";
Date createdAt = new Date();
@@ -178,7 +177,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
ruleToInsert1.setDescription(newDescription);
ruleToInsert1.setStatus(newStatus);
ruleToInsert1.setConfigKey(newConfigKey);
- ruleToInsert1.setPriority(newPriority);
+ ruleToInsert1.setSeverity(newSeverity);
ruleToInsert1.setCardinality(newCardinality);
ruleToInsert1.setLanguage(newLanguage);
ruleToInsert1.setCreatedAt(createdAt);
@@ -191,7 +190,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
String newDescription1 = "new description2";
String newStatus1 = Rule.STATUS_DEPRECATED;
String newConfigKey1 = "NewConfigKey2";
- Priority newPriority1 = Priority.INFO;
+ Integer newSeverity1 = 0;
Cardinality newCardinality1 = Cardinality.MULTIPLE;
String newLanguage1 = "dart";
Date createdAt1 = new Date();
@@ -203,7 +202,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
ruleToInsert2.setDescription(newDescription1);
ruleToInsert2.setStatus(newStatus1);
ruleToInsert2.setConfigKey(newConfigKey1);
- ruleToInsert2.setPriority(newPriority1);
+ ruleToInsert2.setSeverity(newSeverity1);
ruleToInsert2.setCardinality(newCardinality1);
ruleToInsert2.setLanguage(newLanguage1);
ruleToInsert2.setCreatedAt(createdAt1);
@@ -245,4 +244,20 @@ public class RuleDaoTest extends AbstractDaoTestCase {
assertThat(ruleDto.getType()).isEqualTo("plop");
assertThat(ruleDto.getRuleId()).isEqualTo(ruleId);
}
+
+ @Test
+ public void insert_parameter() {
+ setupData("insert_parameter");
+
+ RuleParamDto param = new RuleParamDto()
+ .setRuleId(1)
+ .setName("max")
+ .setType("INTEGER")
+ .setDefaultValue("30")
+ .setDescription("My Parameter");
+
+ dao.insert(param);
+
+ checkTables("insert_parameter", "rules_parameters");
+ }
}
diff --git a/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/insert_parameter-result.xml b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/insert_parameter-result.xml
new file mode 100644
index 00000000000..5208b7a4a4c
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/insert_parameter-result.xml
@@ -0,0 +1,3 @@
+<dataset>
+ <rules_parameters id="1" rule_id="1" name="max" param_type="INTEGER" default_value="30" description="My Parameter"/>
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/insert_parameter.xml b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/insert_parameter.xml
new file mode 100644
index 00000000000..871dedcb5e9
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/insert_parameter.xml
@@ -0,0 +1,3 @@
+<dataset>
+
+</dataset>