Browse Source

SONAR-6730 Add boolean measure

tags/5.2-RC1
Julien Lancelot 8 years ago
parent
commit
32428f4944

+ 7
- 0
server/sonar-server/src/main/java/org/sonar/server/computation/measure/api/MeasureComputerContextImpl.java View File

@@ -168,6 +168,13 @@ public class MeasureComputerContextImpl implements MeasureComputerContext {
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());
}

+ 8
- 1
server/sonar-server/src/main/java/org/sonar/server/computation/measure/api/MeasureImpl.java View File

@@ -28,6 +28,7 @@ import org.sonar.api.ce.measure.Measure;

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;
@@ -36,7 +37,7 @@ import static org.sonar.server.computation.measure.Measure.ValueType.STRING;
@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;

@@ -69,6 +70,12 @@ public class MeasureImpl implements 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",

+ 14
- 1
server/sonar-server/src/test/java/org/sonar/server/computation/measure/api/MeasureComputerContextImplTest.java View File

@@ -60,6 +60,7 @@ public class MeasureComputerContextImplTest {
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;
@@ -84,7 +85,9 @@ public class MeasureComputerContextImplTest {
.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);
@@ -216,6 +219,16 @@ public class MeasureComputerContextImplTest {
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);

+ 17
- 2
server/sonar-server/src/test/java/org/sonar/server/computation/measure/api/MeasureImplTest.java View File

@@ -92,10 +92,25 @@ public class MeasureImplTest {
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());
}
@@ -103,7 +118,7 @@ public class MeasureImplTest {
@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));
}

+ 11
- 4
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Measure.java View File

@@ -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();

}

+ 8
- 0
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputer.java View File

@@ -212,6 +212,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.
*/

+ 13
- 0
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasure.java View File

@@ -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;
}
}

+ 11
- 0
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/test/TestMeasureComputerContext.java View File

@@ -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));
}

+ 7
- 0
sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java View File

@@ -91,6 +91,13 @@ public class TestMeasureComputerContextTest {
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);

+ 13
- 0
sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java View File

@@ -51,6 +51,11 @@ public class TestMeasureTest {
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);
@@ -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();
}
}

Loading…
Cancel
Save