diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-04-24 11:26:44 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-04-24 11:44:29 +0200 |
commit | bae0515e253b0f793396825b2229ac110c34d808 (patch) | |
tree | 45e40ca4179b5ebe9d5eb2709c0daaef1934e829 | |
parent | 131af346c37cc3179b027c69f66258ba4b269144 (diff) | |
download | sonarqube-bae0515e253b0f793396825b2229ac110c34d808.tar.gz sonarqube-bae0515e253b0f793396825b2229ac110c34d808.zip |
SONAR-4066 Show contextual infos during persist error of measures
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java | 54 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java | 36 |
2 files changed, 79 insertions, 11 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java index deb29fd3054..aadca4bbd76 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java @@ -75,7 +75,13 @@ public final class MeasurePersister { unsavedMeasuresByResource.put(resource, measure); return; } - MeasureModel model = insertOrUpdate(resource, measure); + MeasureModel model; + try { + model = insertOrUpdate(resource, measure); + } catch (Exception e) { + // SONAR-4066 + throw new SonarException(String.format("Unable to save measure for metric [%s] on resource [%s]", measure.getMetricKey(), resource.getKey()), e); + } if (model != null) { memoryOptimizer.evictDataMeasure(measure, model); } @@ -104,8 +110,8 @@ public final class MeasurePersister { !(ResourceUtils.isEntity(resource) && measure.isBestValue()); } - private List<MeasureModel> getMeasuresToSave() { - List<MeasureModel> measures = Lists.newArrayList(); + private List<MeasureModelAndDetails> getMeasuresToSave() { + List<MeasureModelAndDetails> measures = Lists.newArrayList(); Map<Resource, Collection<Measure>> map = unsavedMeasuresByResource.asMap(); for (Map.Entry<Resource, Collection<Measure>> entry : map.entrySet()) { @@ -113,7 +119,7 @@ public final class MeasurePersister { Snapshot snapshot = resourcePersister.getSnapshot(entry.getKey()); for (Measure measure : entry.getValue()) { if (shouldPersistMeasure(resource, measure)) { - measures.add(model(measure).setSnapshotId(snapshot.getId())); + measures.add(new MeasureModelAndDetails(model(measure).setSnapshotId(snapshot.getId()), resource.getKey(), measure.getMetricKey())); } } } @@ -157,15 +163,20 @@ public final class MeasurePersister { return model; } - private void insert(Iterable<MeasureModel> values) { + private void insert(Iterable<MeasureModelAndDetails> values) { SqlSession session = mybatis.openSession(); try { MeasureMapper mapper = session.getMapper(MeasureMapper.class); - for (MeasureModel value : values) { - mapper.insert(value); - if (value.getMeasureData() != null) { - mapper.insertData(value.getMeasureData()); + for (MeasureModelAndDetails value : values) { + try { + mapper.insert(value.getMeasureModel()); + if (value.getMeasureModel().getMeasureData() != null) { + mapper.insertData(value.getMeasureModel().getMeasureData()); + } + } catch (Exception e) { + // SONAR-4066 + throw new SonarException(String.format("Unable to save measure for metric [%s] on resource [%s]", value.getMetricKey(), value.getResourceKey()), e); } } @@ -218,4 +229,29 @@ public final class MeasurePersister { return value; } + + // SONAR-4066 + private static class MeasureModelAndDetails { + private final MeasureModel measureModel; + private final String resourceKey; + private final String metricKey; + + public MeasureModelAndDetails(MeasureModel measureModel, String resourceKey, String metricKey) { + this.measureModel = measureModel; + this.resourceKey = resourceKey; + this.metricKey = metricKey; + } + + public MeasureModel getMeasureModel() { + return measureModel; + } + + public String getResourceKey() { + return resourceKey; + } + + public String getMetricKey() { + return metricKey; + } + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java index d8244c0a0eb..8ec45589b87 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java @@ -19,11 +19,11 @@ */ package org.sonar.batch.index; -import org.mockito.ArgumentCaptor; - import org.apache.commons.lang.StringUtils; import org.junit.Before; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.ArgumentCaptor; import org.sonar.api.database.model.MeasureModel; import org.sonar.api.database.model.Snapshot; import org.sonar.api.measures.CoreMetrics; @@ -37,6 +37,7 @@ import org.sonar.api.resources.Project; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RulePriority; +import org.sonar.api.utils.SonarException; import org.sonar.core.persistence.AbstractDaoTestCase; import static org.fest.assertions.Assertions.assertThat; @@ -47,8 +48,13 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class MeasurePersisterTest extends AbstractDaoTestCase { + + @org.junit.Rule + public ExpectedException thrown = ExpectedException.none(); + private static final String SHORT = "SHORT"; private static final String LONG = StringUtils.repeat("0123456789", 10); + private static final String TOO_LONG = StringUtils.repeat("0123456789", 401); public static final int PROJECT_SNAPSHOT_ID = 3001; public static final int PACKAGE_SNAPSHOT_ID = 3002; @@ -88,6 +94,18 @@ public class MeasurePersisterTest extends AbstractDaoTestCase { } @Test + public void should_display_contextual_info_when_error_during_insert_measure() { + setupData("empty"); + + Measure measure = new Measure(ncloc()).setValue(1234.0).setAlertText(TOO_LONG); + + thrown.expect(SonarException.class); + thrown.expectMessage("Unable to save measure for metric [ncloc] on resource [foo]"); + + measurePersister.saveMeasure(project, measure); + } + + @Test public void should_reload_measure() { Measure measure = new Measure(ncloc()); @@ -193,6 +211,20 @@ public class MeasurePersisterTest extends AbstractDaoTestCase { } @Test + public void should_display_contextual_info_when_error_during_delay_saving() { + setupData("empty"); + + measurePersister.setDelayedMode(true); + + measurePersister.saveMeasure(project, new Measure(ncloc()).setValue(1234.0).setData(SHORT).setAlertText(TOO_LONG)); + + thrown.expect(SonarException.class); + thrown.expectMessage("Unable to save measure for metric [ncloc] on resource [foo]"); + + measurePersister.dump(); + } + + @Test public void should_not_delay_saving_with_database_only_measure() { setupData("empty"); |