From c611a64bbba6f9a0cd656892f0643d24a97aa34a Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 30 May 2013 23:59:29 +0200 Subject: [PATCH] SONAR-3755 add Issuable#resolvedIssues() --- .../issue/CountFalsePositivesDecorator.java | 2 +- .../issue/CountUnresolvedIssuesDecorator.java | 2 +- .../CountFalsePositivesDecoratorTest.java | 8 +++---- .../CountUnresolvedIssuesDecoratorTest.java | 24 +++++++++---------- .../sonar/batch/issue/DefaultIssuable.java | 13 ++++++---- .../batch/issue/DefaultIssuableTest.java | 10 ++++---- .../java/org/sonar/api/issue/Issuable.java | 6 ++--- 7 files changed, 35 insertions(+), 30 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountFalsePositivesDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountFalsePositivesDecorator.java index 0d16aa179d5..3afac003d1f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountFalsePositivesDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountFalsePositivesDecorator.java @@ -56,7 +56,7 @@ public class CountFalsePositivesDecorator implements Decorator { Issuable issuable = perspectives.as(Issuable.class, resource); if (issuable != null) { int falsePositives = 0; - for (Issue issue : issuable.issues()) { + for (Issue issue : issuable.resolvedIssues()) { if (Issue.RESOLUTION_FALSE_POSITIVE.equals(issue.resolution())) { falsePositives++; } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java index f8495296c02..643bb8d9041 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java @@ -89,7 +89,7 @@ public class CountUnresolvedIssuesDecorator implements Decorator { public void decorate(Resource resource, DecoratorContext context) { Issuable issuable = perspectives.as(Issuable.class, resource); if (issuable != null) { - Collection issues = issuable.unresolvedIssues(); + Collection issues = issuable.issues(); boolean shouldSaveNewMetrics = shouldSaveNewMetrics(context); Multiset severityBag = HashMultiset.create(); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountFalsePositivesDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountFalsePositivesDecoratorTest.java index 714c8aced26..245987ae465 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountFalsePositivesDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountFalsePositivesDecoratorTest.java @@ -44,14 +44,14 @@ public class CountFalsePositivesDecoratorTest { @Test public void should_count_false_positives() { DefaultIssue falsePositive = new DefaultIssue().setRuleKey(RuleKey.parse("squid:AvoidCycles")) - .setResolution(Issue.RESOLUTION_FALSE_POSITIVE).setStatus(Issue.STATUS_OPEN); - DefaultIssue open = new DefaultIssue().setRuleKey(RuleKey.parse("squid:AvoidCycles")) - .setResolution(null).setStatus(Issue.STATUS_OPEN); + .setResolution(Issue.RESOLUTION_FALSE_POSITIVE).setStatus(Issue.STATUS_RESOLVED); + DefaultIssue fixed = new DefaultIssue().setRuleKey(RuleKey.parse("squid:AvoidCycles")) + .setResolution(Issue.RESOLUTION_FIXED).setStatus(Issue.STATUS_RESOLVED); File file = new File("foo.c"); Issuable issuable = mock(Issuable.class); when(perspectives.as(Issuable.class, file)).thenReturn(issuable); - when(issuable.issues()).thenReturn(Arrays.asList(falsePositive, open)); + when(issuable.resolvedIssues()).thenReturn(Arrays.asList(falsePositive, fixed)); DecoratorContext context = mock(DecoratorContext.class); decorator.decorate(file, context); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java index 6ea2df98b52..fe1f9a7c7ac 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java @@ -116,7 +116,7 @@ public class CountUnresolvedIssuesDecoratorTest { @Test public void should_count_issues() { when(resource.getScope()).thenReturn(Scopes.PROJECT); - when(issuable.unresolvedIssues()).thenReturn(createIssues()); + when(issuable.issues()).thenReturn(createissues()); when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.emptyList()); decorator.decorate(resource, context); @@ -141,7 +141,7 @@ public class CountUnresolvedIssuesDecoratorTest { @Test public void should_not_count_issues_if_measure_already_exists() { when(resource.getScope()).thenReturn(Scopes.PROJECT); - when(issuable.unresolvedIssues()).thenReturn(createIssues()); + when(issuable.issues()).thenReturn(createissues()); when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.emptyList()); when(context.getMeasure(CoreMetrics.VIOLATIONS)).thenReturn(new Measure(CoreMetrics.VIOLATIONS, 3000.0)); when(context.getMeasure(CoreMetrics.MAJOR_VIOLATIONS)).thenReturn(new Measure(CoreMetrics.MAJOR_VIOLATIONS, 500.0)); @@ -156,7 +156,7 @@ public class CountUnresolvedIssuesDecoratorTest { @Test public void should_save_zero_on_projects() { when(resource.getScope()).thenReturn(Scopes.PROJECT); - when(issuable.unresolvedIssues()).thenReturn(Lists.newArrayList()); + when(issuable.issues()).thenReturn(Lists.newArrayList()); when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.emptyList()); decorator.decorate(resource, context); @@ -167,7 +167,7 @@ public class CountUnresolvedIssuesDecoratorTest { @Test public void should_save_zero_on_directories() { when(resource.getScope()).thenReturn(Scopes.DIRECTORY); - when(issuable.unresolvedIssues()).thenReturn(Lists.newArrayList()); + when(issuable.issues()).thenReturn(Lists.newArrayList()); when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.emptyList()); decorator.decorate(resource, context); @@ -178,7 +178,7 @@ public class CountUnresolvedIssuesDecoratorTest { @Test public void should_count_issues_by_severity() { when(resource.getScope()).thenReturn(Scopes.PROJECT); - when(issuable.unresolvedIssues()).thenReturn(createIssues()); + when(issuable.issues()).thenReturn(createissues()); when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.emptyList()); decorator.decorate(resource, context); @@ -196,7 +196,7 @@ public class CountUnresolvedIssuesDecoratorTest { issues.add(new DefaultIssue().setRuleKey(ruleA1.ruleKey()).setSeverity(RulePriority.CRITICAL.name())); issues.add(new DefaultIssue().setRuleKey(ruleA1.ruleKey()).setSeverity(RulePriority.CRITICAL.name())); issues.add(new DefaultIssue().setRuleKey(ruleA2.ruleKey()).setSeverity(RulePriority.MAJOR.name())); - when(issuable.unresolvedIssues()).thenReturn(issues); + when(issuable.issues()).thenReturn(issues); decorator.decorate(resource, context); @@ -211,7 +211,7 @@ public class CountUnresolvedIssuesDecoratorTest { issues.add(new DefaultIssue().setRuleKey(ruleA1.ruleKey()).setSeverity(RulePriority.CRITICAL.name())); issues.add(new DefaultIssue().setRuleKey(ruleA1.ruleKey()).setSeverity(RulePriority.CRITICAL.name())); issues.add(new DefaultIssue().setRuleKey(ruleA1.ruleKey()).setSeverity(RulePriority.MINOR.name())); - when(issuable.unresolvedIssues()).thenReturn(issues); + when(issuable.issues()).thenReturn(issues); decorator.decorate(resource, context); @@ -232,7 +232,7 @@ public class CountUnresolvedIssuesDecoratorTest { public void should_clear_cache_after_execution() { Issue issue1 = new DefaultIssue().setRuleKey(RuleKey.of(ruleA1.getRepositoryKey(), ruleA1.getKey())).setSeverity(RulePriority.CRITICAL.name()).setCreationDate(rightNow); Issue issue2 = new DefaultIssue().setRuleKey(RuleKey.of(ruleA2.getRepositoryKey(), ruleA2.getKey())).setSeverity(RulePriority.CRITICAL.name()).setCreationDate(rightNow); - when(issuable.unresolvedIssues()).thenReturn(newArrayList(issue1)).thenReturn(newArrayList(issue2)); + when(issuable.issues()).thenReturn(newArrayList(issue1)).thenReturn(newArrayList(issue2)); decorator.decorate(resource, context); decorator.decorate(resource, context); @@ -243,7 +243,7 @@ public class CountUnresolvedIssuesDecoratorTest { @Test public void should_save_severity_new_issues() { - when(issuable.unresolvedIssues()).thenReturn(createIssuesForNewMetrics()); + when(issuable.issues()).thenReturn(createIssuesForNewMetrics()); decorator.decorate(resource, context); @@ -257,7 +257,7 @@ public class CountUnresolvedIssuesDecoratorTest { @Test public void should_save_rule_new_issues() { - when(issuable.unresolvedIssues()).thenReturn(createIssuesForNewMetrics()); + when(issuable.issues()).thenReturn(createIssuesForNewMetrics()); decorator.decorate(resource, context); @@ -270,7 +270,7 @@ public class CountUnresolvedIssuesDecoratorTest { @Test public void should_not_save_new_issues_if_measure_already_computed() { when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(new Measure()); - when(issuable.unresolvedIssues()).thenReturn(createIssuesForNewMetrics()); + when(issuable.issues()).thenReturn(createIssuesForNewMetrics()); decorator.decorate(resource, context); @@ -282,7 +282,7 @@ public class CountUnresolvedIssuesDecoratorTest { verify(context, never()).saveMeasure(argThat(new IsMetricMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS))); } - List createIssues() { + List createissues() { List issues = newArrayList(); issues.add(new DefaultIssue().setRuleKey(ruleA1.ruleKey()).setSeverity(Severity.CRITICAL).setStatus(Issue.STATUS_OPEN)); issues.add(new DefaultIssue().setRuleKey(ruleA1.ruleKey()).setSeverity(Severity.CRITICAL).setStatus(Issue.STATUS_REOPENED)); diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java index 475afd38f71..bcdd61f4847 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java @@ -55,14 +55,19 @@ public class DefaultIssuable implements Issuable { @SuppressWarnings("unchecked") @Override - public List issues() { - Iterable elements = cache.byComponent(component.key()); - return Lists.newArrayList(elements); + public List resolvedIssues() { + List result = Lists.newArrayList(); + for (DefaultIssue issue : cache.byComponent(component.key())) { + if (issue.resolution()!=null) { + result.add(issue); + } + } + return result; } @SuppressWarnings("unchecked") @Override - public List unresolvedIssues() { + public List issues() { List result = Lists.newArrayList(); for (DefaultIssue issue : cache.byComponent(component.key())) { if (issue.resolution()==null) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssuableTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssuableTest.java index 364aa4df207..15d362324e0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssuableTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssuableTest.java @@ -38,7 +38,7 @@ public class DefaultIssuableTest { Component component = mock(Component.class); @Test - public void test_unresolvedIssues() throws Exception { + public void test_unresolved_issues() throws Exception { when(component.key()).thenReturn("struts:org.apache.Action"); DefaultIssue resolved = new DefaultIssue().setResolution(Issue.RESOLUTION_FALSE_POSITIVE); DefaultIssue unresolved = new DefaultIssue(); @@ -46,12 +46,12 @@ public class DefaultIssuableTest { DefaultIssuable perspective = new DefaultIssuable(component, scanIssues, cache); - List issues = perspective.unresolvedIssues(); + List issues = perspective.issues(); assertThat(issues).containsOnly(unresolved); } @Test - public void test_issues() throws Exception { + public void test_resolved_issues() throws Exception { when(component.key()).thenReturn("struts:org.apache.Action"); DefaultIssue resolved = new DefaultIssue().setResolution(Issue.RESOLUTION_FALSE_POSITIVE); DefaultIssue unresolved = new DefaultIssue(); @@ -59,7 +59,7 @@ public class DefaultIssuableTest { DefaultIssuable perspective = new DefaultIssuable(component, scanIssues, cache); - List issues = perspective.issues(); - assertThat(issues).containsOnly(unresolved, resolved); + List issues = perspective.resolvedIssues(); + assertThat(issues).containsOnly(resolved); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/Issuable.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/Issuable.java index d5cc74389c9..7af6bef3bf8 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/Issuable.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/Issuable.java @@ -59,10 +59,10 @@ public interface Issuable extends Perspective { /** * The issues that are not resolved (=open). They include the manual issues reported by end-users. */ - List unresolvedIssues(); + List issues(); /** - * All issues, even the issues that are resolved or closed during this analysis. + * The issues marked as resolved during this scan. */ - List issues(); + List resolvedIssues(); } -- 2.39.5