]> source.dussan.org Git - sonarqube.git/commitdiff
SOAR-1663 the purge task by period can be reused by plugins
authorsimonbrandhof <simon.brandhof@gmail.com>
Fri, 19 Nov 2010 14:17:09 +0000 (14:17 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Fri, 19 Nov 2010 14:17:09 +0000 (14:17 +0000)
14 files changed:
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PeriodCleaner.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/PeriodCleaner.java [deleted file]
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/PeriodPurge.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Periods.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/DefaultPurgeContext.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/PurgeRunner.java
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntegrationTest.java
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodCleanerTest.java [deleted file]
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodsTest.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasuresTest.java
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/runner/PurgeRunnerTest.java

index 1196faab1436ba45e12cbd821686783c2d92d459..41fa4916cf6f4f6bb400eb95b93e8722a540134e 100644 (file)
@@ -22,7 +22,8 @@ package org.sonar.plugins.dbcleaner;
 import org.sonar.api.Plugin;\r
 import org.sonar.api.Properties;\r
 import org.sonar.api.Property;\r
-import org.sonar.plugins.dbcleaner.period.PeriodCleaner;\r
+import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner;\r
+import org.sonar.plugins.dbcleaner.period.PeriodPurge;\r
 import org.sonar.plugins.dbcleaner.purges.*;\r
 import org.sonar.plugins.dbcleaner.runner.PurgeRunner;\r
 import org.sonar.plugins.dbcleaner.util.DbCleanerConstants;\r
@@ -63,10 +64,13 @@ public final class DbCleanerPlugin implements Plugin {
 \r
   public List getExtensions() {\r
     return Arrays.asList(\r
+        // shared components\r
+        DefaultPeriodCleaner.class,\r
+\r
         // purges\r
         PurgeOrphanResources.class, PurgeEntities.class, PurgeRuleMeasures.class, PurgeUnprocessed.class,\r
         PurgeDeletedResources.class, PurgeDeprecatedLast.class, UnflagLastDoublons.class, PurgeDisabledResources.class,\r
-        PurgeResourceRoles.class, PurgeEventOrphans.class, PurgePropertyOrphans.class, PeriodCleaner.class,\r
+        PurgeResourceRoles.class, PurgeEventOrphans.class, PurgePropertyOrphans.class, PeriodPurge.class,\r
 \r
         // post-job\r
         PurgeRunner.class);\r
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 (file)
index 0000000..fa26501
--- /dev/null
@@ -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);
+}
index f7c72adad54e3f1fe47d1b80ddbb4df660344e25..abaa9cb08ccc09f2521b4f713565b55a9337d701 100644 (file)
  */
 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/DefaultPeriodCleaner.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java
new file mode 100644 (file)
index 0000000..cc02ae0
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * 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 com.google.common.collect.Lists;
+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.PeriodCleaner;
+import org.sonar.plugins.dbcleaner.util.PurgeUtils;
+
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
+
+public final class DefaultPeriodCleaner implements PeriodCleaner {
+
+  private static final Logger LOG = LoggerFactory.getLogger(DefaultPeriodCleaner.class);
+  private final SQLRequests sql;
+  private DatabaseSession session;
+
+  public DefaultPeriodCleaner(DatabaseSession session) {
+    this.session = session;
+    this.sql = new SQLRequests(session);
+  }
+
+  public void purge(Project project, int projectSnapshotId) {
+    Periods periods = new Periods(project);
+    periods.log();
+    purge(project, projectSnapshotId, periods);
+  }
+
+  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> selectProjectSnapshots(Project project, int snapshotId) {
+    List<Snapshot> snapshotHistory = Lists.newLinkedList(sql.getProjectSnapshotsOrderedByCreatedAt(snapshotId));
+    LOG.debug("The project '" + project.getName() + "' has " + snapshotHistory.size() + " snapshots.");
+    return snapshotHistory;
+  }
+
+  private void deleteSnapshotsAndAllRelatedData(List<Snapshot> snapshotHistory) {
+    if (snapshotHistory.isEmpty()) {
+      LOG.info("There are no snapshots to purge");
+      return;
+    }
+
+    List<Integer> ids = Lists.newArrayList();
+    for (Snapshot snapshot : snapshotHistory) {
+      ids.addAll(sql.getChildIds(snapshot));
+    }
+    LOG.info("There are " + snapshotHistory.size() + " snapshots and " + (ids.size() - snapshotHistory.size())
+        + " children snapshots which are obsolete and are going to be deleted.");
+    if (LOG.isDebugEnabled()) {
+      DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
+      for (Snapshot snapshot : snapshotHistory) {
+        LOG.debug("Delete snapshot created at " + format.format(snapshot.getCreatedAt()));
+      }
+    }
+    PurgeUtils.deleteSnapshotsData(session, ids);
+  }
+
+  private void applyFilters(List<Snapshot> snapshotHistory, List<SnapshotFilter> filters) {
+    for (SnapshotFilter filter : filters) {
+      filter.filter(snapshotHistory);
+    }
+  }
+
+  private List<SnapshotFilter> newFilters(Periods periods) {
+    List<SnapshotFilter> filters = Lists.newArrayList();
+    filters.add(new KeepLibrarySnapshotFilter());
+    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;
+  }
+}
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/PeriodCleaner.java
deleted file mode 100644 (file)
index 89d9ebb..0000000
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * 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 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.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 {
-
-  private static final Logger LOG = LoggerFactory.getLogger(PeriodCleaner.class);
-  private final SQLRequests sql;
-  private final Project project;
-
-  Date dateToStartKeepingOneSnapshotByWeek;
-  Date dateToStartKeepingOneSnapshotByMonth;
-  Date dateToStartDeletingAllSnapshots;
-
-  public PeriodCleaner(DatabaseSession session, Project project) {
-    super(session);
-    this.sql = new SQLRequests(session);
-    this.project = project;
-    initMilestones();
-  }
-
-  public void purge(PurgeContext context) {
-    purge(context.getSnapshotId());
-  }
-
-  public void purge(int snapshotId) {
-    List<SnapshotFilter> filters = initDbCleanerFilters();
-    List<Snapshot> snapshotHistory = getAllProjectSnapshots(snapshotId);
-    applyFilters(snapshotHistory, filters);
-    deleteSnapshotsAndAllRelatedData(snapshotHistory);
-  }
-
-  private List<Snapshot> getAllProjectSnapshots(int snapshotId) {
-    List<Snapshot> snapshotHistory = Lists.newLinkedList(sql.getProjectSnapshotsOrderedByCreatedAt(snapshotId));
-    LOG.info("The project '" + project.getName() + "' has " + snapshotHistory.size() + " snapshots.");
-    return snapshotHistory;
-  }
-
-  private void deleteSnapshotsAndAllRelatedData(List<Snapshot> snapshotHistory) {
-    if (snapshotHistory.isEmpty()) {
-      LOG.info("There are no snapshots to purge");
-      return;
-    }
-    List<Integer> ids = Lists.newArrayList();
-    for (Snapshot snapshot : snapshotHistory) {
-      ids.addAll(sql.getChildIds(snapshot));
-    }
-    LOG.info("There are " + snapshotHistory.size() + " snapshots and " + (ids.size() - snapshotHistory.size())
-        + " children snapshots which are obsolete and are going to be deleted.");
-    if (LOG.isDebugEnabled()) {
-      DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
-      for (Snapshot snapshot : snapshotHistory) {
-        LOG.debug("Delete snapshot created at " + format.format(snapshot.getCreatedAt()));
-      }
-    }
-    PurgeUtils.deleteSnapshotsData(getSession(), ids);
-  }
-
-  private void applyFilters(List<Snapshot> snapshotHistory, List<SnapshotFilter> filters) {
-    for (SnapshotFilter filter : filters) {
-      filter.filter(snapshotHistory);
-    }
-  }
-
-  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() {
-    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 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 (file)
index 0000000..f4abfbd
--- /dev/null
@@ -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 (file)
index 0000000..19b9ba3
--- /dev/null
@@ -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();
+  }
+}
index c914dbbf1226aa69d744aec3e742928a2b3df9ef..91b711baa81e4adedd5087fa30200ccbd1db078e 100644 (file)
@@ -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;
index e947885ecacff906d1b55e8410ac626821081976..9548f8c853a8c818febc8596a565753b8325d5a0 100644 (file)
@@ -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());
index 5c70a57162b5384510f1ba3306d399449d410852..3ddd1e3d1b76885494319094dcca7b4f9615666b 100644 (file)
  */
 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/PeriodCleanerTest.java
