From 6c30b79fcdb15e16c440976f47b4f5725f45a91f Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 17 Nov 2015 11:35:28 +0100 Subject: SONAR-6752 Deprecate org.sonar.batch.issue.IssueFilters extension point and add new one --- .../batch/issue/DefaultFilterableIssueTest.java | 86 +++++++++++++++++++ .../batch/issue/DefaultIssueFilterChainTest.java | 19 ++--- .../issue/DeprecatedIssueFilterChainTest.java | 96 ++++++++++++++++++++++ .../org/sonar/batch/issue/IssueFiltersTest.java | 57 ------------- .../issue/ignore/EnforceIssuesFilterTest.java | 10 +-- .../batch/issue/ignore/IgnoreIssuesFilterTest.java | 10 +-- .../issue/ignore/pattern/IssuePatternTest.java | 11 +-- .../issue/ignore/pattern/PatternMatcherTest.java | 7 +- .../ignore/scanner/IssueExclusionsLoaderTest.java | 3 +- 9 files changed, 212 insertions(+), 87 deletions(-) create mode 100644 sonar-batch/src/test/java/org/sonar/batch/issue/DefaultFilterableIssueTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedIssueFilterChainTest.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/issue/IssueFiltersTest.java (limited to 'sonar-batch/src/test') diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultFilterableIssueTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultFilterableIssueTest.java new file mode 100644 index 00000000000..480c09aa276 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultFilterableIssueTest.java @@ -0,0 +1,86 @@ +/* + * 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.issue; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; +import org.sonar.batch.protocol.Constants.Severity; + +import java.util.Date; + +import static org.mockito.Mockito.mock; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.resources.Project; +import org.sonar.batch.protocol.output.BatchReport.Issue; + +public class DefaultFilterableIssueTest { + private DefaultFilterableIssue issue; + private Project mockedProject; + private String componentKey; + private Issue rawIssue; + + @Before + public void setUp() { + mockedProject = mock(Project.class); + componentKey = "component"; + } + + private Issue createIssue() { + Issue.Builder builder = Issue.newBuilder(); + + builder.setEffortToFix(3.0); + builder.setLine(30); + builder.setSeverity(Severity.MAJOR); + return builder.build(); + } + + private Issue createIssueWithoutFields() { + Issue.Builder builder = Issue.newBuilder(); + builder.setSeverity(Severity.MAJOR); + return builder.build(); + } + + @Test + public void testRoundTrip() { + rawIssue = createIssue(); + issue = new DefaultFilterableIssue(mockedProject, rawIssue, componentKey); + + when(mockedProject.getAnalysisDate()).thenReturn(new Date(10_000)); + when(mockedProject.getEffectiveKey()).thenReturn("projectKey"); + + assertThat(issue.componentKey()).isEqualTo(componentKey); + assertThat(issue.creationDate()).isEqualTo(new Date(10_000)); + assertThat(issue.line()).isEqualTo(30); + assertThat(issue.projectKey()).isEqualTo("projectKey"); + assertThat(issue.effortToFix()).isEqualTo(3.0); + assertThat(issue.severity()).isEqualTo("MAJOR"); + } + + @Test + public void nullValues() { + rawIssue = createIssueWithoutFields(); + issue = new DefaultFilterableIssue(mockedProject, rawIssue, componentKey); + + assertThat(issue.line()).isNull(); + assertThat(issue.effortToFix()).isNull(); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssueFilterChainTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssueFilterChainTest.java index 25dc340fe30..e0aa7d15e31 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssueFilterChainTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssueFilterChainTest.java @@ -19,18 +19,17 @@ */ package org.sonar.batch.issue; -import org.junit.Test; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.batch.IssueFilter; -import org.sonar.api.issue.batch.IssueFilterChain; +import org.sonar.api.scan.issue.filter.FilterableIssue; +import org.junit.Test; +import org.sonar.api.scan.issue.filter.IssueFilter; +import org.sonar.api.scan.issue.filter.IssueFilterChain; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; public class DefaultIssueFilterChainTest { - - private final Issue issue = mock(Issue.class); + private final FilterableIssue issue = mock(FilterableIssue.class); @Test public void should_accept_when_no_filter() { @@ -39,28 +38,28 @@ public class DefaultIssueFilterChainTest { class PassingFilter implements IssueFilter { @Override - public boolean accept(Issue issue, IssueFilterChain chain) { + public boolean accept(FilterableIssue issue, IssueFilterChain chain) { return chain.accept(issue); } } class AcceptingFilter implements IssueFilter { @Override - public boolean accept(Issue issue, IssueFilterChain chain) { + public boolean accept(FilterableIssue issue, IssueFilterChain chain) { return true; } } class RefusingFilter implements IssueFilter { @Override - public boolean accept(Issue issue, IssueFilterChain chain) { + public boolean accept(FilterableIssue issue, IssueFilterChain chain) { return false; } } class FailingFilter implements IssueFilter { @Override - public boolean accept(Issue issue, IssueFilterChain chain) { + public boolean accept(FilterableIssue issue, IssueFilterChain chain) { fail(); return false; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedIssueFilterChainTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedIssueFilterChainTest.java new file mode 100644 index 00000000000..400b306101d --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedIssueFilterChainTest.java @@ -0,0 +1,96 @@ +/* + * 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.issue; + +import org.junit.Test; +import org.sonar.api.issue.Issue; +import org.sonar.api.issue.batch.IssueFilter; +import org.sonar.api.issue.batch.IssueFilterChain; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +public class DeprecatedIssueFilterChainTest { + + private final Issue issue = mock(Issue.class); + + @Test + public void should_accept_when_no_filter() { + assertThat(new DeprecatedIssueFilterChain().accept(issue)).isTrue(); + } + + class PassingFilter implements IssueFilter { + @Override + public boolean accept(Issue issue, IssueFilterChain chain) { + return chain.accept(issue); + } + } + + class AcceptingFilter implements IssueFilter { + @Override + public boolean accept(Issue issue, IssueFilterChain chain) { + return true; + } + } + + class RefusingFilter implements IssueFilter { + @Override + public boolean accept(Issue issue, IssueFilterChain chain) { + return false; + } + } + + class FailingFilter implements IssueFilter { + @Override + public boolean accept(Issue issue, IssueFilterChain chain) { + fail(); + return false; + } + + } + + @Test + public void should_accept_if_all_filters_pass() { + assertThat(new DeprecatedIssueFilterChain( + new PassingFilter(), + new PassingFilter(), + new PassingFilter() + ).accept(issue)).isTrue(); + } + + @Test + public void should_accept_and_not_go_further_if_filter_accepts() { + assertThat(new DeprecatedIssueFilterChain( + new PassingFilter(), + new AcceptingFilter(), + new FailingFilter() + ).accept(issue)).isTrue(); + } + + @Test + public void should_refuse_and_not_go_further_if_filter_refuses() { + assertThat(new DeprecatedIssueFilterChain( + new PassingFilter(), + new RefusingFilter(), + new FailingFilter() + ).accept(issue)).isFalse(); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/IssueFiltersTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/IssueFiltersTest.java deleted file mode 100644 index e83ce83d157..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/IssueFiltersTest.java +++ /dev/null @@ -1,57 +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.issue; - -import org.junit.Test; -import org.sonar.api.issue.Issue; -import org.sonar.api.resources.Project; -import org.sonar.batch.protocol.output.BatchReport; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class IssueFiltersTest { - - @Test - public void accept_when_filter_chain_is_empty() { - org.sonar.api.issue.IssueFilter ok = mock(org.sonar.api.issue.IssueFilter.class); - when(ok.accept(any(Issue.class))).thenReturn(true); - - org.sonar.api.issue.IssueFilter ko = mock(org.sonar.api.issue.IssueFilter.class); - when(ko.accept(any(Issue.class))).thenReturn(false); - - IssueFilters filters = new IssueFilters(new Project("foo"), new org.sonar.api.issue.IssueFilter[] {ok, ko}); - assertThat(filters.accept("foo:src/Foo.java", BatchReport.Issue.newBuilder().build())).isFalse(); - - filters = new IssueFilters(new Project("foo"), new org.sonar.api.issue.IssueFilter[] {ok}); - assertThat(filters.accept("foo:src/Foo.java", BatchReport.Issue.newBuilder().build())).isTrue(); - - filters = new IssueFilters(new Project("foo"), new org.sonar.api.issue.IssueFilter[] {ko}); - assertThat(filters.accept("foo:src/Foo.java", BatchReport.Issue.newBuilder().build())).isFalse(); - } - - @Test - public void should_always_accept_if_no_filters() { - IssueFilters filters = new IssueFilters(new Project("foo")); - assertThat(filters.accept("foo:src/Foo.java", BatchReport.Issue.newBuilder().build())).isTrue(); - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/EnforceIssuesFilterTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/EnforceIssuesFilterTest.java index 5996ca78d36..fc1886957e8 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/EnforceIssuesFilterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/EnforceIssuesFilterTest.java @@ -20,16 +20,16 @@ package org.sonar.batch.issue.ignore; +import org.sonar.api.scan.issue.filter.FilterableIssue; + import com.google.common.collect.ImmutableList; import org.junit.Before; import org.junit.Test; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.batch.IssueFilterChain; +import org.sonar.api.scan.issue.filter.IssueFilterChain; import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.WildcardPattern; import org.sonar.batch.issue.ignore.pattern.IssueInclusionPatternInitializer; import org.sonar.batch.issue.ignore.pattern.IssuePattern; - import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -40,13 +40,13 @@ public class EnforceIssuesFilterTest { private IssueInclusionPatternInitializer exclusionPatternInitializer; private EnforceIssuesFilter ignoreFilter; - private Issue issue; + private FilterableIssue issue; private IssueFilterChain chain; @Before public void init() { exclusionPatternInitializer = mock(IssueInclusionPatternInitializer.class); - issue = mock(Issue.class); + issue = mock(FilterableIssue.class); chain = mock(IssueFilterChain.class); when(chain.accept(issue)).thenReturn(true); diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/IgnoreIssuesFilterTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/IgnoreIssuesFilterTest.java index 13b1ec042de..e7e06728d54 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/IgnoreIssuesFilterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/IgnoreIssuesFilterTest.java @@ -20,14 +20,14 @@ package org.sonar.batch.issue.ignore; +import org.sonar.api.scan.issue.filter.FilterableIssue; + import org.junit.Before; import org.junit.Test; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.batch.IssueFilterChain; +import org.sonar.api.scan.issue.filter.IssueFilterChain; import org.sonar.batch.issue.ignore.pattern.IssueExclusionPatternInitializer; import org.sonar.batch.issue.ignore.pattern.IssuePattern; import org.sonar.batch.issue.ignore.pattern.PatternMatcher; - import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -37,7 +37,7 @@ public class IgnoreIssuesFilterTest { private IssueExclusionPatternInitializer exclusionPatternInitializer; private PatternMatcher exclusionPatternMatcher; private IgnoreIssuesFilter ignoreFilter; - private Issue issue; + private FilterableIssue issue; private IssueFilterChain chain; @Before @@ -45,7 +45,7 @@ public class IgnoreIssuesFilterTest { exclusionPatternMatcher = mock(PatternMatcher.class); exclusionPatternInitializer = mock(IssueExclusionPatternInitializer.class); when(exclusionPatternInitializer.getPatternMatcher()).thenReturn(exclusionPatternMatcher); - issue = mock(Issue.class); + issue = mock(FilterableIssue.class); chain = mock(IssueFilterChain.class); when(chain.accept(issue)).thenReturn(true); diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/pattern/IssuePatternTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/pattern/IssuePatternTest.java index bf42b241300..dfbf6f2591b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/pattern/IssuePatternTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/pattern/IssuePatternTest.java @@ -20,11 +20,11 @@ package org.sonar.batch.issue.ignore.pattern; +import org.sonar.api.scan.issue.filter.FilterableIssue; + import org.junit.Test; -import org.sonar.api.issue.Issue; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.Rule; - import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -88,8 +88,8 @@ public class IssuePatternTest { assertThat(pattern.match(create((Rule) null, null, null))).isFalse(); } - private Issue create(Rule rule, String component, Integer line) { - Issue mockIssue = mock(Issue.class); + private FilterableIssue create(Rule rule, String component, Integer line) { + FilterableIssue mockIssue = mock(FilterableIssue.class); RuleKey ruleKey = null; if (rule != null) { ruleKey = rule.ruleKey(); @@ -109,6 +109,7 @@ public class IssuePatternTest { public void shouldPrintPatternToString() { IssuePattern pattern = new IssuePattern("*", "checkstyle:*"); - assertThat(pattern.toString()).isEqualTo("IssuePattern[resourcePattern=*,rulePattern=checkstyle:*,lines=[],lineRanges=[],beginBlockRegexp=,endBlockRegexp=,allFileRegexp=,checkLines=true]"); + assertThat(pattern.toString()).isEqualTo( + "IssuePattern[resourcePattern=*,rulePattern=checkstyle:*,lines=[],lineRanges=[],beginBlockRegexp=,endBlockRegexp=,allFileRegexp=,checkLines=true]"); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/pattern/PatternMatcherTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/pattern/PatternMatcherTest.java index 3d0acd15b5b..fa5135d9c0b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/pattern/PatternMatcherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/pattern/PatternMatcherTest.java @@ -19,10 +19,11 @@ */ package org.sonar.batch.issue.ignore.pattern; +import org.sonar.api.scan.issue.filter.FilterableIssue; + import com.google.common.collect.Sets; import org.junit.Before; import org.junit.Test; -import org.sonar.api.issue.Issue; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.Rule; @@ -99,8 +100,8 @@ public class PatternMatcherTest { assertThat(patternMatcher.getMatchingPattern(create(CHECKSTYLE_RULE, JAVA_FILE, 5))).isNull(); } - private Issue create(Rule rule, String component, Integer line) { - Issue mockIssue = mock(Issue.class); + private FilterableIssue create(Rule rule, String component, Integer line) { + FilterableIssue mockIssue = mock(FilterableIssue.class); RuleKey ruleKey = null; if (rule != null) { ruleKey = rule.ruleKey(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoaderTest.java index 4963cd13b30..b93a767f92d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoaderTest.java @@ -30,7 +30,6 @@ import org.mockito.MockitoAnnotations; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.utils.SonarException; import org.sonar.batch.issue.ignore.pattern.IssueExclusionPatternInitializer; import org.sonar.batch.issue.ignore.pattern.IssueInclusionPatternInitializer; import org.sonar.batch.issue.ignore.pattern.PatternMatcher; @@ -151,7 +150,7 @@ public class IssueExclusionsLoaderTest { when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true); doThrow(new IOException("BUG")).when(regexpScanner).scan("polop:src/Foo.php", phpFile1, UTF_8); - thrown.expect(SonarException.class); + thrown.expect(IllegalStateException.class); thrown.expectMessage("Unable to read the source file"); scanner.execute(); -- cgit v1.2.3