From 904b462c56317b1fb5a616534bd58aa9c9097d0b Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Fri, 20 Jun 2014 14:13:01 +0200 Subject: [PATCH] Fix some quality flaws --- .../core/issue/CountUnresolvedIssuesDecorator.java | 6 ++---- .../core/timemachine/VariationDecorator.java | 13 +++++++++++-- .../src/main/java/org/sonar/batch/index/Cache.java | 10 +++++++--- .../java/org/sonar/batch/index/DefaultIndex.java | 3 +++ .../org/sonar/batch/scan/measure/MeasureCache.java | 11 +++++++---- .../main/java/org/sonar/api/measures/Measure.java | 1 + .../db/migrations/v44/MeasureDataMigration.java | 6 +++--- 7 files changed, 34 insertions(+), 16 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java index d82a34e6c99..b80221a2a1a 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java @@ -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); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java index 46f8f9d93e8..8c8a6b622bf 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java @@ -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; diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java b/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java index 1cbe6af68ca..ff57e4c065d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java @@ -308,10 +308,14 @@ public class Cache { KeyFilter filter = new KeyFilter().append(KeyFilter.simpleTerm(firstKey)).append(KeyFilter.simpleTerm(secondKey)); return new ValueIterable(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 { KeyFilter filter = new KeyFilter().append(KeyFilter.simpleTerm(firstKey)); return new ValueIterable(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 { KeyFilter filter = new KeyFilter().append(KeyFilter.ALL); return new ValueIterable(iteratorExchange, filter); } catch (Exception e) { - throw new IllegalStateException("Fail to get values from cache " + name, e); + throw failToGetValues(e); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index f8e1dc601e3..c5dca91b146 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -186,6 +186,9 @@ public class DefaultIndex extends SonarIndex { public M getMeasures(Resource resource, MeasuresFilter 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 unfiltered; if (filter instanceof MeasuresFilters.MetricFilter) { // optimization diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java index a3d23bf4fde..46f569c4127 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java @@ -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("|"); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java index 170b2c5531b..d367aeb4839 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java @@ -655,6 +655,7 @@ public class Measure implements Serializable { /** * @since 2.14 */ + @CheckForNull @Beta public Integer getPersonId() { return personId; diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/MeasureDataMigration.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/MeasureDataMigration.java index fedeb01bd79..f666884d715 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/MeasureDataMigration.java +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/MeasureDataMigration.java @@ -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; } } -- 2.39.5