aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-dbcleaner-plugin
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-11-19 14:17:09 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-11-19 14:17:09 +0000
commit952108dca62b740a9c197a06b52aad3ab79a2031 (patch)
tree7fda120f5cf4431b59f4e9a112be1a3da626323d /plugins/sonar-dbcleaner-plugin
parent95c1689c46624d4f3f77e6a1bbc5f6e00019d691 (diff)
downloadsonarqube-952108dca62b740a9c197a06b52aad3ab79a2031.tar.gz
sonarqube-952108dca62b740a9c197a06b52aad3ab79a2031.zip
SOAR-1663 the purge task by period can be reused by plugins
Diffstat (limited to 'plugins/sonar-dbcleaner-plugin')
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java8
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PeriodCleaner.java27
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java4
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java (renamed from plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/PeriodCleaner.java)75
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/PeriodPurge.java39
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Periods.java67
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/DefaultPurgeContext.java13
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/PurgeRunner.java31
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntegrationTest.java40
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodsTest.java (renamed from plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodCleanerTest.java)6
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasuresTest.java5
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/runner/PurgeRunnerTest.java6
12 files changed, 225 insertions, 96 deletions
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java
index 1196faab143..41fa4916cf6 100644
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java
@@ -22,7 +22,8 @@ package org.sonar.plugins.dbcleaner;
import org.sonar.api.Plugin;
import org.sonar.api.Properties;
import org.sonar.api.Property;
-import org.sonar.plugins.dbcleaner.period.PeriodCleaner;
+import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner;
+import org.sonar.plugins.dbcleaner.period.PeriodPurge;
import org.sonar.plugins.dbcleaner.purges.*;
import org.sonar.plugins.dbcleaner.runner.PurgeRunner;
import org.sonar.plugins.dbcleaner.util.DbCleanerConstants;
@@ -63,10 +64,13 @@ public final class DbCleanerPlugin implements Plugin {
public List getExtensions() {
return Arrays.asList(
+ // shared components
+ DefaultPeriodCleaner.class,
+
// purges
PurgeOrphanResources.class, PurgeEntities.class, PurgeRuleMeasures.class, PurgeUnprocessed.class,
PurgeDeletedResources.class, PurgeDeprecatedLast.class, UnflagLastDoublons.class, PurgeDisabledResources.class,
- PurgeResourceRoles.class, PurgeEventOrphans.class, PurgePropertyOrphans.class, PeriodCleaner.class,
+ PurgeResourceRoles.class, PurgeEventOrphans.class, PurgePropertyOrphans.class, PeriodPurge.class,
// post-job
PurgeRunner.class);
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PeriodCleaner.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PeriodCleaner.java
new file mode 100644
index 00000000000..fa2650130b7
--- /dev/null
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PeriodCleaner.java
@@ -0,0 +1,27 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.dbcleaner.api;
+
+import org.sonar.api.BatchExtension;
+import org.sonar.api.resources.Project;
+
+public interface PeriodCleaner extends BatchExtension {
+ void purge(Project project, int projectSnapshotId);
+}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java
index f7c72adad54..abaa9cb08cc 100644
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java
@@ -19,12 +19,16 @@
*/
package org.sonar.plugins.dbcleaner.api;
+import org.sonar.api.resources.Project;
+
/**
*
* @since 2.5
*/
public interface PurgeContext {
+ Project getProject();
+
/**
* @return the snapshot id of the current project
*/
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/PeriodCleaner.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java
index 89d9ebbed6f..cc02ae077e0 100644
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/PeriodCleaner.java
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java
@@ -20,54 +20,46 @@
package org.sonar.plugins.dbcleaner.period;
import com.google.common.collect.Lists;
-import org.apache.commons.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Project;
-import org.sonar.plugins.dbcleaner.api.Purge;
-import org.sonar.plugins.dbcleaner.api.PurgeContext;
-import org.sonar.plugins.dbcleaner.util.DbCleanerConstants;
+import org.sonar.plugins.dbcleaner.api.PeriodCleaner;
import org.sonar.plugins.dbcleaner.util.PurgeUtils;
import java.text.DateFormat;
-import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
-public final class PeriodCleaner extends Purge {
+public final class DefaultPeriodCleaner implements PeriodCleaner {
- private static final Logger LOG = LoggerFactory.getLogger(PeriodCleaner.class);
+ private static final Logger LOG = LoggerFactory.getLogger(DefaultPeriodCleaner.class);
private final SQLRequests sql;
- private final Project project;
+ private DatabaseSession session;
- Date dateToStartKeepingOneSnapshotByWeek;
- Date dateToStartKeepingOneSnapshotByMonth;
- Date dateToStartDeletingAllSnapshots;
-
- public PeriodCleaner(DatabaseSession session, Project project) {
- super(session);
+ public DefaultPeriodCleaner(DatabaseSession session) {
+ this.session = session;
this.sql = new SQLRequests(session);
- this.project = project;
- initMilestones();
}
- public void purge(PurgeContext context) {
- purge(context.getSnapshotId());
+ public void purge(Project project, int projectSnapshotId) {
+ Periods periods = new Periods(project);
+ periods.log();
+ purge(project, projectSnapshotId, periods);
}
- public void purge(int snapshotId) {
- List<SnapshotFilter> filters = initDbCleanerFilters();
- List<Snapshot> snapshotHistory = getAllProjectSnapshots(snapshotId);
+ void purge(Project project, int projectSnapshotId, Periods periods) {
+ List<SnapshotFilter> filters = newFilters(periods);
+ List<Snapshot> snapshotHistory = selectProjectSnapshots(project, projectSnapshotId);
applyFilters(snapshotHistory, filters);
deleteSnapshotsAndAllRelatedData(snapshotHistory);
}
- private List<Snapshot> getAllProjectSnapshots(int snapshotId) {
+ private List<Snapshot> selectProjectSnapshots(Project project, int snapshotId) {
List<Snapshot> snapshotHistory = Lists.newLinkedList(sql.getProjectSnapshotsOrderedByCreatedAt(snapshotId));
- LOG.info("The project '" + project.getName() + "' has " + snapshotHistory.size() + " snapshots.");
+ LOG.debug("The project '" + project.getName() + "' has " + snapshotHistory.size() + " snapshots.");
return snapshotHistory;
}
@@ -76,6 +68,7 @@ public final class PeriodCleaner extends Purge {
LOG.info("There are no snapshots to purge");
return;
}
+
List<Integer> ids = Lists.newArrayList();
for (Snapshot snapshot : snapshotHistory) {
ids.addAll(sql.getChildIds(snapshot));
@@ -88,7 +81,7 @@ public final class PeriodCleaner extends Purge {
LOG.debug("Delete snapshot created at " + format.format(snapshot.getCreatedAt()));
}
}
- PurgeUtils.deleteSnapshotsData(getSession(), ids);
+ PurgeUtils.deleteSnapshotsData(session, ids);
}
private void applyFilters(List<Snapshot> snapshotHistory, List<SnapshotFilter> filters) {
@@ -97,35 +90,17 @@ public final class PeriodCleaner extends Purge {
}
}
- private void initMilestones() {
- SimpleDateFormat dateFormat = new SimpleDateFormat();
- dateToStartKeepingOneSnapshotByWeek = getDate(project.getConfiguration(),
- DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK, DbCleanerConstants.ONE_MONTH);
- LOG.debug("Keep only one snapshot by week after : " + dateFormat.format(dateToStartKeepingOneSnapshotByWeek));
- dateToStartKeepingOneSnapshotByMonth = getDate(project.getConfiguration(),
- DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH, DbCleanerConstants.ONE_YEAR);
- LOG.debug("Keep only one snapshot by month after : " + dateFormat.format(dateToStartKeepingOneSnapshotByMonth));
- dateToStartDeletingAllSnapshots = getDate(project.getConfiguration(), DbCleanerConstants.MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS,
- DbCleanerConstants.THREE_YEARS);
- LOG.debug("Delete all snapshots after : " + dateFormat.format(dateToStartDeletingAllSnapshots));
- }
-
- private List<SnapshotFilter> initDbCleanerFilters() {
+ private List<SnapshotFilter> newFilters(Periods periods) {
List<SnapshotFilter> filters = Lists.newArrayList();
filters.add(new KeepLibrarySnapshotFilter());
- filters.add(new KeepSnapshotsBetweenTwoDatesFilter(new Date(), dateToStartKeepingOneSnapshotByWeek));
- filters.add(new KeepOneSnapshotByPeriodBetweenTwoDatesFilter(GregorianCalendar.WEEK_OF_YEAR, dateToStartKeepingOneSnapshotByWeek,
- dateToStartKeepingOneSnapshotByMonth));
- filters.add(new KeepOneSnapshotByPeriodBetweenTwoDatesFilter(GregorianCalendar.MONTH, dateToStartKeepingOneSnapshotByMonth,
- dateToStartDeletingAllSnapshots));
+ filters.add(new KeepSnapshotsBetweenTwoDatesFilter(new Date(), periods.dateToStartKeepingOneSnapshotByWeek));
+ filters.add(new KeepOneSnapshotByPeriodBetweenTwoDatesFilter(GregorianCalendar.WEEK_OF_YEAR,
+ periods.dateToStartKeepingOneSnapshotByWeek,
+ periods.dateToStartKeepingOneSnapshotByMonth));
+ filters.add(new KeepOneSnapshotByPeriodBetweenTwoDatesFilter(GregorianCalendar.MONTH,
+ periods.dateToStartKeepingOneSnapshotByMonth,
+ periods.dateToStartDeletingAllSnapshots));
filters.add(new KeepLastSnapshotFilter());
return filters;
}
-
- protected Date getDate(Configuration conf, String propertyKey, String defaultNumberOfMonths) {
- int months = conf.getInt(propertyKey, Integer.parseInt(defaultNumberOfMonths));
- GregorianCalendar calendar = new GregorianCalendar();
- calendar.add(GregorianCalendar.MONTH, -months);
- return calendar.getTime();
- }
}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/PeriodPurge.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/PeriodPurge.java
new file mode 100644
index 00000000000..f4abfbd3c0c
--- /dev/null
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/PeriodPurge.java
@@ -0,0 +1,39 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2010 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.dbcleaner.period;
+
+import org.sonar.api.database.DatabaseSession;
+import org.sonar.plugins.dbcleaner.api.PeriodCleaner;
+import org.sonar.plugins.dbcleaner.api.Purge;
+import org.sonar.plugins.dbcleaner.api.PurgeContext;
+
+public final class PeriodPurge extends Purge {
+
+ private PeriodCleaner periodCleaner;
+
+ public PeriodPurge(DatabaseSession session, PeriodCleaner periodCleaner) {
+ super(session);
+ this.periodCleaner = periodCleaner;
+ }
+
+ public void purge(PurgeContext context) {
+ periodCleaner.purge(context.getProject(), context.getSnapshotId());
+ }
+}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Periods.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Periods.java
new file mode 100644
index 00000000000..19b9ba349bf
--- /dev/null
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Periods.java
@@ -0,0 +1,67 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.dbcleaner.period;
+
+import org.apache.commons.configuration.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.resources.Project;
+import org.sonar.plugins.dbcleaner.util.DbCleanerConstants;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+public final class Periods {
+
+ Date dateToStartKeepingOneSnapshotByWeek;
+ Date dateToStartKeepingOneSnapshotByMonth;
+ Date dateToStartDeletingAllSnapshots;
+
+ public Periods(Date dateToStartKeepingOneSnapshotByWeek, Date dateToStartKeepingOneSnapshotByMonth, Date dateToStartDeletingAllSnapshots) {
+ this.dateToStartKeepingOneSnapshotByWeek = dateToStartKeepingOneSnapshotByWeek;
+ this.dateToStartKeepingOneSnapshotByMonth = dateToStartKeepingOneSnapshotByMonth;
+ this.dateToStartDeletingAllSnapshots = dateToStartDeletingAllSnapshots;
+ }
+
+ public Periods(Project project) {
+ dateToStartKeepingOneSnapshotByWeek = getDate(project.getConfiguration(),
+ DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK, DbCleanerConstants.ONE_MONTH);
+ dateToStartKeepingOneSnapshotByMonth = getDate(project.getConfiguration(),
+ DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH, DbCleanerConstants.ONE_YEAR);
+ dateToStartDeletingAllSnapshots = getDate(project.getConfiguration(), DbCleanerConstants.MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS,
+ DbCleanerConstants.THREE_YEARS);
+ }
+
+ void log() {
+ Logger logger = LoggerFactory.getLogger(getClass());
+ SimpleDateFormat dateFormat = new SimpleDateFormat();
+ logger.debug("Keep only one snapshot by week after : " + dateFormat.format(dateToStartKeepingOneSnapshotByWeek));
+ logger.debug("Keep only one snapshot by month after : " + dateFormat.format(dateToStartKeepingOneSnapshotByMonth));
+ logger.debug("Delete all snapshots after : " + dateFormat.format(dateToStartDeletingAllSnapshots));
+ }
+
+ static Date getDate(Configuration conf, String propertyKey, String defaultNumberOfMonths) {
+ int months = conf.getInt(propertyKey, Integer.parseInt(defaultNumberOfMonths));
+ GregorianCalendar calendar = new GregorianCalendar();
+ calendar.add(GregorianCalendar.MONTH, -months);
+ return calendar.getTime();
+ }
+}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/DefaultPurgeContext.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/DefaultPurgeContext.java
index c914dbbf122..91b711baa81 100644
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/DefaultPurgeContext.java
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/DefaultPurgeContext.java
@@ -21,18 +21,21 @@ package org.sonar.plugins.dbcleaner.runner;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.Project;
import org.sonar.plugins.dbcleaner.api.PurgeContext;
public final class DefaultPurgeContext implements org.sonar.api.batch.PurgeContext, PurgeContext {
+ private Project project;
private Integer currentSid;
private Integer previousSid;
- public DefaultPurgeContext(Snapshot currentSnapshot) {
- this(currentSnapshot, null);
+ public DefaultPurgeContext(Project project, Snapshot currentSnapshot) {
+ this(project, currentSnapshot, null);
}
- public DefaultPurgeContext(Snapshot currentSnapshot, Snapshot previousSnapshot) {
+ public DefaultPurgeContext(Project project, Snapshot currentSnapshot, Snapshot previousSnapshot) {
+ this.project = project;
if (currentSnapshot != null) {
currentSid = currentSnapshot.getId();
}
@@ -54,6 +57,10 @@ public final class DefaultPurgeContext implements org.sonar.api.batch.PurgeConte
return previousSid;
}
+ public Project getProject() {
+ return project;
+ }
+
@Deprecated
public Integer getLastSnapshotId() {
return currentSid;
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/PurgeRunner.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/PurgeRunner.java
index e947885ecac..9548f8c853a 100644
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/PurgeRunner.java
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/PurgeRunner.java
@@ -37,16 +37,20 @@ public final class PurgeRunner implements PostJob {
private Snapshot snapshot;
private Purge[] purges;
private org.sonar.api.batch.Purge[] deprecatedPurges;
+ private Project project;
+ private static final Logger LOG = LoggerFactory.getLogger(PurgeRunner.class);
- public PurgeRunner(DatabaseSession session, Snapshot snapshot, Purge[] purges) {
+ public PurgeRunner(DatabaseSession session, Project project, Snapshot snapshot, Purge[] purges) {
this.session = session;
+ this.project = project;
this.snapshot = snapshot;
this.purges = purges.clone();
this.deprecatedPurges = new org.sonar.api.batch.Purge[0];
}
- public PurgeRunner(DatabaseSession session, Snapshot snapshot, Purge[] purges, org.sonar.api.batch.Purge[] deprecatedPurges) {
+ public PurgeRunner(DatabaseSession session, Project project, Snapshot snapshot, Purge[] purges, org.sonar.api.batch.Purge[] deprecatedPurges) {
this.session = session;
+ this.project = project;
this.snapshot = snapshot;
this.purges = purges.clone();
this.deprecatedPurges = deprecatedPurges.clone();
@@ -63,31 +67,34 @@ public final class PurgeRunner implements PostJob {
}
public void purge() {
- final Logger logger = LoggerFactory.getLogger(PurgeRunner.class);
- TimeProfiler profiler = new TimeProfiler(logger).start("Database optimization");
+ TimeProfiler profiler = new TimeProfiler(LOG).start("Database optimization");
DefaultPurgeContext context = newContext();
- logger.debug("Snapshots to purge: " + context);
- executeDeprecatedPurges(logger, context);
- executePurges(logger, context);
+ LOG.debug("Snapshots to purge: " + context);
+ executeDeprecatedPurges(context);
+ executePurges(context);
profiler.stop();
}
- private void executeDeprecatedPurges(Logger logger, DefaultPurgeContext context) {
+ private void executeDeprecatedPurges(DefaultPurgeContext context) {
+ TimeProfiler profiler = new TimeProfiler();
for (org.sonar.api.batch.Purge purge : deprecatedPurges) {
- logger.debug("Executing {}...", purge.getClass().getName());
+ profiler.start("Purge " + purge.getClass().getName());
purge.purge(context);
+ profiler.stop();
}
}
- private void executePurges(Logger logger, DefaultPurgeContext context) {
+ private void executePurges(DefaultPurgeContext context) {
+ TimeProfiler profiler = new TimeProfiler();
for (Purge purge : purges) {
- logger.debug("Executing {}...", purge.getClass().getName());
+ profiler.start("Purge " + purge.getClass().getName());
purge.purge(context);
+ profiler.stop();
}
}
private DefaultPurgeContext newContext() {
- DefaultPurgeContext context = new DefaultPurgeContext(snapshot);
+ DefaultPurgeContext context = new DefaultPurgeContext(project, snapshot);
Snapshot previousLastSnapshot = getPreviousLastSnapshot();
if (previousLastSnapshot != null && previousLastSnapshot.getCreatedAt().before(snapshot.getCreatedAt())) {
context.setLastSnapshotId(previousLastSnapshot.getId());
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntegrationTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntegrationTest.java
index 5c70a57162b..3ddd1e3d1b7 100644
--- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntegrationTest.java
+++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntegrationTest.java
@@ -19,49 +19,45 @@
*/
package org.sonar.plugins.dbcleaner.period;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.util.GregorianCalendar;
-
import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.resources.Project;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
-import org.sonar.plugins.dbcleaner.api.PurgeContext;
+
+import java.util.Date;
+import java.util.GregorianCalendar;
public class IntegrationTest extends AbstractDbUnitTestCase {
- PeriodCleaner purge;
+ DefaultPeriodCleaner cleaner;
@Before
public void init() {
+ cleaner = new DefaultPeriodCleaner(getSession());
+ }
+
+ @Test
+ public void dbCleanerITTest() {
+ setupData("dbContent");
Project project = new Project("myproject");
project.setConfiguration(new PropertiesConfiguration());
- purge = new PeriodCleaner(getSession(), project);
+
GregorianCalendar calendar = new GregorianCalendar(2010, 10, 1);
- purge.dateToStartKeepingOneSnapshotByWeek = calendar.getTime();
+ Date dateToStartKeepingOneSnapshotByWeek = calendar.getTime();
calendar.set(2010, 7, 1);
- purge.dateToStartKeepingOneSnapshotByMonth = calendar.getTime();
+ Date dateToStartKeepingOneSnapshotByMonth = calendar.getTime();
calendar.set(2010, 2, 1);
- purge.dateToStartDeletingAllSnapshots = calendar.getTime();
- }
+ Date dateToStartDeletingAllSnapshots = calendar.getTime();
+ Periods periods = new Periods(dateToStartKeepingOneSnapshotByWeek, dateToStartKeepingOneSnapshotByMonth, dateToStartDeletingAllSnapshots);
- @Test
- public void dbCleanerITTest() {
- setupData("dbContent");
- PurgeContext context = mock(PurgeContext.class);
- when(context.getSnapshotId()).thenReturn(1010);
- purge.purge(context);
+ cleaner.purge(project, 1010, periods);
checkTables("dbContent", "snapshots");
-
+
//After a first run, no more snapshot should be deleted
setupData("dbContent-result");
- context = mock(PurgeContext.class);
- when(context.getSnapshotId()).thenReturn(1010);
- purge.purge(context);
+ cleaner.purge(project, 1010, periods);
checkTables("dbContent");
}
}
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodCleanerTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodsTest.java
index dd3d1963c50..307952cc1fb 100644
--- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodCleanerTest.java
+++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodsTest.java
@@ -29,7 +29,7 @@ import java.util.GregorianCalendar;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-public class PeriodCleanerTest {
+public class PeriodsTest {
@Test
public void getDateShouldReturnCurrentTimeMinusDesiredMonths() {
@@ -38,9 +38,7 @@ public class PeriodCleanerTest {
conf.setProperty("KEY", "2");
project.setConfiguration(conf);
- PeriodCleaner purge = new PeriodCleaner(null, project);
-
- Date date = purge.getDate(conf, "KEY", "2");
+ Date date = Periods.getDate(conf, "KEY", "2");
GregorianCalendar calendar = new GregorianCalendar();
calendar.add(GregorianCalendar.MONTH, -2);
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasuresTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasuresTest.java
index a3e715157e4..f1ca3db58af 100644
--- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasuresTest.java
+++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasuresTest.java
@@ -20,6 +20,7 @@
package org.sonar.plugins.dbcleaner.purges;
import org.junit.Test;
+import org.sonar.api.resources.Project;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import org.sonar.plugins.dbcleaner.api.PurgeContext;
@@ -30,6 +31,10 @@ public class PurgeRuleMeasuresTest extends AbstractDbUnitTestCase {
setupData("sharedFixture", "purgeRuleMeasures");
new PurgeRuleMeasures(getSession()).purge(new PurgeContext() {
+ public Project getProject() {
+ return null;
+ }
+
public Integer getSnapshotId() {
return 4;
}
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/runner/PurgeRunnerTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/runner/PurgeRunnerTest.java
index 37832b2d7df..64c756228d9 100644
--- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/runner/PurgeRunnerTest.java
+++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/runner/PurgeRunnerTest.java
@@ -48,7 +48,7 @@ public class PurgeRunnerTest extends AbstractDbUnitTestCase {
Purge purge2 = mock(Purge.class);
Purge[] purges = new Purge[]{purge1, purge2};
- new PurgeRunner(getSession(), snapshot, purges).purge();
+ new PurgeRunner(getSession(), new Project("key"), snapshot, purges).purge();
verify(purge1).purge(argThat(new BaseMatcher<PurgeContext>() {
public boolean matches(Object o) {
@@ -71,7 +71,7 @@ public class PurgeRunnerTest extends AbstractDbUnitTestCase {
Purge purge2 = mock(Purge.class);
Purge[] purges = new Purge[]{purge1, purge2};
- new PurgeRunner(getSession(), snapshot, purges).purge();
+ new PurgeRunner(getSession(), new Project("key"), snapshot, purges).purge();
verify(purge1).purge(argThat(new BaseMatcher<PurgeContext>() {
public boolean matches(Object o) {
@@ -96,7 +96,7 @@ public class PurgeRunnerTest extends AbstractDbUnitTestCase {
org.sonar.api.batch.Purge deprecated1 = mock(org.sonar.api.batch.Purge.class), deprecated2 = mock(org.sonar.api.batch.Purge.class);
org.sonar.api.batch.Purge[] deprecatedPurges = new org.sonar.api.batch.Purge[]{deprecated1, deprecated2};
- new PurgeRunner(getSession(), snapshot, new Purge[0], deprecatedPurges).purge();
+ new PurgeRunner(getSession(), new Project("key"), snapshot, new Purge[0], deprecatedPurges).purge();
verify(deprecated1).purge(argThat(new BaseMatcher<org.sonar.api.batch.PurgeContext>() {
public boolean matches(Object o) {