aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-07-21 16:25:51 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-07-21 17:31:55 +0200
commit1f655d484cecd606c7c4af4a3452dd25801f1bd6 (patch)
treef95347ed8a23c0a1b72d1cb62de5ad36ae58c716 /sonar-batch
parent1066a59abeb3e3cafda581b84b87fada2a5947a2 (diff)
downloadsonarqube-1f655d484cecd606c7c4af4a3452dd25801f1bd6.tar.gz
sonarqube-1f655d484cecd606c7c4af4a3452dd25801f1bd6.zip
Remove useless usage of BatchReport.Measure.setDescription() and setVariationXXX()
Batch doesn't send anymore measure with variations, and description is no more used
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java79
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/report/MeasuresPublisherTest.java30
2 files changed, 45 insertions, 64 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java
index 1c6d7920deb..0f601de4fd4 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java
@@ -22,6 +22,7 @@ package org.sonar.batch.report;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import java.io.Serializable;
+import javax.annotation.Nonnull;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric.ValueType;
import org.sonar.batch.index.BatchComponent;
@@ -49,7 +50,8 @@ public class MeasuresPublisher implements ReportPublisherStep {
private final BatchReport.Measure.Builder builder = BatchReport.Measure.newBuilder();
@Override
- public BatchReport.Measure apply(Measure input) {
+ public BatchReport.Measure apply(@Nonnull Measure input) {
+ validateMeasure(input, resource.key());
return toReportMeasure(builder, input);
}
});
@@ -57,6 +59,12 @@ public class MeasuresPublisher implements ReportPublisherStep {
}
}
+ private static void validateMeasure(Measure measure, String componentKey) {
+ if (measure.getValue() == null && measure.getData() == null) {
+ throw new IllegalArgumentException(String.format("Measure on metric '%s' and component '%s' has no value, but it's not allowed", measure.getMetricKey(), componentKey));
+ }
+ }
+
private BatchReport.Measure toReportMeasure(BatchReport.Measure.Builder builder, Measure measure) {
builder.clear();
@@ -68,62 +76,29 @@ public class MeasuresPublisher implements ReportPublisherStep {
builder.setStringValue(data);
}
builder.setMetricKey(measure.getMetricKey());
-
- // temporary fields during development of computation stack
- String description = measure.getDescription();
- if (description != null) {
- builder.setDescription(description);
- }
- Double variation1 = measure.getVariation1();
- if (variation1 != null) {
- builder.setVariationValue1(variation1);
- }
- Double variation2 = measure.getVariation2();
- if (variation2 != null) {
- builder.setVariationValue2(variation2);
- }
- Double variation3 = measure.getVariation3();
- if (variation3 != null) {
- builder.setVariationValue3(variation3);
- }
- Double variation4 = measure.getVariation4();
- if (variation4 != null) {
- builder.setVariationValue4(variation4);
- }
- Double variation5 = measure.getVariation5();
- if (variation5 != null) {
- builder.setVariationValue5(variation5);
- }
- Integer personId = measure.getPersonId();
- if (personId != null) {
- builder.setPersonId(personId);
- }
return builder.build();
}
private void setValueAccordingToType(BatchReport.Measure.Builder builder, Measure measure) {
Serializable value = measure.value();
- // Value is null for new_xxx measures where only variations are populated
- if (value != null) {
- switch (builder.getValueType()) {
- case BOOLEAN:
- builder.setBooleanValue((Boolean) value);
- break;
- case DOUBLE:
- builder.setDoubleValue((Double) value);
- break;
- case INT:
- builder.setIntValue((Integer) value);
- break;
- case LONG:
- builder.setLongValue((Long) value);
- break;
- case STRING:
- builder.setStringValue((String) value);
- break;
- default:
- throw new IllegalStateException("Unknown value type: " + builder.getValueType());
- }
+ switch (builder.getValueType()) {
+ case BOOLEAN:
+ builder.setBooleanValue((Boolean) value);
+ break;
+ case DOUBLE:
+ builder.setDoubleValue((Double) value);
+ break;
+ case INT:
+ builder.setIntValue((Integer) value);
+ break;
+ case LONG:
+ builder.setLongValue((Long) value);
+ break;
+ case STRING:
+ builder.setStringValue((String) value);
+ break;
+ default:
+ throw new IllegalStateException("Unknown value type: " + builder.getValueType());
}
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/MeasuresPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/MeasuresPublisherTest.java
index 7d37b88a07a..5ee11f7cdf9 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/report/MeasuresPublisherTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/report/MeasuresPublisherTest.java
@@ -73,20 +73,12 @@ public class MeasuresPublisherTest {
@Test
public void publishMeasures() throws Exception {
- Measure measure1 = new Measure<>(CoreMetrics.COVERAGE)
+ Measure measure = new Measure<>(CoreMetrics.COVERAGE)
.setValue(2.0)
.setPersonId(2);
- // No value on new_xxx
- Measure measure2 = new Measure<>(CoreMetrics.NEW_BLOCKER_VIOLATIONS)
- .setVariation1(1.0)
- .setVariation2(2.0)
- .setVariation3(3.0)
- .setVariation4(4.0)
- .setVariation5(5.0);
// Manual measure
Measure manual = new Measure<>(new Metric<>("manual_metric", ValueType.BOOL))
- .setValue(1.0)
- .setDescription("Manual");
+ .setValue(1.0);
// Sqale rating have both a value and a data
Measure rating = new Measure<>(CoreMetrics.SQALE_RATING)
.setValue(2.0)
@@ -97,7 +89,7 @@ public class MeasuresPublisherTest {
// String value
Measure stringMeasure = new Measure<>(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION)
.setData("foo bar");
- when(measureCache.byResource(sampleFile)).thenReturn(asList(measure1, measure2, manual, rating, longMeasure, stringMeasure));
+ when(measureCache.byResource(sampleFile)).thenReturn(asList(measure, manual, rating, longMeasure, stringMeasure));
File outputDir = temp.newFolder();
BatchReportWriter writer = new BatchReportWriter(outputDir);
@@ -108,8 +100,22 @@ public class MeasuresPublisherTest {
assertThat(reader.readComponentMeasures(1)).hasSize(0);
try (CloseableIterator<BatchReport.Measure> componentMeasures = reader.readComponentMeasures(2)) {
- assertThat(componentMeasures).hasSize(6);
+ assertThat(componentMeasures).hasSize(5);
}
}
+ @Test
+ public void fail_with_IAE_when_measure_has_no_value() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Measure on metric 'coverage' and component 'foo:src/Foo.php' has no value, but it's not allowed");
+
+ Measure measure = new Measure<>(CoreMetrics.COVERAGE);
+ when(measureCache.byResource(sampleFile)).thenReturn(Collections.singletonList(measure));
+
+ File outputDir = temp.newFolder();
+ BatchReportWriter writer = new BatchReportWriter(outputDir);
+
+ publisher.publish(writer);
+ }
+
}