deleted file mode 100644 (file)
index dd3d196..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.apache.commons.configuration.PropertiesConfiguration;
-import org.junit.Test;
-import org.sonar.api.resources.Project;
-
-import java.util.Date;
-import java.util.GregorianCalendar;
-
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
-public class PeriodCleanerTest {
-
-  @Test
-  public void getDateShouldReturnCurrentTimeMinusDesiredMonths() {
-    Project project = new Project("myproject");
-    PropertiesConfiguration conf = new PropertiesConfiguration();
-    conf.setProperty("KEY", "2");
-    project.setConfiguration(conf);
-
-    PeriodCleaner purge = new PeriodCleaner(null, project);
-
-    Date date = purge.getDate(conf, "KEY", "2");
-
-    GregorianCalendar calendar = new GregorianCalendar();
-    calendar.add(GregorianCalendar.MONTH, -2);
-    Date expectedDate = calendar.getTime();
-
-    assertThat(date.getMonth(), is(expectedDate.getMonth()));
-  }
-}
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodsTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/PeriodsTest.java
new file mode 100644 (file)
index 0000000..307952c
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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.apache.commons.configuration.PropertiesConfiguration;
+import org.junit.Test;
+import org.sonar.api.resources.Project;
+
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class PeriodsTest {
+
+  @Test
+  public void getDateShouldReturnCurrentTimeMinusDesiredMonths() {
+    Project project = new Project("myproject");
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty("KEY", "2");
+    project.setConfiguration(conf);
+
+    Date date = Periods.getDate(conf, "KEY", "2");
+
+    GregorianCalendar calendar = new GregorianCalendar();
+    calendar.add(GregorianCalendar.MONTH, -2);
+    Date expectedDate = calendar.getTime();
+
+    assertThat(date.getMonth(), is(expectedDate.getMonth()));
+  }
+}
index a3e715157e459df44ce56d69763a07da0725f358..f1ca3db58afa3b39543dbe8d639560b84e1eef86 100644 (file)
@@ -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;
       }
index 37832b2d7df1c7e85025f62f5562d5bf094ebe02..64c756228d94ca465177dea46812f9b3e472e057 100644 (file)
@@ -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) {