diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-06-24 09:11:30 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-02 16:07:04 +0200 |
commit | 534bda208505d82734a41f44791ccd50ea928432 (patch) | |
tree | 21841906bf21de140050dffdc7d070262208fc99 /sonar-batch | |
parent | 08cdf04215f4b0b3a4ccfe7902c36df4906688c3 (diff) | |
download | sonarqube-534bda208505d82734a41f44791ccd50ea928432.tar.gz sonarqube-534bda208505d82734a41f44791ccd50ea928432.zip |
SONAR-6623 distribution of issues by debt in Compute Engine
This allows to deprecate org.sonar.api.measures.RuleMeasure
Diffstat (limited to 'sonar-batch')
6 files changed, 367 insertions, 633 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java index 879ae9afb82..58a2e630d3c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java @@ -37,7 +37,6 @@ import org.sonar.batch.compute.OverallCoverageDecorator; import org.sonar.batch.compute.OverallLineCoverageDecorator; import org.sonar.batch.compute.UnitTestDecorator; import org.sonar.batch.cpd.CpdComponents; -import org.sonar.batch.debt.DebtDecorator; import org.sonar.batch.debt.IssueChangelogDebtCalculator; import org.sonar.batch.debt.NewDebtDecorator; import org.sonar.batch.issue.tracking.IssueTracking; @@ -86,7 +85,6 @@ public class BatchComponents { // Debt IssueChangelogDebtCalculator.class, - DebtDecorator.class, NewDebtDecorator.class, // to be moved to compute engine diff --git a/sonar-batch/src/main/java/org/sonar/batch/debt/DebtDecorator.java b/sonar-batch/src/main/java/org/sonar/batch/debt/DebtDecorator.java deleted file mode 100644 index b339ead086a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/debt/DebtDecorator.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.batch.debt; - -import com.google.common.annotations.VisibleForTesting; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Set; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.sonar.api.batch.Decorator; -import org.sonar.api.batch.DecoratorBarriers; -import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.batch.DependedUpon; -import org.sonar.api.batch.DependsUpon; -import org.sonar.api.batch.RequiresDB; -import org.sonar.api.batch.rule.Rule; -import org.sonar.api.batch.rule.Rules; -import org.sonar.api.component.ResourcePerspectives; -import org.sonar.api.issue.Issuable; -import org.sonar.api.issue.Issue; -import org.sonar.core.issue.DefaultIssue; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.MeasuresFilters; -import org.sonar.api.measures.Metric; -import org.sonar.api.measures.PersistenceMode; -import org.sonar.api.measures.RuleMeasure; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.ResourceUtils; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.technicaldebt.batch.Characteristic; -import org.sonar.api.technicaldebt.batch.TechnicalDebtModel; - -import static com.google.common.collect.Lists.newArrayList; -import static com.google.common.collect.Maps.newHashMap; - -/** - * Decorator that computes the technical debt metric - */ -@DependsUpon(DecoratorBarriers.ISSUES_TRACKED) -@RequiresDB -public final class DebtDecorator implements Decorator { - - private final ResourcePerspectives perspectives; - private final TechnicalDebtModel model; - private final Rules rules; - - /** - * ruleFinder is needed to load "old" rule in order to persist rule measure - */ - private final RuleFinder ruleFinder; - - public DebtDecorator(ResourcePerspectives perspectives, TechnicalDebtModel model, Rules rules, RuleFinder ruleFinder) { - this.perspectives = perspectives; - this.model = model; - this.rules = rules; - this.ruleFinder = ruleFinder; - } - - @Override - public boolean shouldExecuteOnProject(Project project) { - return true; - } - - @DependedUpon - public List<Metric> generatesMetrics() { - return Arrays.<Metric>asList(CoreMetrics.TECHNICAL_DEBT); - } - - @Override - public void decorate(Resource resource, DecoratorContext context) { - Issuable issuable = perspectives.as(Issuable.class, resource); - if (issuable != null && shouldSaveMeasure(context)) { - List<Issue> issues = newArrayList(issuable.issues()); - saveMeasures(context, issues); - } - } - - private void saveMeasures(DecoratorContext context, List<Issue> issues) { - Long total = 0L; - SumMap<RuleKey> ruleDebts = new SumMap<>(); - SumMap<Characteristic> characteristicDebts = new SumMap<>(); - - // Aggregate rules debt from current issues (and populate current characteristic debt) - for (Issue issue : issues) { - Long debt = ((DefaultIssue) issue).debtInMinutes(); - total += computeDebt(debt, issue.ruleKey(), ruleDebts, characteristicDebts); - } - - // Aggregate rules debt from children (and populate children characteristics debt) - for (Measure measure : context.getChildrenMeasures(MeasuresFilters.rules(CoreMetrics.TECHNICAL_DEBT))) { - Long debt = measure.getValue().longValue(); - RuleMeasure ruleMeasure = (RuleMeasure) measure; - total += computeDebt(debt, ruleMeasure.ruleKey(), ruleDebts, characteristicDebts); - } - - context.saveMeasure(CoreMetrics.TECHNICAL_DEBT, total.doubleValue()); - saveOnRule(context, ruleDebts); - for (Characteristic characteristic : model.characteristics()) { - Long debt = characteristicDebts.get(characteristic); - saveCharacteristicMeasure(context, characteristic, debt != null ? debt.doubleValue() : 0d, false); - } - } - - private Long computeDebt(@Nullable Long debt, RuleKey ruleKey, SumMap<RuleKey> ruleDebts, SumMap<Characteristic> characteristicDebts) { - if (debt != null) { - Rule rule = rules.find(ruleKey); - if (rule != null) { - String characteristicKey = rule.debtSubCharacteristic(); - if (characteristicKey != null) { - Characteristic characteristic = model.characteristicByKey(characteristicKey); - if (characteristic != null) { - ruleDebts.add(ruleKey, debt); - characteristicDebts.add(characteristic, debt); - propagateTechnicalDebtInParents(characteristic.parent(), debt, characteristicDebts); - return debt; - } - } - } - } - return 0L; - } - - private void propagateTechnicalDebtInParents(@Nullable Characteristic characteristic, long value, SumMap<Characteristic> characteristicDebts) { - if (characteristic != null) { - characteristicDebts.add(characteristic, value); - propagateTechnicalDebtInParents(characteristic.parent(), value, characteristicDebts); - } - } - - private void saveOnRule(DecoratorContext context, SumMap<RuleKey> ruleDebts) { - for (Map.Entry<RuleKey, Long> entry : ruleDebts.entrySet()) { - org.sonar.api.rules.Rule oldRule = ruleFinder.findByKey(entry.getKey()); - if (oldRule != null) { - saveRuleMeasure(context, oldRule, entry.getValue().doubleValue(), ResourceUtils.isEntity(context.getResource())); - } - } - } - - @VisibleForTesting - void saveCharacteristicMeasure(DecoratorContext context, Characteristic characteristic, Double value, boolean inMemory) { - // we need the value on projects (root or module) even if value==0 in order to display correctly the SQALE history chart (see SQALE-122) - // BUT we don't want to save zero-values for non top-characteristics (see SQALE-147) - if (value > 0.0 || (ResourceUtils.isProject(context.getResource()) && characteristic.isRoot())) { - Measure measure = new Measure(CoreMetrics.TECHNICAL_DEBT); - measure.setCharacteristic(characteristic); - saveMeasure(context, measure, value, inMemory); - } - } - - @VisibleForTesting - void saveRuleMeasure(DecoratorContext context, org.sonar.api.rules.Rule rule, Double value, boolean inMemory) { - // we need the value on projects (root or module) even if value==0 in order to display correctly the SQALE history chart (see SQALE-122) - // BUT we don't want to save zero-values for non top-characteristics (see SQALE-147) - if (value > 0.0) { - RuleMeasure measure = new RuleMeasure(CoreMetrics.TECHNICAL_DEBT, rule, null, null); - saveMeasure(context, measure, value, inMemory); - } - } - - private static void saveMeasure(DecoratorContext context, Measure measure, Double value, boolean inMemory) { - measure.setValue(value); - if (inMemory) { - measure.setPersistenceMode(PersistenceMode.MEMORY); - } - context.saveMeasure(measure); - } - - private static boolean shouldSaveMeasure(DecoratorContext context) { - return context.getMeasure(CoreMetrics.TECHNICAL_DEBT) == null; - } - - private static class SumMap<E> { - private Map<E, Long> sumByKeys; - - public SumMap() { - sumByKeys = newHashMap(); - } - - public void add(@Nullable E key, Long value) { - if (key != null) { - Long currentValue = sumByKeys.get(key); - sumByKeys.put(key, currentValue != null ? (currentValue + value) : value); - } - } - - @CheckForNull - public Long get(E key) { - return sumByKeys.get(key); - } - - public Set<Map.Entry<E, Long>> entrySet() { - return sumByKeys.entrySet(); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java index fe57b624502..9aad7276659 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java @@ -111,13 +111,6 @@ public class MeasuresPublisher implements ReportPublisherStep { if (description != null) { builder.setDescription(description); } - if (measure instanceof RuleMeasure) { - RuleMeasure ruleMeasure = (RuleMeasure) measure; - RuleKey ruleKey = ruleMeasure.ruleKey(); - if (ruleKey != null) { - builder.setRuleKey(ruleKey.toString()); - } - } Level alertStatus = measure.getAlertStatus(); if (alertStatus != null) { builder.setAlertStatus(alertStatus.toString()); diff --git a/sonar-batch/src/test/java/org/sonar/batch/debt/DebtDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/debt/DebtDecoratorTest.java index 94a58c79b0d..4b96aba9ac0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/debt/DebtDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/debt/DebtDecoratorTest.java @@ -1,353 +1,353 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.batch.debt; - -import com.google.common.collect.Lists; -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.hamcrest.Description; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentMatcher; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; -import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.batch.rule.Rules; -import org.sonar.api.batch.rule.internal.RulesBuilder; -import org.sonar.api.component.ResourcePerspectives; -import org.sonar.api.issue.Issuable; -import org.sonar.api.issue.Issue; -import org.sonar.core.issue.DefaultIssue; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.MeasuresFilter; -import org.sonar.api.measures.Metric; -import org.sonar.api.measures.RuleMeasure; -import org.sonar.api.resources.File; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Resource; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.technicaldebt.batch.Characteristic; -import org.sonar.api.technicaldebt.batch.TechnicalDebtModel; -import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; -import org.sonar.api.test.IsMeasure; -import org.sonar.api.utils.Duration; - -import java.util.Collections; - -import static com.google.common.collect.Lists.newArrayList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class DebtDecoratorTest { - - static final int HOURS_IN_DAY = 8; - - static final Long ONE_DAY_IN_MINUTES = 1L * HOURS_IN_DAY * 60; - - @Mock - DecoratorContext context; - - @Mock - Resource resource; - - @Mock - TechnicalDebtModel debtModel; - - @Mock - Issuable issuable; - - @Mock - ResourcePerspectives perspectives; - - @Mock - RuleFinder ruleFinder; - - RuleKey ruleKey1 = RuleKey.of("repo1", "rule1"); - RuleKey ruleKey2 = RuleKey.of("repo2", "rule2"); - Rules rules; - - DefaultCharacteristic efficiency = new DefaultCharacteristic().setKey("EFFICIENCY"); - DefaultCharacteristic memoryEfficiency = new DefaultCharacteristic().setKey("MEMORY_EFFICIENCY").setParent(efficiency); - - DefaultCharacteristic reusability = new DefaultCharacteristic().setKey("REUSABILITY"); - DefaultCharacteristic modularity = new DefaultCharacteristic().setKey("MODULARITY").setParent(reusability); - - DebtDecorator decorator; - - @Before - public void before() { - when(perspectives.as(Issuable.class, resource)).thenReturn(issuable); - RulesBuilder rulesBuilder = new RulesBuilder(); - rulesBuilder.add(ruleKey1).setName("rule1").setDebtSubCharacteristic("MEMORY_EFFICIENCY"); - rulesBuilder.add(ruleKey2).setName("rule2").setDebtSubCharacteristic("MODULARITY"); - rules = rulesBuilder.build(); - - when(ruleFinder.findByKey(ruleKey1)).thenReturn(org.sonar.api.rules.Rule.create(ruleKey1.repository(), ruleKey1.rule())); - when(ruleFinder.findByKey(ruleKey2)).thenReturn(org.sonar.api.rules.Rule.create(ruleKey2.repository(), ruleKey2.rule())); - - when(debtModel.characteristics()).thenReturn(newArrayList(efficiency, memoryEfficiency, reusability, modularity)); - when(debtModel.characteristicByKey("EFFICIENCY")).thenReturn(efficiency); - when(debtModel.characteristicByKey("MEMORY_EFFICIENCY")).thenReturn(memoryEfficiency); - when(debtModel.characteristicByKey("REUSABILITY")).thenReturn(reusability); - when(debtModel.characteristicByKey("MODULARITY")).thenReturn(modularity); - - decorator = new DebtDecorator(perspectives, debtModel, rules, ruleFinder); - } - - @Test - public void generates_metrics() { - assertThat(decorator.generatesMetrics()).hasSize(1); - } - - @Test - public void execute_on_project() { - assertThat(decorator.shouldExecuteOnProject(null)).isTrue(); - } - - @Test - public void not_save_if_measure_already_computed() { - when(context.getMeasure(CoreMetrics.TECHNICAL_DEBT)).thenReturn(new Measure()); - - decorator.decorate(resource, context); - - verify(context, never()).saveMeasure(argThat(new IsMeasure(CoreMetrics.TECHNICAL_DEBT))); - } - - @Test - public void add_technical_debt_from_one_issue_and_no_parent() { - Issue issue = createIssue("rule1", "repo1").setDebt(Duration.create(ONE_DAY_IN_MINUTES)); - when(issuable.issues()).thenReturn(newArrayList(issue)); - - decorator.decorate(resource, context); - - verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, ONE_DAY_IN_MINUTES.doubleValue()); - verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, ONE_DAY_IN_MINUTES.doubleValue()))); - } - - @Test - public void add_technical_debt_from_one_issue_without_debt() { - Issue issue = createIssue("rule1", "repo1").setDebt(null); - when(issuable.issues()).thenReturn(newArrayList(issue)); - - decorator.decorate(resource, context); - - verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, 0.0); - } - - @Test - public void add_technical_debt_from_one_issue_and_propagate_to_parents() { - Issue issue = createIssue("rule1", "repo1").setDebt(Duration.create(ONE_DAY_IN_MINUTES)); - when(issuable.issues()).thenReturn(newArrayList(issue)); - - decorator.decorate(resource, context); - - verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, ONE_DAY_IN_MINUTES.doubleValue()); - verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, ONE_DAY_IN_MINUTES.doubleValue()))); - verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, efficiency, ONE_DAY_IN_MINUTES.doubleValue()))); - verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, memoryEfficiency, ONE_DAY_IN_MINUTES.doubleValue()))); - } - - @Test - public void add_technical_debt_from_issues() { - Long technicalDebt1 = ONE_DAY_IN_MINUTES; - Long technicalDebt2 = 2 * ONE_DAY_IN_MINUTES; - - Issue issue1 = createIssue("rule1", "repo1").setDebt(Duration.create(technicalDebt1)); - Issue issue2 = createIssue("rule1", "repo1").setDebt(Duration.create(technicalDebt1)); - Issue issue3 = createIssue("rule2", "repo2").setDebt(Duration.create(technicalDebt2)); - Issue issue4 = createIssue("rule2", "repo2").setDebt(Duration.create(technicalDebt2)); - when(issuable.issues()).thenReturn(newArrayList(issue1, issue2, issue3, issue4)); - - decorator.decorate(resource, context); - - verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, 6d * ONE_DAY_IN_MINUTES); - verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, 2d * ONE_DAY_IN_MINUTES))); - verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey2, 4d * ONE_DAY_IN_MINUTES))); - } - - @Test - public void add_technical_debt_from_current_and_children_measures() { - Issue issue1 = createIssue("rule1", "repo1").setDebt(Duration.create(ONE_DAY_IN_MINUTES)); - Issue issue2 = createIssue("rule1", "repo1").setDebt(Duration.create(ONE_DAY_IN_MINUTES)); - when(issuable.issues()).thenReturn(newArrayList(issue1, issue2)); - - when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Lists.<Measure>newArrayList( - new RuleMeasure(CoreMetrics.TECHNICAL_DEBT, - org.sonar.api.rules.Rule.create(ruleKey1.repository(), ruleKey1.rule()), null, null) - .setValue(5d * ONE_DAY_IN_MINUTES) - )); - decorator.decorate(resource, context); - - verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, 7d * ONE_DAY_IN_MINUTES); - verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, 7d * ONE_DAY_IN_MINUTES))); - verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, memoryEfficiency, 7d * ONE_DAY_IN_MINUTES))); - verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, efficiency, 7d * ONE_DAY_IN_MINUTES))); - } - - @Test - public void add_technical_debt_only_from_children_measures() { - when(issuable.issues()).thenReturn(Collections.<Issue>emptyList()); - - when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Lists.<Measure>newArrayList( - new RuleMeasure(CoreMetrics.TECHNICAL_DEBT, - org.sonar.api.rules.Rule.create(ruleKey1.repository(), ruleKey1.rule()) - , null, null).setValue(5d * ONE_DAY_IN_MINUTES), - - new RuleMeasure(CoreMetrics.TECHNICAL_DEBT, - org.sonar.api.rules.Rule.create(ruleKey2.repository(), ruleKey2.rule()) - , null, null).setValue(10d * ONE_DAY_IN_MINUTES) - )); - decorator.decorate(resource, context); - - verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, 15d * ONE_DAY_IN_MINUTES); - verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, 5d * ONE_DAY_IN_MINUTES))); - verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey2, 10d * ONE_DAY_IN_MINUTES))); - verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, memoryEfficiency, 5d * ONE_DAY_IN_MINUTES))); - verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, efficiency, 5d * ONE_DAY_IN_MINUTES))); - verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, reusability, 10d * ONE_DAY_IN_MINUTES))); - verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, modularity, 10d * ONE_DAY_IN_MINUTES))); - } - - @Test - public void always_save_technical_debt_for_positive_values() { - // for a project - DecoratorContext context = mock(DecoratorContext.class); - when(context.getResource()).thenReturn(new Project("foo")); - decorator.saveCharacteristicMeasure(context, (Characteristic) null, 12.0, false); - verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT)); - - // or for a file - context = mock(DecoratorContext.class); - when(context.getResource()).thenReturn(File.create("foo")); - decorator.saveCharacteristicMeasure(context, (Characteristic) null, 12.0, false); - verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT)); - } - - @Test - public void always_save_technical_debt_for_project_if_top_characteristic() { - DecoratorContext context = mock(DecoratorContext.class); - when(context.getResource()).thenReturn(new Project("foo")); - - // this is a top characteristic - DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("root"); - - decorator.saveCharacteristicMeasure(context, rootCharacteristic, 0.0, true); - verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT).setCharacteristic(rootCharacteristic)); - } - - /** - * SQALE-147 - */ - @Test - public void never_save_technical_debt_for_project_if_not_top_characteristic() { - DecoratorContext context = mock(DecoratorContext.class); - when(context.getResource()).thenReturn(new Project("foo")); - - DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("EFFICIENCY"); - DefaultCharacteristic characteristic = new DefaultCharacteristic().setKey("MEMORY_EFFICIENCY").setParent(rootCharacteristic); - - decorator.saveCharacteristicMeasure(context, characteristic, 0.0, true); - verify(context, never()).saveMeasure(any(Measure.class)); - } - - @Test - public void not_save_technical_debt_for_file_if_zero() { - DecoratorContext context = mock(DecoratorContext.class); - when(context.getResource()).thenReturn(File.create("foo")); - - decorator.saveCharacteristicMeasure(context, null, 0.0, true); - verify(context, never()).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT)); - } - - private DefaultIssue createIssue(String ruleKey, String repositoryKey) { - return new DefaultIssue().setRuleKey(RuleKey.of(repositoryKey, ruleKey)); - } - - class IsCharacteristicMeasure extends ArgumentMatcher<Measure> { - Metric metric = null; - Characteristic characteristic = null; - Double value = null; - - public IsCharacteristicMeasure(Metric metric, Characteristic characteristic, Double value) { - this.metric = metric; - this.characteristic = characteristic; - this.value = value; - } - - @Override - public boolean matches(Object o) { - if (!(o instanceof Measure)) { - return false; - } - Measure m = (Measure) o; - return ObjectUtils.equals(metric, m.getMetric()) && - ObjectUtils.equals(characteristic, m.getCharacteristic()) && - ObjectUtils.equals(value, m.getValue()); - } - - @Override - public void describeTo(Description description) { - description.appendText(new StringBuilder() - .append("value=").append(value).append(",") - .append("characteristic=").append(characteristic.key()).append(",") - .append("metric=").append(metric.getKey()).toString()); - } - } - - class IsRuleMeasure extends ArgumentMatcher<RuleMeasure> { - Metric metric = null; - RuleKey ruleKey = null; - Double value = null; - - public IsRuleMeasure(Metric metric, RuleKey ruleKey, Double value) { - this.metric = metric; - this.ruleKey = ruleKey; - this.value = value; - } - - @Override - public boolean matches(Object o) { - if (!(o instanceof RuleMeasure)) { - return false; - } - RuleMeasure m = (RuleMeasure) o; - return ObjectUtils.equals(metric, m.getMetric()) && - ObjectUtils.equals(ruleKey, m.ruleKey()) && - ObjectUtils.equals(value, m.getValue()); - } - - @Override - public void describeTo(Description description) { - description.appendText(ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE)); - } - } -} +///* +// * SonarQube, open source software quality management tool. +// * Copyright (C) 2008-2014 SonarSource +// * mailto:contact AT sonarsource DOT com +// * +// * SonarQube is free software; you can redistribute it and/or +// * modify it under the terms of the GNU Lesser General Public +// * License as published by the Free Software Foundation; either +// * version 3 of the License, or (at your option) any later version. +// * +// * SonarQube is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// * Lesser General Public License for more details. +// * +// * You should have received a copy of the GNU Lesser General Public License +// * along with this program; if not, write to the Free Software Foundation, +// * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// */ +// +//package org.sonar.batch.debt; +// +//import com.google.common.collect.Lists; +//import org.apache.commons.lang.ObjectUtils; +//import org.apache.commons.lang.builder.ToStringBuilder; +//import org.apache.commons.lang.builder.ToStringStyle; +//import org.hamcrest.Description; +//import org.junit.Before; +//import org.junit.Test; +//import org.junit.runner.RunWith; +//import org.mockito.ArgumentMatcher; +//import org.mockito.Mock; +//import org.mockito.runners.MockitoJUnitRunner; +//import org.sonar.api.batch.DecoratorContext; +//import org.sonar.api.batch.rule.Rules; +//import org.sonar.api.batch.rule.internal.RulesBuilder; +//import org.sonar.api.component.ResourcePerspectives; +//import org.sonar.api.issue.Issuable; +//import org.sonar.api.issue.Issue; +//import org.sonar.core.issue.DefaultIssue; +//import org.sonar.api.measures.CoreMetrics; +//import org.sonar.api.measures.Measure; +//import org.sonar.api.measures.MeasuresFilter; +//import org.sonar.api.measures.Metric; +//import org.sonar.api.measures.RuleMeasure; +//import org.sonar.api.resources.File; +//import org.sonar.api.resources.Project; +//import org.sonar.api.resources.Resource; +//import org.sonar.api.rule.RuleKey; +//import org.sonar.api.rules.RuleFinder; +//import org.sonar.api.technicaldebt.batch.Characteristic; +//import org.sonar.api.technicaldebt.batch.TechnicalDebtModel; +//import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; +//import org.sonar.api.test.IsMeasure; +//import org.sonar.api.utils.Duration; +// +//import java.util.Collections; +// +//import static com.google.common.collect.Lists.newArrayList; +//import static org.assertj.core.api.Assertions.assertThat; +//import static org.mockito.Matchers.any; +//import static org.mockito.Matchers.argThat; +//import static org.mockito.Mockito.mock; +//import static org.mockito.Mockito.never; +//import static org.mockito.Mockito.times; +//import static org.mockito.Mockito.verify; +//import static org.mockito.Mockito.when; +// +//@RunWith(MockitoJUnitRunner.class) +//public class DebtDecoratorTest { +// +// static final int HOURS_IN_DAY = 8; +// +// static final Long ONE_DAY_IN_MINUTES = 1L * HOURS_IN_DAY * 60; +// +// @Mock +// DecoratorContext context; +// +// @Mock +// Resource resource; +// +// @Mock +// TechnicalDebtModel debtModel; +// +// @Mock +// Issuable issuable; +// +// @Mock +// ResourcePerspectives perspectives; +// +// @Mock +// RuleFinder ruleFinder; +// +// RuleKey ruleKey1 = RuleKey.of("repo1", "rule1"); +// RuleKey ruleKey2 = RuleKey.of("repo2", "rule2"); +// Rules rules; +// +// DefaultCharacteristic efficiency = new DefaultCharacteristic().setKey("EFFICIENCY"); +// DefaultCharacteristic memoryEfficiency = new DefaultCharacteristic().setKey("MEMORY_EFFICIENCY").setParent(efficiency); +// +// DefaultCharacteristic reusability = new DefaultCharacteristic().setKey("REUSABILITY"); +// DefaultCharacteristic modularity = new DefaultCharacteristic().setKey("MODULARITY").setParent(reusability); +// +// DebtDecorator decorator; +// +// @Before +// public void before() { +// when(perspectives.as(Issuable.class, resource)).thenReturn(issuable); +// RulesBuilder rulesBuilder = new RulesBuilder(); +// rulesBuilder.add(ruleKey1).setName("rule1").setDebtSubCharacteristic("MEMORY_EFFICIENCY"); +// rulesBuilder.add(ruleKey2).setName("rule2").setDebtSubCharacteristic("MODULARITY"); +// rules = rulesBuilder.build(); +// +// when(ruleFinder.findByKey(ruleKey1)).thenReturn(org.sonar.api.rules.Rule.create(ruleKey1.repository(), ruleKey1.rule())); +// when(ruleFinder.findByKey(ruleKey2)).thenReturn(org.sonar.api.rules.Rule.create(ruleKey2.repository(), ruleKey2.rule())); +// +// when(debtModel.characteristics()).thenReturn(newArrayList(efficiency, memoryEfficiency, reusability, modularity)); +// when(debtModel.characteristicByKey("EFFICIENCY")).thenReturn(efficiency); +// when(debtModel.characteristicByKey("MEMORY_EFFICIENCY")).thenReturn(memoryEfficiency); +// when(debtModel.characteristicByKey("REUSABILITY")).thenReturn(reusability); +// when(debtModel.characteristicByKey("MODULARITY")).thenReturn(modularity); +// +// decorator = new DebtDecorator(perspectives, debtModel, rules, ruleFinder); +// } +// +// @Test +// public void generates_metrics() { +// assertThat(decorator.generatesMetrics()).hasSize(1); +// } +// +// @Test +// public void execute_on_project() { +// assertThat(decorator.shouldExecuteOnProject(null)).isTrue(); +// } +// +// @Test +// public void not_save_if_measure_already_computed() { +// when(context.getMeasure(CoreMetrics.TECHNICAL_DEBT)).thenReturn(new Measure()); +// +// decorator.decorate(resource, context); +// +// verify(context, never()).saveMeasure(argThat(new IsMeasure(CoreMetrics.TECHNICAL_DEBT))); +// } +// +// @Test +// public void add_technical_debt_from_one_issue_and_no_parent() { +// Issue issue = createIssue("rule1", "repo1").setDebt(Duration.create(ONE_DAY_IN_MINUTES)); +// when(issuable.issues()).thenReturn(newArrayList(issue)); +// +// decorator.decorate(resource, context); +// +// verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, ONE_DAY_IN_MINUTES.doubleValue()); +// verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, ONE_DAY_IN_MINUTES.doubleValue()))); +// } +// +// @Test +// public void add_technical_debt_from_one_issue_without_debt() { +// Issue issue = createIssue("rule1", "repo1").setDebt(null); +// when(issuable.issues()).thenReturn(newArrayList(issue)); +// +// decorator.decorate(resource, context); +// +// verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, 0.0); +// } +// +// @Test +// public void add_technical_debt_from_one_issue_and_propagate_to_parents() { +// Issue issue = createIssue("rule1", "repo1").setDebt(Duration.create(ONE_DAY_IN_MINUTES)); +// when(issuable.issues()).thenReturn(newArrayList(issue)); +// +// decorator.decorate(resource, context); +// +// verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, ONE_DAY_IN_MINUTES.doubleValue()); +// verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, ONE_DAY_IN_MINUTES.doubleValue()))); +// verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, efficiency, ONE_DAY_IN_MINUTES.doubleValue()))); +// verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, memoryEfficiency, ONE_DAY_IN_MINUTES.doubleValue()))); +// } +// +// @Test +// public void add_technical_debt_from_issues() { +// Long technicalDebt1 = ONE_DAY_IN_MINUTES; +// Long technicalDebt2 = 2 * ONE_DAY_IN_MINUTES; +// +// Issue issue1 = createIssue("rule1", "repo1").setDebt(Duration.create(technicalDebt1)); +// Issue issue2 = createIssue("rule1", "repo1").setDebt(Duration.create(technicalDebt1)); +// Issue issue3 = createIssue("rule2", "repo2").setDebt(Duration.create(technicalDebt2)); +// Issue issue4 = createIssue("rule2", "repo2").setDebt(Duration.create(technicalDebt2)); +// when(issuable.issues()).thenReturn(newArrayList(issue1, issue2, issue3, issue4)); +// +// decorator.decorate(resource, context); +// +// verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, 6d * ONE_DAY_IN_MINUTES); +// verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, 2d * ONE_DAY_IN_MINUTES))); +// verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey2, 4d * ONE_DAY_IN_MINUTES))); +// } +// +// @Test +// public void add_technical_debt_from_current_and_children_measures() { +// Issue issue1 = createIssue("rule1", "repo1").setDebt(Duration.create(ONE_DAY_IN_MINUTES)); +// Issue issue2 = createIssue("rule1", "repo1").setDebt(Duration.create(ONE_DAY_IN_MINUTES)); +// when(issuable.issues()).thenReturn(newArrayList(issue1, issue2)); +// +// when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Lists.<Measure>newArrayList( +// new RuleMeasure(CoreMetrics.TECHNICAL_DEBT, +// org.sonar.api.rules.Rule.create(ruleKey1.repository(), ruleKey1.rule()), null, null) +// .setValue(5d * ONE_DAY_IN_MINUTES) +// )); +// decorator.decorate(resource, context); +// +// verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, 7d * ONE_DAY_IN_MINUTES); +// verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, 7d * ONE_DAY_IN_MINUTES))); +// verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, memoryEfficiency, 7d * ONE_DAY_IN_MINUTES))); +// verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, efficiency, 7d * ONE_DAY_IN_MINUTES))); +// } +// +// @Test +// public void add_technical_debt_only_from_children_measures() { +// when(issuable.issues()).thenReturn(Collections.<Issue>emptyList()); +// +// when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Lists.<Measure>newArrayList( +// new RuleMeasure(CoreMetrics.TECHNICAL_DEBT, +// org.sonar.api.rules.Rule.create(ruleKey1.repository(), ruleKey1.rule()) +// , null, null).setValue(5d * ONE_DAY_IN_MINUTES), +// +// new RuleMeasure(CoreMetrics.TECHNICAL_DEBT, +// org.sonar.api.rules.Rule.create(ruleKey2.repository(), ruleKey2.rule()) +// , null, null).setValue(10d * ONE_DAY_IN_MINUTES) +// )); +// decorator.decorate(resource, context); +// +// verify(context).saveMeasure(CoreMetrics.TECHNICAL_DEBT, 15d * ONE_DAY_IN_MINUTES); +// verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey1, 5d * ONE_DAY_IN_MINUTES))); +// verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.TECHNICAL_DEBT, ruleKey2, 10d * ONE_DAY_IN_MINUTES))); +// verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, memoryEfficiency, 5d * ONE_DAY_IN_MINUTES))); +// verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, efficiency, 5d * ONE_DAY_IN_MINUTES))); +// verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, reusability, 10d * ONE_DAY_IN_MINUTES))); +// verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, modularity, 10d * ONE_DAY_IN_MINUTES))); +// } +// +// @Test +// public void always_save_technical_debt_for_positive_values() { +// // for a project +// DecoratorContext context = mock(DecoratorContext.class); +// when(context.getResource()).thenReturn(new Project("foo")); +// decorator.saveCharacteristicMeasure(context, (Characteristic) null, 12.0, false); +// verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT)); +// +// // or for a file +// context = mock(DecoratorContext.class); +// when(context.getResource()).thenReturn(File.create("foo")); +// decorator.saveCharacteristicMeasure(context, (Characteristic) null, 12.0, false); +// verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT)); +// } +// +// @Test +// public void always_save_technical_debt_for_project_if_top_characteristic() { +// DecoratorContext context = mock(DecoratorContext.class); +// when(context.getResource()).thenReturn(new Project("foo")); +// +// // this is a top characteristic +// DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("root"); +// +// decorator.saveCharacteristicMeasure(context, rootCharacteristic, 0.0, true); +// verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT).setCharacteristic(rootCharacteristic)); +// } +// +// /** +// * SQALE-147 +// */ +// @Test +// public void never_save_technical_debt_for_project_if_not_top_characteristic() { +// DecoratorContext context = mock(DecoratorContext.class); +// when(context.getResource()).thenReturn(new Project("foo")); +// +// DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("EFFICIENCY"); +// DefaultCharacteristic characteristic = new DefaultCharacteristic().setKey("MEMORY_EFFICIENCY").setParent(rootCharacteristic); +// +// decorator.saveCharacteristicMeasure(context, characteristic, 0.0, true); +// verify(context, never()).saveMeasure(any(Measure.class)); +// } +// +// @Test +// public void not_save_technical_debt_for_file_if_zero() { +// DecoratorContext context = mock(DecoratorContext.class); +// when(context.getResource()).thenReturn(File.create("foo")); +// +// decorator.saveCharacteristicMeasure(context, null, 0.0, true); +// verify(context, never()).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT)); +// } +// +// private DefaultIssue createIssue(String ruleKey, String repositoryKey) { +// return new DefaultIssue().setRuleKey(RuleKey.of(repositoryKey, ruleKey)); +// } +// +// class IsCharacteristicMeasure extends ArgumentMatcher<Measure> { +// Metric metric = null; +// Characteristic characteristic = null; +// Double value = null; +// +// public IsCharacteristicMeasure(Metric metric, Characteristic characteristic, Double value) { +// this.metric = metric; +// this.characteristic = characteristic; +// this.value = value; +// } +// +// @Override +// public boolean matches(Object o) { +// if (!(o instanceof Measure)) { +// return false; +// } +// Measure m = (Measure) o; +// return ObjectUtils.equals(metric, m.getMetric()) && +// ObjectUtils.equals(characteristic, m.getCharacteristic()) && +// ObjectUtils.equals(value, m.getValue()); +// } +// +// @Override +// public void describeTo(Description description) { +// description.appendText(new StringBuilder() +// .append("value=").append(value).append(",") +// .append("characteristic=").append(characteristic.key()).append(",") +// .append("metric=").append(metric.getKey()).toString()); +// } +// } +// +// class IsRuleMeasure extends ArgumentMatcher<RuleMeasure> { +// Metric metric = null; +// RuleKey ruleKey = null; +// Double value = null; +// +// public IsRuleMeasure(Metric metric, RuleKey ruleKey, Double value) { +// this.metric = metric; +// this.ruleKey = ruleKey; +// this.value = value; +// } +// +// @Override +// public boolean matches(Object o) { +// if (!(o instanceof RuleMeasure)) { +// return false; +// } +// RuleMeasure m = (RuleMeasure) o; +// return ObjectUtils.equals(metric, m.getMetric()) && +// ObjectUtils.equals(ruleKey, m.ruleKey()) && +// ObjectUtils.equals(value, m.getValue()); +// } +// +// @Override +// public void describeTo(Description description) { +// description.appendText(ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE)); +// } +// } +//} diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/MeasuresPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/MeasuresPublisherTest.java index 658cafcc62d..94204a3afbc 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/MeasuresPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/MeasuresPublisherTest.java @@ -19,6 +19,11 @@ */ package org.sonar.batch.report; +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -30,23 +35,14 @@ import org.sonar.api.measures.Metric; import org.sonar.api.measures.Metric.Level; import org.sonar.api.measures.Metric.ValueType; import org.sonar.api.measures.MetricFinder; -import org.sonar.api.measures.RuleMeasure; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rules.RulePriority; import org.sonar.api.technicaldebt.batch.Characteristic; import org.sonar.batch.index.BatchComponentCache; import org.sonar.batch.protocol.output.BatchReportReader; import org.sonar.batch.protocol.output.BatchReportWriter; import org.sonar.batch.scan.measure.MeasureCache; -import java.io.File; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; @@ -103,9 +99,6 @@ public class MeasuresPublisherTest { Measure manual = new Measure<>(new Metric<>("manual_metric", ValueType.BOOL)) .setValue(1.0) .setDescription("Manual"); - // Rule measure - RuleMeasure ruleMeasureBySeverity = RuleMeasure.createForPriority(CoreMetrics.NCLOC, RulePriority.BLOCKER, 1.0); - RuleMeasure ruleMeasureByRule = RuleMeasure.createForRule(CoreMetrics.NCLOC, RuleKey.of("squid", "S12345"), 1.0); // Sqale rating have both a value and a data Measure rating = new Measure<>(CoreMetrics.SQALE_RATING) .setValue(2.0) @@ -117,7 +110,7 @@ public class MeasuresPublisherTest { Measure stringMeasure = new Measure<>(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION) .setData("foo bar"); - when(measureCache.byResource(sampleFile)).thenReturn(Arrays.asList(measure1, measure2, manual, ruleMeasureBySeverity, ruleMeasureByRule, rating, longMeasure, stringMeasure)); + when(measureCache.byResource(sampleFile)).thenReturn(Arrays.asList(measure1, measure2, manual, rating, longMeasure, stringMeasure)); File outputDir = temp.newFolder(); BatchReportWriter writer = new BatchReportWriter(outputDir); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/measure/MeasureCacheTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/measure/MeasureCacheTest.java index 0575a850182..58dcf64ffdf 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/measure/MeasureCacheTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/measure/MeasureCacheTest.java @@ -19,7 +19,8 @@ */ package org.sonar.batch.scan.measure; -import org.sonar.batch.index.AbstractCachesTest; +import java.util.Date; +import java.util.Iterator; import org.apache.commons.lang.builder.EqualsBuilder; import org.junit.Before; import org.junit.Rule; @@ -29,22 +30,15 @@ import org.sonar.api.batch.measure.MetricFinder; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.measures.Metric.Level; -import org.sonar.api.measures.RuleMeasure; import org.sonar.api.resources.Directory; import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rules.RulePriority; import org.sonar.api.technicaldebt.batch.Characteristic; import org.sonar.api.technicaldebt.batch.Requirement; -import org.sonar.api.technicaldebt.batch.TechnicalDebtModel; -import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; +import org.sonar.batch.index.AbstractCachesTest; import org.sonar.batch.index.Cache.Entry; -import java.util.Date; -import java.util.Iterator; - import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -55,8 +49,6 @@ public class MeasureCacheTest extends AbstractCachesTest { private MetricFinder metricFinder; - private TechnicalDebtModel techDebtModel; - private MeasureCache measureCache; @Before @@ -64,8 +56,7 @@ public class MeasureCacheTest extends AbstractCachesTest { super.start(); metricFinder = mock(MetricFinder.class); when(metricFinder.findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC); - techDebtModel = mock(TechnicalDebtModel.class); - measureCache = new MeasureCache(caches, metricFinder, techDebtModel); + measureCache = new MeasureCache(caches, metricFinder); } @Test @@ -89,13 +80,6 @@ public class MeasureCacheTest extends AbstractCachesTest { assertThat(measureCache.byResource(p)).hasSize(1); assertThat(measureCache.byResource(p).iterator().next()).isEqualTo(m); - - Measure mRule = RuleMeasure.createForPriority(CoreMetrics.CRITICAL_VIOLATIONS, RulePriority.BLOCKER, 1.0); - measureCache.put(p, mRule); - - assertThat(measureCache.entries()).hasSize(2); - - assertThat(measureCache.byResource(p)).hasSize(2); } @Test @@ -112,7 +96,7 @@ public class MeasureCacheTest extends AbstractCachesTest { for (int i = 0; i < 1_048_575; i++) { data.append("a"); } - + m.setData(data.toString()); measureCache.put(p, m); @@ -127,12 +111,6 @@ public class MeasureCacheTest extends AbstractCachesTest { assertThat(measureCache.byResource(p)).hasSize(1); assertThat(measureCache.byResource(p).iterator().next()).isEqualTo(m); - - RuleMeasure mRule = RuleMeasure.createForPriority(CoreMetrics.CRITICAL_VIOLATIONS, RulePriority.BLOCKER, 1.0); - mRule.setRuleKey(RuleKey.of("repo", "rule")); - measureCache.put(p, mRule); - - assertThat(measureCache.entries()).hasSize(2); } /** @@ -166,11 +144,6 @@ public class MeasureCacheTest extends AbstractCachesTest { assertThat(measureCache.byResource(p)).hasSize(1); assertThat(measureCache.byResource(p).iterator().next()).isEqualTo(m); - RuleMeasure mRule = RuleMeasure.createForPriority(CoreMetrics.CRITICAL_VIOLATIONS, RulePriority.BLOCKER, 1.0); - mRule.setRuleKey(RuleKey.of("repo", "rule")); - measureCache.put(p, mRule); - - assertThat(measureCache.entries()).hasSize(2); } @Test @@ -203,17 +176,12 @@ public class MeasureCacheTest extends AbstractCachesTest { assertThat(measureCache.byResource(p)).hasSize(0); Measure m1 = new Measure(CoreMetrics.NCLOC, 1.0); - Measure m2 = new Measure(CoreMetrics.NCLOC, 1.0).setCharacteristic(new DefaultCharacteristic().setKey("charac")); - Measure m3 = new Measure(CoreMetrics.NCLOC, 1.0).setPersonId(2); - Measure m4 = new RuleMeasure(CoreMetrics.NCLOC, RuleKey.of("repo", "rule"), RulePriority.BLOCKER, null); + Measure m2 = new Measure(CoreMetrics.NCLOC, 1.0).setPersonId(2); measureCache.put(p, m1); measureCache.put(p, m2); - measureCache.put(p, m3); - measureCache.put(p, m4); - - assertThat(measureCache.entries()).hasSize(4); - assertThat(measureCache.byResource(p)).hasSize(4); + assertThat(measureCache.entries()).hasSize(2); + assertThat(measureCache.byResource(p)).hasSize(2); } @Test |