]> source.dussan.org Git - sonarqube.git/commitdiff
fix quality flaws and SRP
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 9 Dec 2014 17:27:11 +0000 (18:27 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 9 Dec 2014 17:27:23 +0000 (18:27 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/ComponentCleanerService.java
server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/ProjectCleaner.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/ProjectPurgeTask.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/package-info.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/ProjectCleanerTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/ProjectPurgeTaskTest.java [deleted file]

index 7bf1872218379c34352ba52d074d7bfdaf588337..3626869b849db20b3a48f1937d9d80ea0ed999db 100644 (file)
@@ -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;
index 1bb8802150eff76989b8217e51ec82f40c51d41f..9327464fd155644bd9090440b36253c1767b1431 100644 (file)
 
 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/ProjectCleaner.java b/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/ProjectCleaner.java
new file mode 100644 (file)
index 0000000..60e7119
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * 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);
+    }
+  }
+}
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/ProjectPurgeTask.java
deleted file mode 100644 (file)
index d909c55..0000000
+++ /dev/null
@@ -1,87 +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.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);
-  }
-}
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 (file)
index 0000000..f3a8c77
--- /dev/null
@@ -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;
index b1f54a0cced7ea4094c5f4ea218c8fb171396b96..a98e375fd0c9a35ab6d647d032d8a7b9d1915cde 100644 (file)
@@ -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);
 
index 512285c89112e50d7296991594228a4681d3d8c9..15e560d0c25b9af777ed548730b29898140106de 100644 (file)
@@ -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
index 800b1f7ff47e8de31844f305c1bbe604b52a7f43..e170e6f21db3f660c9b5c6b0a5465491b26e8bf6 100644 (file)
@@ -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/ProjectCleanerTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/ProjectCleanerTest.java
new file mode 100644 (file)
index 0000000..31f8e02
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ * 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));
+  }
+}
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/ProjectPurgeTaskTest.java
deleted file mode 100644 (file)
index 25c4c7c..0000000
+++ /dev/null
@@ -1,94 +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.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));
-  }
-}