aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java4
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/properties/ProjectSettingsFactory.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/component/SnapshotTesting.java3
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepMediumTest.java151
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/properties/ProjectSettingsFactoryTest.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java12
-rw-r--r--sonar-core/src/main/java/org/sonar/core/computation/dbcleaner/DefaultPurgeTask.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/purge/PurgeConfiguration.java2
9 files changed, 174 insertions, 8 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java
index 6c6b7f9a280..86e953a638c 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/DataCleanerStep.java
@@ -46,8 +46,8 @@ public class DataCleanerStep implements ComputationStep {
public void execute(DbSession session, AnalysisReportDto report, ComponentDto project) {
Long projectId = project.getId();
- Settings settings = projectSettingsFactory.newProjectSettings(projectId, session);
- PurgeConfiguration purgeConfiguration = newDefaultPurgeConfiguration(projectId, settings);
+ Settings settings = projectSettingsFactory.newProjectSettings(session, projectId);
+ PurgeConfiguration purgeConfiguration = newDefaultPurgeConfiguration(settings, projectId);
purgeTask.purge(session, purgeConfiguration, settings);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/properties/ProjectSettingsFactory.java b/server/sonar-server/src/main/java/org/sonar/server/properties/ProjectSettingsFactory.java
index 782266222df..3a9793e7fb9 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/properties/ProjectSettingsFactory.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/properties/ProjectSettingsFactory.java
@@ -41,7 +41,7 @@ public class ProjectSettingsFactory implements ServerComponent {
this.settings = settings;
}
- public Settings newProjectSettings(long projectId, DbSession session) {
+ public Settings newProjectSettings(DbSession session, long projectId) {
List<PropertyDto> propertyList = dao.selectProjectProperties(projectId, session);
return new ProjectSettings(settings, getPropertyMap(propertyList));
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/SnapshotTesting.java b/server/sonar-server/src/test/java/org/sonar/server/component/SnapshotTesting.java
index ee5ac9102a1..eb27722379a 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/component/SnapshotTesting.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/component/SnapshotTesting.java
@@ -40,6 +40,9 @@ public class SnapshotTesting {
return new SnapshotDto()
.setResourceId(project.getId())
.setRootProjectId(project.getId())
+ .setStatus(SnapshotDto.STATUS_PROCESSED)
+ .setQualifier(project.qualifier())
+ .setScope(project.scope())
.setLast(true);
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepMediumTest.java
new file mode 100644
index 00000000000..23e886589b0
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepMediumTest.java
@@ -0,0 +1,151 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.computation;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.core.component.ComponentDto;
+import org.sonar.core.component.SnapshotDto;
+import org.sonar.core.computation.db.AnalysisReportDto;
+import org.sonar.core.computation.db.AnalysisReportDto.Status;
+import org.sonar.core.computation.dbcleaner.DbCleanerConstants;
+import org.sonar.core.computation.dbcleaner.ProjectPurgeTask;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.properties.PropertyDto;
+import org.sonar.server.component.ComponentTesting;
+import org.sonar.server.component.SnapshotTesting;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.issue.index.IssueIndex;
+import org.sonar.server.properties.ProjectSettingsFactory;
+import org.sonar.server.search.IndexClient;
+import org.sonar.server.tester.ServerTester;
+
+import java.util.Date;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DataCleanerStepMediumTest {
+
+ @ClassRule
+ public static ServerTester tester = new ServerTester();
+
+ private DataCleanerStep sut;
+ private DbClient dbClient;
+ private DbSession dbSession;
+ private IndexClient indexClient;
+ private ProjectSettingsFactory projectSettingsFactory;
+ private ProjectPurgeTask purgeTask;
+
+ @Before
+ public void before() throws Exception {
+ this.dbClient = tester.get(DbClient.class);
+ this.dbSession = dbClient.openSession(false);
+
+ this.indexClient = tester.get(IndexClient.class);
+ this.projectSettingsFactory = tester.get(ProjectSettingsFactory.class);
+ this.purgeTask = tester.get(ProjectPurgeTask.class);
+
+ this.sut = new DataCleanerStep(projectSettingsFactory, purgeTask, indexClient.get(IssueIndex.class));
+ }
+
+ @After
+ public void after() throws Exception {
+ MyBatis.closeQuietly(dbSession);
+ }
+
+ @Test
+ public void use_global_settings_when_no_other_specified() throws Exception {
+ // ARRANGE
+ Date now = new Date();
+ Date aWeekAgo = DateUtils.addDays(now, -7);
+
+ ComponentDto project = ComponentTesting.newProjectDto()
+ .setId(1L)
+ .setKey("123")
+ .setCreatedAt(aWeekAgo)
+ .setUpdatedAt(aWeekAgo);
+ dbClient.componentDao().insert(dbSession, project);
+
+ SnapshotDto snapshot = SnapshotTesting.createForProject(project)
+ .setCreatedAt(aWeekAgo)
+ .setUpdatedAt(aWeekAgo);
+ dbClient.snapshotDao().insert(dbSession, snapshot);
+
+ AnalysisReportDto report = AnalysisReportDto.newForTests(1L)
+ .setProjectKey("123")
+ .setSnapshotId(snapshot.getId())
+ .setStatus(Status.PENDING);
+ dbClient.analysisReportDao().insert(dbSession, report);
+
+ dbClient.propertiesDao().setProperty(new PropertyDto().setKey(DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS).setValue("52"));
+ dbSession.commit();
+
+ // ACT
+ sut.execute(dbSession, report, project);
+ dbSession.commit();
+
+ // ASSERT
+ assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshot.getId())).isNotNull();
+ }
+
+ @Test
+ public void use_project_settings_if_specified() throws Exception {
+ // ARRANGE
+ Date now = new Date();
+ Date twoWeeksAgo = DateUtils.addDays(now, -2 * 7);
+ Date aLongTimeAgo = DateUtils.addDays(now, -365 * 7);
+
+ ComponentDto project = ComponentTesting.newProjectDto()
+ .setKey("123")
+ .setCreatedAt(aLongTimeAgo)
+ .setUpdatedAt(aLongTimeAgo);
+ dbClient.componentDao().insert(dbSession, project);
+
+ SnapshotDto snapshot = SnapshotTesting.createForProject(project)
+ .setCreatedAt(twoWeeksAgo)
+ .setUpdatedAt(twoWeeksAgo)
+ .setStatus("P")
+ .setLast(true);
+
+ dbClient.snapshotDao().insert(dbSession, snapshot);
+
+ AnalysisReportDto report = new AnalysisReportDto()
+ .setProjectKey("123")
+ .setSnapshotId(snapshot.getId())
+ .setStatus(Status.PENDING);
+ dbClient.analysisReportDao().insert(dbSession, report);
+
+ dbClient.propertiesDao().setProperty(new PropertyDto().setKey(DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS).setValue("4"));
+ dbClient.propertiesDao().setProperty(new PropertyDto().setKey(DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS).setValue("1").setResourceId(project.getId()));
+ dbSession.commit();
+
+ // ACT
+ sut.execute(dbSession, report, project);
+ dbSession.commit();
+
+ // ASSERT
+ assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshot.getId())).isNull();
+ }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java
index 562a9ad8e42..ad016a66ec1 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/DataCleanerStepTest.java
@@ -53,7 +53,7 @@ public class DataCleanerStepTest {
this.issueIndex = mock(IssueIndex.class);
this.settings = mock(ProjectSettings.class);
this.projectSettingsFactory = mock(ProjectSettingsFactory.class);
- when(projectSettingsFactory.newProjectSettings(anyLong(), any(DbSession.class))).thenReturn(settings);
+ when(projectSettingsFactory.newProjectSettings(any(DbSession.class), anyLong())).thenReturn(settings);
when(settings.getInt(any(String.class))).thenReturn(123);
this.sut = new DataCleanerStep(projectSettingsFactory, purgeTask, issueIndex);
@@ -66,7 +66,7 @@ public class DataCleanerStepTest {
sut.execute(mock(DbSession.class), report, project);
- verify(projectSettingsFactory).newProjectSettings(anyLong(), any(DbSession.class));
+ verify(projectSettingsFactory).newProjectSettings(any(DbSession.class), anyLong());
verify(purgeTask).purge(any(DbSession.class), any(PurgeConfiguration.class), any(Settings.class));
verify(issueIndex).deleteClosedIssuesOfProjectBefore(anyString(), any(Date.class));
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/properties/ProjectSettingsFactoryTest.java b/server/sonar-server/src/test/java/org/sonar/server/properties/ProjectSettingsFactoryTest.java
index 45c72b01a9c..7a02b665a6c 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/properties/ProjectSettingsFactoryTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/properties/ProjectSettingsFactoryTest.java
@@ -50,7 +50,7 @@ public class ProjectSettingsFactoryTest {
@Test
public void newProjectSettings_returns_a_ProjectSettings() throws Exception {
- Settings projectSettings = sut.newProjectSettings(1L, mock(DbSession.class));
+ Settings projectSettings = sut.newProjectSettings(mock(DbSession.class), 1L);
assertThat(projectSettings).isInstanceOf(ProjectSettings.class);
}
diff --git a/sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java b/sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java
index 0a25f91081a..719cd46ba12 100644
--- a/sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java
+++ b/sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java
@@ -243,4 +243,16 @@ public class ComponentDto extends Dto<String> implements Component {
public int hashCode() {
return id.hashCode();
}
+
+ @Override
+ public ComponentDto setCreatedAt(Date datetime) {
+ super.setCreatedAt(datetime);
+ return this;
+ }
+
+ @Override
+ public ComponentDto setUpdatedAt(Date datetime) {
+ super.setUpdatedAt(datetime);
+ return this;
+ }
}
diff --git a/sonar-core/src/main/java/org/sonar/core/computation/dbcleaner/DefaultPurgeTask.java b/sonar-core/src/main/java/org/sonar/core/computation/dbcleaner/DefaultPurgeTask.java
index b405f04ae66..51128905fa0 100644
--- a/sonar-core/src/main/java/org/sonar/core/computation/dbcleaner/DefaultPurgeTask.java
+++ b/sonar-core/src/main/java/org/sonar/core/computation/dbcleaner/DefaultPurgeTask.java
@@ -103,6 +103,6 @@ public class DefaultPurgeTask implements PurgeTask {
}
public PurgeConfiguration newPurgeConfigurationOnResource(long resourceId) {
- return newDefaultPurgeConfiguration(resourceId, settings);
+ return newDefaultPurgeConfiguration(settings, resourceId);
}
}
diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeConfiguration.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeConfiguration.java
index 1078be70dbd..d3fc9c1c4bd 100644
--- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeConfiguration.java
+++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeConfiguration.java
@@ -49,7 +49,7 @@ public class PurgeConfiguration {
this.system2 = system2;
}
- public static PurgeConfiguration newDefaultPurgeConfiguration(long resourceId, Settings settings) {
+ public static PurgeConfiguration newDefaultPurgeConfiguration(Settings settings, long resourceId) {
String[] scopes = new String[] {Scopes.FILE};
if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) {
scopes = new String[] {Scopes.DIRECTORY, Scopes.FILE};