]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2757 extract purge task into a dedicated component
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 27 Jan 2012 15:48:54 +0000 (16:48 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 27 Jan 2012 15:48:54 +0000 (16:48 +0100)
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJob.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeTask.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJobTest.java [deleted file]

index ca35757b6e014965a134cf66ff71f6dd44709dee..0155f2c3187b916ca80ce76be46335bff26e77aa 100644 (file)
@@ -51,7 +51,6 @@ import java.util.List;
 public final class DbCleanerPlugin extends SonarPlugin {
 
   public List getExtensions() {
-    return Arrays.asList(
-      DefaultPeriodCleaner.class, ProjectPurgePostJob.class);
+    return Arrays.asList(DefaultPeriodCleaner.class, DefaultPurgeTask.class, ProjectPurgePostJob.class);
   }
 }
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java
new file mode 100644 (file)
index 0000000..0027fcc
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.Properties;
+import org.sonar.api.Property;
+import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Scopes;
+import org.sonar.core.purge.PurgeDao;
+import org.sonar.core.purge.PurgeSnapshotQuery;
+import org.sonar.plugins.dbcleaner.api.DbCleanerConstants;
+import org.sonar.plugins.dbcleaner.api.PurgeTask;
+import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner;
+
+/**
+ * @since 2.14
+ */
+@Properties({
+  @Property(
+    key = DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY,
+    defaultValue = "false",
+    name = "Clean history data of directories/packages")
+})
+public class DefaultPurgeTask implements PurgeTask {
+  private static final Logger LOG = LoggerFactory.getLogger(ProjectPurgePostJob.class);
+
+  private PurgeDao purgeDao;
+  private Settings settings;
+  private DefaultPeriodCleaner periodCleaner;
+
+  public DefaultPurgeTask(PurgeDao purgeDao, Settings settings, DefaultPeriodCleaner periodCleaner) {
+    this.purgeDao = purgeDao;
+    this.settings = settings;
+    this.periodCleaner = periodCleaner;
+  }
+
+  public PurgeTask purgeProject(long projectId) {
+    cleanHistoricalData(projectId);
+    deleteAbortedBuilds(projectId);
+    deleteFileHistory(projectId);
+    if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) {
+      deleteDirectoryHistory(projectId);
+    }
+    purgeProjectResources(projectId);
+    return this;
+  }
+
+  public PurgeTask deleteProject(long projectId) {
+    purgeDao.deleteProject(projectId);
+    return this;
+  }
+
+  private void cleanHistoricalData(long projectId) {
+    try {
+      LOG.debug("Clean project historical data [id=" + projectId + "]");
+      periodCleaner.purge(projectId);
+    } catch (Exception e) {
+      // purge errors must no fail the batch
+      LOG.error("Fail to clean project historical data [id=" + projectId + "]", e);
+    }
+  }
+
+  private void purgeProjectResources(long projectId) {
+    try {
+      LOG.debug("Purge project [id=" + projectId + "]");
+      purgeDao.purgeProject(projectId);
+    } catch (Exception e) {
+      // purge errors must no fail the batch
+      LOG.error("Fail to purge project [id=" + projectId + "]", e);
+    }
+  }
+
+  private void deleteDirectoryHistory(long projectId) {
+    try {
+      LOG.debug("Delete historical data of directories [id=" + projectId + "]");
+      PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
+        .setRootProjectId(projectId)
+        .setIslast(false)
+        .setScopes(new String[]{Scopes.DIRECTORY});
+      purgeDao.deleteSnapshots(query);
+    } catch (Exception e) {
+      // purge errors must no fail the batch
+      LOG.error("Fail to delete historical data of directories [id=" + projectId + "]", e);
+    }
+  }
+
+  private void deleteFileHistory(long projectId) {
+    try {
+      LOG.debug("Delete historical data of files [id=" + projectId + "]");
+      PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
+        .setRootProjectId(projectId)
+        .setIslast(false)
+        .setScopes(new String[]{Scopes.FILE});
+      purgeDao.deleteSnapshots(query);
+    } catch (Exception e) {
+      // purge errors must no fail the batch
+      LOG.error("Fail to delete historical data of files [id=" + projectId + "]", e);
+    }
+  }
+
+  private void deleteAbortedBuilds(long projectId) {
+    try {
+      LOG.debug("Delete aborted builds [id=" + projectId + "]");
+      PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
+        .setRootProjectId(projectId)
+        .setIslast(false)
+        .setStatus(new String[]{"U"});
+      purgeDao.deleteSnapshots(query);
+    } catch (Exception e) {
+      // purge errors must no fail the batch
+      LOG.error("Fail to delete historical aborted builds [id=" + projectId + "]", e);
+    }
+  }
+}
index 4bbc9d38807c974641452151483e8082e1aa15e0..ba8150cdbd62e18f9d91f255970d9d1de149cb67 100644 (file)
  */
 package org.sonar.plugins.dbcleaner;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.Properties;
-import org.sonar.api.Property;
 import org.sonar.api.batch.PostJob;
 import org.sonar.api.batch.SensorContext;
