aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-10-27 17:22:42 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-10-29 12:22:53 +0100
commit969ebd8714a717d10932858fe7bfc3afe36fb73e (patch)
tree9e4b1fb51462172452575bb988a621e11ae038fd /sonar-db
parentcf645d53f903ebfa6a6c36001a59636a28b8e389 (diff)
downloadsonarqube-969ebd8714a717d10932858fe7bfc3afe36fb73e.tar.gz
sonarqube-969ebd8714a717d10932858fe7bfc3afe36fb73e.zip
SONAR-6356 Always feed the 'since previous version' period
Since previous version period use the first snapshot when no previous version exist
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java14
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java19
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java3
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml14
-rw-r--r--sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java7
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java23
6 files changed, 66 insertions, 14 deletions
diff --git a/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java b/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java
index 2765f3d8764..fbb06254223 100644
--- a/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java
+++ b/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java
@@ -22,13 +22,13 @@ package org.sonar.core.timemachine;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
-import java.util.Locale;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.config.Settings;
import org.sonar.api.i18n.I18n;
import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Locale.ENGLISH;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_DATE;
import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_DAYS;
@@ -47,10 +47,6 @@ public class Periods {
this.i18n = i18n;
}
- private static Locale getLocale() {
- return Locale.ENGLISH;
- }
-
@CheckForNull
private static String convertDate(@Nullable Date date) {
if (date != null) {
@@ -129,9 +125,13 @@ public class Periods {
}
private String labelForPreviousVersion(@Nullable String param, @Nullable String date, boolean shortLabel) {
- if (param == null) {
+ if (param == null && date == null) {
return label("since_previous_version", shortLabel);
}
+ if (param == null) {
+ // Special case when no snapshot for previous version is found. The first analysis is then returned -> Display only the date.
+ return label("since_previous_version_with_only_date", shortLabel, date);
+ }
if (date == null) {
return label("since_previous_version_detailed", shortLabel, param);
}
@@ -143,7 +143,7 @@ public class Periods {
if (shortLabel) {
msgKey += ".short";
}
- return i18n.message(getLocale(), msgKey, null, parameters);
+ return i18n.message(ENGLISH, msgKey, null, parameters);
}
private static class PeriodParameters {
diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java
index 55d3fbbb630..575d5ac26a0 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java
@@ -26,6 +26,7 @@ import java.util.Collection;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import org.apache.ibatis.session.RowBounds;
import org.sonar.api.resources.Scopes;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
@@ -35,6 +36,10 @@ import static com.google.common.collect.FluentIterable.from;
public class SnapshotDao implements Dao {
+ public static boolean isLast(SnapshotDto snapshotTested, @Nullable SnapshotDto previousLastSnapshot) {
+ return previousLastSnapshot == null || previousLastSnapshot.getCreatedAt() < snapshotTested.getCreatedAt();
+ }
+
@CheckForNull
public SnapshotDto selectById(DbSession session, long id) {
return mapper(session).selectByKey(id);
@@ -69,6 +74,12 @@ public class SnapshotDao implements Dao {
return mapper(session).selectPreviousVersionSnapshots(componentId, lastVersion);
}
+ @CheckForNull
+ public SnapshotDto selectOldestSnapshot(DbSession session, long componentId) {
+ List<SnapshotDto> snapshotDtos = mapper(session).selectOldestSnapshots(componentId, new RowBounds(0, 1));
+ return snapshotDtos.isEmpty() ? null : snapshotDtos.get(0);
+ }
+
public List<SnapshotDto> selectSnapshotAndChildrenOfProjectScope(DbSession session, long snapshotId) {
return mapper(session).selectSnapshotAndChildrenOfScope(snapshotId, Scopes.PROJECT);
}
@@ -89,10 +100,6 @@ public class SnapshotDao implements Dao {
return mapper(session).updateSnapshotAndChildrenLastFlag(rootId, pathRootId, path, isLast);
}
- public static boolean isLast(SnapshotDto snapshotTested, @Nullable SnapshotDto previousLastSnapshot) {
- return previousLastSnapshot == null || previousLastSnapshot.getCreatedAt() < snapshotTested.getCreatedAt();
- }
-
public SnapshotDto insert(DbSession session, SnapshotDto item) {
mapper(session).insert(item);
return item;
@@ -111,8 +118,8 @@ public class SnapshotDao implements Dao {
@CheckForNull
public ViewsSnapshotDto selectSnapshotBefore(long componentId, long date, DbSession dbSession) {
return from(mapper(dbSession).selectSnapshotBefore(componentId, date))
- .first()
- .orNull();
+ .first()
+ .orNull();
}
@CheckForNull
diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java
index a77633f7d1a..5cc7e177d5b 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java
@@ -23,6 +23,7 @@ package org.sonar.db.component;
import java.util.List;
import javax.annotation.CheckForNull;
import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.session.RowBounds;
public interface SnapshotMapper {
@@ -40,6 +41,8 @@ public interface SnapshotMapper {
List<SnapshotDto> selectPreviousVersionSnapshots(@Param(value = "componentId") Long componentId, @Param(value = "lastVersion") String lastVersion);
+ List<SnapshotDto> selectOldestSnapshots(@Param(value = "componentId") Long componentId, RowBounds rowBounds);
+
List<SnapshotDto> selectSnapshotAndChildrenOfScope(@Param(value = "snapshot") Long resourceId, @Param(value = "scope") String scope);
int updateSnapshotAndChildrenLastFlagAndStatus(@Param(value = "root") Long rootId, @Param(value = "pathRootId") Long pathRootId,
diff --git a/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
index 01b89c7e1aa..597f033cc50 100644
--- a/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
+++ b/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
@@ -114,10 +114,22 @@
<include refid="snapshotColumns"/>
FROM snapshots s
INNER JOIN events e ON s.id = e.snapshot_id AND e.name &lt;&gt; #{lastVersion} AND e.category='Version'
- INNER JOIN projects p ON p.uuid=e.component_uuid AND p.id=#{componentId}
+ <where>
+ s.project_id=#{componentId}
+ </where>
ORDER BY e.event_date DESC
</select>
+ <select id="selectOldestSnapshots" parameterType="map" resultType="Snapshot">
+ SELECT
+ <include refid="snapshotColumns"/>
+ FROM snapshots s
+ <where>
+ s.project_id=#{componentId}
+ </where>
+ ORDER BY s.created_at ASC
+ </select>
+
<select id="selectSnapshotAndChildrenOfScope" parameterType="map" resultType="Snapshot">
select
<include refid="snapshotColumns"/>
diff --git a/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java b/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java
index fc1e34b98fe..f124e96d8ff 100644
--- a/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java
+++ b/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java
@@ -202,6 +202,13 @@ public class PeriodsTest {
}
@Test
+ public void return_since_previous_version_with_only_date_label_when_no_param_and_date_is_set() {
+ periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, STRING_DATE);
+
+ verify(i18n).message(any(Locale.class), eq("since_previous_version_with_only_date"), isNull(String.class), eq(STRING_DATE));
+ }
+
+ @Test
public void return_since_previous_version_detailed_abbreviation_when_param_and_date_are_set() {
periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, DATE);
diff --git a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java
index 9768a52e1c6..37675670851 100644
--- a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java
@@ -29,6 +29,7 @@ import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
+import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.test.DbTests;
@@ -36,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.component.SnapshotQuery.SORT_FIELD.BY_DATE;
import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.ASC;
import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.DESC;
+import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
@Category(DbTests.class)
public class SnapshotDaoTest {
@@ -43,6 +45,8 @@ public class SnapshotDaoTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
+ DbSession dbSession = db.getSession();
+
SnapshotDao underTest = db.getDbClient().snapshotDao();
@Test
@@ -173,6 +177,25 @@ public class SnapshotDaoTest {
}
@Test
+ public void select_first_snapshots() throws Exception {
+ ComponentDto project = ComponentTesting.newProjectDto();
+ db.getDbClient().componentDao().insert(dbSession, project);
+
+ db.getDbClient().snapshotDao().insert(dbSession,
+ newSnapshotForProject(project).setCreatedAt(5L),
+ newSnapshotForProject(project).setCreatedAt(2L),
+ newSnapshotForProject(project).setCreatedAt(1L)
+ );
+ dbSession.commit();
+
+ SnapshotDto dto = underTest.selectOldestSnapshot(dbSession, project.getId());
+ assertThat(dto).isNotNull();
+ assertThat(dto.getCreatedAt()).isEqualTo(1L);
+
+ assertThat(underTest.selectOldestSnapshot(dbSession, 123456789)).isNull();
+ }
+
+ @Test
public void insert() {
db.prepareDbUnit(getClass(), "empty.xml");