aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-09-08 14:49:49 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-09-09 09:50:57 +0200
commit85ec83484696d1745f8103ef13dd644872fd19cd (patch)
tree70b797aa2cf2577beb2c5318ec618fd5e9597747 /sonar-plugin-api
parent4fd276fb8febe54800f3f23fe4b815dba0cd8fb4 (diff)
downloadsonarqube-85ec83484696d1745f8103ef13dd644872fd19cd.tar.gz
sonarqube-85ec83484696d1745f8103ef13dd644872fd19cd.zip
SONAR-6827 Check output metrics are not core metrics
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinition.java33
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java12
2 files changed, 45 insertions, 0 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinition.java
index 877a41a736b..050fd33d7d3 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinition.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinition.java
@@ -20,10 +20,18 @@
package org.sonar.api.ce.measure.test;
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
+import java.util.Arrays;
+import java.util.List;
import java.util.Set;
+import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.sonar.api.ce.measure.MeasureComputer;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Metric;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
@@ -81,6 +89,7 @@ public class TestMeasureComputerDefinition implements MeasureComputer.MeasureCom
private static String[] validateOutputMetricKeys(@Nullable String[] outputMetrics) {
requireNonNull(outputMetrics, "Output metrics cannot be null");
checkArgument(outputMetrics.length > 0, "At least one output metric must be defined");
+ FluentIterable.from(CoreMetrics.getMetrics()).transform(MetricToKey.INSTANCE).filter(new MatchMetricKey(outputMetrics)).size();
checkNotNull(outputMetrics);
return outputMetrics;
}
@@ -91,4 +100,28 @@ public class TestMeasureComputerDefinition implements MeasureComputer.MeasureCom
requireNonNull(metric, "Null metric is not allowed");
}
}
+
+ private enum MetricToKey implements Function<Metric, String> {
+ INSTANCE;
+
+ @Override
+ public String apply(@Nonnull Metric input) {
+ return input.getKey();
+ }
+ }
+
+ private static class MatchMetricKey implements Predicate<String> {
+
+ private final List<String> metrics;
+
+ public MatchMetricKey(String[] metrics) {
+ this.metrics = Arrays.asList(metrics);
+ }
+
+ @Override
+ public boolean apply(@Nonnull String metric) {
+ checkArgument(!metrics.contains(metric), "Core metrics are not allowed");
+ return true;
+ }
+ }
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java
index 6a34b47c6f1..f30843613ec 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java
@@ -25,6 +25,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.ce.measure.MeasureComputer.MeasureComputerDefinition;
import org.sonar.api.ce.measure.test.TestMeasureComputerDefinition.MeasureComputerDefinitionBuilderImpl;
+import org.sonar.api.measures.CoreMetrics;
import static org.assertj.core.api.Assertions.assertThat;
@@ -118,4 +119,15 @@ public class TestMeasureComputerDefinitionTest {
.setOutputMetrics()
.build();
}
+
+ @Test
+ public void fail_with_IAE_when_building_definition_with_core_metrics_in_output_metrics() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Core metrics are not allowed");
+
+ new MeasureComputerDefinitionBuilderImpl()
+ .setInputMetrics("INPUT_1", "INPUT_2")
+ .setOutputMetrics(CoreMetrics.NCLOC_KEY)
+ .build();
+ }
}