public class SqaleMeasuresVisitor extends PathAwareVisitorAdapter<SqaleMeasuresVisitor.DevelopmentCostCounter> {
private static final Logger LOG = Loggers.get(SqaleMeasuresVisitor.class);
- private final MetricRepository metricRepository;
private final MeasureRepository measureRepository;
private final SqaleRatingSettings sqaleRatingSettings;
+ private final Metric nclocMetric;
private final Metric developmentCostMetric;
private final Metric technicalDebtMetric;
private final Metric debtRatioMetric;
public SqaleMeasuresVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, SqaleRatingSettings sqaleRatingSettings) {
super(CrawlerDepthLimit.LEAVES, POST_ORDER, DevelopmentCostCounterFactory.INSTANCE);
- this.metricRepository = metricRepository;
this.measureRepository = measureRepository;
this.sqaleRatingSettings = sqaleRatingSettings;
- this.developmentCostMetric = this.metricRepository.getByKey(CoreMetrics.DEVELOPMENT_COST_KEY);
- this.technicalDebtMetric = this.metricRepository.getByKey(CoreMetrics.TECHNICAL_DEBT_KEY);
- this.debtRatioMetric = this.metricRepository.getByKey(CoreMetrics.SQALE_DEBT_RATIO_KEY);
- this.sqaleRatingMetric = this.metricRepository.getByKey(CoreMetrics.SQALE_RATING_KEY);
+ this.nclocMetric = metricRepository.getByKey(CoreMetrics.NCLOC_KEY);
+ this.developmentCostMetric = metricRepository.getByKey(CoreMetrics.DEVELOPMENT_COST_KEY);
+ this.technicalDebtMetric = metricRepository.getByKey(CoreMetrics.TECHNICAL_DEBT_KEY);
+ this.debtRatioMetric = metricRepository.getByKey(CoreMetrics.SQALE_DEBT_RATIO_KEY);
+ this.sqaleRatingMetric = metricRepository.getByKey(CoreMetrics.SQALE_RATING_KEY);
}
@Override
}
private long computeDevelopmentCost(Component file) {
- String languageKey = file.getFileAttributes().getLanguageKey();
- String sizeMetricKey = sqaleRatingSettings.getSizeMetricKey(languageKey);
- Metric sizeMetric = metricRepository.getByKey(sizeMetricKey);
- return getLongValue(measureRepository.getRawMeasure(file, sizeMetric)) * sqaleRatingSettings.getDevCost(languageKey);
+ long ncloc = getLongValue(measureRepository.getRawMeasure(file, this.nclocMetric));
+ return ncloc * sqaleRatingSettings.getDevCost(file.getFileAttributes().getLanguageKey());
}
private static long getLongValue(Optional<Measure> measure) {
import org.sonar.server.computation.component.VisitorsCrawler;
import org.sonar.server.computation.measure.Measure;
import org.sonar.server.computation.measure.MeasureRepositoryRule;
-import org.sonar.server.computation.metric.Metric;
-import org.sonar.server.computation.metric.MetricImpl;
import org.sonar.server.computation.metric.MetricRepositoryRule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.api.measures.CoreMetrics.DEVELOPMENT_COST_KEY;
+import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
import static org.sonar.api.measures.CoreMetrics.SQALE_DEBT_RATIO_KEY;
import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY;
import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
public class ReportSqaleMeasuresVisitorTest {
- private static final String METRIC_KEY_1 = "mKey1";
- private static final String METRIC_KEY_2 = "mKey2";
- private static final Metric METRIC_1 = new MetricImpl(1, METRIC_KEY_1, "metric1", Metric.MetricType.FLOAT);
- private static final Metric METRIC_2 = new MetricImpl(2, METRIC_KEY_2, "metric2", Metric.MetricType.WORK_DUR);
private static final String LANGUAGE_KEY_1 = "lKey1";
private static final String LANGUAGE_KEY_2 = "lKey2";
private static final double[] RATING_GRID = new double[] {34, 50, 362, 900, 36258};
public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
@Rule
public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
- .add(METRIC_1)
- .add(METRIC_2)
+ .add(CoreMetrics.NCLOC)
.add(CoreMetrics.DEVELOPMENT_COST)
.add(CoreMetrics.TECHNICAL_DEBT)
.add(CoreMetrics.SQALE_DEBT_RATIO)
public void setUp() {
// assumes SQALE rating configuration is consistent
when(sqaleRatingSettings.getRatingGrid()).thenReturn(RATING_GRID);
- when(sqaleRatingSettings.getSizeMetricKey(LANGUAGE_KEY_1)).thenReturn(METRIC_KEY_1);
- when(sqaleRatingSettings.getSizeMetricKey(LANGUAGE_KEY_2)).thenReturn(METRIC_KEY_2);
when(sqaleRatingSettings.getDevCost(LANGUAGE_KEY_1)).thenReturn(DEV_COST_LANGUAGE_1);
when(sqaleRatingSettings.getDevCost(LANGUAGE_KEY_2)).thenReturn(DEV_COST_LANGUAGE_2);
}
@Test
public void verify_computation_of_measures_for_file_depending_upon_language_1() {
- verify_computation_of_measure_for_file(33000l, DEV_COST_LANGUAGE_1, METRIC_KEY_1, LANGUAGE_KEY_1, C);
+ verify_computation_of_measure_for_file(33000l, DEV_COST_LANGUAGE_1, LANGUAGE_KEY_1, C);
}
@Test
public void verify_computation_of_measures_for_file_depending_upon_language_2() {
- verify_computation_of_measure_for_file(4200l, DEV_COST_LANGUAGE_2, METRIC_KEY_2, LANGUAGE_KEY_2, A);
+ verify_computation_of_measure_for_file(4200l, DEV_COST_LANGUAGE_2, LANGUAGE_KEY_2, A);
}
/**
* Verify the computation of measures values depending upon which language is associated to the file by
* processing a tree of a single Component of type FILE.
*/
- private void verify_computation_of_measure_for_file(long debt, long languageCost, String metricKey, String languageKey,
- SqaleRatingGrid.SqaleRating expectedRating) {
+ private void verify_computation_of_measure_for_file(long debt, long languageCost, String languageKey, SqaleRatingGrid.SqaleRating expectedRating) {
long measureValue = 10;
int componentRef = 1;
ReportComponent fileComponent = createFileComponent(languageKey, componentRef);
treeRootHolder.setRoot(fileComponent);
- addRawMeasure(metricKey, componentRef, measureValue);
+ addRawMeasure(NCLOC_KEY, componentRef, measureValue);
addRawMeasure(TECHNICAL_DEBT_KEY, componentRef, debt);
underTest.visit(fileComponent);
long measureValue1111 = 10;
long debt1111 = 66000l;
- addRawMeasure(METRIC_KEY_1, 1111, measureValue1111);
+ addRawMeasure(NCLOC_KEY, 1111, measureValue1111);
addRawMeasure(TECHNICAL_DEBT_KEY, 1111, debt1111);
long measureValue1112 = 10;
long debt1112 = 4200l;
- addRawMeasure(METRIC_KEY_2, 1112, measureValue1112);
+ addRawMeasure(NCLOC_KEY, 1112, measureValue1112);
addRawMeasure(TECHNICAL_DEBT_KEY, 1112, debt1112);
long debt111 = 96325l;
long measureValue1121 = 30;
long debt1121 = 25200l;
- addRawMeasure(METRIC_KEY_2, 1121, measureValue1121);
+ addRawMeasure(NCLOC_KEY, 1121, measureValue1121);
addRawMeasure(TECHNICAL_DEBT_KEY, 1121, debt1121);
long debt112 = 99633l;
long measureValue1211 = 20;
long debt1211 = 33000l;
- addRawMeasure(METRIC_KEY_1, 1211, measureValue1211);
+ addRawMeasure(NCLOC_KEY, 1211, measureValue1211);
addRawMeasure(TECHNICAL_DEBT_KEY, 1211, debt1211);
long debt121 = 7524l;
assertThat(configurationLoader.getDevCost("defaultLanguage")).isEqualTo(50L);
}
- @Test
- public void load_size_metric_for_language() {
- settings.setProperty(CoreProperties.SIZE_METRIC, "complexity");
- SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
-
- assertThat(configurationLoader.getSizeMetricKey("defaultLanguage")).isEqualTo("complexity");
- }
-
@Test
public void load_overridden_values_for_language() {
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
- assertThat(configurationLoader.getSizeMetricKey(aLanguage)).isEqualTo(CoreMetrics.NCLOC_KEY);
- assertThat(configurationLoader.getSizeMetricKey(anotherLanguage)).isEqualTo(CoreMetrics.COMPLEXITY_KEY);
assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(30L);
assertThat(configurationLoader.getDevCost(anotherLanguage)).isEqualTo(40L);
}
public void use_generic_value_when_specific_setting_is_missing() {
String aLanguage = "aLanguage";
- settings.setProperty(CoreProperties.SIZE_METRIC, "complexity");
settings.setProperty(CoreProperties.DEVELOPMENT_COST, "30");
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS, "0");
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY, aLanguage);
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
- assertThat(configurationLoader.getSizeMetricKey(aLanguage)).isEqualTo(CoreMetrics.COMPLEXITY_KEY);
assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(40L);
}
}
import org.sonar.api.PropertyType;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.PropertyFieldDefinition;
-import org.sonar.api.measures.CoreMetrics;
class DebtProperties {
.deprecatedKey("sqale.hoursInDay")
.build(),
- PropertyDefinition.builder(CoreProperties.SIZE_METRIC)
- .defaultValue("" + CoreMetrics.NCLOC_KEY)
- .name("Size metric")
- .description("Metric used to estimate artifact's development cost.")
- .type(PropertyType.METRIC)
- .options("key:^(ncloc|complexity)$")
- .category(CoreProperties.CATEGORY_TECHNICAL_DEBT)
- .deprecatedKey("sizeMetric")
- .build(),
-
PropertyDefinition.builder(CoreProperties.DEVELOPMENT_COST)
.defaultValue("" + CoreProperties.DEVELOPMENT_COST_DEF_VALUE)
.name("Development cost")
.name("Development cost")
.description("If left blank, the generic value defined in this section will be used.")
.type(PropertyType.FLOAT)
- .build(),
- PropertyFieldDefinition.build(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_SIZE_METRIC_KEY)
- .name("Size metric")
- .description("If left blank, the generic value defined in this section will be used.")
- .type(PropertyType.METRIC)
- .options("key:^(ncloc|complexity)$")
.build()
)
.build()