]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 20 Jun 2014 12:13:01 +0000 (14:13 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 20 Jun 2014 12:13:33 +0000 (14:13 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java
sonar-batch/src/main/java/org/sonar/batch/index/Cache.java
sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java
sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java
sonar-server/src/main/java/org/sonar/server/db/migrations/v44/MeasureDataMigration.java

index d82a34e6c995a2482a72babc537cf10db8855d99..b80221a2a1a4773eb73cfba1d461e50c66cd0c2a 100644 (file)
@@ -46,7 +46,6 @@ import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.ResourceUtils;
 import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleFinder;
 import org.sonar.api.rules.RulePriority;
 import org.sonar.batch.components.Period;
@@ -228,12 +227,11 @@ public class CountUnresolvedIssuesDecorator implements Decorator {
       }
 
       for (RuleKey ruleKey : ruleKeys) {
-        Rule rule = rulefinder.findByKey(ruleKey);
-        RuleMeasure measure = RuleMeasure.createForRule(metric, rule, null);
+        RuleMeasure measure = RuleMeasure.createForRule(metric, ruleKey, null);
         measure.setSeverity(severity);
         for (Period period : timeMachineConfiguration.periods()) {
           int variationIndex = period.getIndex();
-          double sum = MeasureUtils.sumOnVariation(true, variationIndex, childMeasuresPerRuleKeys.get(rule.ruleKey())) + countIssues(issuesPerRuleKeys.get(rule.ruleKey()), period);
+          double sum = MeasureUtils.sumOnVariation(true, variationIndex, childMeasuresPerRuleKeys.get(ruleKey)) + countIssues(issuesPerRuleKeys.get(ruleKey), period);
           measure.setVariation(variationIndex, sum);
         }
         context.saveMeasure(measure);
index 46f8f9d93e8f8eab0cb1109b64e90039e7366460..8c8a6b622bf738ca15fc583da262bae470c1e7fd 100644 (file)
@@ -35,12 +35,15 @@ import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.Scopes;
+import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleFinder;
 import org.sonar.api.technicaldebt.batch.Characteristic;
 import org.sonar.batch.components.PastMeasuresLoader;
 import org.sonar.batch.components.PastSnapshot;
 import org.sonar.batch.components.TimeMachineConfiguration;
 
+import javax.annotation.Nullable;
+
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -108,7 +111,13 @@ public class VariationDecorator implements Decorator {
       Characteristic characteristic = measure.getCharacteristic();
       Integer characteristicId = characteristic != null ? characteristic.id() : null;
       Integer personId = measure.getPersonId();
-      Integer ruleId = measure instanceof RuleMeasure ? ruleFinder.findByKey(((RuleMeasure) measure).ruleKey()).getId() : null;
+      Integer ruleId = null;
+      if (measure instanceof RuleMeasure) {
+        Rule rule = ruleFinder.findByKey(((RuleMeasure) measure).ruleKey());
+        if (rule != null) {
+          ruleId = rule.getId();
+        }
+      }
 
       Object[] pastMeasure = pastMeasuresByKey.get(new MeasureKey(metricId, characteristicId, personId, ruleId));
       if (updateVariation(measure, pastMeasure, index)) {
@@ -144,7 +153,7 @@ public class VariationDecorator implements Decorator {
       ruleId = PastMeasuresLoader.getRuleId(pastFields);
     }
 
-    MeasureKey(int metricId, Integer characteristicId, Integer personId, Integer ruleId) {
+    MeasureKey(int metricId, Integer characteristicId, Integer personId, @Nullable Integer ruleId) {
       this.metricId = metricId;
       this.characteristicId = characteristicId;
       this.personId = personId;
index 1cbe6af68cad2c3c2e8ae538b0c986a3eebd6006..ff57e4c065d7416ee24624c832d6ace861694ac4 100644 (file)
@@ -308,10 +308,14 @@ public class Cache<V extends Serializable> {
       KeyFilter filter = new KeyFilter().append(KeyFilter.simpleTerm(firstKey)).append(KeyFilter.simpleTerm(secondKey));
       return new ValueIterable<V>(iteratorExchange, filter);
     } catch (Exception e) {
-      throw new IllegalStateException("Fail to get values from cache " + name, e);
+      throw failToGetValues(e);
     }
   }
 
+  private IllegalStateException failToGetValues(Exception e) {
+    return new IllegalStateException("Fail to get values from cache " + name, e);
+  }
+
   /**
    * Lazy-loading values for a given key
    */
@@ -323,7 +327,7 @@ public class Cache<V extends Serializable> {
       KeyFilter filter = new KeyFilter().append(KeyFilter.simpleTerm(firstKey));
       return new ValueIterable<V>(iteratorExchange, filter);
     } catch (Exception e) {
-      throw new IllegalStateException("Fail to get values from cache " + name, e);
+      throw failToGetValues(e);
     }
   }
 
@@ -337,7 +341,7 @@ public class Cache<V extends Serializable> {
       KeyFilter filter = new KeyFilter().append(KeyFilter.ALL);
       return new ValueIterable<V>(iteratorExchange, filter);
     } catch (Exception e) {
-      throw new IllegalStateException("Fail to get values from cache " + name, e);
+      throw failToGetValues(e);
     }
   }
 
index f8e1dc601e34d5a17497bc70ee8ca4ec581fc294..c5dca91b146d8288a897ec7a13a16ca71350ac97 100644 (file)
@@ -186,6 +186,9 @@ public class DefaultIndex extends SonarIndex {
   public <M> M getMeasures(Resource resource, MeasuresFilter<M> filter) {
     // Reload resource so that effective key is populated
     Resource indexedResource = getResource(resource);
+    if (indexedResource == null) {
+      throw new IllegalStateException("Resource is not indexed " + resource);
+    }
     Iterable<Measure> unfiltered;
     if (filter instanceof MeasuresFilters.MetricFilter) {
       // optimization
index a3d23bf4fdea0ca73d20f8524dd0fe7f22a2b19a..46f569c41274788514b724bcf315520271e71cd5 100644 (file)
@@ -25,6 +25,7 @@ import org.sonar.api.measures.Measure;
 import org.sonar.api.measures.MetricFinder;
 import org.sonar.api.measures.RuleMeasure;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.technicaldebt.batch.Characteristic;
 import org.sonar.api.technicaldebt.batch.TechnicalDebtModel;
 import org.sonar.batch.index.Cache;
 import org.sonar.batch.index.Cache.Entry;
@@ -77,12 +78,14 @@ public class MeasureCache implements BatchComponent {
       sb.append(m.getMetricKey());
     }
     sb.append("|");
-    if (m.getCharacteristic() != null) {
-      sb.append(m.getCharacteristic().key());
+    Characteristic characteristic = m.getCharacteristic();
+    if (characteristic != null) {
+      sb.append(characteristic.key());
     }
     sb.append("|");
-    if (m.getPersonId() != null) {
-      sb.append(m.getPersonId());
+    Integer personId = m.getPersonId();
+    if (personId != null) {
+      sb.append(personId);
     }
     if (m instanceof RuleMeasure) {
       sb.append("|");
index 170b2c5531bc14df1955aa7a04712e07347374ee..d367aeb4839a9c3bf754838cba3f86de82bc7ece 100644 (file)
@@ -655,6 +655,7 @@ public class Measure<G extends Serializable> implements Serializable {
   /**
    * @since 2.14
    */
+  @CheckForNull
   @Beta
   public Integer getPersonId() {
     return personId;
index fedeb01bd79d06778990b614a63b703510c0fd92..f666884d715354d3650d07d301b548ec23f25ce4 100644 (file)
@@ -62,7 +62,7 @@ public class MeasureDataMigration implements DatabaseMigration {
         public Row load(ResultSet rs) throws SQLException {
           Row row = new Row();
           row.id = SqlUtil.getLong(rs, 1);
-          row.measure_id = SqlUtil.getLong(rs, 2);
+          row.measureId = SqlUtil.getLong(rs, 2);
           return row;
         }
       },
@@ -77,7 +77,7 @@ public class MeasureDataMigration implements DatabaseMigration {
         public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException {
           ids.add(row.id);
           updateStatement.setLong(1, row.id);
-          updateStatement.setLong(2, row.measure_id);
+          updateStatement.setLong(2, row.measureId);
           return true;
         }
       },
@@ -113,7 +113,7 @@ public class MeasureDataMigration implements DatabaseMigration {
 
   private static class Row {
     private Long id;
-    private Long measure_id;
+    private Long measureId;
   }
 
 }