]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 10 Apr 2014 15:11:32 +0000 (17:11 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 10 Apr 2014 15:11:40 +0000 (17:11 +0200)
sonar-batch/src/main/java/org/sonar/batch/qualitygate/ConditionUtils.java
sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDto.java
sonar-server/src/main/java/org/sonar/server/component/DefaultRubyComponentService.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/ProfileRuleQuery.java
sonar-server/src/main/java/org/sonar/server/user/DoPrivileged.java

index 95c10767ae57634f3dfbb14e161af214247aeb7e..f2e5b36bbe24588fd2eec1e1d03d1e35cdefc33e 100644 (file)
@@ -66,19 +66,19 @@ class ConditionUtils {
   }
 
   private static boolean isNotEquals(int comparison, ResolvedCondition condition) {
-    return condition.operator().equals("NE") && comparison == 0;
+    return "NE".equals(condition.operator()) && comparison == 0;
   }
 
   private static boolean isGreater(int comparison, ResolvedCondition condition) {
-    return condition.operator().equals("GT") && comparison != 1;
+    return "GT".equals(condition.operator()) && comparison != 1;
   }
 
   private static boolean isSmaller(int comparison, ResolvedCondition condition) {
-    return condition.operator().equals("LT") && comparison != -1;
+    return "LT".equals(condition.operator()) && comparison != -1;
   }
 
   private static boolean isEquals(int comparison, ResolvedCondition condition) {
-    return condition.operator().equals("EQ") && comparison != 0;
+    return "EQ".equals(condition.operator()) && comparison != 0;
   }
 
   private static String getValueToEval(ResolvedCondition condition, Metric.Level alertLevel) {
index a64d43af0c84d8f0172883472b411aee3bd09f66..819d271350aba1fbc8e660bd48bf04678c9ae53e 100644 (file)
  */
 package org.sonar.core.qualitygate.db;
 
+import com.google.common.collect.ImmutableMap;
+
 import com.google.common.collect.ImmutableList;
 import org.sonar.api.measures.Metric.ValueType;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 /**
  * @since 4.3
@@ -63,6 +62,18 @@ public class QualityGateConditionDto {
     OPERATOR_EQUALS
   );
 
+  private static final Map<ValueType, List<String>> OPERATORS_BY_TYPE = ImmutableMap.<ValueType, List<String>>builder()
+      .put(ValueType.BOOL, BOOLEAN_OPERATORS)
+      .put(ValueType.LEVEL, LEVEL_OPERATORS)
+      .put(ValueType.STRING, STRING_OPERATORS)
+      .put(ValueType.INT, NUMERIC_OPERATORS)
+      .put(ValueType.FLOAT, NUMERIC_OPERATORS)
+      .put(ValueType.PERCENT, NUMERIC_OPERATORS)
+      .put(ValueType.MILLISEC, NUMERIC_OPERATORS)
+      .put(ValueType.RATING, NUMERIC_OPERATORS)
+      .put(ValueType.WORK_DUR, NUMERIC_OPERATORS)
+      .build();
+
   private long id;
 
   private long qualityGateId;
@@ -180,30 +191,10 @@ public class QualityGateConditionDto {
   }
 
   public static Collection<String> getOperatorsForType(ValueType metricType) {
-    Collection<String> operators = Collections.emptySet();
-    if (metricType != null) {
-      switch(metricType) {
-        case BOOL:
-          operators = BOOLEAN_OPERATORS;
-          break;
-        case LEVEL:
-          operators = LEVEL_OPERATORS;
-          break;
-        case STRING:
-          operators = STRING_OPERATORS;
-          break;
-        case INT:
-        case FLOAT:
-        case PERCENT:
-        case MILLISEC:
-        case RATING:
-        case WORK_DUR:
-          operators = NUMERIC_OPERATORS;
-          break;
-        default:
-          operators = Collections.emptySet();
-      }
+    if (OPERATORS_BY_TYPE.containsKey(metricType)) {
+      return OPERATORS_BY_TYPE.get(metricType);
+    } else {
+      return Collections.emptySet();
     }
-    return operators;
   }
 }
index 29293d7e3269c45fa71265df64090cfd9d4dd34a..ace86157de141e23d2b96699b8515ec7fa4dc8b7 100644 (file)
@@ -53,7 +53,7 @@ public class DefaultRubyComponentService implements RubyComponentService {
   }
 
   @Override
-  public Component<?> findByKey(String key) {
+  public Component findByKey(String key) {
     return resourceDao.findByKey(key);
   }
 
index 4d3c0fc5b98375ba0884d11d83d03089968c9221..4080b760b33b9e1e3243dbfa92ecfbfa1ee53852 100644 (file)
@@ -105,6 +105,19 @@ public class ProfileRuleQuery {
       result.addTags(optionalVarargs(params.get(PARAM_TAGS)));
     }
 
+    parseInheritance(params, errors, result);
+
+    parseParams(params, errors, result);
+
+    if (!errors.isEmpty()) {
+      throw BadRequestException.of("Incorrect rule search parameters", errors);
+    } else {
+      result.profileId = RubyUtils.toInteger(params.get(PARAM_PROFILE_ID));
+    }
+    return result;
+  }
+
+  private static void parseInheritance(Map<String, Object> params, List<BadRequestException.Message> errors, ProfileRuleQuery result) {
     if (params.containsKey(PARAM_INHERITANCE)) {
       String inheritance = (String) params.get(PARAM_INHERITANCE);
       validateInheritance(inheritance, errors);
@@ -118,7 +131,9 @@ public class ProfileRuleQuery {
     } else {
       result.setAnyInheritance(true);
     }
+  }
 
+  private static void parseParams(Map<String, Object> params, List<BadRequestException.Message> errors, ProfileRuleQuery result) {
     if (params.get(PARAM_SORT) != null) {
       String sort = (String) params.get(PARAM_SORT);
       Boolean asc = RubyUtils.toBoolean(params.get(PARAM_ASC));
@@ -128,13 +143,6 @@ public class ProfileRuleQuery {
         result.setAsc(asc);
       }
     }
-
-    if (!errors.isEmpty()) {
-      throw BadRequestException.of("Incorrect rule search parameters", errors);
-    } else {
-      result.profileId = RubyUtils.toInteger(params.get(PARAM_PROFILE_ID));
-    }
-    return result;
   }
 
   private static void validatePresenceOf(Map<String, Object> params, List<BadRequestException.Message> errors, String... paramNames) {
index 9e323a63919d6141b527278d2e07972ba541b4c9..edae46ab7aea7a34df1463e0f108d44a8c7a8903 100644 (file)
@@ -53,7 +53,7 @@ public final class DoPrivileged {
    * Define a task that will be executed using the highest privileges available. The privileged section is restricted
    * to the execution of the {@link #doPrivileged()} method.
    */
-  public static abstract class Task {
+  public abstract static class Task {
 
     /**
      * Code placed in this method will be executed in a privileged environment.