]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5056 Only load characteristics without requirement when Synchronizing rule...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 7 Mar 2014 08:12:15 +0000 (09:12 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 7 Mar 2014 08:12:24 +0000 (09:12 +0100)
sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicDao.java
sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicMapper.java
sonar-core/src/main/resources/org/sonar/core/technicaldebt/db/CharacteristicMapper.xml
sonar-core/src/test/java/org/sonar/core/technicaldebt/db/CharacteristicDaoTest.java
sonar-server/src/main/java/org/sonar/server/rule/RuleRegistration.java
sonar-server/src/test/java/org/sonar/server/rule/RuleRegistrationTest.java
sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistrationTest/ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic-result.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistrationTest/ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistrationTest/shared.xml
sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistrationTest/update_rule_fields.xml
sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistrationTest/update_template_rule_language.xml

index c608784ba68eec0d26ded05d9e16b4797dfe3766..6e6ff49879ae84bb5117d1d6ebe8572a444b4808 100644 (file)
@@ -39,18 +39,34 @@ public class CharacteristicDao implements BatchComponent, ServerComponent {
 
   /**
    * @return enabled root characteristics, characteristics and requirements
+   *
+   * @deprecated since 4.3
    */
+  @Deprecated
   public List<CharacteristicDto> selectEnabledCharacteristics() {
     SqlSession session = mybatis.openSession();
     try {
-      return selectEnabledCharacteristics(session);
+      return session.getMapper(CharacteristicMapper.class).selectEnabledCharacteristics();
     } finally {
       MyBatis.closeQuietly(session);
     }
   }
 
-  public List<CharacteristicDto> selectEnabledCharacteristics(SqlSession session) {
-    return session.getMapper(CharacteristicMapper.class).selectEnabledCharacteristics();
+  /**
+   * @return enabled root characteristics, and characteristics
+   *
+   */
+  public List<CharacteristicDto> selectCharacteristics() {
+    SqlSession session = mybatis.openSession();
+    try {
+      return selectCharacteristics(session);
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  public List<CharacteristicDto> selectCharacteristics(SqlSession session) {
+    return session.getMapper(CharacteristicMapper.class).selectCharacteristics();
   }
 
   /**
index 78b0e2b17c9bcb56892db90962bc214dd57aad7a..7ab1b291874aacafeb4f47cac8111a6a9984e3a3 100644 (file)
@@ -26,6 +26,8 @@ public interface CharacteristicMapper {
 
   List<CharacteristicDto> selectEnabledCharacteristics();
 
+  List<CharacteristicDto> selectCharacteristics();
+
   List<CharacteristicDto> selectEnabledRootCharacteristics();
 
   CharacteristicDto selectByKey(String key);
index b6b3fe2be4ca09dccd3258cdb43c7c2386fbc3c7..2988115ae1688f599b35d74c699cbb66e14fc1bc 100644 (file)
     </where>
   </select>
 
+  <select id="selectCharacteristics" parameterType="map" resultType="Characteristic">
+    select <include refid="characteristicColumns"/>
+    from characteristics c
+    <where>
+      and c.rule_id is null
+      and c.enabled=${_true}
+    </where>
+  </select>
+
   <select id="selectEnabledRootCharacteristics" parameterType="map" resultType="Characteristic">
     select <include refid="characteristicColumns"/>
     from characteristics c
index e9d39138a6eed76ca90d8c18c0b357559eabe75d..0e4341ab81c77ecce792b12f050e879f64d39cd0 100644 (file)
@@ -90,6 +90,37 @@ public class CharacteristicDaoTest extends AbstractDaoTestCase {
     assertThat(requirement.getUpdatedAt()).isNull();
   }
 
+  @Test
+  public void select_characteristics() {
+    setupData("shared");
+
+    List<CharacteristicDto> dtos = dao.selectCharacteristics();
+
+    assertThat(dtos).hasSize(2);
+
+    CharacteristicDto rootCharacteristic = dtos.get(0);
+    assertThat(rootCharacteristic.getId()).isEqualTo(1);
+    assertThat(rootCharacteristic.getKey()).isEqualTo("PORTABILITY");
+    assertThat(rootCharacteristic.getName()).isEqualTo("Portability");
+    assertThat(rootCharacteristic.getParentId()).isNull();
+    assertThat(rootCharacteristic.getRootId()).isNull();
+    assertThat(rootCharacteristic.getOrder()).isEqualTo(1);
+    assertThat(rootCharacteristic.isEnabled()).isTrue();
+    assertThat(rootCharacteristic.getCreatedAt()).isNotNull();
+    assertThat(rootCharacteristic.getUpdatedAt()).isNotNull();
+
+    CharacteristicDto characteristic = dtos.get(1);
+    assertThat(characteristic.getId()).isEqualTo(2);
+    assertThat(characteristic.getKey()).isEqualTo("COMPILER_RELATED_PORTABILITY");
+    assertThat(characteristic.getName()).isEqualTo("Compiler related portability");
+    assertThat(characteristic.getParentId()).isEqualTo(1);
+    assertThat(characteristic.getRootId()).isEqualTo(1);
+    assertThat(characteristic.getOrder()).isNull();
+    assertThat(characteristic.isEnabled()).isTrue();
+    assertThat(characteristic.getCreatedAt()).isNotNull();
+    assertThat(characteristic.getUpdatedAt()).isNotNull();
+  }
+
   @Test
   public void select_enabled_root_characteristics() {
     setupData("select_enabled_root_characteristics");
index 057b61bd598ef30ac7b8642b03463cab72cd743d..e615bc7b138d4829933caf6cd8f2ddfd1afa9461 100644 (file)
@@ -96,7 +96,7 @@ public class RuleRegistration implements Startable {
     try {
       RuleDefinitions.Context context = defLoader.load();
       Buffer buffer = new Buffer(system.now());
-      List<CharacteristicDto> characteristicDtos = characteristicDao.selectEnabledCharacteristics();
+      List<CharacteristicDto> characteristicDtos = characteristicDao.selectCharacteristics();
       selectRulesFromDb(buffer, sqlSession);
       enableRuleDefinitions(context, buffer, characteristicDtos, sqlSession);
       List<RuleDto> removedRules = processRemainingDbRules(buffer, sqlSession);
@@ -546,14 +546,17 @@ public class RuleRegistration implements Startable {
       @Override
       public boolean apply(CharacteristicDto input) {
         String characteristicKey = input.getKey();
-        // TODO remove check on null rule id when only characteristics without requirements will be returned
-        return input.getRuleId() == null && characteristicKey != null && characteristicKey.equals(key);
+        return characteristicKey != null && characteristicKey.equals(key);
       }
     }, null);
-    // TODO check not root characteristic
+
     if (characteristicDto == null) {
-      LOG.warn(String.format("Characteristic : '%s' has not been found, Technical debt definitions on rule '%s:%s' will be ignored", key,
+      LOG.warn(String.format("Characteristic '%s' has not been found, Technical debt definitions on rule '%s:%s' will be ignored",
+        key, ruleDef.repository().name(), ruleDef.key()));
+    } else if (characteristicDto.getParentId() == null) {
+      LOG.error(String.format("Rule '%s:%s' should not be linked on the root characteristic '%s'. Technical debt definitions on this rule wll be ignored", key,
         ruleDef.repository().name(), ruleDef.key()));
+      return null;
     }
     return characteristicDto;
   }
index 7b75b8b8401b5a783664b28c9261fab9d8223e93..f3e9b413fa948efb5da6d4a5aeebdcba2809f90f 100644 (file)
@@ -173,6 +173,14 @@ public class RuleRegistrationTest extends AbstractDaoTestCase {
     checkTables("remove_rule_debt_definitions_if_characteristic_not_found", EXCLUDED_COLUMN_NAMES, "rules");
   }
 
+  @Test
+  public void ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic() {
+    setupData("ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic");
+    task.start();
+
+    checkTables("ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic", EXCLUDED_COLUMN_NAMES, "rules");
+  }
+
   @Test
   public void not_disable_template_rules_if_parent_is_enabled() {
     setupData("not_disable_template_rules_if_parent_is_enabled");
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistrationTest/ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic-result.xml b/sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistrationTest/ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic-result.xml
new file mode 100644 (file)
index 0000000..a773983
--- /dev/null
@@ -0,0 +1,19 @@
+<dataset>
+
+  <rules id="1" plugin_rule_key="rule1" plugin_name="fake" plugin_config_key="config1" name="One" description="Description of One"
+         status="READY" priority="4" cardinality="SINGLE" parent_id="[null]" language="java"
+         characteristic_id="[null]" default_characteristic_id="[null]"
+         remediation_function="[null]" default_remediation_function="[null]"
+         remediation_factor="[null]" default_remediation_factor="[null]"
+         remediation_offset="[null]" default_remediation_offset="[null]"
+         effort_to_fix_l10n_key="[null]"/>
+
+  <rules id="2" plugin_rule_key="rule2" plugin_name="fake" plugin_config_key="[null]" name="Two" description="Description of Two"
+         status="DEPRECATED" priority="0" cardinality="SINGLE" parent_id="[null]" language="java"
+         characteristic_id="[null]" default_characteristic_id="[null]"
+         remediation_function="[null]" default_remediation_function="[null]"
+         remediation_factor="[null]" default_remediation_factor="[null]"
+         remediation_offset="[null]" default_remediation_offset="[null]"
+         effort_to_fix_l10n_key="[null]"/>
+
+</dataset>
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistrationTest/ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic.xml b/sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistrationTest/ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic.xml
new file mode 100644 (file)
index 0000000..8c99f5e
--- /dev/null
@@ -0,0 +1,17 @@
+<dataset>
+
+  <characteristics id="2" kee="MEMORY_EFFICIENCY" name="Efficiency" root_id="[null]" characteristic_order="[null]" enabled="[true]"/>
+
+  <rules id="1" plugin_rule_key="rule1" plugin_name="fake" plugin_config_key="old_config_key" name="old name" description="old description"
+         status="READY" priority="2" cardinality="SINGLE" parent_id="[null]"
+         characteristic_id="[null]" default_characteristic_id="200"
+         remediation_function="[null]" default_remediation_function="LINEAR"
+         remediation_factor="[null]" default_remediation_factor="14min"
+         remediation_offset="[null]" default_remediation_offset="[null]"
+         effort_to_fix_l10n_key="squid.S1115.effortTotFix"/>
+
+  <rules id="2" plugin_rule_key="rule2" plugin_name="fake" plugin_config_key="old_config_key2" name="old name2" description="old description2"
+         status="READY" priority="1" cardinality="SINGLE" parent_id="[null]"/>
+
+
+</dataset>
index 17f652e7255831acf3a4260e8a6998d57c31a0a0..f652085767c6eab5ce435491b25f65b26a6fc304 100644 (file)
@@ -1,6 +1,6 @@
 <dataset>
 
-  <characteristics id="2" kee="MEMORY_EFFICIENCY" name="Memory Efficiency" root_id="1" characteristic_order="1" enabled="[true]"/>
+  <characteristics id="2" kee="MEMORY_EFFICIENCY" name="Memory Efficiency" parent_id="1" root_id="1" characteristic_order="1" enabled="[true]"/>
 
   <rules id="1" plugin_rule_key="deprecated-key" plugin_name="deprecated-repo" plugin_config_key="[null]" name="Deprecated" description="[null]"
                    status="READY" priority="4" cardinality="SINGLE" parent_id="[null]"/>
index cfa18f014e5c6f8be56fe8fbb92b579a803b3378..52e585b5fe9e771555929e0d3353791f4b95ee05 100644 (file)
@@ -1,6 +1,6 @@
 <dataset>
 
-  <characteristics id="200" kee="MEMORY_EFFICIENCY" name="Memory Efficiency" root_id="1" characteristic_order="1" enabled="[true]"/>
+  <characteristics id="200" kee="MEMORY_EFFICIENCY" name="Memory Efficiency" root_id="1" parent_id="1" characteristic_order="1" enabled="[true]"/>
 
   <rules id="1" plugin_rule_key="rule1" plugin_name="fake" plugin_config_key="old_config_key" name="old name" description="old description"
          status="READY" priority="2" cardinality="SINGLE" parent_id="[null]"
index 0ea5e06aa1735b3cc78023908290032dc80d63ee..a4f65e79ca4f5ac661c5a3a8900a08cddb970265 100644 (file)
@@ -1,6 +1,6 @@
 <dataset>
 
-  <characteristics id="100" kee="MEMORY_EFFICIENCY" name="Memory Efficiency" root_id="1" characteristic_order="1" enabled="[true]"/>
+  <characteristics id="100" kee="MEMORY_EFFICIENCY" name="Memory Efficiency" root_id="1" parent_id="1" characteristic_order="1" enabled="[true]"/>
 
   <rules id="1" plugin_rule_key="rule1" plugin_name="fake" plugin_config_key="[null]" name="Rule one" description="[null]"
          status="READY" priority="4" cardinality="MULTIPLE" parent_id="[null]" language="[null]"