aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-02-20 22:05:03 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-02-20 22:05:03 +0100
commit726c17ed044d5e23a4ee1465a8a8c487ac1a9646 (patch)
tree8524a33e71c1ade6ee20c777cc2a8155b3d85d1a /plugins
parentc62f38d64c56ef9215e78b9adeb224cd76bfa4be (diff)
downloadsonarqube-726c17ed044d5e23a4ee1465a8a8c487ac1a9646.tar.gz
sonarqube-726c17ed044d5e23a4ee1465a8a8c487ac1a9646.zip
Add unit tests and fix some quality flaws
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java10
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/package-info.java25
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleanerTest.java77
3 files changed, 109 insertions, 3 deletions
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
index 5c092c61c04..efc9ff49d64 100644
--- 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
@@ -19,6 +19,7 @@
*/
package org.sonar.plugins.dbcleaner.period;
+import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.config.Settings;
@@ -47,10 +48,13 @@ public class DefaultPeriodCleaner implements PeriodCleaner {
}
public void purge(long projectId) {
- List<PurgeableSnapshotDto> history = selectProjectSnapshots(projectId);
+ doPurge(projectId, new Filters(settings).getFilters());
+ }
- Filters filters = new Filters(settings);
- for (Filter filter : filters.getFilters()) {
+ @VisibleForTesting
+ void doPurge(long projectId, List<Filter> filters) {
+ List<PurgeableSnapshotDto> history = selectProjectSnapshots(projectId);
+ for (Filter filter : filters) {
filter.log();
delete(filter.filter(history));
}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/package-info.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/package-info.java
new file mode 100644
index 00000000000..1e098ea24f8
--- /dev/null
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * 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
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.plugins.dbcleaner.period;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleanerTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleanerTest.java
new file mode 100644
index 00000000000..ca806d648fc
--- /dev/null
+++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleanerTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.period;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.sonar.api.config.Settings;
+import org.sonar.core.purge.PurgeDao;
+import org.sonar.core.purge.PurgeSnapshotQuery;
+import org.sonar.core.purge.PurgeableSnapshotDto;
+
+import java.util.Arrays;
+import java.util.Date;
+
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.*;
+
+public class DefaultPeriodCleanerTest {
+
+
+ @Test
+ public void doPurge() {
+ PurgeDao dao = mock(PurgeDao.class);
+ when(dao.selectPurgeableSnapshots(123L)).thenReturn(Arrays.asList(
+ new PurgeableSnapshotDto().setSnapshotId(999L).setDate(new Date())));
+ Filter filter1 = newLazyFilter();
+ Filter filter2 = newLazyFilter();
+
+ DefaultPeriodCleaner cleaner = new DefaultPeriodCleaner(dao, mock(Settings.class));
+ cleaner.doPurge(123L, Arrays.asList(filter1, filter2));
+
+ verify(filter1).log();
+ verify(filter2).log();
+ verify(dao, times(2)).deleteSnapshots(argThat(newRootSnapshotQuery()));
+ }
+
+ private BaseMatcher<PurgeSnapshotQuery> newRootSnapshotQuery() {
+ return new BaseMatcher<PurgeSnapshotQuery>() {
+ public boolean matches(Object o) {
+ return ((PurgeSnapshotQuery) o).getRootSnapshotId() == 999L;
+ }
+
+ public void describeTo(Description description) {
+ }
+ };
+ }
+
+ private Filter newLazyFilter() {
+ Filter filter1 = mock(Filter.class);
+ when(filter1.filter(anyListOf(PurgeableSnapshotDto.class))).thenAnswer(new Answer<Object>() {
+ public Object answer(InvocationOnMock invocation) throws Throwable {
+ return invocation.getArguments()[0];
+ }
+ });
+ return filter1;
+ }
+}