diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-01-30 13:01:19 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-01-30 13:01:19 +0100 |
commit | d9350849080811c8f53abb062a21d9a07d3a309d (patch) | |
tree | d8d2aa3c2521872cf02c363c54250c91b4a5fb4e /plugins/sonar-dbcleaner-plugin | |
parent | f8f391ca48e926ff8010fe465a131e41dc80daed (diff) | |
download | sonarqube-d9350849080811c8f53abb062a21d9a07d3a309d.tar.gz sonarqube-d9350849080811c8f53abb062a21d9a07d3a309d.zip |
SONAR-2807 fix support of period cleaner over multiple years
Diffstat (limited to 'plugins/sonar-dbcleaner-plugin')
2 files changed, 29 insertions, 6 deletions
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Interval.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Interval.java index 40e422f9faf..e8c40e184eb 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Interval.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Interval.java @@ -23,6 +23,7 @@ import com.google.common.collect.Lists; import org.apache.commons.lang.time.DateUtils; import org.sonar.core.purge.PurgeableSnapshotDto; +import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; @@ -46,6 +47,7 @@ final class Interval { List<Interval> intervals = Lists.newArrayList(); GregorianCalendar calendar = new GregorianCalendar(); + int lastYear = -1; int lastFieldValue = -1; Interval currentInterval = null; @@ -54,11 +56,13 @@ final class Interval { (snapshot.getDate().before(end) || DateUtils.isSameDay(end, snapshot.getDate()))) { calendar.setTime(snapshot.getDate()); int currentFieldValue = calendar.get(calendarField); - if (lastFieldValue != currentFieldValue) { + int currentYear = calendar.get(Calendar.YEAR); + if (lastYear!=currentYear || lastFieldValue != currentFieldValue) { currentInterval = new Interval(); intervals.add(currentInterval); } lastFieldValue = currentFieldValue; + lastYear = currentYear; if (currentInterval != null) { currentInterval.add(snapshot); } diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntervalTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntervalTest.java index b369383d1e3..99ca5a991ca 100644 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntervalTest.java +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/IntervalTest.java @@ -52,13 +52,32 @@ public class IntervalTest { assertThat(intervals.size(), is(3)); assertThat(intervals.get(0).count(), is(1)); - assertThat(month(intervals.get(0)), is(Calendar.APRIL)); + assertThat(calendarField(intervals.get(0), Calendar.MONTH), is(Calendar.APRIL)); assertThat(intervals.get(1).count(), is(2)); - assertThat(month(intervals.get(1)), is(Calendar.MAY)); + assertThat(calendarField(intervals.get(1), Calendar.MONTH), is(Calendar.MAY)); assertThat(intervals.get(2).count(), is(2)); - assertThat(month(intervals.get(2)), is(Calendar.JUNE)); + assertThat(calendarField(intervals.get(2), Calendar.MONTH), is(Calendar.JUNE)); + } + + @Test + public void shouldNotJoinMonthsOfDifferentYears() { + List<PurgeableSnapshotDto> snapshots = Arrays.asList( + createSnapshotWithDate(1L, "2010-04-03"), + createSnapshotWithDate(2L, "2011-04-13") + ); + + List<Interval> intervals = Interval.group(snapshots, DateUtils.parseDate("2010-01-01"), DateUtils.parseDate("2011-12-31"), Calendar.MONTH); + assertThat(intervals.size(), is(2)); + + assertThat(intervals.get(0).count(), is(1)); + assertThat(calendarField(intervals.get(0), Calendar.MONTH), is(Calendar.APRIL)); + assertThat(calendarField(intervals.get(0), Calendar.YEAR), is(2010)); + + assertThat(intervals.get(1).count(), is(1)); + assertThat(calendarField(intervals.get(1), Calendar.MONTH), is(Calendar.APRIL)); + assertThat(calendarField(intervals.get(1), Calendar.YEAR), is(2011)); } @Test @@ -75,7 +94,7 @@ public class IntervalTest { assertThat(intervals.get(0).get().get(0).getSnapshotId(), is(2L)); } - static int month(Interval interval) { + static int calendarField(Interval interval, int field) { if (interval.count() == 0) { return -1; } @@ -83,6 +102,6 @@ public class IntervalTest { PurgeableSnapshotDto first = interval.get().iterator().next(); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(first.getDate()); - return cal.get(Calendar.MONTH); + return cal.get(field); } } |