import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.measure.Measure;
import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
-import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
import org.sonar.server.computation.task.projectanalysis.metric.Metric;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
import org.sonar.server.computation.task.projectanalysis.period.Period;
CRITICAL, CRITICAL_VIOLATIONS_KEY,
MAJOR, MAJOR_VIOLATIONS_KEY,
MINOR, MINOR_VIOLATIONS_KEY,
- INFO, INFO_VIOLATIONS_KEY
- );
+ INFO, INFO_VIOLATIONS_KEY);
private static final Map<String, String> SEVERITY_TO_NEW_METRIC_KEY = ImmutableMap.of(
BLOCKER, NEW_BLOCKER_VIOLATIONS_KEY,
CRITICAL, NEW_CRITICAL_VIOLATIONS_KEY,
MAJOR, NEW_MAJOR_VIOLATIONS_KEY,
MINOR, NEW_MINOR_VIOLATIONS_KEY,
- INFO, NEW_INFO_VIOLATIONS_KEY
- );
+ INFO, NEW_INFO_VIOLATIONS_KEY);
private static final Map<RuleType, String> TYPE_TO_METRIC_KEY = ImmutableMap.<RuleType, String>builder()
.put(RuleType.CODE_SMELL, CoreMetrics.CODE_SMELLS_KEY)
@Override
public void onIssue(Component component, DefaultIssue issue) {
currentCounters.add(issue);
- for (Period period : periodsHolder.getPeriods()) {
- // Add one second to not take into account issues created during current analysis
- if (issue.creationDate().getTime() >= period.getSnapshotDate() + 1000L) {
- currentCounters.addOnPeriod(issue, period.getIndex());
- }
+ if (!periodsHolder.hasPeriod()) {
+ return;
+ }
+ Period period = periodsHolder.getPeriod();
+ // Add one second to not take into account issues created during current analysis
+ if (issue.creationDate().getTime() >= period.getSnapshotDate() + 1000L) {
+ currentCounters.addOnPeriod(issue);
}
}
}
private void addMeasuresByPeriod(Component component) {
- if (!periodsHolder.getPeriods().isEmpty()) {
- Double[] unresolvedVariations = new Double[PeriodsHolder.MAX_NUMBER_OF_PERIODS];
- for (Period period : periodsHolder.getPeriods()) {
- unresolvedVariations[period.getIndex() - 1] = (double) currentCounters.counterForPeriod(period.getIndex()).unresolved;
- }
- measureRepository.add(component, metricRepository.getByKey(NEW_VIOLATIONS_KEY), Measure.newMeasureBuilder()
- .setVariations(new MeasureVariations(unresolvedVariations))
- .createNoValue());
+ if (!periodsHolder.hasPeriod()) {
+ return;
+ }
+ Double unresolvedVariations = (double) currentCounters.counterForPeriod().unresolved;
+ measureRepository.add(component, metricRepository.getByKey(NEW_VIOLATIONS_KEY), Measure.newMeasureBuilder()
+ .setVariation(unresolvedVariations)
+ .createNoValue());
- for (Map.Entry<String, String> entry : SEVERITY_TO_NEW_METRIC_KEY.entrySet()) {
- String severity = entry.getKey();
- String metricKey = entry.getValue();
- Double[] variations = new Double[PeriodsHolder.MAX_NUMBER_OF_PERIODS];
- for (Period period : periodsHolder.getPeriods()) {
- Multiset<String> bag = currentCounters.counterForPeriod(period.getIndex()).severityBag;
- variations[period.getIndex() - 1] = (double) bag.count(severity);
- }
- Metric metric = metricRepository.getByKey(metricKey);
- measureRepository.add(component, metric, Measure.newMeasureBuilder()
- .setVariations(new MeasureVariations(variations))
- .createNoValue());
- }
+ for (Map.Entry<String, String> entry : SEVERITY_TO_NEW_METRIC_KEY.entrySet()) {
+ String severity = entry.getKey();
+ String metricKey = entry.getValue();
+ Multiset<String> bag = currentCounters.counterForPeriod().severityBag;
+ Metric metric = metricRepository.getByKey(metricKey);
+ measureRepository.add(component, metric, Measure.newMeasureBuilder()
+ .setVariation((double) bag.count(severity))
+ .createNoValue());
+ }
- // waiting for Java 8 lambda in order to factor this loop with the previous one
- // (see call currentCounters.counterForPeriod(period.getIndex()).xxx with xxx as severityBag or typeBag)
- for (Map.Entry<RuleType, String> entry : TYPE_TO_NEW_METRIC_KEY.entrySet()) {
- RuleType type = entry.getKey();
- String metricKey = entry.getValue();
- Double[] variations = new Double[PeriodsHolder.MAX_NUMBER_OF_PERIODS];
- for (Period period : periodsHolder.getPeriods()) {
- Multiset<RuleType> bag = currentCounters.counterForPeriod(period.getIndex()).typeBag;
- variations[period.getIndex() - 1] = (double) bag.count(type);
- }
- Metric metric = metricRepository.getByKey(metricKey);
- measureRepository.add(component, metric, Measure.newMeasureBuilder()
- .setVariations(new MeasureVariations(variations))
- .createNoValue());
- }
+ // waiting for Java 8 lambda in order to factor this loop with the previous one
+ // (see call currentCounters.counterForPeriod(period.getIndex()).xxx with xxx as severityBag or typeBag)
+ for (Map.Entry<RuleType, String> entry : TYPE_TO_NEW_METRIC_KEY.entrySet()) {
+ RuleType type = entry.getKey();
+ String metricKey = entry.getValue();
+ Multiset<RuleType> bag = currentCounters.counterForPeriod().typeBag;
+ Metric metric = metricRepository.getByKey(metricKey);
+ measureRepository.add(component, metric, Measure.newMeasureBuilder()
+ .setVariation((double) bag.count(type))
+ .createNoValue());
}
}
}
/**
- * List of {@link Counter} for regular value and periods.
+ * List of {@link Counter} for regular value and period.
*/
private static class Counters {
- private final Counter[] array = new Counter[1 + PeriodsHolder.MAX_NUMBER_OF_PERIODS];
-
- Counters() {
- array[0] = new Counter();
- for (int i = 1; i <= PeriodsHolder.MAX_NUMBER_OF_PERIODS; i++) {
- array[i] = new Counter();
- }
- }
+ private final Counter counter = new Counter();
+ private final Counter counterForPeriod = new Counter();
void add(@Nullable Counters other) {
if (other != null) {
- for (int i = 0; i < array.length; i++) {
- array[i].add(other.array[i]);
- }
+ counter.add(other.counter);
+ counterForPeriod.add(other.counterForPeriod);
}
}
- void addOnPeriod(DefaultIssue issue, int periodIndex) {
- array[periodIndex].add(issue);
+ void addOnPeriod(DefaultIssue issue) {
+ counterForPeriod.add(issue);
}
void add(DefaultIssue issue) {
- array[0].add(issue);
+ counter.add(issue);
}
Counter counter() {
- return array[0];
+ return counter;
}
- Counter counterForPeriod(int periodIndex) {
- return array[periodIndex];
+ Counter counterForPeriod() {
+ return counterForPeriod;
}
}
}
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.measure.Measure;
import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
-import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
import org.sonar.server.computation.task.projectanalysis.metric.Metric;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
import org.sonar.server.computation.task.projectanalysis.period.Period;
@Override
public void onIssue(Component component, DefaultIssue issue) {
- if (issue.resolution() == null && issue.effortInMinutes() != null && !periodsHolder.getPeriods().isEmpty()) {
+ if (issue.resolution() == null && issue.effortInMinutes() != null && periodsHolder.hasPeriod()) {
List<IssueChangeDto> changelog = changesByIssueUuid.get(issue.key());
- for (Period period : periodsHolder.getPeriods()) {
- counter.add(issue, period, changelog);
- }
+ counter.add(issue, periodsHolder.getPeriod(), changelog);
}
}
private void computeMeasure(Component component, Metric metric, EffortSum effortSum) {
if (!effortSum.isEmpty) {
- MeasureVariations variations = new MeasureVariations(effortSum.sums);
- measureRepository.add(component, metric, Measure.newMeasureBuilder().setVariations(variations).createNoValue());
+ measureRepository.add(component, metric, Measure.newMeasureBuilder().setVariation(effortSum.newEffort).createNoValue());
}
}
long newEffort = calculator.calculate(issue, changelog, period);
switch (issue.type()) {
case CODE_SMELL:
- maintainabilitySum.add(period.getIndex(), newEffort);
+ maintainabilitySum.add(newEffort);
break;
case BUG:
- reliabilitySum.add(period.getIndex(), newEffort);
+ reliabilitySum.add(newEffort);
break;
case VULNERABILITY:
- securitySum.add(period.getIndex(), newEffort);
+ securitySum.add(newEffort);
break;
default:
throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
}
private static class EffortSum {
- private final Double[] sums = new Double[PeriodsHolder.MAX_NUMBER_OF_PERIODS];
+ private Double newEffort;
private boolean isEmpty = true;
- void add(int periodIndex, long newEffort) {
- double previous = MoreObjects.firstNonNull(sums[periodIndex - 1], 0d);
- sums[periodIndex - 1] = previous + newEffort;
+ void add(long newEffort) {
+ double previous = MoreObjects.firstNonNull(this.newEffort, 0d);
+ this.newEffort = previous + newEffort;
isEmpty = false;
}
void add(EffortSum other) {
- for (int i = 0; i < sums.length; i++) {
- Double otherValue = other.sums[i];
- if (otherValue != null) {
- add(i + 1, otherValue.longValue());
- }
+ Double otherValue = other.newEffort;
+ if (otherValue != null) {
+ add(otherValue.longValue());
}
}
}
import org.sonar.server.computation.task.projectanalysis.issue.IntegrateIssuesVisitor;
import org.sonar.server.computation.task.projectanalysis.measure.Measure;
import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
-import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
import org.sonar.server.computation.task.projectanalysis.metric.Metric;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
import org.sonar.server.computation.task.projectanalysis.period.Period;
import static org.sonar.api.utils.KeyValueFormat.newIntegerConverter;
import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
-import static org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations.newMeasureVariationsBuilder;
/**
* This visitor depends on {@link IntegrateIssuesVisitor} for the computation of
}
private void computeAndSaveNewDebtRatioMeasure(Component component, Path<Counter> path) {
- MeasureVariations.Builder newDebtRatio = newMeasureVariationsBuilder();
- MeasureVariations.Builder newMaintainability = newMeasureVariationsBuilder();
- for (Period period : periodsHolder.getPeriods()) {
- double density = computeDensity(path.current(), period);
- newDebtRatio.setVariation(period, 100.0 * density);
- newMaintainability.setVariation(period, ratingGrid.getRatingForDensity(density).getIndex());
- }
- if (!newDebtRatio.isEmpty()) {
- measureRepository.add(component, this.newDebtRatioMetric, newMeasureBuilder().setVariations(newDebtRatio.build()).createNoValue());
- }
- if (!newMaintainability.isEmpty()) {
- measureRepository.add(component, this.newMaintainabilityRatingMetric, newMeasureBuilder().setVariations(newMaintainability.build()).createNoValue());
+ if (!periodsHolder.hasPeriod()) {
+ return;
}
+ double density = computeDensity(path.current());
+ double newDebtRatio = 100.0 * density;
+ double newMaintainability = ratingGrid.getRatingForDensity(density).getIndex();
+ measureRepository.add(component, this.newDebtRatioMetric, newMeasureBuilder().setVariation(newDebtRatio).createNoValue());
+ measureRepository.add(component, this.newMaintainabilityRatingMetric, newMeasureBuilder().setVariation(newMaintainability).createNoValue());
}
- private static double computeDensity(Counter counter, Period period) {
- LongVariationValue newDebt = counter.getNewDebt(period);
+ private static double computeDensity(Counter counter) {
+ LongVariationValue newDebt = counter.getNewDebt();
if (newDebt.isSet()) {
- long developmentCost = counter.getDevCost(period).getValue();
+ long developmentCost = counter.getDevCost().getValue();
if (developmentCost != 0L) {
return newDebt.getValue() / (double) developmentCost;
}
return 0d;
}
- private static long getLongValue(Optional<Measure> measure, Period period) {
+ private static long getLongValue(Optional<Measure> measure) {
if (!measure.isPresent()) {
return 0L;
}
- return getLongValue(measure.get(), period);
+ return getLongValue(measure.get());
}
- private static long getLongValue(Measure measure, Period period) {
- if (measure.hasVariations() && measure.getVariations().hasVariation(period.getIndex())) {
- return (long) measure.getVariations().getVariation(period.getIndex());
+ private static long getLongValue(Measure measure) {
+ if (measure.hasVariation()) {
+ return (long) measure.getVariation();
}
return 0L;
}
private void initNewDebtRatioCounter(Component file, Path<Counter> path) {
// first analysis, no period, no differential value to compute, save processing time and return now
- if (periodsHolder.getPeriods().isEmpty()) {
+ if (!periodsHolder.hasPeriod()) {
return;
}
}
private void initNewDebtRatioCounter(Counter devCostCounter, Component file, Measure nclocDataMeasure, ScmInfo scmInfo) {
- boolean[] hasDevCost = new boolean[PeriodsHolder.MAX_NUMBER_OF_PERIODS];
+ boolean hasDevCost = false;
long lineDevCost = ratingSettings.getDevCost(file.getFileAttributes().getLanguageKey());
for (Integer nclocLineIndex : nclocLineIndexes(nclocDataMeasure)) {
Changeset changeset = scmInfo.getChangesetForLine(nclocLineIndex);
- for (Period period : periodsHolder.getPeriods()) {
- if (isLineInPeriod(changeset.getDate(), period)) {
- devCostCounter.incrementDevCost(period, lineDevCost);
- hasDevCost[period.getIndex() - 1] = true;
- }
+ Period period = periodsHolder.getPeriod();
+ if (isLineInPeriod(changeset.getDate(), period)) {
+ devCostCounter.incrementDevCost(lineDevCost);
+ hasDevCost = true;
}
}
- for (Period period : periodsHolder.getPeriods()) {
- if (hasDevCost[period.getIndex() - 1]) {
- long newDebt = getLongValue(measureRepository.getRawMeasure(file, this.newDebtMetric), period);
- devCostCounter.incrementNewDebt(period, newDebt);
- }
+ if (hasDevCost) {
+ long newDebt = getLongValue(measureRepository.getRawMeasure(file, this.newDebtMetric));
+ devCostCounter.incrementNewDebt(newDebt);
}
}
}
public static final class Counter {
- private final LongVariationValue.Array newDebt = LongVariationValue.newArray();
- private final LongVariationValue.Array devCost = LongVariationValue.newArray();
+ private final LongVariationValue newDebt = new LongVariationValue();
+ private final LongVariationValue devCost = new LongVariationValue();
public void add(Counter counter) {
- this.newDebt.incrementAll(counter.newDebt);
- this.devCost.incrementAll(counter.devCost);
+ this.newDebt.increment(counter.newDebt);
+ this.devCost.increment(counter.devCost);
}
- LongVariationValue.Array incrementNewDebt(Period period, long value) {
- return newDebt.increment(period, value);
+ LongVariationValue incrementNewDebt(long value) {
+ return newDebt.increment(value);
}
- LongVariationValue.Array incrementDevCost(Period period, long value) {
- return devCost.increment(period, value);
+ LongVariationValue incrementDevCost(long value) {
+ return devCost.increment(value);
}
- LongVariationValue getNewDebt(Period period) {
- return this.newDebt.get(period);
+ LongVariationValue getNewDebt() {
+ return this.newDebt;
}
- LongVariationValue getDevCost(Period period) {
- return this.devCost.get(period);
+ LongVariationValue getDevCost() {
+ return this.devCost;
}
}
private final ComponentIssuesRepository componentIssuesRepository;
private final PeriodsHolder periodsHolder;
- // Output metrics
- private final Metric newReliabilityRatingMetric;
- private final Metric newSecurityRatingMetric;
-
private final Map<String, Metric> metricsByKey;
public NewReliabilityAndSecurityRatingMeasuresVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, ComponentIssuesRepository componentIssuesRepository,
this.periodsHolder = periodsHolder;
// Output metrics
- this.newReliabilityRatingMetric = metricRepository.getByKey(NEW_RELIABILITY_RATING_KEY);
- this.newSecurityRatingMetric = metricRepository.getByKey(NEW_SECURITY_RATING_KEY);
-
this.metricsByKey = ImmutableMap.of(
- NEW_RELIABILITY_RATING_KEY, newReliabilityRatingMetric,
- NEW_SECURITY_RATING_KEY, newSecurityRatingMetric);
+ NEW_RELIABILITY_RATING_KEY, metricRepository.getByKey(NEW_RELIABILITY_RATING_KEY),
+ NEW_SECURITY_RATING_KEY, metricRepository.getByKey(NEW_SECURITY_RATING_KEY));
}
@Override
}
private void computeAndSaveMeasures(Component component, Path<Counter> path) {
+ if (!periodsHolder.hasPeriod()) {
+ return;
+ }
initRatingsToA(path);
processIssues(component, path);
- path.current().newRatingValueByMetric.entrySet().forEach(
- entry -> entry.getValue().toMeasureVariations()
- .ifPresent(measureVariations -> measureRepository.add(
+ path.current().newRatingValueByMetric.entrySet()
+ .stream()
+ .filter(entry -> entry.getValue().isSet())
+ .forEach(
+ entry -> measureRepository.add(
component,
metricsByKey.get(entry.getKey()),
- newMeasureBuilder().setVariations(measureVariations).createNoValue())));
+ newMeasureBuilder().setVariation(entry.getValue().getValue().getIndex()).createNoValue()));
addToParent(path);
}
- private void initRatingsToA(Path<Counter> path) {
- periodsHolder.getPeriods().forEach(period -> path.current().newRatingValueByMetric.values()
- .forEach(entry -> entry.increment(period, A)));
+ private static void initRatingsToA(Path<Counter> path) {
+ path.current().newRatingValueByMetric.values().forEach(entry -> entry.increment(A));
}
private void processIssues(Component component, Path<Counter> path) {
.stream()
.filter(issue -> issue.resolution() == null)
.filter(issue -> issue.type().equals(BUG) || issue.type().equals(VULNERABILITY))
- .forEach(issue -> periodsHolder.getPeriods().forEach(period -> path.current().processIssue(issue, period)));
+ .forEach(issue -> path.current().processIssue(issue, periodsHolder.getPeriod()));
}
private static void addToParent(Path<Counter> path) {
}
static final class Counter {
- private Map<String, RatingVariationValue.Array> newRatingValueByMetric = ImmutableMap.of(
- NEW_RELIABILITY_RATING_KEY, new RatingVariationValue.Array(),
- NEW_SECURITY_RATING_KEY, new RatingVariationValue.Array());
+ private Map<String, RatingVariationValue> newRatingValueByMetric = ImmutableMap.of(
+ NEW_RELIABILITY_RATING_KEY, new RatingVariationValue(),
+ NEW_SECURITY_RATING_KEY, new RatingVariationValue());
private Counter() {
// prevents instantiation
}
void add(Counter otherCounter) {
- newRatingValueByMetric.entrySet().forEach(e -> e.getValue().incrementAll(otherCounter.newRatingValueByMetric.get(e.getKey())));
+ newRatingValueByMetric.entrySet().forEach(e -> e.getValue().increment(otherCounter.newRatingValueByMetric.get(e.getKey())));
}
void processIssue(Issue issue, Period period) {
if (isOnPeriod((DefaultIssue) issue, period)) {
Rating rating = RATING_BY_SEVERITY.get(issue.severity());
if (issue.type().equals(BUG)) {
- newRatingValueByMetric.get(NEW_RELIABILITY_RATING_KEY).increment(period, rating);
+ newRatingValueByMetric.get(NEW_RELIABILITY_RATING_KEY).increment(rating);
} else if (issue.type().equals(VULNERABILITY)) {
- newRatingValueByMetric.get(NEW_SECURITY_RATING_KEY).increment(period, rating);
+ newRatingValueByMetric.get(NEW_SECURITY_RATING_KEY).increment(rating);
}
}
}
import java.util.Date;
import javax.annotation.Nullable;
import org.assertj.core.data.Offset;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
+import org.sonar.server.computation.task.projectanalysis.measure.Measure;
import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
-import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
import org.sonar.server.computation.task.projectanalysis.metric.Metric;
import org.sonar.server.computation.task.projectanalysis.metric.MetricImpl;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
@Rule
public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
- IssueCounter underTest;
-
- @Before
- public void setUp() {
- underTest = new IssueCounter(periodsHolder, metricRepository, measureRepository);
- }
+ private IssueCounter underTest = new IssueCounter(periodsHolder, metricRepository, measureRepository);;
@Test
public void count_issues_by_status() {
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
// bottom-up traversal -> from files to project
underTest.beforeComponent(FILE1);
@Test
public void count_issues_by_resolution() {
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
// bottom-up traversal -> from files to project
underTest.beforeComponent(FILE1);
@Test
public void count_unresolved_issues_by_severity() {
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
// bottom-up traversal -> from files to project
underTest.beforeComponent(FILE1);
@Test
public void count_unresolved_issues_by_type() {
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
// bottom-up traversal -> from files to project
// file1 : one open code smell, one closed code smell (which will be excluded from metric)
@Test
public void count_new_issues() {
- Period period = newPeriod(3, 1500000000000L);
- periodsHolder.setPeriods(period);
+ Period period = newPeriod(1500000000000L);
+ periodsHolder.setPeriod(period);
underTest.beforeComponent(FILE1);
// created before -> existing issues (so ignored)
underTest.beforeComponent(PROJECT);
underTest.afterComponent(PROJECT);
- assertVariation(FILE1, NEW_ISSUES_METRIC, period.getIndex(), 2);
- assertVariation(FILE1, NEW_CRITICAL_ISSUES_METRIC, period.getIndex(), 2);
- assertVariation(FILE1, NEW_BLOCKER_ISSUES_METRIC, period.getIndex(), 0);
- assertVariation(FILE1, NEW_MAJOR_ISSUES_METRIC, period.getIndex(), 0);
- assertVariation(FILE1, NEW_CODE_SMELLS_METRIC, period.getIndex(), 1);
- assertVariation(FILE1, NEW_BUGS_METRIC, period.getIndex(), 1);
- assertVariation(FILE1, NEW_VULNERABILITIES_METRIC, period.getIndex(), 0);
-
- assertVariation(PROJECT, NEW_ISSUES_METRIC, period.getIndex(), 2);
- assertVariation(PROJECT, NEW_CRITICAL_ISSUES_METRIC, period.getIndex(), 2);
- assertVariation(PROJECT, NEW_BLOCKER_ISSUES_METRIC, period.getIndex(), 0);
- assertVariation(PROJECT, NEW_MAJOR_ISSUES_METRIC, period.getIndex(), 0);
- assertVariation(PROJECT, NEW_CODE_SMELLS_METRIC, period.getIndex(), 1);
- assertVariation(PROJECT, NEW_BUGS_METRIC, period.getIndex(), 1);
- assertVariation(PROJECT, NEW_VULNERABILITIES_METRIC, period.getIndex(), 0);
+ assertVariation(FILE1, NEW_ISSUES_METRIC, 2);
+ assertVariation(FILE1, NEW_CRITICAL_ISSUES_METRIC, 2);
+ assertVariation(FILE1, NEW_BLOCKER_ISSUES_METRIC, 0);
+ assertVariation(FILE1, NEW_MAJOR_ISSUES_METRIC, 0);
+ assertVariation(FILE1, NEW_CODE_SMELLS_METRIC, 1);
+ assertVariation(FILE1, NEW_BUGS_METRIC, 1);
+ assertVariation(FILE1, NEW_VULNERABILITIES_METRIC, 0);
+
+ assertVariation(PROJECT, NEW_ISSUES_METRIC, 2);
+ assertVariation(PROJECT, NEW_CRITICAL_ISSUES_METRIC, 2);
+ assertVariation(PROJECT, NEW_BLOCKER_ISSUES_METRIC, 0);
+ assertVariation(PROJECT, NEW_MAJOR_ISSUES_METRIC, 0);
+ assertVariation(PROJECT, NEW_CODE_SMELLS_METRIC, 1);
+ assertVariation(PROJECT, NEW_BUGS_METRIC, 1);
+ assertVariation(PROJECT, NEW_VULNERABILITIES_METRIC, 0);
}
- private void assertVariation(Component component, Metric metric, int periodIndex, int expectedVariation) {
- MeasureVariations variations = measureRepository.getRawMeasure(component, metric).get().getVariations();
- assertThat(variations.getVariation(periodIndex)).isEqualTo((double) expectedVariation, Offset.offset(0.01));
+ private void assertVariation(Component component, Metric metric, int expectedVariation) {
+ Measure measure = measureRepository.getRawMeasure(component, metric).get();
+ assertThat(measure.getVariation()).isEqualTo((double) expectedVariation, Offset.offset(0.01));
}
private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity) {
.setCreationDate(new Date(creationDate));
}
- private static Period newPeriod(int index, long date) {
- return new Period(index, "mode", null, date, "U1");
+ private static Period newPeriod(long date) {
+ return new Period("mode", null, date, "U1");
}
}
private static final Period PERIOD = new Period(1, TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, 1_500_000_000L, "U1");
- static final Component FILE = ReportComponent.builder(Component.Type.FILE, 1).setUuid("FILE").build();
- static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 2).addChildren(FILE).build();
+ private static final Component FILE = ReportComponent.builder(Component.Type.FILE, 1).setUuid("FILE").build();
+ private static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 2).addChildren(FILE).build();
- NewEffortCalculator calculator = mock(NewEffortCalculator.class);
+ private NewEffortCalculator calculator = mock(NewEffortCalculator.class);
@org.junit.Rule
public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
@org.junit.Rule
public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create();
- DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
+ private DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
- NewEffortAggregator underTest = new NewEffortAggregator(calculator, periodsHolder, dbClient, metricRepository, measureRepository);
+ private NewEffortAggregator underTest = new NewEffortAggregator(calculator, periodsHolder, dbClient, metricRepository, measureRepository);
@Test
public void sum_new_maintainability_effort_of_issues() {
- periodsHolder.setPeriods(PERIOD);
+ periodsHolder.setPeriod(PERIOD);
DefaultIssue unresolved1 = newCodeSmellIssue(10L);
DefaultIssue unresolved2 = newCodeSmellIssue(30L);
DefaultIssue unresolvedWithoutDebt = newCodeSmellIssueWithoutEffort();
@Test
public void new_maintainability_effort_is_only_computed_using_code_smell_issues() {
- periodsHolder.setPeriods(PERIOD);
+ periodsHolder.setPeriod(PERIOD);
DefaultIssue codeSmellIssue = newCodeSmellIssue(10);
// Issues of type BUG and VULNERABILITY should be ignored
DefaultIssue bugIssue = newBugIssue(15);
@Test
public void sum_new_reliability_effort_of_issues() {
- periodsHolder.setPeriods(PERIOD);
+ periodsHolder.setPeriod(PERIOD);
DefaultIssue unresolved1 = newBugIssue(10L);
DefaultIssue unresolved2 = newBugIssue(30L);
DefaultIssue unresolvedWithoutDebt = newBugIssueWithoutEffort();
@Test
public void new_reliability_effort_is_only_computed_using_bug_issues() {
- periodsHolder.setPeriods(PERIOD);
+ periodsHolder.setPeriod(PERIOD);
DefaultIssue bugIssue = newBugIssue(15);
// Issues of type CODE SMELL and VULNERABILITY should be ignored
DefaultIssue codeSmellIssue = newCodeSmellIssue(10);
@Test
public void sum_new_securtiy_effort_of_issues() {
- periodsHolder.setPeriods(PERIOD);
+ periodsHolder.setPeriod(PERIOD);
DefaultIssue unresolved1 = newVulnerabilityIssue(10L);
DefaultIssue unresolved2 = newVulnerabilityIssue(30L);
DefaultIssue unresolvedWithoutDebt = newVulnerabilityIssueWithoutEffort();
@Test
public void new_security_effort_is_only_computed_using_vulnerability_issues() {
- periodsHolder.setPeriods(PERIOD);
+ periodsHolder.setPeriod(PERIOD);
DefaultIssue vulnerabilityIssue = newVulnerabilityIssue(12);
// Issues of type CODE SMELL and BUG should be ignored
DefaultIssue codeSmellIssue = newCodeSmellIssue(10);
@Test
public void aggregate_new_characteristic_measures_of_children() {
- periodsHolder.setPeriods(PERIOD);
+ periodsHolder.setPeriod(PERIOD);
DefaultIssue codeSmellIssue = newCodeSmellIssue(10);
when(calculator.calculate(same(codeSmellIssue), anyList(), same(PERIOD))).thenReturn(4L);
@Test
public void no_measures_if_no_periods() {
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
DefaultIssue unresolved = new DefaultIssue().setEffort(Duration.create(10));
verifyZeroInteractions(calculator);
private void assertVariation(Component component, String metricKey, int variation) {
Measure newMeasure = measureRepository.getRawMeasure(component, metricRepository.getByKey(metricKey)).get();
- assertThat(newMeasure.getVariations().getVariation(PERIOD.getIndex())).isEqualTo(variation);
- assertThat(newMeasure.getVariations().hasVariation(PERIOD.getIndex() + 1)).isFalse();
+ assertThat(newMeasure.getVariation()).isEqualTo(variation);
}
private static DefaultIssue newCodeSmellIssue(long effort) {
return this;
}
- public MeasureAssert hasVariation1(double expected) {
+ public MeasureAssert hasVariation(double expected) {
isNotNull();
- hasVariations();
+ hasVariation();
- if (!actual.getVariations().hasVariation1()) {
- failWithMessage("Expected Measure to have a Variation 1 but it did not");
+ if (!actual.hasVariation()) {
+ failWithMessage("Expected Measure to have a variation but it did not");
}
- if (actual.getVariations().getVariation1() != expected) {
- failWithMessage("Expected Variation 1 of Measure to be <%s> but was <%s>", expected, actual.getVariations().getVariation1());
+ double variation = actual.getVariation();
+ if (variation != expected) {
+ failWithMessage("Expected variation of Measure to be <%s> but was <%s>", expected, variation);
}
return this;
}
- public MeasureAssert hasVariation1(double expected, Offset<Double> offset) {
+ public MeasureAssert hasVariation(double expected, Offset<Double> offset) {
isNotNull();
- hasVariations();
+ hasVariation();
- if (!actual.getVariations().hasVariation1()) {
- failWithMessage("Expected Measure to have a Variation 1 but it did not");
+ if (!actual.hasVariation()) {
+ failWithMessage("Expected Measure to have a variation but it did not");
}
- if (abs(expected - actual.getVariations().getVariation1()) <= offset.value) {
+ double variation = actual.getVariation();
+ if (abs(expected - variation) > offset.value) {
failWithMessage(
- "Expected Variation 1 of Measure to be close to <%s> by less than <%s> but was <%s>",
- expected, offset.value, actual.getVariations().getVariation1());
+ "Expected variation of Measure to be close to <%s> by less than <%s> but was <%s>",
+ expected, offset.value, variation);
}
return this;
}
- public MeasureAssert hasVariation2(double expected) {
- isNotNull();
- hasVariations();
-
- if (!actual.getVariations().hasVariation2()) {
- failWithMessage("Expected Measure to have a Variation 2 but it did not");
- }
-
- if (actual.getVariations().getVariation2() != expected) {
- failWithMessage("Expected Variation 2 of Measure to be <%s> but was <%s>", expected, actual.getVariations().getVariation2());
- }
-
- return this;
- }
-
- public MeasureAssert hasVariation2(double expected, Offset<Double> offset) {
- isNotNull();
- hasVariations();
-
- if (!actual.getVariations().hasVariation2()) {
- failWithMessage("Expected Measure to have a Variation 2 but it did not");
- }
-
- if (abs(expected - actual.getVariations().getVariation2()) > offset.value) {
- failWithMessage(
- "Expected Variation 2 of Measure to be close to <%s> by less than <%s> but was <%s>",
- expected, offset.value, actual.getVariations().getVariation2());
- }
-
- return this;
- }
-
- public MeasureAssert hasVariation3(double expected) {
- isNotNull();
- hasVariations();
-
- if (!actual.getVariations().hasVariation3()) {
- failWithMessage("Expected Measure to have a Variation 3 but it did not");
- }
-
- if (actual.getVariations().getVariation3() != expected) {
- failWithMessage("Expected Variation 3 of Measure to be <%s> but was <%s>", expected, actual.getVariations().getVariation3());
- }
-
- return this;
- }
-
- public MeasureAssert hasVariation3(double expected, Offset<Double> offset) {
- isNotNull();
- hasVariations();
-
- if (!actual.getVariations().hasVariation3()) {
- failWithMessage("Expected Measure to have a Variation 3 but it did not");
- }
-
- if (abs(expected - actual.getVariations().getVariation3()) > offset.value) {
- failWithMessage(
- "Expected Variation 3 of Measure to be close to <%s> by less than <%s> but was <%s>",
- expected, offset.value, actual.getVariations().getVariation3());
- }
-
- return this;
- }
-
- public MeasureAssert hasVariation4(double expected) {
- isNotNull();
- hasVariations();
-
- if (!actual.getVariations().hasVariation4()) {
- failWithMessage("Expected Measure to have a Variation 4 but it did not");
- }
-
- if (actual.getVariations().getVariation4() != expected) {
- failWithMessage("Expected Variation 4 of Measure to be <%s> but was <%s>", expected, actual.getVariations().getVariation4());
- }
-
- return this;
- }
-
- public MeasureAssert hasVariation4(double expected, Offset<Double> offset) {
- isNotNull();
- hasVariations();
-
- if (!actual.getVariations().hasVariation4()) {
- failWithMessage("Expected Measure to have a Variation 4 but it did not");
- }
-
- if (abs(expected - actual.getVariations().getVariation4()) > offset.value) {
- failWithMessage(
- "Expected Variation 4 of Measure to be close to <%s> by less than <%s> but was <%s>",
- expected, offset.value, actual.getVariations().getVariation4());
- }
-
- return this;
- }
-
- public MeasureAssert hasVariation5(double expected) {
- isNotNull();
- hasVariations();
-
- if (!actual.getVariations().hasVariation5()) {
- failWithMessage("Expected Measure to have a Variation 5 but it did not");
- }
-
- if (actual.getVariations().getVariation5() != expected) {
- failWithMessage("Expected Variation 5 of Measure to be <%s> but was <%s>", expected, actual.getVariations().getVariation5());
- }
-
- return this;
- }
-
- public MeasureAssert hasVariation5(double expected, Offset<Double> offset) {
- isNotNull();
- hasVariations();
-
- if (!actual.getVariations().hasVariation5()) {
- failWithMessage("Expected Measure to have a Variation 5 but it did not");
- }
-
- if (abs(expected - actual.getVariations().getVariation5()) > offset.value) {
- failWithMessage(
- "Expected Variation 5 of Measure to be close to <%s> by less than <%s> but was <%s>",
- expected, offset.value, actual.getVariations().getVariation5());
- }
-
- return this;
- }
-
- private void hasVariations() {
- if (!actual.hasVariations()) {
- failWithMessage("Expected Measure to have a Variations but it did not");
+ private void hasVariation() {
+ if (!actual.hasVariation()) {
+ failWithMessage("Expected Measure to have a variation but it did not");
}
}
import java.util.Arrays;
import java.util.List;
+import javax.annotation.Nullable;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
return this;
}
- public PeriodsHolderRule setPeriod(Period period) {
+ public PeriodsHolderRule setPeriod(@Nullable Period period) {
delegate = new PeriodsHolderImpl();
delegate.setPeriod(period);
return this;
import org.sonar.server.computation.task.projectanalysis.component.VisitorsCrawler;
import org.sonar.server.computation.task.projectanalysis.measure.Measure;
import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
-import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
import org.sonar.server.computation.task.projectanalysis.period.Period;
import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolderRule;
private static final String LANGUAGE_1_KEY = "language 1 key";
private static final long LANGUAGE_1_DEV_COST = 30l;
- private static final long PERIOD_2_SNAPSHOT_DATE = 12323l;
- private static final long PERIOD_5_SNAPSHOT_DATE = 99999999l;
+ private static final long PERIOD_SNAPSHOT_DATE = 12323l;
private static final String SOME_ANALYSIS_UUID = "9993l";
private static final String SOME_PERIOD_MODE = "some mode";
private static final int ROOT_REF = 1;
@Test
public void project_has_new_measures_for_each_defined_period() {
- setTwoPeriods();
+ setPeriod();
treeRootHolder.setRoot(builder(PROJECT, ROOT_REF).build());
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(ROOT_REF, 0, 0);
- assertNewMaintainability(ROOT_REF, A, A);
+ assertNewDebtRatioValues(ROOT_REF, 0);
+ assertNewMaintainability(ROOT_REF, A);
}
@Test
public void project_has_no_measure_if_there_is_no_period() {
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
treeRootHolder.setRoot(builder(PROJECT, ROOT_REF).build());
underTest.visit(treeRootHolder.getRoot());
@Test
public void file_has_no_new_debt_ratio_variation_if_there_is_no_period() {
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(50, 12, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50, 12));
+ setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
underTest.visit(treeRootHolder.getRoot());
@Test
public void file_has_0_new_debt_ratio_if_all_scm_dates_are_before_snapshot_dates() {
- setTwoPeriods();
+ setPeriod();
treeRootHolder.setRoot(
builder(PROJECT, ROOT_REF)
.addChildren(
builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_1_KEY, 1)).build())
.build());
- measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50, 12));
+ measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
- scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_2_SNAPSHOT_DATE - 100, 4));
+ scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_SNAPSHOT_DATE - 100, 4));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0, 0);
- assertNewDebtRatioValues(ROOT_REF, 0, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
+ assertNewDebtRatioValues(ROOT_REF, 0);
}
@Test
public void file_has_new_debt_ratio_if_some_scm_dates_are_after_snapshot_dates() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(50, 12, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50, 12));
+ setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 83.33, 0);
- assertNewDebtRatioValues(ROOT_REF, 83.33, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 83.33);
+ assertNewDebtRatioValues(ROOT_REF, 83.33);
}
@Test
public void new_debt_ratio_changes_with_language_cost() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST * 10);
- setupOneFileAloneInAProject(50, 12, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50, 12));
+ setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 8.33, 0);
- assertNewDebtRatioValues(ROOT_REF, 8.33, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 8.33);
+ assertNewDebtRatioValues(ROOT_REF, 8.33);
}
@Test
public void new_debt_ratio_changes_with_new_technical_debt() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(500, 120, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(500, 120));
+ setupOneFileAloneInAProject(500, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(500));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33, 0);
- assertNewDebtRatioValues(ROOT_REF, 833.33, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33);
+ assertNewDebtRatioValues(ROOT_REF, 833.33);
}
@Test
public void new_debt_ratio_on_non_file_level_is_based_on_new_technical_debt_of_that_level() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(500, 120, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(1200, 820));
+ setupOneFileAloneInAProject(500, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(1200));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33, 0);
- assertNewDebtRatioValues(ROOT_REF, 833.33, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33);
+ assertNewDebtRatioValues(ROOT_REF, 833.33);
}
@Test
public void new_debt_ratio_when_file_is_unit_test() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(500, 120, Flag.UT_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(1200, 820));
+ setupOneFileAloneInAProject(500, Flag.UT_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(1200));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33, 0);
- assertNewDebtRatioValues(ROOT_REF, 833.33, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33);
+ assertNewDebtRatioValues(ROOT_REF, 833.33);
}
@Test
public void new_debt_ratio_is_0_when_file_has_no_changesets() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(50, 12, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.NO_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50, 12));
+ setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.NO_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0, 0);
- assertNewDebtRatioValues(ROOT_REF, 0, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
+ assertNewDebtRatioValues(ROOT_REF, 0);
}
@Test
public void new_debt_ratio_is_0_on_non_file_level_when_no_file_has_changesets() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(50, 12, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.NO_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200, 162));
+ setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.NO_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0, 0);
- assertNewDebtRatioValues(ROOT_REF, 0, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
+ assertNewDebtRatioValues(ROOT_REF, 0);
}
@Test
public void new_debt_ratio_is_0_when_there_is_no_ncloc_in_file() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(50, 12, Flag.SRC_FILE, Flag.NO_NCLOC, Flag.WITH_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50, 12));
+ setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.NO_NCLOC, Flag.WITH_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0, 0);
- assertNewDebtRatioValues(ROOT_REF, 0, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
+ assertNewDebtRatioValues(ROOT_REF, 0);
}
@Test
public void new_debt_ratio_is_0_on_non_file_level_when_one_file_has_zero_new_debt_because_of_no_changeset() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(50, 12, Flag.SRC_FILE, Flag.NO_NCLOC, Flag.WITH_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200, 162));
+ setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.NO_NCLOC, Flag.WITH_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0, 0);
- assertNewDebtRatioValues(ROOT_REF, 0, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
+ assertNewDebtRatioValues(ROOT_REF, 0);
}
@Test
public void new_debt_ratio_is_0_when_ncloc_measure_is_missing() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(50, 12, Flag.SRC_FILE, Flag.MISSING_MEASURE_NCLOC, Flag.WITH_CHANGESET);
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50, 12));
+ setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.MISSING_MEASURE_NCLOC, Flag.WITH_CHANGESET);
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0, 0);
- assertNewDebtRatioValues(ROOT_REF, 0, 0);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
+ assertNewDebtRatioValues(ROOT_REF, 0);
}
@Test
public void leaf_components_always_have_a_measure_when_at_least_one_period_exist_and_ratio_is_computed_from_current_level_new_debt() {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
treeRootHolder.setRoot(
builder(PROJECT, ROOT_REF)
.build())
.build());
- Measure newDebtMeasure = createNewDebtMeasure(50, 12);
+ Measure newDebtMeasure = createNewDebtMeasure(50);
measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, newDebtMeasure);
- measureRepository.addRawMeasure(111, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(150, 112));
- measureRepository.addRawMeasure(11, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200, 112));
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(250, 212));
+ measureRepository.addRawMeasure(111, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(150));
+ measureRepository.addRawMeasure(11, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200));
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(250));
// 4 lines file, only first one is not ncloc
measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
// first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
- scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_2_SNAPSHOT_DATE - 100, 2, PERIOD_2_SNAPSHOT_DATE + 100, 2));
+ scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_SNAPSHOT_DATE - 100, 2, PERIOD_SNAPSHOT_DATE + 100, 2));
underTest.visit(treeRootHolder.getRoot());
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 83.33, 0);
- assertNewDebtRatioValues(111, 83.33, 0);
- assertNewDebtRatioValues(11, 83.33, 0);
- assertNewDebtRatioValues(ROOT_REF, 83.33, 0);
- }
-
- @Test
- public void new_debt_ratio_is_computed_for_five_periods() throws Exception {
- long period1 = 10000L;
- long period2 = 20000L;
- long period3 = 30000L;
- long period4 = 40000L;
- long period5 = 50000L;
-
- periodsHolder.setPeriods(
- new Period(1, SOME_PERIOD_MODE, null, period1, SOME_ANALYSIS_UUID),
- new Period(2, SOME_PERIOD_MODE, null, period2, SOME_ANALYSIS_UUID),
- new Period(3, SOME_PERIOD_MODE, null, period3, SOME_ANALYSIS_UUID),
- new Period(4, SOME_PERIOD_MODE, null, period4, SOME_ANALYSIS_UUID),
- new Period(5, SOME_PERIOD_MODE, null, period5, SOME_ANALYSIS_UUID));
-
- when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
-
- treeRootHolder.setRoot(
- builder(PROJECT, ROOT_REF)
- .addChildren(builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_1_KEY, 1)).build())
- .build());
-
- Measure newDebtMeasure = newMeasureBuilder().setVariations(new MeasureVariations(500d, 500d, 500d, 120d, 120d)).createNoValue();
- measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, newDebtMeasure);
- // 4 lines file, only first one is not ncloc
- measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
- // first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
- scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(period2 - 100, 2, period2 + 100, 2));
-
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY,
- newMeasureBuilder().setVariations(new MeasureVariations(1200d, 1200d, 1200d, 820d, 820d)).createNoValue());
-
- underTest.visit(treeRootHolder.getRoot());
-
- assertThat(measureRepository.getAddedRawMeasure(LANGUAGE_1_FILE_REF, NEW_SQALE_DEBT_RATIO_KEY))
- .hasVariation1(833.333, VARIATION_COMPARISON_OFFSET)
- .hasVariation2(833.333, VARIATION_COMPARISON_OFFSET)
- .hasVariation3(0d, VARIATION_COMPARISON_OFFSET)
- .hasVariation4(0d, VARIATION_COMPARISON_OFFSET)
- .hasVariation5(0d, VARIATION_COMPARISON_OFFSET);
+ assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 83.33);
+ assertNewDebtRatioValues(111, 83.33);
+ assertNewDebtRatioValues(11, 83.33);
+ assertNewDebtRatioValues(ROOT_REF, 83.33);
}
@Test
public void compute_new_maintainability_rating() throws Exception {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
treeRootHolder.setRoot(
builder(PROJECT, ROOT_REF)
.build())
.build());
- Measure newDebtMeasure = createNewDebtMeasure(50, 12);
+ Measure newDebtMeasure = createNewDebtMeasure(50);
measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, newDebtMeasure);
- measureRepository.addRawMeasure(111, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(150, 112));
- measureRepository.addRawMeasure(11, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200, 112));
- measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(250, 212));
+ measureRepository.addRawMeasure(111, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(150));
+ measureRepository.addRawMeasure(11, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200));
+ measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(250));
// 4 lines file, only first one is not ncloc
measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
// first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
- scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_2_SNAPSHOT_DATE - 100, 2, PERIOD_2_SNAPSHOT_DATE + 100, 2));
+ scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_SNAPSHOT_DATE - 100, 2, PERIOD_SNAPSHOT_DATE + 100, 2));
underTest.visit(treeRootHolder.getRoot());
- assertNewMaintainability(LANGUAGE_1_FILE_REF, D, A);
- assertNewMaintainability(111, D, A);
- assertNewMaintainability(11, D, A);
- assertNewMaintainability(ROOT_REF, D, A);
+ assertNewMaintainability(LANGUAGE_1_FILE_REF, D);
+ assertNewMaintainability(111, D);
+ assertNewMaintainability(11, D);
+ assertNewMaintainability(ROOT_REF, D);
}
@Test
public void compute_new_maintainability_rating_to_A_when_no_debt() throws Exception {
- setTwoPeriods();
+ setPeriod();
when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
treeRootHolder.setRoot(
builder(PROJECT, ROOT_REF)
underTest.visit(treeRootHolder.getRoot());
- assertNewMaintainability(LANGUAGE_1_FILE_REF, A, A);
- assertNewMaintainability(111, A, A);
- assertNewMaintainability(11, A, A);
- assertNewMaintainability(ROOT_REF, A, A);
+ assertNewMaintainability(LANGUAGE_1_FILE_REF, A);
+ assertNewMaintainability(111, A);
+ assertNewMaintainability(11, A);
+ assertNewMaintainability(ROOT_REF, A);
}
- private void setupOneFileAloneInAProject(int newDebtPeriod2, int newDebtPeriod4, Flag isUnitTest, Flag withNclocLines, Flag withChangeSets) {
+ private void setupOneFileAloneInAProject(int newDebt, Flag isUnitTest, Flag withNclocLines, Flag withChangeSets) {
checkArgument(isUnitTest == Flag.UT_FILE || isUnitTest == Flag.SRC_FILE);
checkArgument(withNclocLines == Flag.WITH_NCLOC || withNclocLines == Flag.NO_NCLOC || withNclocLines == Flag.MISSING_MEASURE_NCLOC);
checkArgument(withChangeSets == Flag.WITH_CHANGESET || withChangeSets == Flag.NO_CHANGESET);
builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(isUnitTest == Flag.UT_FILE, LANGUAGE_1_KEY, 1)).build())
.build());
- Measure newDebtMeasure = createNewDebtMeasure(newDebtPeriod2, newDebtPeriod4);
+ Measure newDebtMeasure = createNewDebtMeasure(newDebt);
measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, newDebtMeasure);
if (withNclocLines == Flag.WITH_NCLOC) {
// 4 lines file, only first one is not ncloc
}
if (withChangeSets == Flag.WITH_CHANGESET) {
// first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
- scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_2_SNAPSHOT_DATE - 100, 2, PERIOD_2_SNAPSHOT_DATE + 100, 2));
+ scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_SNAPSHOT_DATE - 100, 2, PERIOD_SNAPSHOT_DATE + 100, 2));
}
}
return ReportComponent.builder(type, ref).setKey(String.valueOf(ref));
}
- private Measure createNewDebtMeasure(double period2Value, double period4Value) {
- return newMeasureBuilder().setVariations(new MeasureVariations(null, period2Value, null, period4Value, null)).createNoValue();
+ private Measure createNewDebtMeasure(double variation) {
+ return newMeasureBuilder().setVariation(variation).createNoValue();
}
private static Measure createNclocDataMeasure(Integer... nclocLines) {
.isAbsent();
}
- private void assertNewDebtRatioValues(int componentRef, double expectedPeriod2Value, double expectedPeriod4Value) {
- assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_SQALE_DEBT_RATIO_KEY))
- .hasVariation2(expectedPeriod2Value, VARIATION_COMPARISON_OFFSET)
- .hasVariation4(expectedPeriod4Value, VARIATION_COMPARISON_OFFSET);
+ private void assertNewDebtRatioValues(int componentRef, double expectedVariation) {
+ assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_SQALE_DEBT_RATIO_KEY)).hasVariation(expectedVariation, VARIATION_COMPARISON_OFFSET);
}
- private void assertNewMaintainability(int componentRef, Rating expectedPeriod2Value, Rating expectedPeriod4Value) {
- assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_MAINTAINABILITY_RATING_KEY))
- .hasVariation2(expectedPeriod2Value.getIndex())
- .hasVariation4(expectedPeriod4Value.getIndex());
+ private void assertNewMaintainability(int componentRef, Rating expectedVariation) {
+ assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_MAINTAINABILITY_RATING_KEY)).hasVariation(expectedVariation.getIndex());
}
private void assertNoNewMaintainability(int componentRef) {
assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_MAINTAINABILITY_RATING_KEY))
- .isAbsent(); }
+ .isAbsent();
+ }
- private void setTwoPeriods() {
- periodsHolder.setPeriods(
- new Period(2, SOME_PERIOD_MODE, null, PERIOD_2_SNAPSHOT_DATE, SOME_ANALYSIS_UUID),
- new Period(4, SOME_PERIOD_MODE, null, PERIOD_5_SNAPSHOT_DATE, SOME_ANALYSIS_UUID));
+ private void setPeriod() {
+ periodsHolder.setPeriod(new Period(SOME_PERIOD_MODE, null, PERIOD_SNAPSHOT_DATE, SOME_ANALYSIS_UUID));
}
}
public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
@Rule
- public PeriodsHolderRule periodsHolder = new PeriodsHolderRule().setPeriods(new Period(1, "mode", null, LEAK_PERIOD_SNAPSHOT_IN_MILLISEC, "UUID"));
+ public PeriodsHolderRule periodsHolder = new PeriodsHolderRule().setPeriod(new Period("mode", null, LEAK_PERIOD_SNAPSHOT_IN_MILLISEC, "UUID"));
@Rule
public ComponentIssuesRepositoryRule componentIssuesRepositoryRule = new ComponentIssuesRepositoryRule(treeRootHolder);
@Rule
public FillComponentIssuesVisitorRule fillComponentIssuesVisitorRule = new FillComponentIssuesVisitorRule(componentIssuesRepositoryRule, treeRootHolder);
- VisitorsCrawler underTest = new VisitorsCrawler(Arrays.asList(fillComponentIssuesVisitorRule,
+ private VisitorsCrawler underTest = new VisitorsCrawler(Arrays.asList(fillComponentIssuesVisitorRule,
new NewReliabilityAndSecurityRatingMeasuresVisitor(metricRepository, measureRepository, componentIssuesRepositoryRule, periodsHolder)));
@Test
@Test
public void no_measure_if_there_is_no_period() {
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
treeRootHolder.setRoot(builder(PROJECT, 1).build());
underTest.visit(treeRootHolder.getRoot());
private void verifyAddedRawMeasureOnLeakPeriod(int componentRef, String metricKey, Rating rating) {
MeasureAssert.assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey))
- .hasVariation1(rating.getIndex());
+ .hasVariation(rating.getIndex());
}
private static DefaultIssue newBugIssue(long effort, String severity) {