measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
}
+ @Override
+ public void addMeasure(String metricKey, boolean value) {
+ Metric metric = metricRepository.getByKey(metricKey);
+ validateAddMeasure(metric);
+ measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
+ }
+
private void validateInputMetric(String metric) {
checkArgument(allowedMetrics.contains(metric), "Only metrics in %s can be used to load measures", definition.getInputMetrics());
}
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
+import static org.sonar.server.computation.measure.Measure.ValueType.BOOLEAN;
import static org.sonar.server.computation.measure.Measure.ValueType.DOUBLE;
import static org.sonar.server.computation.measure.Measure.ValueType.INT;
import static org.sonar.server.computation.measure.Measure.ValueType.LONG;
@Immutable
public class MeasureImpl implements Measure {
- private static final Set<org.sonar.server.computation.measure.Measure.ValueType> ALLOWED_VALUE_TYPES = ImmutableSet.of(INT, LONG, DOUBLE, STRING);
+ private static final Set<org.sonar.server.computation.measure.Measure.ValueType> ALLOWED_VALUE_TYPES = ImmutableSet.of(INT, LONG, DOUBLE, STRING, BOOLEAN);
private final org.sonar.server.computation.measure.Measure measure;
return measure.getStringValue();
}
+ @Override
+ public boolean getBooleanValue() {
+ checkValueType(BOOLEAN);
+ return measure.getBooleanValue();
+ }
+
private void checkValueType(org.sonar.server.computation.measure.Measure.ValueType expected) {
checkState(measure.getValueType() == expected, String.format(
"Value can not be converted to %s because current value type is a %s",
private static final String DOUBLE_METRIC_KEY = "double_metric_key";
private static final String LONG_METRIC_KEY = "long_metric_key";
private static final String STRING_METRIC_KEY = "string_metric_key";
+ private static final String BOOLEAN_METRIC_KEY = "boolean_metric_key";
private static final int PROJECT_REF = 1;
private static final int FILE_1_REF = 12341;
.add(new MetricImpl(2, INT_METRIC_KEY, "int metric", Metric.MetricType.INT))
.add(new MetricImpl(3, DOUBLE_METRIC_KEY, "double metric", Metric.MetricType.FLOAT))
.add(new MetricImpl(4, LONG_METRIC_KEY, "long metric", Metric.MetricType.MILLISEC))
- .add(new MetricImpl(5, STRING_METRIC_KEY, "string metric", Metric.MetricType.STRING));
+ .add(new MetricImpl(5, STRING_METRIC_KEY, "string metric", Metric.MetricType.STRING))
+ .add(new MetricImpl(6, BOOLEAN_METRIC_KEY, "boolean metric", Metric.MetricType.BOOL))
+ ;
@Rule
public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
assertThat(measure.get().getStringValue()).isEqualTo("data");
}
+ @Test
+ public void add_boolean_measure_create_measure_of_type_boolean_with_right_value() throws Exception {
+ MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, BOOLEAN_METRIC_KEY);
+ underTest.addMeasure(BOOLEAN_METRIC_KEY, true);
+
+ Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, BOOLEAN_METRIC_KEY);
+ assertThat(measure).isPresent();
+ assertThat(measure.get().getBooleanValue()).isTrue();
+ }
+
@Test
public void fail_with_IAE_when_add_measure_is_called_on_metric_not_in_output_list() throws Exception {
thrown.expect(IllegalArgumentException.class);
measure.getStringValue();
}
+ @Test
+ public void get_boolean_value() throws Exception {
+ MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(true));
+ assertThat(measure.getBooleanValue()).isTrue();
+ }
+
+ @Test
+ public void fail_with_ISE_when_not_boolean_value() throws Exception {
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Value can not be converted to boolean because current value type is a DOUBLE");
+
+ MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1d));
+ measure.getBooleanValue();
+ }
+
@Test
public void fail_with_ISE_when_creating_measure_with_no_value() throws Exception {
thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Only following types are allowed [INT, LONG, DOUBLE, STRING]");
+ thrown.expectMessage("Only following types are allowed [INT, LONG, DOUBLE, STRING, BOOLEAN]");
new MeasureImpl(Measure.newMeasureBuilder().createNoValue());
}
@Test
public void fail_with_ISE_when_creating_measure_with_not_allowed_value() throws Exception {
thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Only following types are allowed [INT, LONG, DOUBLE, STRING]");
+ thrown.expectMessage("Only following types are allowed [INT, LONG, DOUBLE, STRING, BOOLEAN]");
new MeasureImpl(Measure.newMeasureBuilder().create(Measure.Level.ERROR));
}
/**
* 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();
+
}
*/
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.
*/
private Long longValue;
private Double doubleValue;
private String stringValue;
+ private Boolean booleanValue;
public static TestMeasure createMeasure(double doubleValue){
TestMeasure measure = new TestMeasure();
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");
checkState(stringValue != null, "Not a string measure");
return stringValue;
}
+
+ @Override
+ public boolean getBooleanValue() {
+ checkState(booleanValue != null, "Not a boolean measure");
+ return booleanValue;
+ }
}
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));
}
assertThat(underTest.getMeasure(INPUT_METRIC).getStringValue()).isEqualTo("text");
}
+ @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);
assertThat(TestMeasure.createMeasure("value").getStringValue()).isEqualTo("value");
}
+ @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);
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();
+ }
}