use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.
the class QualityGatesTest method should_do_a_sanity_check_when_listing_conditions.
@Test(expected = IllegalStateException.class)
public void should_do_a_sanity_check_when_listing_conditions() {
long qGateId = QUALITY_GATE_ID;
long metric1Id = 1L;
String metric1Key = "polop";
long metric2Id = 2L;
QualityGateConditionDto cond1 = new QualityGateConditionDto().setMetricId(metric1Id);
QualityGateConditionDto cond2 = new QualityGateConditionDto().setMetricId(metric2Id);
Collection<QualityGateConditionDto> conditions = ImmutableList.of(cond1, cond2);
when(conditionDao.selectForQualityGate(dbSession, qGateId)).thenReturn(conditions);
Metric metric1 = mock(Metric.class);
when(metric1.getKey()).thenReturn(metric1Key);
when(metricFinder.findById((int) metric1Id)).thenReturn(metric1);
underTest.listConditions(qGateId);
}
use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.
the class MeasureSensorTest method testExecution.
@Test
public void testExecution() throws IOException {
File measures = new File(baseDir, "src/foo.xoo.measures");
FileUtils.write(measures, "ncloc:12\nbranch_coverage:5.3\nsqale_index:300\nbool:true\n\n#comment");
InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
context.fileSystem().add(inputFile);
Metric<Boolean> booleanMetric = new Metric.Builder("bool", "Bool", Metric.ValueType.BOOL).create();
when(metricFinder.<Integer>findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
when(metricFinder.<Double>findByKey("branch_coverage")).thenReturn(CoreMetrics.BRANCH_COVERAGE);
when(metricFinder.<Long>findByKey("sqale_index")).thenReturn(CoreMetrics.TECHNICAL_DEBT);
when(metricFinder.<Boolean>findByKey("bool")).thenReturn(booleanMetric);
sensor.execute(context);
assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.NCLOC).value()).isEqualTo(12);
assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.BRANCH_COVERAGE).value()).isEqualTo(5.3);
assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.TECHNICAL_DEBT).value()).isEqualTo(300L);
assertThat(context.measure("foo:src/foo.xoo", booleanMetric).value()).isTrue();
}
use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.
the class RegisterMetricsTest method update_non_custom_metrics.
/**
* Update existing metrics, except if custom metric
*/
@Test
public void update_non_custom_metrics() {
dbTester.prepareDbUnit(getClass(), "update_non_custom_metrics.xml");
RegisterMetrics register = new RegisterMetrics(dbClient);
Metric m1 = new Metric.Builder("m1", "New name", Metric.ValueType.FLOAT).setDescription("new description").setDirection(-1).setQualitative(true).setDomain("new domain").setUserManaged(false).setDecimalScale(3).setHidden(true).create();
Metric custom = new Metric.Builder("custom", "New custom", Metric.ValueType.FLOAT).setDescription("New description of custom metric").setUserManaged(true).create();
register.register(asList(m1, custom));
dbTester.assertDbUnit(getClass(), "update_non_custom_metrics-result.xml", "metrics");
}
use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.
the class DefaultIndex method addMeasure.
public Measure addMeasure(String key, Measure measure) {
InputComponent component = componentStore.getByKey(key);
if (component == null) {
throw new IllegalStateException("Invalid component key: " + key);
}
if (sensorStorage.isDeprecatedMetric(measure.getMetricKey())) {
// Ignore deprecated metrics
return measure;
}
org.sonar.api.batch.measure.Metric<?> metric = metricFinder.findByKey(measure.getMetricKey());
if (metric == null) {
throw new UnsupportedOperationException("Unknown metric: " + measure.getMetricKey());
}
DefaultMeasure<?> newMeasure;
if (Boolean.class.equals(metric.valueType())) {
newMeasure = new DefaultMeasure<Boolean>().forMetric((Metric<Boolean>) metric).withValue(measure.getValue() != 0.0);
} else if (Integer.class.equals(metric.valueType())) {
newMeasure = new DefaultMeasure<Integer>().forMetric((Metric<Integer>) metric).withValue(measure.getValue().intValue());
} else if (Double.class.equals(metric.valueType())) {
newMeasure = new DefaultMeasure<Double>().forMetric((Metric<Double>) metric).withValue(measure.getValue());
} else if (String.class.equals(metric.valueType())) {
newMeasure = new DefaultMeasure<String>().forMetric((Metric<String>) metric).withValue(measure.getData());
} else if (Long.class.equals(metric.valueType())) {
newMeasure = new DefaultMeasure<Long>().forMetric((Metric<Long>) metric).withValue(measure.getValue().longValue());
} else {
throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
}
sensorStorage.saveMeasure(component, newMeasure);
return measure;
}
Aggregations