From f5ff50a3a00fac3e9d7237b653771b812b01aa3e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 27 Jan 2012 16:48:54 +0100 Subject: [PATCH] SONAR-2757 extract purge task into a dedicated component --- .../plugins/dbcleaner/DbCleanerPlugin.java | 3 +- .../plugins/dbcleaner/DefaultPurgeTask.java | 133 ++++++++++++++++++ .../dbcleaner/ProjectPurgePostJob.java | 101 +------------ .../plugins/dbcleaner/api/PurgeTask.java | 30 ++++ .../dbcleaner/DbCleanerPluginTest.java | 3 +- ...JobTest.java => DefaultPurgeTaskTest.java} | 32 ++--- 6 files changed, 181 insertions(+), 121 deletions(-) create mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java create mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeTask.java rename plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/{ProjectPurgePostJobTest.java => DefaultPurgeTaskTest.java} (68%) 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 ca35757b6e0..0155f2c3187 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 @@ -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 index 00000000000..0027fcc3bf4 --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java @@ -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); + } + } +} diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJob.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJob.java index 4bbc9d38807..ba8150cdbd6 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJob.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJob.java @@ -19,113 +19,22 @@ */ 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 index 00000000000..326143d8d8e --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeTask.java @@ -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); +} diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java index e46d3bb44bb..30476ece23f 100644 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java @@ -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/ProjectPurgePostJobTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java similarity index 68% rename from plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJobTest.java rename to plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java index ffc6cdf4c5d..f40c61b5fb2 100644 --- 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/DefaultPurgeTaskTest.java @@ -23,10 +23,8 @@ 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; @@ -34,14 +32,14 @@ import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner; import static org.mockito.Mockito.*; -public class ProjectPurgePostJobTest { +public class DefaultPurgeTaskTest { @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)); + Settings settings = new Settings(new PropertyDefinitions(DefaultPurgeTask.class)); + DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, settings, mock(DefaultPeriodCleaner.class)); - job.executeOn(newProject(), mock(SensorContext.class)); + task.purgeProject(1L); verify(purgeDao, never()).deleteSnapshots(argThat(newDirectoryQueryMatcher())); } @@ -49,11 +47,11 @@ public class ProjectPurgePostJobTest { @Test public void shouldDeleteHistoricalDataOfDirectories() { PurgeDao purgeDao = mock(PurgeDao.class); - Settings settings = new Settings(new PropertyDefinitions(ProjectPurgePostJob.class)); + Settings settings = new Settings(new PropertyDefinitions(DefaultPurgeTask.class)); settings.setProperty(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY, "true"); - ProjectPurgePostJob job = new ProjectPurgePostJob(purgeDao, settings, mock(DefaultPeriodCleaner.class)); + DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, settings, mock(DefaultPeriodCleaner.class)); - job.executeOn(newProject(), mock(SensorContext.class)); + task.purgeProject(1L); verify(purgeDao, times(1)).deleteSnapshots(argThat(newDirectoryQueryMatcher())); } @@ -61,13 +59,12 @@ public class ProjectPurgePostJobTest { @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)); + when(purgeDao.purgeProject(anyLong())).thenThrow(new RuntimeException()); + DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, new Settings(), mock(DefaultPeriodCleaner.class)); - job.executeOn(newProject(), mock(SensorContext.class)); + task.purgeProject(1L); - verify(purgeDao).purgeProject(anyInt()); + verify(purgeDao).purgeProject(anyLong()); } private BaseMatcher newDirectoryQueryMatcher() { @@ -81,11 +78,4 @@ public class ProjectPurgePostJobTest { } }; } - - private Project newProject() { - Project project = new Project("foo"); - project.setId(1); - return project; - } - } -- 2.39.5