-import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Project;
-import org.sonar.api.resources.Scopes;
 import org.sonar.core.NotDryRun;
-import org.sonar.core.purge.PurgeDao;
-import org.sonar.core.purge.PurgeSnapshotQuery;
-import org.sonar.plugins.dbcleaner.api.DbCleanerConstants;
-import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner;
+import org.sonar.plugins.dbcleaner.api.PurgeTask;
 
-@Properties({
-  @Property(
-    key = DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY,
-    defaultValue = "false",
-    name = "Clean history data of directories/packages")
-})
 @NotDryRun
 public class ProjectPurgePostJob implements PostJob {
 
-  private static final Logger LOG = LoggerFactory.getLogger(ProjectPurgePostJob.class);
+  private PurgeTask purgeTask;
 
-  private PurgeDao purgeDao;
-  private Settings settings;
-  private DefaultPeriodCleaner periodCleaner;
-
-  public ProjectPurgePostJob(PurgeDao purgeDao, Settings settings, DefaultPeriodCleaner periodCleaner) {
-    this.purgeDao = purgeDao;
-    this.settings = settings;
-    this.periodCleaner = periodCleaner;
+  public ProjectPurgePostJob(PurgeTask purgeTask) {
+    this.purgeTask = purgeTask;
   }
 
   public void executeOn(final Project project, SensorContext context) {
-    long projectId = (long) project.getId();
-
-    cleanHistoricalData(projectId);
-    deleteAbortedBuilds(projectId);
-    deleteFileHistory(projectId);
-    if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) {
-      deleteDirectoryHistory(projectId);
-    }
-    purgeProject(projectId);
-  }
-
-  private void cleanHistoricalData(long projectId) {
-    try {
-      LOG.debug("Clean project historical data");
-      periodCleaner.purge(projectId);
-    } catch (Exception e) {
-      // purge errors must no fail the batch
-      LOG.error("Fail to clean project historical data", e);
-    }
-  }
-
-  private void purgeProject(long projectId) {
-    try {
-      LOG.debug("Purge project");
-      purgeDao.purgeProject(projectId);
-    } catch (Exception e) {
-      // purge errors must no fail the batch
-      LOG.error("Fail to purge project", e);
-    }
-  }
-
-  private void deleteDirectoryHistory(long projectId) {
-    try {
-      LOG.debug("Delete historical data of directories");
-      PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
-        .setRootProjectId(projectId)
-        .setIslast(false)
-        .setScopes(new String[]{Scopes.DIRECTORY});
-      purgeDao.deleteSnapshots(query);
-    } catch (Exception e) {
-      // purge errors must no fail the batch
-      LOG.error("Fail to delete historical data of directories", e);
-    }
-  }
-
-  private void deleteFileHistory(long projectId) {
-    try {
-      LOG.debug("Delete historical data of files");
-      PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
-        .setRootProjectId(projectId)
-        .setIslast(false)
-        .setScopes(new String[]{Scopes.FILE});
-      purgeDao.deleteSnapshots(query);
-    } catch (Exception e) {
-      // purge errors must no fail the batch
-      LOG.error("Fail to delete historical data of files", e);
-    }
-  }
-
-  private void deleteAbortedBuilds(long projectId) {
-    try {
-      LOG.debug("Delete aborted builds");
-      PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
-        .setRootProjectId(projectId)
-        .setIslast(false)
-        .setStatus(new String[]{"U"});
-      purgeDao.deleteSnapshots(query);
-    } catch (Exception e) {
-      // purge errors must no fail the batch
-      LOG.error("Fail to delete historical aborted builds", e);
-    }
+    purgeTask.purgeProject((long) project.getId());
   }
 }
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeTask.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeTask.java
new file mode 100644 (file)
index 0000000..326143d
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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.api;
+
+import org.sonar.api.BatchExtension;
+
+/**
+ * @since 2.14
+ */
+public interface PurgeTask extends BatchExtension {
+  PurgeTask purgeProject(long projectId);
+  PurgeTask deleteProject(long projectId);
+}
index e46d3bb44bb8523d7741ae760ea332cb7781d155..30476ece23ffd5ee68e61a494a50ddb93acdbf6e 100644 (file)
@@ -23,12 +23,11 @@ import org.junit.Test;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
-import static org.hamcrest.number.OrderingComparisons.greaterThan;
 
 public class DbCleanerPluginTest {
 
   @Test
   public void shouldGetExtensions() {
-    assertThat(new DbCleanerPlugin().getExtensions().size(), is(2));
+    assertThat(new DbCleanerPlugin().getExtensions().size(), is(3));
   }
 }
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java
new file mode 100644 (file)
index 0000000..f40c61b
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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;
+
+import org.apache.commons.lang.ArrayUtils;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.junit.Test;
+import org.sonar.api.config.PropertyDefinitions;
+import org.sonar.api.config.Settings;
+import org.sonar.core.purge.PurgeDao;
+import org.sonar.core.purge.PurgeSnapshotQuery;
+import org.sonar.plugins.dbcleaner.api.DbCleanerConstants;
+import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner;
+
+import static org.mockito.Mockito.*;
+
+public class DefaultPurgeTaskTest {
+  @Test
+  public void shouldNotDeleteHistoricalDataOfDirectoriesByDefault() {
+    PurgeDao purgeDao = mock(PurgeDao.class);
+    Settings settings = new Settings(new PropertyDefinitions(DefaultPurgeTask.class));
+    DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, settings, mock(DefaultPeriodCleaner.class));
+
+    task.purgeProject(1L);
+
+    verify(purgeDao, never()).deleteSnapshots(argThat(newDirectoryQueryMatcher()));
+  }
+
+  @Test
+  public void shouldDeleteHistoricalDataOfDirectories() {
+    PurgeDao purgeDao = mock(PurgeDao.class);
+    Settings settings = new Settings(new PropertyDefinitions(DefaultPurgeTask.class));
+    settings.setProperty(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY, "true");
+    DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, settings, mock(DefaultPeriodCleaner.class));
+
+    task.purgeProject(1L);
+
+    verify(purgeDao, times(1)).deleteSnapshots(argThat(newDirectoryQueryMatcher()));
+  }
+
+  @Test
+  public void shouldNotFailOnErrors() {
+    PurgeDao purgeDao = mock(PurgeDao.class);
+    when(purgeDao.purgeProject(anyLong())).thenThrow(new RuntimeException());
+    DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, new Settings(), mock(DefaultPeriodCleaner.class));
+
+    task.purgeProject(1L);
+
+    verify(purgeDao).purgeProject(anyLong());
+  }
+
+  private BaseMatcher<PurgeSnapshotQuery> newDirectoryQueryMatcher() {
+    return new BaseMatcher<PurgeSnapshotQuery>() {
+      public boolean matches(Object o) {
+        return ArrayUtils.contains(((PurgeSnapshotQuery) o).getScopes(), "DIR");
+      }
+
+      public void describeTo(Description description) {
+        description.appendText("Query on scope DIR");
+      }
+    };
+  }
+}
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJobTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJobTest.java
deleted file mode 100644 (file)
index ffc6cdf..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 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;
-
-import org.apache.commons.lang.ArrayUtils;
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.junit.Test;
-import org.sonar.api.batch.SensorContext;
-import org.sonar.api.config.PropertyDefinitions;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Project;
-import org.sonar.core.purge.PurgeDao;
-import org.sonar.core.purge.PurgeSnapshotQuery;
-import org.sonar.plugins.dbcleaner.api.DbCleanerConstants;
-import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner;
-
-import static org.mockito.Mockito.*;
-
-public class ProjectPurgePostJobTest {
-  @Test
-  public void shouldNotDeleteHistoricalDataOfDirectoriesByDefault() {
-    PurgeDao purgeDao = mock(PurgeDao.class);
-    Settings settings = new Settings(new PropertyDefinitions(ProjectPurgePostJob.class));
-    ProjectPurgePostJob job = new ProjectPurgePostJob(purgeDao, settings, mock(DefaultPeriodCleaner.class));
-
-    job.executeOn(newProject(), mock(SensorContext.class));
-
-    verify(purgeDao, never()).deleteSnapshots(argThat(newDirectoryQueryMatcher()));
-  }
-
-  @Test
-  public void shouldDeleteHistoricalDataOfDirectories() {
-    PurgeDao purgeDao = mock(PurgeDao.class);
-    Settings settings = new Settings(new PropertyDefinitions(ProjectPurgePostJob.class));
-    settings.setProperty(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY, "true");
-    ProjectPurgePostJob job = new ProjectPurgePostJob(purgeDao, settings, mock(DefaultPeriodCleaner.class));
-
-    job.executeOn(newProject(), mock(SensorContext.class));
-
-    verify(purgeDao, times(1)).deleteSnapshots(argThat(newDirectoryQueryMatcher()));
-  }
-
-  @Test
-  public void shouldNotFailOnErrors() {
-    PurgeDao purgeDao = mock(PurgeDao.class);
-    when(purgeDao.purgeProject(anyInt())).thenThrow(new RuntimeException());
-      
-    ProjectPurgePostJob job = new ProjectPurgePostJob(purgeDao, new Settings(), mock(DefaultPeriodCleaner.class));
-
-    job.executeOn(newProject(), mock(SensorContext.class));
-
-    verify(purgeDao).purgeProject(anyInt());
-  }
-
-  private BaseMatcher<PurgeSnapshotQuery> newDirectoryQueryMatcher() {
-    return new BaseMatcher<PurgeSnapshotQuery>() {
-      public boolean matches(Object o) {
-        return ArrayUtils.contains(((PurgeSnapshotQuery) o).getScopes(), "DIR");
-      }
-
-      public void describeTo(Description description) {
-        description.appendText("Query on scope DIR");
-      }
-    };
-  }
-
-  private Project newProject() {
-    Project project = new Project("foo");
-    project.setId(1);
-    return project;
-  }
-
-}