]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 6 Apr 2016 08:31:09 +0000 (10:31 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 6 Apr 2016 08:31:15 +0000 (10:31 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/measure/api/MeasureComputerContextImpl.java
server/sonar-server/src/main/java/org/sonar/server/issue/SetTypeAction.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/IssuesWs.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/SetTypeAction.java
server/sonar-server/src/main/java/org/sonar/server/rule/NewCustomRule.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java
sonar-db/src/main/java/org/sonar/db/version/v50/ReplaceIssueFiltersProjectKeyByUuid.java
sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java
sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleType.java
sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTypeTest.java

index 8a0c597be6ecf57f63c7566407699d7d788b7b3c..d3112cdd51db73f1966b8755a4c671265c15e25c 100644 (file)
@@ -68,15 +68,6 @@ public class MeasureComputerContextImpl implements MeasureComputerContext {
     this.componentIssues = componentIssuesRepository.getIssues(component);
   }
 
-  private Component newComponent(org.sonar.server.computation.component.Component component) {
-    return new ComponentImpl(
-      component.getKey(),
-      Component.Type.valueOf(component.getType().name()),
-      component.getType() == org.sonar.server.computation.component.Component.Type.FILE ?
-        new ComponentImpl.FileAttributesImpl(component.getFileAttributes().getLanguageKey(), component.getFileAttributes().isUnitTest()) :
-        null);
-  }
-
   /**
    * Definition needs to be reset each time a new computer is processed.
    * Defining it by a setter allows to reduce the number of this class to be created (one per component instead of one per component and per computer).
@@ -191,6 +182,14 @@ public class MeasureComputerContextImpl implements MeasureComputerContext {
     return componentIssues;
   }
 
+  private static Component newComponent(org.sonar.server.computation.component.Component component) {
+    return new ComponentImpl(
+      component.getKey(),
+      Component.Type.valueOf(component.getType().name()),
+      component.getType() == org.sonar.server.computation.component.Component.Type.FILE
+        ? new ComponentImpl.FileAttributesImpl(component.getFileAttributes().getLanguageKey(), component.getFileAttributes().isUnitTest()) : null);
+  }
+
   private class ComponentToMeasure implements Function<org.sonar.server.computation.component.Component, Optional<org.sonar.server.computation.measure.Measure>> {
 
     private final Metric metric;
index 9a7887cf23c7d7aa7b1f9f2bfe64fcddf9816cdd..a1b2fbb1b987e84468dd73fc59d0af51775675f9 100644 (file)
@@ -58,7 +58,7 @@ public class SetTypeAction extends Action {
   private static String newValue(Map<String, Object> properties) {
     String type = (String) properties.get(TYPE_PARAMETER);
     Preconditions.checkArgument(!isNullOrEmpty(type), "Missing parameter: '%s'", TYPE_PARAMETER);
-    Preconditions.checkArgument(RuleType.ALL_NAMES.contains(type), "Unknown type: %s", type);
+    Preconditions.checkArgument(RuleType.names().contains(type), "Unknown type: %s", type);
     return type;
   }
 }
index f79064ed21128a5e15710ae08bc2aa21ea71a511..8b0c4db9f28046a68216d1a12ab2598309af7780 100644 (file)
@@ -164,7 +164,7 @@ public class IssuesWs implements WebService {
     action.createParam("set_type.type")
       .setDescription("To change the type of the list of issues")
       .setExampleValue(RuleType.BUG)
-      .setPossibleValues(RuleType.ALL_NAMES)
+      .setPossibleValues(RuleType.names())
       .setSince("5.5");
     action.createParam("plan.plan")
       .setDescription("In 5.5, action plans are dropped. Has no effect. To plan the list of issues to a specific action plan (key), or unlink all the issues from an action plan")
index bef890e4a95cb0ededba635f7f04abb9853c4337..09dfe2cb7dbe664769df2bc6a0495bfbd6d98b6e 100644 (file)
@@ -361,7 +361,7 @@ public class SearchAction implements IssuesWsAction {
     addMandatoryValuesToFacet(facets, RULES, request.getRules());
     addMandatoryValuesToFacet(facets, LANGUAGES, request.getLanguages());
     addMandatoryValuesToFacet(facets, TAGS, request.getTags());
-    addMandatoryValuesToFacet(facets, TYPES, RuleType.ALL_NAMES);
+    addMandatoryValuesToFacet(facets, TYPES, RuleType.names());
     List<String> actionPlans = Lists.newArrayList("");
     List<String> actionPlansFromRequest = request.getActionPlans();
     if (actionPlansFromRequest != null) {
index af082ffb6706a2bb0e109ff929265ca6333b1989..d02b73cadeab0524263e0da29ad929e15eed7262 100644 (file)
@@ -53,7 +53,7 @@ public class SetTypeAction implements IssuesWsAction {
     action.createParam("type")
       .setDescription("New type")
       .setRequired(true)
-      .setPossibleValues(RuleType.ALL_NAMES);
+      .setPossibleValues(RuleType.names());
   }
 
   @Override
index 15c3314ddd735f673c8c09d1dffd3ba645670fbd..f83be6f95114afce2c9cf2f4abfb08776d473046 100644 (file)
@@ -104,10 +104,6 @@ public class NewCustomRule {
     return this;
   }
 
-  public Map<String, String> parameters() {
-    return parameters;
-  }
-
   @CheckForNull
   public String parameter(final String paramKey) {
     return parameters.get(paramKey);
index d931ebf91622fcabd664b7b5e23641ec9b6a45e7..f59dd90752c8788b036d0368e747c8c896c07ab7 100644 (file)
@@ -434,7 +434,7 @@ public class SearchAction implements RulesWsAction {
     addMandatoryFacetValues(results, FACET_SEVERITIES, Severity.ALL);
     addMandatoryFacetValues(results, FACET_ACTIVE_SEVERITIES, Severity.ALL);
     addMandatoryFacetValues(results, FACET_TAGS, request.getTags());
-    addMandatoryFacetValues(results, FACET_TYPES, RuleType.ALL_NAMES);
+    addMandatoryFacetValues(results, FACET_TYPES, RuleType.names());
 
     Common.Facet.Builder facet = Common.Facet.newBuilder();
     Common.FacetValue.Builder value = Common.FacetValue.newBuilder();
@@ -478,10 +478,10 @@ public class SearchAction implements RulesWsAction {
     }
   }
 
-  protected void addMandatoryFacetValues(SearchResult results, String facetName, @Nullable List<String> mandatoryValues) {
+  protected void addMandatoryFacetValues(SearchResult results, String facetName, @Nullable Collection<String> mandatoryValues) {
     Map<String, Long> facetValues = results.facets.get(facetName);
     if (facetValues != null) {
-      List<String> valuesToAdd = mandatoryValues == null ? Lists.<String>newArrayList() : mandatoryValues;
+      Collection<String> valuesToAdd = mandatoryValues == null ? Lists.<String>newArrayList() : mandatoryValues;
       for (String item : valuesToAdd) {
         if (!facetValues.containsKey(item)) {
           facetValues.put(item, 0L);
index d8945971a12e2a046963892abdb5109b31e35bf5..edcba952483d7aea0a55a44ad8dd671f9e9727bb 100644 (file)
@@ -82,7 +82,7 @@ public class ReplaceIssueFiltersProjectKeyByUuid extends BaseDataChange {
     }
   }
 
-  private String convertData(PreparedStatement pstmt, String data) throws SQLException {
+  private static String convertData(PreparedStatement pstmt, String data) throws SQLException {
     StringBuilder newFields = new StringBuilder();
     String[] fields = data.split("\\|");
     for (int i = 0; i < fields.length; i++) {
index 17d05a9d09b8b98df12bffb718a90fee8d32f274..9567f8ecaa96577323104b9910c9e0720d98eacb 100644 (file)
@@ -48,8 +48,6 @@ import static org.sonar.db.qualityprofile.ActiveRuleDto.INHERITED;
 import static org.sonar.db.qualityprofile.ActiveRuleDto.OVERRIDES;
 import static org.sonar.db.qualityprofile.ActiveRuleDto.createFor;
 
-// TODO add missing tests
-
 public class ActiveRuleDaoTest {
 
   @Rule
@@ -232,6 +230,30 @@ public class ActiveRuleDaoTest {
     assertThat(result.getUpdatedAt()).isEqualTo(2000L);
   }
 
+  @Test
+  public void fail_to_insert_when_profile_id_is_null() throws Exception {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Quality profile is not persisted (missing id)");
+
+    underTest.insert(dbTester.getSession(), createFor(profile1, rule1).setProfileId(null));
+  }
+
+  @Test
+  public void fail_to_insert_when_rule_id_is_null() throws Exception {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Rule is not persisted");
+
+    underTest.insert(dbTester.getSession(), createFor(profile1, rule1).setRuleId(null));
+  }
+
+  @Test
+  public void fail_to_insert_when_id_is_not_null() throws Exception {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("ActiveRule is already persisted");
+
+    underTest.insert(dbTester.getSession(), createFor(profile1, rule1).setId(100));
+  }
+
   @Test
   public void update() throws Exception {
     ActiveRuleDto activeRule = createFor(profile1, rule1)
@@ -262,6 +284,30 @@ public class ActiveRuleDaoTest {
     assertThat(result.getUpdatedAt()).isEqualTo(4000L);
   }
 
+  @Test
+  public void fail_to_update_when_profile_id_is_null() throws Exception {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Quality profile is not persisted (missing id)");
+
+    underTest.update(dbTester.getSession(), createFor(profile1, rule1).setId(100).setProfileId(null));
+  }
+
+  @Test
+  public void fail_to_update_when_rule_id_is_null() throws Exception {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Rule is not persisted");
+
+    underTest.update(dbTester.getSession(), createFor(profile1, rule1).setId(100).setRuleId(null));
+  }
+
+  @Test
+  public void fail_to_update_when_id_is_null() throws Exception {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("ActiveRule is not persisted");
+
+    underTest.update(dbTester.getSession(), createFor(profile1, rule1).setId(null));
+  }
+
   @Test
   public void delete() throws Exception {
     ActiveRuleDto activeRule = createFor(profile1, rule1)
@@ -278,6 +324,11 @@ public class ActiveRuleDaoTest {
     assertThat(underTest.selectByKey(dbSession,  ActiveRuleKey.of(profile1.getKey(), rule1.getKey()))).isAbsent();
   }
 
+  @Test
+  public void does_not_fail_when_active_rule_does_not_exist() throws Exception {
+    underTest.delete(dbSession, ActiveRuleKey.of(profile1.getKey(), rule1.getKey()));
+  }
+
   @Test
   public void select_params_by_active_rule_id() throws Exception {
     ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(BLOCKER);
@@ -354,6 +405,36 @@ public class ActiveRuleDaoTest {
     assertThat(result.getValue()).isEqualTo("activeValue1");
   }
 
+  @Test
+  public void fail_to_insert_param_when_active_rule_id_is_null() throws Exception {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("ActiveRule is not persisted");
+
+    underTest.insertParam(dbTester.getSession(),
+      createFor(profile1, rule1).setId(null),
+      ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1"));
+  }
+
+  @Test
+  public void fail_to_insert_param_when_active_rule_param_id_is_null() throws Exception {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("ActiveRuleParam is already persisted");
+
+    underTest.insertParam(dbTester.getSession(),
+      createFor(profile1, rule1).setId(100),
+      ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setId(100));
+  }
+
+  @Test
+  public void fail_to_insert_param_when_id_is_not_null() throws Exception {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("Rule param is not persisted");
+
+    underTest.insertParam(dbTester.getSession(),
+      createFor(profile1, rule1).setId(100),
+      ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setRulesParameterId(null));
+  }
+
   @Test
   public void update_param() throws Exception {
     ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(BLOCKER);
@@ -401,6 +482,22 @@ public class ActiveRuleDaoTest {
     assertThat(underTest.selectParamByKeyAndName(activeRule.getKey(), activeRuleParam1.getKey(), dbSession)).isNull();
   }
 
+  @Test
+  public void does_not_fail_to_delete_param_by_key_and_name_when_active_rule_does_not_exist() throws Exception {
+    underTest.deleteParamByKeyAndName(dbSession, ActiveRuleKey.of(profile1.getKey(), rule1.getKey()), rule1Param1.getName());
+  }
+
+  @Test
+  public void does_not_fail_to_delete_param_by_key_and_name_when_active_rule_param_does_not_exist() throws Exception {
+    ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(BLOCKER);
+    underTest.insert(dbTester.getSession(), activeRule);
+    ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1");
+    underTest.insertParam(dbSession, activeRule, activeRuleParam1);
+    dbSession.commit();
+
+    underTest.deleteParamByKeyAndName(dbSession, activeRule.getKey(), "unknown");
+  }
+
   @Test
   public void delete_param_by_rule_param() throws Exception {
     ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
@@ -421,4 +518,21 @@ public class ActiveRuleDaoTest {
     assertThat(underTest.selectParamByKeyAndName(activeRule1.getKey(), activeRuleParam1.getKey(), dbSession)).isNull();
     assertThat(underTest.selectParamByKeyAndName(activeRule2.getKey(), activeRuleParam2.getKey(), dbSession)).isNull();
   }
+
+  @Test
+  public void does_not_fail_to_delete_param_by_rule_param_when_active_param_name_not_found() throws Exception {
+    ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
+    underTest.insert(dbTester.getSession(), activeRule1);
+    ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1");
+    underTest.insertParam(dbSession, activeRule1, activeRuleParam1);
+
+    ActiveRuleDto activeRule2 = createFor(profile2, rule1).setSeverity(BLOCKER);
+    underTest.insert(dbTester.getSession(), activeRule2);
+    ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue2");
+    underTest.insertParam(dbSession, activeRule2, activeRuleParam2);
+
+    dbSession.commit();
+
+    underTest.deleteParamsByRuleParam(dbSession, rule1, "unknown");
+  }
 }
index 66222702a69263446d39244abbce25a1cd997e1d..5755b8050fb010c2f43a7dfe99bf410ef06da286 100644 (file)
  */
 package org.sonar.api.rules;
 
-import com.google.common.base.Enums;
-import com.google.common.collect.Lists;
-import java.util.List;
+import com.google.common.base.Function;
+import java.util.Arrays;
+import java.util.Set;
+import javax.annotation.Nonnull;
 
+import static com.google.common.collect.FluentIterable.from;
 import static java.lang.String.format;
 
 public enum RuleType {
   CODE_SMELL(1), BUG(2), VULNERABILITY(3);
 
-  public static final List<String> ALL_NAMES = Lists.transform(Lists.newArrayList(values()), Enums.stringConverter(RuleType.class).reverse());
+  private static final Set<String> ALL_NAMES = from(Arrays.asList(values())).transform(ToName.INSTANCE).toSet();
 
   private final int dbConstant;
 
@@ -40,6 +42,10 @@ public enum RuleType {
     return dbConstant;
   }
 
+  public static Set<String> names() {
+    return ALL_NAMES;
+  }
+
   /**
    * Returns the enum constant of the specified DB column value.
    */
@@ -53,4 +59,13 @@ public enum RuleType {
     throw new IllegalArgumentException(format("Unsupported type value : %d", dbConstant));
   }
 
+  private enum ToName implements Function<RuleType, String> {
+    INSTANCE;
+
+    @Override
+    public String apply(@Nonnull RuleType input) {
+      return input.name();
+    }
+  }
+
 }
index 54f914049bf28e48bba058a4160cfa9c0868a216..a2ecc6cddf90cb9c6d474d046bda3bf603cf2598 100644 (file)
@@ -45,12 +45,12 @@ public class RuleTypeTest {
 
   @Test
   public void test_ALL_NAMES() {
-    assertThat(RuleType.ALL_NAMES).containsOnly("BUG", "VULNERABILITY", "CODE_SMELL");
+    assertThat(RuleType.names()).containsOnly("BUG", "VULNERABILITY", "CODE_SMELL");
   }
 
   @Test
   public void ALL_NAMES_is_immutable() {
     expectedException.expect(UnsupportedOperationException.class);
-    RuleType.ALL_NAMES.add("foo");
+    RuleType.names().add("foo");
   }
 }