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);
}
}
--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
*/
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());
}
}
--- /dev/null
+/*
+ * 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);
+}
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));
}
}
--- /dev/null
+/*
+ * 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");
+ }
+ };
+ }
+}
+++ /dev/null
-/*
- * 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;
- }
-
-}