diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-09-01 16:30:44 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-09-02 14:28:19 +0200 |
commit | 32428f49443f18796d73237b892117e171c1f5af (patch) | |
tree | a29bd10a5fab167b0658780f036feee44633d69c /sonar-plugin-api | |
parent | 65a791b7eab29403268ea9b3f9c9eea2850949be (diff) | |
download | sonarqube-32428f49443f18796d73237b892117e171c1f5af.tar.gz sonarqube-32428f49443f18796d73237b892117e171c1f5af.zip |
SONAR-6730 Add boolean measure
Diffstat (limited to 'sonar-plugin-api')
6 files changed, 63 insertions, 4 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Measure.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Measure.java index 948f33eee6c..dc53a428d3f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Measure.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Measure.java @@ -30,29 +30,36 @@ public interface Measure { /** * The value of this measure as a integer. * - * @throws IllegalStateException if the value type of the metric is not a integer {see @link org.sonar.api.measures.Metric.ValueType} + * @throws IllegalStateException if the value type of the metric is not a integer. See {@link org.sonar.api.measures.Metric.ValueType} */ int getIntValue(); /** * The value of this measure as a long. * - * @throws IllegalStateException if the value type of the metric is not a long {see @link org.sonar.api.measures.Metric.ValueType} + * @throws IllegalStateException if the value type of the metric is not a long. See {@link org.sonar.api.measures.Metric.ValueType} */ long getLongValue(); /** * The value of this measure as a double. * - * @throws IllegalStateException if the value type of the metric is not a double {see @link org.sonar.api.measures.Metric.ValueType} + * @throws IllegalStateException if the value type of the metric is not a double. See {@link org.sonar.api.measures.Metric.ValueType} */ double getDoubleValue(); /** * The value of this measure as a string. * - * @throws IllegalStateException if the value type of the metric is not a string {see @link org.sonar.api.measures.Metric.ValueType} + * @throws IllegalStateException if the value type of the metric is not a string. See {@link org.sonar.api.measures.Metric.ValueType} */ String getStringValue(); + /** + * The value of this measure as a boolean. + * + * @throws IllegalStateException if the value type of the metric is not a boolean. See {@link org.sonar.api.measures.Metric.ValueType} + */ + boolean getBooleanValue(); + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputer.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputer.java index 76a8dfa5c96..df3e4f2ce37 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputer.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputer.java @@ -213,6 +213,14 @@ public interface MeasureComputer { void addMeasure(String metric, String value); /** + * Add a new measure of a given metric which measure type will be boolean + * + * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputerDefinition#getOutputMetrics()} + * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component + */ + void addMeasure(String metric, boolean value); + + /** * Return list of all issues (open, closed, etc.) of current component. */ List<? extends Issue> getIssues(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasure.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasure.java index fbe8bc90679..b74eda4aee0 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasure.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasure.java @@ -33,6 +33,7 @@ public class TestMeasure implements Measure { private Long longValue; private Double doubleValue; private String stringValue; + private Boolean booleanValue; public static TestMeasure createMeasure(double doubleValue){ TestMeasure measure = new TestMeasure(); @@ -58,6 +59,12 @@ public class TestMeasure implements Measure { return measure; } + public static TestMeasure createMeasure(boolean booleanValue) { + TestMeasure measure = new TestMeasure(); + measure.booleanValue = requireNonNull(booleanValue, "Value cannot be null"); + return measure; + } + @Override public int getIntValue() { checkState(intValue != null, "Not an integer measure"); @@ -81,4 +88,10 @@ public class TestMeasure implements Measure { checkState(stringValue != null, "Not a string measure"); return stringValue; } + + @Override + public boolean getBooleanValue() { + checkState(booleanValue != null, "Not a boolean measure"); + return booleanValue; + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerContext.java index 55ed0218b99..9a0aac70bc4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerContext.java @@ -131,6 +131,17 @@ public class TestMeasureComputerContext implements MeasureComputerContext { componentMeasureByMetricKey.put(metricKey, TestMeasure.createMeasure(value)); } + @Override + public void addMeasure(String metricKey, boolean value) { + validateAddMeasure(metricKey); + componentMeasureByMetricKey.put(metricKey, TestMeasure.createMeasure(value)); + } + + + public void addInputMeasure(String metricKey, boolean value) { + componentMeasureByMetricKey.put(metricKey, TestMeasure.createMeasure(value)); + } + public void addInputMeasure(String metricKey, String value) { componentMeasureByMetricKey.put(metricKey, TestMeasure.createMeasure(value)); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java index 8ad7b6730b0..ea3cda844a6 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java @@ -92,6 +92,13 @@ public class TestMeasureComputerContextTest { } @Test + public void get_boolean_measure() throws Exception { + underTest.addInputMeasure(INPUT_METRIC, true); + + assertThat(underTest.getMeasure(INPUT_METRIC).getBooleanValue()).isTrue(); + } + + @Test public void fail_with_IAE_when_trying_to_get_measure_on_unknown_metric() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Only metrics in [INPUT_METRIC] can be used to load measures"); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java index fb54b4ba8f1..b1786bdd143 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java @@ -52,6 +52,11 @@ public class TestMeasureTest { } @Test + public void create_boolean_measure() throws Exception { + assertThat(TestMeasure.createMeasure(true).getBooleanValue()).isTrue(); + } + + @Test public void getDoubleValue_fails_with_ISE_when_not_a_double() throws Exception { thrown.expect(IllegalStateException.class); thrown.expectMessage("Not a double measure"); @@ -82,4 +87,12 @@ public class TestMeasureTest { TestMeasure.createMeasure(10).getStringValue(); } + + @Test + public void getBooleanValue_fails_with_ISE_when_not_a_boolean() throws Exception { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Not a boolean measure"); + + TestMeasure.createMeasure(10).getBooleanValue(); + } } |