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;
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
--- /dev/null
+/*
+ * 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.server.computation.dbcleaner;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
+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.*;
+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.sonar.core.purge.PurgeConfiguration.newDefaultPurgeConfiguration;
+
+public class ProjectCleaner implements ServerComponent {
+ private static final Logger LOG = LoggerFactory.getLogger(ProjectCleaner.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 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 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");
+ }
+ }
+
+ private void cleanHistoricalData(DbSession session, long resourceId, Settings settings) {
+ try {
+ periodCleaner.clean(session, resourceId, settings);
+ } catch (Exception e) {
+ // purge errors must no fail the batch
+ LOG.error("Fail to clean historical data [id=" + resourceId + "]", e);
+ }
+ }
+
+ private void doPurge(DbSession session, PurgeConfiguration configuration) {
+ try {
+ purgeDao.purge(session, configuration, purgeListener);
+ } catch (Exception e) {
+ // purge errors must no fail the report analysis
+ LOG.error("Fail to purge data [id=" + configuration.rootProjectId() + "]", e);
+ }
+ }
+}
+++ /dev/null
-/*
- * 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.server.computation.dbcleaner;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.CoreProperties;
-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.server.computation.dbcleaner.period.DefaultPeriodCleaner;
-
-import java.util.List;
-
-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;
-
- public ProjectPurgeTask(PurgeDao purgeDao, DefaultPeriodCleaner periodCleaner, PurgeProfiler profiler, PurgeListener purgeListener) {
- this.purgeDao = purgeDao;
- this.periodCleaner = periodCleaner;
- this.profiler = profiler;
- this.purgeListener = purgeListener;
- }
-
- public ProjectPurgeTask purge(DbSession session, PurgeConfiguration configuration, Settings settings) {
- long start = System.currentTimeMillis();
- profiler.reset();
- cleanHistoricalData(session, configuration.rootProjectId(), settings);
- doPurge(session, configuration);
- 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) {
- try {
- periodCleaner.clean(session, resourceId, settings);
- } catch (Exception e) {
- // purge errors must no fail the batch
- LOG.error("Fail to clean historical data [id=" + resourceId + "]", e);
- }
- }
-
- private void doPurge(DbSession session, PurgeConfiguration configuration) {
- try {
- purgeDao.purge(session, configuration, purgeListener);
- } catch (Exception e) {
- // purge errors must no fail the report analysis
- LOG.error("Fail to purge data [id=" + configuration.rootProjectId() + "]", e);
- }
- }
-
- public List<String> findUuidsToDisable(DbSession session, Long projectId) {
- return purgeDao.selectPurgeableFiles(session, projectId);
- }
-}
--- /dev/null
+/*
+ * 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;
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;
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);
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;
private IndexClient indexClient;
private SourceLineIndexer sourceLineIndexer;
private ProjectSettingsFactory projectSettingsFactory;
- private ProjectPurgeTask purgeTask;
+ private ProjectCleaner purgeTask;
@Before
public void before() throws Exception {
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
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));
}
}
--- /dev/null
+/*
+ * 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.server.computation.dbcleaner;
+
+import org.junit.Before;
+import org.junit.Test;
+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.*;
+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 ProjectCleanerTest {
+
+ 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 {
+ this.dao = mock(PurgeDao.class);
+ 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 ProjectCleaner(dao, periodCleaner, profiler, purgeListener, projectSettingsFactory, indexClient);
+ }
+
+ @Test
+ public void no_profiling_when_property_is_false() throws Exception {
+ 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(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 {
+ when(settings.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY)).thenReturn(true);
+
+ 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(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(IdUuidPair.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(IdUuidPair.class));
+
+ verify(periodCleaner).clean(any(DbSession.class), anyLong(), any(Settings.class));
+ }
+}
+++ /dev/null
-/*
- * 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.server.computation.dbcleaner;
-
-import org.junit.Before;
-import org.junit.Test;
-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.server.computation.dbcleaner.period.DefaultPeriodCleaner;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyLong;
-import static org.mockito.Mockito.*;
-
-public class ProjectPurgeTaskTest {
-
- private ProjectPurgeTask sut;
- private PurgeDao dao;
- private PurgeProfiler profiler;
- private DefaultPeriodCleaner periodCleaner;
- private PurgeListener purgeListener;
-
- @Before
- public void before() throws Exception {
- this.dao = mock(PurgeDao.class);
- this.profiler = mock(PurgeProfiler.class);
- this.periodCleaner = mock(DefaultPeriodCleaner.class);
- this.purgeListener = mock(PurgeListener.class);
-
- this.sut = new ProjectPurgeTask(dao, periodCleaner, profiler, purgeListener);
- }
-
- @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);
-
- sut.purge(mock(DbSession.class), mock(PurgeConfiguration.class), settings);
-
- verify(profiler, never()).dump(anyLong(), any(Logger.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);
-
- verify(profiler, times(1)).dump(anyLong(), any(Logger.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));
-
- verify(dao, times(1)).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));
-
- verify(periodCleaner, times(1)).clean(any(DbSession.class), anyLong(), any(Settings.class));
- }
-}