diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2014-12-09 18:27:11 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2014-12-09 18:27:23 +0100 |
commit | 7af27c56b4dc3077c483a044df47af65b713fde1 (patch) | |
tree | 32eed3f2ecc41d3016ea7bc768d1f12a358b70de | |
parent | 150f04e1cceafb5cb577f170afdd3b7e636345f8 (diff) | |
download | sonarqube-7af27c56b4dc3077c483a044df47af65b713fde1.tar.gz sonarqube-7af27c56b4dc3077c483a044df47af65b713fde1.zip |
fix quality flaws and SRP
8 files changed, 133 insertions, 95 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentCleanerService.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentCleanerService.java index 7bf18722183..3626869b849 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentCleanerService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentCleanerService.java @@ -40,7 +40,8 @@ public class ComponentCleanerService implements ServerComponent { private final IssueIndexer issueIndexer; private final SourceLineIndexer sourceLineIndexer; - public ComponentCleanerService(DbClient dbClient, PurgeDao purgeDao, IssueAuthorizationIndexer issueAuthorizationIndexer, IssueIndexer issueIndexer, SourceLineIndexer sourceLineIndexer) { + public ComponentCleanerService(DbClient dbClient, PurgeDao purgeDao, IssueAuthorizationIndexer issueAuthorizationIndexer, IssueIndexer issueIndexer, + SourceLineIndexer sourceLineIndexer) { this.dbClient = dbClient; this.purgeDao = purgeDao; this.issueAuthorizationIndexer = issueAuthorizationIndexer; diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java index 1bb8802150e..9327464fd15 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java @@ -20,43 +20,22 @@ package org.sonar.server.computation; -import org.sonar.api.config.Settings; import org.sonar.core.component.ComponentDto; import org.sonar.core.computation.db.AnalysisReportDto; import org.sonar.core.persistence.DbSession; -import org.sonar.core.purge.PurgeConfiguration; -import org.sonar.server.computation.dbcleaner.ProjectPurgeTask; -import org.sonar.server.issue.index.IssueIndex; -import org.sonar.server.properties.ProjectSettingsFactory; -import org.sonar.server.source.index.SourceLineIndexer; - -import static org.sonar.core.purge.PurgeConfiguration.newDefaultPurgeConfiguration; +import org.sonar.core.purge.IdUuidPair; +import org.sonar.server.computation.dbcleaner.ProjectCleaner; public class DataCleanerStep implements ComputationStep { - private final ProjectPurgeTask purgeTask; - private final IssueIndex issueIndex; - private final SourceLineIndexer sourceLineIndexer; - private final ProjectSettingsFactory projectSettingsFactory; + private final ProjectCleaner projectCleaner; - public DataCleanerStep(ProjectSettingsFactory projectSettingsFactory, ProjectPurgeTask purgeTask, IssueIndex issueIndex, SourceLineIndexer sourceLineIndexer) { - this.projectSettingsFactory = projectSettingsFactory; - this.purgeTask = purgeTask; - this.issueIndex = issueIndex; - this.sourceLineIndexer = sourceLineIndexer; + public DataCleanerStep(ProjectCleaner projectCleaner) { + this.projectCleaner = projectCleaner; } @Override public void execute(DbSession session, AnalysisReportDto report, ComponentDto project) { - Long projectId = project.getId(); - - Settings settings = projectSettingsFactory.newProjectSettings(session, projectId); - PurgeConfiguration purgeConfiguration = newDefaultPurgeConfiguration(settings, projectId); - - purgeTask.purge(session, purgeConfiguration, settings); - - if (purgeConfiguration.maxLiveDateOfClosedIssues() != null) { - issueIndex.deleteClosedIssuesOfProjectBefore(project.uuid(), purgeConfiguration.maxLiveDateOfClosedIssues()); - } + projectCleaner.purge(session, new IdUuidPair(project.getId(), project.uuid())); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/ProjectPurgeTask.java b/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/ProjectCleaner.java index d909c5548a5..60e7119144b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/ProjectPurgeTask.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/ProjectCleaner.java @@ -27,40 +27,65 @@ import org.sonar.api.ServerComponent; import org.sonar.api.config.Settings; import org.sonar.api.utils.TimeUtils; import org.sonar.core.persistence.DbSession; -import org.sonar.core.purge.PurgeConfiguration; -import org.sonar.core.purge.PurgeDao; -import org.sonar.core.purge.PurgeListener; -import org.sonar.core.purge.PurgeProfiler; +import org.sonar.core.purge.*; import org.sonar.server.computation.dbcleaner.period.DefaultPeriodCleaner; +import org.sonar.server.issue.index.IssueIndex; +import org.sonar.server.properties.ProjectSettingsFactory; +import org.sonar.server.search.IndexClient; -import java.util.List; +import java.util.Date; + +import static org.sonar.core.purge.PurgeConfiguration.newDefaultPurgeConfiguration; + +public class ProjectCleaner implements ServerComponent { + private static final Logger LOG = LoggerFactory.getLogger(ProjectCleaner.class); -public class ProjectPurgeTask implements ServerComponent { - private static final Logger LOG = LoggerFactory.getLogger(ProjectPurgeTask.class); private final PurgeProfiler profiler; private final PurgeListener purgeListener; private final PurgeDao purgeDao; private final DefaultPeriodCleaner periodCleaner; + private final ProjectSettingsFactory projectSettingsFactory; + private final IndexClient indexClient; - public ProjectPurgeTask(PurgeDao purgeDao, DefaultPeriodCleaner periodCleaner, PurgeProfiler profiler, PurgeListener purgeListener) { + public ProjectCleaner(PurgeDao purgeDao, DefaultPeriodCleaner periodCleaner, PurgeProfiler profiler, PurgeListener purgeListener, + ProjectSettingsFactory projectSettingsFactory, IndexClient indexClient) { this.purgeDao = purgeDao; this.periodCleaner = periodCleaner; this.profiler = profiler; this.purgeListener = purgeListener; + this.projectSettingsFactory = projectSettingsFactory; + this.indexClient = indexClient; } - public ProjectPurgeTask purge(DbSession session, PurgeConfiguration configuration, Settings settings) { + public ProjectCleaner purge(DbSession session, IdUuidPair idUuidPair) { long start = System.currentTimeMillis(); profiler.reset(); + + Settings settings = projectSettingsFactory.newProjectSettings(session, idUuidPair.getId()); + PurgeConfiguration configuration = newDefaultPurgeConfiguration(settings, idUuidPair.getId()); + cleanHistoricalData(session, configuration.rootProjectId(), settings); doPurge(session, configuration); + + deleteIndexedIssuesBefore(idUuidPair.getUuid(), configuration.maxLiveDateOfClosedIssues()); + + logProfiling(start, settings); + return this; + } + + private void deleteIndexedIssuesBefore(String uuid, Date lastDateWithClosedIssues) { + if (lastDateWithClosedIssues != null) { + indexClient.get(IssueIndex.class).deleteClosedIssuesOfProjectBefore(uuid, lastDateWithClosedIssues); + } + } + + private void logProfiling(long start, Settings settings) { if (settings.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY)) { long duration = System.currentTimeMillis() - start; LOG.info("\n -------- Profiling for purge: " + TimeUtils.formatDuration(duration) + " --------\n"); profiler.dump(duration, LOG); LOG.info("\n -------- End of profiling for purge --------\n"); } - return this; } private void cleanHistoricalData(DbSession session, long resourceId, Settings settings) { @@ -80,8 +105,4 @@ public class ProjectPurgeTask implements ServerComponent { LOG.error("Fail to purge data [id=" + configuration.rootProjectId() + "]", e); } } - - public List<String> findUuidsToDisable(DbSession session, Long projectId) { - return purgeDao.selectPurgeableFiles(session, projectId); - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/package-info.java new file mode 100644 index 00000000000..f3a8c77da99 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/package-info.java @@ -0,0 +1,24 @@ +/* + * 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. + */ + +@ParametersAreNonnullByDefault +package org.sonar.server.computation.dbcleaner; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index b1f54a0cced..a98e375fd0c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -89,7 +89,7 @@ import org.sonar.server.computation.*; import org.sonar.server.computation.db.AnalysisReportDao; import org.sonar.server.computation.dbcleaner.DefaultPurgeTask; import org.sonar.server.computation.dbcleaner.IndexPurgeListener; -import org.sonar.server.computation.dbcleaner.ProjectPurgeTask; +import org.sonar.server.computation.dbcleaner.ProjectCleaner; import org.sonar.server.computation.dbcleaner.period.DefaultPeriodCleaner; import org.sonar.server.computation.ws.*; import org.sonar.server.config.ws.PropertiesWs; @@ -619,7 +619,7 @@ class ServerComponents { pico.addSingleton(AnalysisReportHistorySearchAction.class); pico.addSingleton(DefaultPeriodCleaner.class); pico.addSingleton(DefaultPurgeTask.class); - pico.addSingleton(ProjectPurgeTask.class); + pico.addSingleton(ProjectCleaner.class); pico.addSingleton(ProjectSettingsFactory.class); pico.addSingleton(IndexPurgeListener.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepMediumTest.java index 512285c8911..15e560d0c25 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepMediumTest.java @@ -30,14 +30,13 @@ import org.sonar.core.component.SnapshotDto; import org.sonar.core.computation.db.AnalysisReportDto; import org.sonar.core.computation.db.AnalysisReportDto.Status; import org.sonar.server.computation.dbcleaner.DbCleanerConstants; -import org.sonar.server.computation.dbcleaner.ProjectPurgeTask; +import org.sonar.server.computation.dbcleaner.ProjectCleaner; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import org.sonar.core.properties.PropertyDto; import org.sonar.server.component.ComponentTesting; import org.sonar.server.component.SnapshotTesting; import org.sonar.server.db.DbClient; -import org.sonar.server.issue.index.IssueIndex; import org.sonar.server.properties.ProjectSettingsFactory; import org.sonar.server.search.IndexClient; import org.sonar.server.source.index.SourceLineIndexer; @@ -58,7 +57,7 @@ public class DataCleanerStepMediumTest { private IndexClient indexClient; private SourceLineIndexer sourceLineIndexer; private ProjectSettingsFactory projectSettingsFactory; - private ProjectPurgeTask purgeTask; + private ProjectCleaner purgeTask; @Before public void before() throws Exception { @@ -67,10 +66,10 @@ public class DataCleanerStepMediumTest { this.indexClient = tester.get(IndexClient.class); this.projectSettingsFactory = tester.get(ProjectSettingsFactory.class); - this.purgeTask = tester.get(ProjectPurgeTask.class); + this.purgeTask = tester.get(ProjectCleaner.class); this.sourceLineIndexer = tester.get(SourceLineIndexer.class); - this.sut = new DataCleanerStep(projectSettingsFactory, purgeTask, indexClient.get(IssueIndex.class), sourceLineIndexer); + this.sut = new DataCleanerStep(purgeTask); } @After diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java index 800b1f7ff47..e170e6f21db 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java @@ -22,55 +22,37 @@ package org.sonar.server.computation; import org.junit.Before; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.component.ComponentDto; import org.sonar.core.computation.db.AnalysisReportDto; -import org.sonar.server.computation.dbcleaner.ProjectPurgeTask; import org.sonar.core.persistence.DbSession; -import org.sonar.core.purge.PurgeConfiguration; -import org.sonar.server.issue.index.IssueIndex; -import org.sonar.server.properties.ProjectSettings; -import org.sonar.server.properties.ProjectSettingsFactory; -import org.sonar.server.source.index.SourceLineIndexer; - -import java.util.Date; +import org.sonar.core.purge.IdUuidPair; +import org.sonar.server.computation.dbcleaner.ProjectCleaner; import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyLong; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class DataCleanerStepTest { private DataCleanerStep sut; - private ProjectPurgeTask purgeTask; - private IssueIndex issueIndex; - private SourceLineIndexer sourceLineIndexer; - private Settings settings; - private ProjectSettingsFactory projectSettingsFactory; + private ProjectCleaner projectCleaner; @Before public void before() { - this.purgeTask = mock(ProjectPurgeTask.class); - this.issueIndex = mock(IssueIndex.class); - this.sourceLineIndexer = mock(SourceLineIndexer.class); - this.settings = mock(ProjectSettings.class); - this.projectSettingsFactory = mock(ProjectSettingsFactory.class); - when(projectSettingsFactory.newProjectSettings(any(DbSession.class), anyLong())).thenReturn(settings); - when(settings.getInt(any(String.class))).thenReturn(123); + this.projectCleaner = mock(ProjectCleaner.class); - this.sut = new DataCleanerStep(projectSettingsFactory, purgeTask, issueIndex, sourceLineIndexer); + this.sut = new DataCleanerStep(projectCleaner); } @Test public void call_purge_method_of_the_purge_task() { - AnalysisReportDto report = mock(AnalysisReportDto.class); ComponentDto project = mock(ComponentDto.class); + when(project.getId()).thenReturn(123L); + when(project.uuid()).thenReturn("UUID-1234"); - sut.execute(mock(DbSession.class), report, project); + sut.execute(mock(DbSession.class), mock(AnalysisReportDto.class), project); - verify(projectSettingsFactory).newProjectSettings(any(DbSession.class), anyLong()); - verify(purgeTask).purge(any(DbSession.class), any(PurgeConfiguration.class), any(Settings.class)); - verify(issueIndex).deleteClosedIssuesOfProjectBefore(anyString(), any(Date.class)); + verify(projectCleaner).purge(any(DbSession.class), any(IdUuidPair.class)); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/ProjectPurgeTaskTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/ProjectCleanerTest.java index 25c4c7cca8f..31f8e024add 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/ProjectPurgeTaskTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/ProjectCleanerTest.java @@ -26,23 +26,29 @@ import org.slf4j.Logger; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; import org.sonar.core.persistence.DbSession; -import org.sonar.core.purge.PurgeConfiguration; -import org.sonar.core.purge.PurgeDao; -import org.sonar.core.purge.PurgeListener; -import org.sonar.core.purge.PurgeProfiler; +import org.sonar.core.purge.*; import org.sonar.server.computation.dbcleaner.period.DefaultPeriodCleaner; +import org.sonar.server.issue.index.IssueIndex; +import org.sonar.server.properties.ProjectSettingsFactory; +import org.sonar.server.search.IndexClient; + +import java.util.Date; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyLong; import static org.mockito.Mockito.*; -public class ProjectPurgeTaskTest { +public class ProjectCleanerTest { - private ProjectPurgeTask sut; + private ProjectCleaner sut; private PurgeDao dao; private PurgeProfiler profiler; private DefaultPeriodCleaner periodCleaner; private PurgeListener purgeListener; + private ProjectSettingsFactory projectSettingsFactory; + private IndexClient indexClient; + private IssueIndex issueIndex; + private Settings settings; @Before public void before() throws Exception { @@ -50,45 +56,71 @@ public class ProjectPurgeTaskTest { this.profiler = mock(PurgeProfiler.class); this.periodCleaner = mock(DefaultPeriodCleaner.class); this.purgeListener = mock(PurgeListener.class); + this.settings = mock(Settings.class); + this.projectSettingsFactory = mock(ProjectSettingsFactory.class); + when(projectSettingsFactory.newProjectSettings(any(DbSession.class), any(Long.class))).thenReturn(settings); + + this.issueIndex = mock(IssueIndex.class); + this.indexClient = mock(IndexClient.class); + when(indexClient.get(IssueIndex.class)).thenReturn(issueIndex); - this.sut = new ProjectPurgeTask(dao, periodCleaner, profiler, purgeListener); + this.sut = new ProjectCleaner(dao, periodCleaner, profiler, purgeListener, projectSettingsFactory, indexClient); } @Test public void no_profiling_when_property_is_false() throws Exception { - Settings settings = mock(Settings.class); when(settings.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY)).thenReturn(false); + when(projectSettingsFactory.newProjectSettings(any(DbSession.class), any(Long.class))).thenReturn(settings); - sut.purge(mock(DbSession.class), mock(PurgeConfiguration.class), settings); + sut.purge(mock(DbSession.class), mock(IdUuidPair.class)); verify(profiler, never()).dump(anyLong(), any(Logger.class)); } @Test + public void no_indexing_when_no_issue_to_delete() throws Exception { + when(projectSettingsFactory.newProjectSettings(any(DbSession.class), any(Long.class))).thenReturn(settings); + + sut.purge(mock(DbSession.class), mock(IdUuidPair.class)); + + verify(indexClient, never()).get(IssueIndex.class); + } + + @Test public void profiling_when_property_is_true() throws Exception { - Settings settings = mock(Settings.class); when(settings.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY)).thenReturn(true); - sut.purge(mock(DbSession.class), mock(PurgeConfiguration.class), settings); + sut.purge(mock(DbSession.class), mock(IdUuidPair.class)); + + verify(profiler).dump(anyLong(), any(Logger.class)); + } + + @Test + public void call_period_cleaner_index_client_and_purge_dao() throws Exception { + when(settings.getInt(DbCleanerConstants.DAYS_BEFORE_DELETING_CLOSED_ISSUES)).thenReturn(5); + + sut.purge(mock(DbSession.class), mock(IdUuidPair.class)); - verify(profiler, times(1)).dump(anyLong(), any(Logger.class)); + verify(periodCleaner).clean(any(DbSession.class), any(Long.class), any(Settings.class)); + verify(dao).purge(any(DbSession.class), any(PurgeConfiguration.class), any(PurgeListener.class)); + verify(issueIndex).deleteClosedIssuesOfProjectBefore(any(String.class), any(Date.class)); } @Test public void if_dao_purge_fails_it_should_not_interrupt_program_execution() throws Exception { doThrow(RuntimeException.class).when(dao).purge(any(DbSession.class), any(PurgeConfiguration.class), any(PurgeListener.class)); - sut.purge(mock(DbSession.class), mock(PurgeConfiguration.class), mock(Settings.class)); + sut.purge(mock(DbSession.class), mock(IdUuidPair.class)); - verify(dao, times(1)).purge(any(DbSession.class), any(PurgeConfiguration.class), any(PurgeListener.class)); + verify(dao).purge(any(DbSession.class), any(PurgeConfiguration.class), any(PurgeListener.class)); } @Test public void if_profiler_cleaning_fails_it_should_not_interrupt_program_execution() throws Exception { doThrow(RuntimeException.class).when(periodCleaner).clean(any(DbSession.class), anyLong(), any(Settings.class)); - sut.purge(mock(DbSession.class), mock(PurgeConfiguration.class), mock(Settings.class)); + sut.purge(mock(DbSession.class), mock(IdUuidPair.class)); - verify(periodCleaner, times(1)).clean(any(DbSession.class), anyLong(), any(Settings.class)); + verify(periodCleaner).clean(any(DbSession.class), anyLong(), any(Settings.class)); } } |