From d44ef046f3dd7d59397c681cb79ebe3173d915bb Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 30 Jun 2014 16:34:38 +0200 Subject: [PATCH] Fix some quality flaws --- .../plugins/core/sensors/ManualMeasureDecorator.java | 7 ++++++- .../plugins/core/timemachine/VariationDecorator.java | 9 ++++++++- .../java/org/sonar/batch/bootstrapper/Batch.java | 4 +++- .../sonar/batch/index/DefaultResourcePersister.java | 12 ++++++++---- .../org/sonar/batch/index/DependencyPersister.java | 2 +- .../org/sonar/batch/scan/AnalyzerContextAdaptor.java | 3 +++ 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ManualMeasureDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ManualMeasureDecorator.java index 41c093c5994..1f9c5728c9d 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ManualMeasureDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ManualMeasureDecorator.java @@ -24,6 +24,7 @@ import org.sonar.api.batch.DecoratorContext; import org.sonar.api.batch.Phase; import org.sonar.api.database.DatabaseSession; import org.sonar.api.measures.Measure; +import org.sonar.api.measures.Metric; import org.sonar.api.measures.MetricFinder; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; @@ -56,7 +57,11 @@ public class ManualMeasureDecorator implements Decorator { } private Measure copy(ManualMeasure manualMeasure) { - Measure measure = new Measure(metricFinder.findById(manualMeasure.getMetricId())); + Metric metric = metricFinder.findById(manualMeasure.getMetricId()); + if (metric == null) { + throw new IllegalStateException("Unable to find manual metric with id: " + manualMeasure.getMetricId()); + } + Measure measure = new Measure(metric); measure.setValue(manualMeasure.getValue(), 5); measure.setData(manualMeasure.getTextValue()); measure.setDescription(manualMeasure.getDescription()); 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 8c8a6b622bf..19d1dd93207 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 @@ -107,7 +107,14 @@ public class VariationDecorator implements Decorator { // for each measure, search equivalent past measure for (Measure measure : context.getMeasures(MeasuresFilters.all())) { // compare with past measure - Integer metricId = measure.getMetric().getId() != null ? measure.getMetric().getId() : metricFinder.findByKey(measure.getMetric().getKey()).getId(); + Integer metricId = measure.getMetric().getId(); + if (metricId == null) { + Metric metric = metricFinder.findByKey(measure.getMetric().getKey()); + if (metric == null) { + throw new IllegalStateException("Unknow metric with key: " + measure.getMetric().getKey()); + } + metricId = metric.getId(); + } Characteristic characteristic = measure.getCharacteristic(); Integer characteristicId = characteristic != null ? characteristic.id() : null; Integer personId = measure.getPersonId(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java index ac107141bc3..b448c2ad076 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java @@ -62,7 +62,7 @@ public final class Batch { * @deprecated since 4.4 use {@link #start()}, {@link #executeTask(Map)} and then {@link #stop()} */ @Deprecated - public Batch execute() { + public synchronized Batch execute() { configureLogging(); start().executeTask(bootstrapProperties).stop(); return this; @@ -105,6 +105,8 @@ public final class Batch { } bootstrapContainer.stopComponents(); + + this.started = false; } private void configureLogging() { diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java index 689b6a0e4cd..7eeb87ea4f9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.index; +import org.sonar.api.resources.Language; + import com.google.common.collect.Maps; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; @@ -294,8 +296,9 @@ public final class DefaultResourcePersister implements ResourcePersister { model.setDescription(resource.getDescription()); model.setKey(resource.getEffectiveKey()); model.setPath(resource.getPath()); - if (resource.getLanguage() != null) { - model.setLanguageKey(resource.getLanguage().getKey()); + Language language = resource.getLanguage(); + if (language != null) { + model.setLanguageKey(language.getKey()); } if (StringUtils.isNotBlank(resource.getName())) { model.setName(resource.getName()); @@ -327,8 +330,9 @@ public final class DefaultResourcePersister implements ResourcePersister { model.setScope(resource.getScope()); model.setQualifier(resource.getQualifier()); } - if (resource.getLanguage() != null) { - model.setLanguageKey(resource.getLanguage().getKey()); + Language language = resource.getLanguage(); + if (language != null) { + model.setLanguageKey(language.getKey()); } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DependencyPersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DependencyPersister.java index 514bbc9d3d4..e20680b10fc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DependencyPersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DependencyPersister.java @@ -38,7 +38,7 @@ public final class DependencyPersister { public void saveDependency(Project project, Dependency dependency, Dependency parentDependency) { Snapshot fromSnapshot = resourcePersister.saveResource(project, dependency.getFrom()); Snapshot toSnapshot = resourcePersister.saveResource(project, dependency.getTo()); - Snapshot projectSnapshot = resourcePersister.getSnapshot(project); + Snapshot projectSnapshot = resourcePersister.getSnapshotOrFail(project); DependencyDto model = new DependencyDto(); model.setProjectSnapshotId(projectSnapshot.getId()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/AnalyzerContextAdaptor.java b/sonar-batch/src/main/java/org/sonar/batch/scan/AnalyzerContextAdaptor.java index bac550fd878..e1bf875059f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/AnalyzerContextAdaptor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/AnalyzerContextAdaptor.java @@ -146,6 +146,9 @@ public class AnalyzerContextAdaptor implements AnalyzerContext { @Override public void addMeasure(AnalyzerMeasure measure) { org.sonar.api.measures.Metric m = metricFinder.findByKey(measure.metric().key()); + if (m == null) { + throw new IllegalStateException("Unknow metric with key: " + measure.metric().key()); + } Measure measureToSave = new Measure(m); switch (m.getType()) { -- 2.39.5