diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2018-11-29 17:41:01 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-01-16 09:43:04 +0100 |
commit | 39018d34ad793430e64f59f71c813d266357ce3b (patch) | |
tree | b4b78ed7cac4b6a8e93a84f44be711dd68bb9713 | |
parent | a00e567bffae6300381d11989bfda011d2f85aba (diff) | |
download | sonarqube-39018d34ad793430e64f59f71c813d266357ce3b.tar.gz sonarqube-39018d34ad793430e64f59f71c813d266357ce3b.zip |
SONAR-11465 Move IssueFilter at project level
11 files changed, 73 insertions, 55 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/issue/filter/IssueFilter.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/issue/filter/IssueFilter.java index 39719e77536..07e6a9ef096 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/issue/filter/IssueFilter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/issue/filter/IssueFilter.java @@ -22,11 +22,12 @@ package org.sonar.api.scan.issue.filter; import javax.annotation.concurrent.ThreadSafe; import org.sonar.api.ExtensionPoint; -import org.sonar.api.batch.ScannerSide; +import org.sonar.api.scanner.ScannerSide; import org.sonarsource.api.sonarlint.SonarLintSide; /** * @since 5.3 + * @since 7.6 moved to project container * @deprecated since 7.6 */ @ScannerSide diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java index 9f36a7aeec0..eaf4e0f1edb 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java @@ -20,14 +20,11 @@ package org.sonar.scanner.issue; import java.util.Date; - import javax.annotation.concurrent.ThreadSafe; - import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; +import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.fs.internal.DefaultTextPointer; import org.sonar.api.batch.fs.internal.DefaultTextRange; import org.sonar.api.rule.RuleKey; @@ -40,10 +37,10 @@ public class DefaultFilterableIssue implements FilterableIssue { private final Issue rawIssue; private final ProjectAnalysisInfo projectAnalysisInfo; private final String componentKey; - private AbstractProjectOrModule module; + private DefaultInputProject project; - public DefaultFilterableIssue(InputModule module, ProjectAnalysisInfo projectAnalysisInfo, Issue rawIssue, String componentKey) { - this.module = (AbstractProjectOrModule) module; + public DefaultFilterableIssue(DefaultInputProject project, ProjectAnalysisInfo projectAnalysisInfo, Issue rawIssue, String componentKey) { + this.project = project; this.projectAnalysisInfo = projectAnalysisInfo; this.rawIssue = rawIssue; this.componentKey = componentKey; @@ -98,7 +95,7 @@ public class DefaultFilterableIssue implements FilterableIssue { @Override public String projectKey() { - return module.key(); + return project.key(); } @Override diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ModuleIssueFilters.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java index 518f8620255..92a8f758249 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ModuleIssueFilters.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java @@ -19,7 +19,7 @@ */ package org.sonar.scanner.issue; -import org.sonar.api.batch.fs.InputModule; +import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.scan.issue.filter.FilterableIssue; import org.sonar.api.scan.issue.filter.IssueFilter; import org.sonar.api.scan.issue.filter.IssueFilterChain; @@ -30,23 +30,23 @@ import org.sonar.scanner.protocol.output.ScannerReport; * @deprecated since 7.6, {@link IssueFilter} is deprecated */ @Deprecated -public class ModuleIssueFilters { +public class IssueFilters { private final IssueFilterChain filterChain; - private final InputModule module; + private final DefaultInputProject project; private final ProjectAnalysisInfo projectAnalysisInfo; - public ModuleIssueFilters(InputModule module, ProjectAnalysisInfo projectAnalysisInfo, IssueFilter[] exclusionFilters) { - this.module = module; + public IssueFilters(DefaultInputProject project, ProjectAnalysisInfo projectAnalysisInfo, IssueFilter[] exclusionFilters) { + this.project = project; this.filterChain = new DefaultIssueFilterChain(exclusionFilters); this.projectAnalysisInfo = projectAnalysisInfo; } - public ModuleIssueFilters(InputModule module, ProjectAnalysisInfo projectAnalysisInfo) { - this(module, projectAnalysisInfo, new IssueFilter[0]); + public IssueFilters(DefaultInputProject project, ProjectAnalysisInfo projectAnalysisInfo) { + this(project, projectAnalysisInfo, new IssueFilter[0]); } public boolean accept(String componentKey, ScannerReport.Issue rawIssue) { - FilterableIssue fIssue = new DefaultFilterableIssue(module, projectAnalysisInfo, rawIssue, componentKey); + FilterableIssue fIssue = new DefaultFilterableIssue(project, projectAnalysisInfo, rawIssue, componentKey); return filterChain.accept(fIssue); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ModuleIssues.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java index 7cff04cee58..ca0a8cc0764 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ModuleIssues.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java @@ -43,13 +43,13 @@ import static com.google.common.base.Strings.nullToEmpty; * Initialize the issues raised during scan. */ @ThreadSafe -public class ModuleIssues { +public class IssuePublisher { private final ActiveRules activeRules; - private final ModuleIssueFilters filters; + private final IssueFilters filters; private final ReportPublisher reportPublisher; - public ModuleIssues(ActiveRules activeRules, ModuleIssueFilters filters, ReportPublisher reportPublisher) { + public IssuePublisher(ActiveRules activeRules, IssueFilters filters, ReportPublisher reportPublisher) { this.activeRules = activeRules; this.filters = filters; this.reportPublisher = reportPublisher; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java index 7ae65e8419c..2199bd3de38 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java @@ -29,9 +29,6 @@ import org.sonar.scanner.DefaultFileLinesContextFactory; import org.sonar.scanner.bootstrap.ExtensionInstaller; import org.sonar.scanner.bootstrap.SensorExtensionDictionnary; import org.sonar.scanner.deprecated.perspectives.ScannerPerspectives; -import org.sonar.scanner.issue.ModuleIssueFilters; -import org.sonar.scanner.issue.ModuleIssues; -import org.sonar.scanner.phases.ModuleCoverageExclusions; import org.sonar.scanner.phases.SensorsExecutor; import org.sonar.scanner.scan.filesystem.DefaultModuleFileSystem; import org.sonar.scanner.scan.filesystem.ModuleInputComponentStore; @@ -79,11 +76,6 @@ public class ModuleScanContainer extends ComponentContainer { DefaultSensorStorage.class, DefaultSensorContext.class, SensorExtensionDictionnary.class, - ModuleIssueFilters.class, - ModuleCoverageExclusions.class, - - // issues - ModuleIssues.class, // Perspectives ScannerPerspectives.class, diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index 318e6af23d0..34024ba5c50 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -52,6 +52,8 @@ import org.sonar.scanner.deprecated.test.TestPlanBuilder; import org.sonar.scanner.deprecated.test.TestableBuilder; import org.sonar.scanner.issue.DefaultProjectIssues; import org.sonar.scanner.issue.IssueCache; +import org.sonar.scanner.issue.IssueFilters; +import org.sonar.scanner.issue.IssuePublisher; import org.sonar.scanner.issue.ignore.EnforceIssuesFilter; import org.sonar.scanner.issue.ignore.IgnoreIssuesFilter; import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer; @@ -202,6 +204,9 @@ public class ProjectScanContainer extends ComponentContainer { DefaultProjectIssues.class, IssueTransition.class, NoSonarFilter.class, + IssueFilters.class, + IssuePublisher.class, + // metrics DefaultMetricFinder.class, diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java index bda3121b6ca..68716b84f37 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java @@ -58,7 +58,7 @@ import org.sonar.core.metric.ScannerMetrics; import org.sonar.duplications.block.Block; import org.sonar.duplications.internal.pmd.PmdBlockChunker; import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; -import org.sonar.scanner.issue.ModuleIssues; +import org.sonar.scanner.issue.IssuePublisher; import org.sonar.scanner.protocol.Constants; import org.sonar.scanner.protocol.output.FileStructure; import org.sonar.scanner.protocol.output.ScannerReport; @@ -207,7 +207,7 @@ public class DefaultSensorStorage implements SensorStorage { } private final MetricFinder metricFinder; - private final ModuleIssues moduleIssues; + private final IssuePublisher moduleIssues; private final ReportPublisher reportPublisher; private final MeasureCache measureCache; private final SonarCpdBlockIndex index; @@ -217,9 +217,9 @@ public class DefaultSensorStorage implements SensorStorage { private final BranchConfiguration branchConfiguration; private final Set<String> alreadyLogged = new HashSet<>(); - public DefaultSensorStorage(MetricFinder metricFinder, ModuleIssues moduleIssues, Configuration settings, - ReportPublisher reportPublisher, MeasureCache measureCache, SonarCpdBlockIndex index, - ContextPropertiesCache contextPropertiesCache, ScannerMetrics scannerMetrics, BranchConfiguration branchConfiguration) { + public DefaultSensorStorage(MetricFinder metricFinder, IssuePublisher moduleIssues, Configuration settings, + ReportPublisher reportPublisher, MeasureCache measureCache, SonarCpdBlockIndex index, + ContextPropertiesCache contextPropertiesCache, ScannerMetrics scannerMetrics, BranchConfiguration branchConfiguration) { this.metricFinder = metricFinder; this.moduleIssues = moduleIssues; this.settings = settings; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java index 75bfc9a6884..0983d76d2ae 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java @@ -19,24 +19,29 @@ */ package org.sonar.scanner; -import java.io.File; +import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.mockito.ArgumentCaptor; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.measure.MetricFinder; -import org.sonar.api.batch.sensor.internal.SensorContextTester; +import org.sonar.api.batch.sensor.internal.SensorStorage; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.measures.CoreMetrics; import org.sonar.scanner.scan.measure.MeasureCache; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.sonar.api.measures.CoreMetrics.EXECUTABLE_LINES_DATA_KEY; import static org.sonar.api.measures.CoreMetrics.NCLOC_DATA_KEY; @@ -55,12 +60,12 @@ public class DefaultFileLinesContextTest { private DefaultFileLinesContext fileLineMeasures; - private SensorContextTester sensorContextTester; private MeasureCache measureCache; + private SensorStorage sensorStorage; + private DefaultInputFile file; @Before public void setUp() throws Exception { - sensorContextTester = SensorContextTester.create(temp.newFolder()); MetricFinder metricFinder = mock(MetricFinder.class); org.sonar.api.batch.measure.Metric<String> hitsMetric = mock(org.sonar.api.batch.measure.Metric.class); when(hitsMetric.valueType()).thenReturn(String.class); @@ -77,7 +82,9 @@ public class DefaultFileLinesContextTest { when(metricFinder.<String>findByKey(CoreMetrics.NCLOC_DATA_KEY)).thenReturn(CoreMetrics.NCLOC_DATA); when(metricFinder.<String>findByKey(CoreMetrics.EXECUTABLE_LINES_DATA_KEY)).thenReturn(CoreMetrics.EXECUTABLE_LINES_DATA); measureCache = mock(MeasureCache.class); - fileLineMeasures = new DefaultFileLinesContext(sensorContextTester, new TestInputFileBuilder("foo", "src/foo.php").initMetadata("Foo\nbar\nbiz").build(), metricFinder, + sensorStorage = mock(SensorStorage.class); + file = new TestInputFileBuilder("foo", "src/foo.php").initMetadata("Foo\nbar\nbiz").build(); + fileLineMeasures = new DefaultFileLinesContext(sensorStorage, file, metricFinder, measureCache); } @@ -89,7 +96,13 @@ public class DefaultFileLinesContextTest { assertThat(fileLineMeasures.toString()).isEqualTo("DefaultFileLinesContext{map={hits={1=2, 3=0}}}"); - assertThat(sensorContextTester.measure("foo:src/foo.php", HITS_METRIC_KEY).value()).isEqualTo("1=2;3=0"); + ArgumentCaptor<DefaultMeasure> captor = ArgumentCaptor.forClass(DefaultMeasure.class); + verify(sensorStorage).store(captor.capture()); + + DefaultMeasure measure = captor.getValue(); + assertThat(measure.inputComponent()).isEqualTo(file); + assertThat(measure.metric().key()).isEqualTo(HITS_METRIC_KEY); + assertThat(measure.value()).isEqualTo("1=2;3=0"); } @Test @@ -112,8 +125,13 @@ public class DefaultFileLinesContextTest { fileLineMeasures.setIntValue(EXECUTABLE_LINES_DATA_KEY, 2, 1); fileLineMeasures.save(); - assertThat(sensorContextTester.measure("foo:src/foo.php", NCLOC_DATA_KEY).value()).isEqualTo("2=1"); - assertThat(sensorContextTester.measure("foo:src/foo.php", EXECUTABLE_LINES_DATA_KEY).value()).isEqualTo("2=1"); + ArgumentCaptor<DefaultMeasure> captor = ArgumentCaptor.forClass(DefaultMeasure.class); + verify(sensorStorage, times(2)).store(captor.capture()); + + List<DefaultMeasure> measures = captor.getAllValues(); + assertThat(measures).extracting(DefaultMeasure::inputComponent, m -> m.metric().key(), DefaultMeasure::value) + .containsExactlyInAnyOrder(tuple(file, NCLOC_DATA_KEY, "2=1"), + tuple(file, EXECUTABLE_LINES_DATA_KEY, "2=1")); } @Test @@ -127,9 +145,14 @@ public class DefaultFileLinesContextTest { fileLineMeasures.setIntValue(BRANCHES_METRIC_KEY, 3, 4); fileLineMeasures.save(); - assertThat(sensorContextTester.measure("foo:src/foo.php", HITS_METRIC_KEY).value()).isEqualTo("1=2;3=4"); - assertThat(sensorContextTester.measure("foo:src/foo.php", AUTHOR_METRIC_KEY).value()).isEqualTo("1=simon;3=evgeny"); - assertThat(sensorContextTester.measure("foo:src/foo.php", BRANCHES_METRIC_KEY).value()).isEqualTo("1=2;3=4"); + ArgumentCaptor<DefaultMeasure> captor = ArgumentCaptor.forClass(DefaultMeasure.class); + verify(sensorStorage, times(3)).store(captor.capture()); + + List<DefaultMeasure> measures = captor.getAllValues(); + assertThat(measures).extracting(DefaultMeasure::inputComponent, m -> m.metric().key(), DefaultMeasure::value) + .containsExactlyInAnyOrder(tuple(file, HITS_METRIC_KEY, "1=2;3=4"), + tuple(file, AUTHOR_METRIC_KEY, "1=simon;3=evgeny"), + tuple(file, BRANCHES_METRIC_KEY, "1=2;3=4")); } @Test(expected = UnsupportedOperationException.class) diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java index 452e7a86565..9b379f021a2 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java @@ -22,7 +22,7 @@ package org.sonar.scanner.issue; import java.util.Date; import org.junit.Before; import org.junit.Test; -import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.scanner.ProjectAnalysisInfo; import org.sonar.scanner.protocol.Constants.Severity; import org.sonar.scanner.protocol.output.ScannerReport.Issue; @@ -34,14 +34,14 @@ import static org.mockito.Mockito.when; public class DefaultFilterableIssueTest { private DefaultFilterableIssue issue; - private DefaultInputModule mockedProject; + private DefaultInputProject mockedProject; private ProjectAnalysisInfo projectAnalysisInfo; private String componentKey; private Issue rawIssue; @Before public void setUp() { - mockedProject = mock(DefaultInputModule.class); + mockedProject = mock(DefaultInputProject.class); projectAnalysisInfo = mock(ProjectAnalysisInfo.class); componentKey = "component"; } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ModuleIssuesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java index c82cd466756..2c472af2ea0 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ModuleIssuesTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java @@ -57,7 +57,7 @@ import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) -public class ModuleIssuesTest { +public class IssuePublisherTest { static final RuleKey SQUID_RULE_KEY = RuleKey.of("squid", "AvoidCycle"); static final String SQUID_RULE_NAME = "Avoid Cycle"; @@ -69,12 +69,12 @@ public class ModuleIssuesTest { public TemporaryFolder temp = new TemporaryFolder(); @Mock - ModuleIssueFilters filters; + IssueFilters filters; ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder(); RulesBuilder ruleBuilder = new RulesBuilder(); - ModuleIssues moduleIssues; + IssuePublisher moduleIssues; DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php").initMetadata("Foo\nBar\nBiz\n").build(); ReportPublisher reportPublisher = mock(ReportPublisher.class, RETURNS_DEEP_STUBS); @@ -249,10 +249,10 @@ public class ModuleIssuesTest { } /** - * Every rules and active rules has to be added in builders before creating ModuleIssues + * Every rules and active rules has to be added in builders before creating IssuePublisher */ private void initModuleIssues() { - moduleIssues = new ModuleIssues(activeRulesBuilder.build(), filters, reportPublisher); + moduleIssues = new IssuePublisher(activeRulesBuilder.build(), filters, reportPublisher); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java index 9da8aaa3535..96aef89bfc0 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java @@ -51,7 +51,7 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.api.utils.KeyValueFormat; import org.sonar.core.metric.ScannerMetrics; import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; -import org.sonar.scanner.issue.ModuleIssues; +import org.sonar.scanner.issue.IssuePublisher; import org.sonar.scanner.protocol.output.FileStructure; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.report.ReportPublisher; @@ -78,7 +78,7 @@ public class DefaultSensorStorageTest { private DefaultSensorStorage underTest; private MapSettings settings; - private ModuleIssues moduleIssues; + private IssuePublisher moduleIssues; private MeasureCache measureCache; private ScannerReportWriter reportWriter; private ContextPropertiesCache contextPropertiesCache = new ContextPropertiesCache(); @@ -93,7 +93,7 @@ public class DefaultSensorStorageTest { when(metricFinder.<Integer>findByKey(CoreMetrics.LINES_TO_COVER_KEY)).thenReturn(CoreMetrics.LINES_TO_COVER); settings = new MapSettings(); - moduleIssues = mock(ModuleIssues.class); + moduleIssues = mock(IssuePublisher.class); measureCache = mock(MeasureCache.class); ReportPublisher reportPublisher = mock(ReportPublisher.class); |