diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-08 12:50:28 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-08 12:50:28 +0000 |
commit | 4a7eae13c556369592da2ed726e4c8546b61a17a (patch) | |
tree | ee8c994cabd5877841e7a2ef227dad2fca6b0ddf | |
parent | 7e65399cc2b3fc91bfd14af6f8979bf9db9a83be (diff) | |
download | sonarqube-4a7eae13c556369592da2ed726e4c8546b61a17a.tar.gz sonarqube-4a7eae13c556369592da2ed726e4c8546b61a17a.zip |
SONAR-1937 fix calculation of new_violations : compare to a date instead of a snapshot
20 files changed, 155 insertions, 119 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index 58e60a08f7a..2f67910eb81 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -136,7 +136,7 @@ import java.util.List; ), @Property( key = "sonar.timemachine.variation4", - name = "Variation 4, only for projects", + name = "Variation 4", description = "To be defined. For the moment the number of days", project = true, global = false @@ -228,7 +228,7 @@ public class CorePlugin implements Plugin { extensions.add(TendencyDecorator.class); extensions.add(PastSnapshotFinderByDate.class); extensions.add(PastSnapshotFinderByDays.class); - extensions.add(PastSnapshotFinderByLastAnalysis.class); + extensions.add(PastSnapshotFinderByPreviousAnalysis.class); extensions.add(PastSnapshotFinderByVersion.class); extensions.add(PastMeasuresLoader.class); extensions.add(PastSnapshotFinder.class); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java index a51d5640247..5f981991dd2 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java @@ -20,6 +20,7 @@ package org.sonar.plugins.core.timemachine; import com.google.common.collect.*; +import org.apache.commons.lang.time.DateUtils; import org.sonar.api.batch.Decorator; import org.sonar.api.batch.DecoratorContext; import org.sonar.api.batch.DependedUpon; @@ -89,10 +90,10 @@ public class NewViolationsDecorator implements Decorator { private void saveNewViolations(DecoratorContext context) { Measure measure = new Measure(CoreMetrics.NEW_VIOLATIONS); - for (PastSnapshot variationSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { - int variationIndex = variationSnapshot.getIndex(); + for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { + int variationIndex = pastSnapshot.getIndex(); Collection<Measure> children = context.getChildrenMeasures(CoreMetrics.NEW_VIOLATIONS); - int count = countViolations(context.getViolations(), variationSnapshot.getDate()); + int count = countViolations(context.getViolations(), pastSnapshot.getTargetDate()); double sum = sumChildren(variationIndex, children) + count; measure.setVariation(variationIndex, sum); } @@ -103,9 +104,9 @@ public class NewViolationsDecorator implements Decorator { for (RulePriority priority : RulePriority.values()) { Metric metric = getMetricForSeverity(priority); Measure measure = new Measure(metric); - for (PastSnapshot variationSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { - int variationIndex = variationSnapshot.getIndex(); - int count = countViolations(violationsBySeverity.get(priority), variationSnapshot.getDate()); + for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { + int variationIndex = pastSnapshot.getIndex(); + int count = countViolations(violationsBySeverity.get(priority), pastSnapshot.getTargetDate()); Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.metric(metric)); double sum = sumChildren(variationIndex, children) + count; measure.setVariation(variationIndex, sum); @@ -132,9 +133,9 @@ public class NewViolationsDecorator implements Decorator { for (Rule rule : rules) { RuleMeasure measure = RuleMeasure.createForRule(CoreMetrics.NEW_VIOLATIONS, rule, null); measure.setRulePriority(ruleToLevel.get(rule)); - for (PastSnapshot variationSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { - int variationIndex = variationSnapshot.getIndex(); - int count = countViolations(violationsByRule.get(rule), variationSnapshot.getDate()); + for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { + int variationIndex = pastSnapshot.getIndex(); + int count = countViolations(violationsByRule.get(rule), pastSnapshot.getTargetDate()); double sum = sumChildren(variationIndex, childrenByRule.get(rule)) + count; measure.setVariation(variationIndex, sum); } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java index f4c0e545652..9b3f34aba7d 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java @@ -29,13 +29,24 @@ public class PastSnapshot { private int index; private String mode, modeParameter; private Snapshot projectSnapshot; + private Date targetDate; - public PastSnapshot(int index, String mode, Snapshot projectSnapshot) { - this.index = index; + public PastSnapshot(String mode, Date targetDate, Snapshot projectSnapshot) { this.mode = mode; + this.targetDate = targetDate; this.projectSnapshot = projectSnapshot; } + public PastSnapshot(String mode, Snapshot projectSnapshot) { + this.mode = mode; + this.projectSnapshot = projectSnapshot; + } + + public PastSnapshot setIndex(int index) { + this.index = index; + return this; + } + public int getIndex() { return index; } @@ -61,6 +72,14 @@ public class PastSnapshot { return this; } + public Integer getProjectSnapshotId() { + return (projectSnapshot!=null ? projectSnapshot.getId() : null); + } + + public Date getTargetDate() { + return targetDate; + } + @Override public String toString() { return ReflectionToStringBuilder.toString(this); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java index 92ca2715ec3..927af7ea361 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java @@ -22,30 +22,23 @@ package org.sonar.plugins.core.timemachine; import org.apache.commons.configuration.Configuration; import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchExtension; -import org.sonar.api.database.model.Snapshot; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class PastSnapshotFinder implements BatchExtension { - - public static final String LAST__ANALYSIS_MODE = "last_analysis"; - public static final String DATE_MODE = "date"; - public static final String VERSION_MODE = "version"; - public static final String DAYS_MODE = "days"; - private PastSnapshotFinderByDays finderByDays; private PastSnapshotFinderByVersion finderByVersion; private PastSnapshotFinderByDate finderByDate; - private PastSnapshotFinderByLastAnalysis finderByLastAnalysis; + private PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis; public PastSnapshotFinder(PastSnapshotFinderByDays finderByDays, PastSnapshotFinderByVersion finderByVersion, - PastSnapshotFinderByDate finderByDate, PastSnapshotFinderByLastAnalysis finderByLastAnalysis) { + PastSnapshotFinderByDate finderByDate, PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis) { this.finderByDays = finderByDays; this.finderByVersion = finderByVersion; this.finderByDate = finderByDate; - this.finderByLastAnalysis = finderByLastAnalysis; + this.finderByPreviousAnalysis = finderByPreviousAnalysis; } public PastSnapshot find(Configuration conf, int index) { @@ -57,62 +50,51 @@ public class PastSnapshotFinder implements BatchExtension { return null; } - PastSnapshot result = findByDays(index, property); + PastSnapshot result = findByDays(property); if (result == null) { - result = findByDate(index, property); + result = findByDate(property); if (result == null) { - result = findByLastAnalysis(index, property); + result = findByPreviousAnalysis(property); if (result == null) { - result = findByVersion(index, property); + result = findByVersion(property); } } } + + if (result != null) { + result.setIndex(index); + } + return result; } - private PastSnapshot findByLastAnalysis(int index, String property) { - if (StringUtils.equals(LAST__ANALYSIS_MODE, property)) { - Snapshot projectSnapshot = finderByLastAnalysis.findLastAnalysis(); - if (projectSnapshot != null) { - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - String date = format.format(projectSnapshot.getCreatedAt()); - return new PastSnapshot(index, LAST__ANALYSIS_MODE, projectSnapshot).setModeParameter(date); - } + private PastSnapshot findByPreviousAnalysis(String property) { + PastSnapshot pastSnapshot = null; + if (StringUtils.equals(PastSnapshotFinderByPreviousAnalysis.MODE, property)) { + pastSnapshot = finderByPreviousAnalysis.findByPreviousAnalysis(); } - return null; + return pastSnapshot; } - private PastSnapshot findByDate(int index, String property) { + private PastSnapshot findByDate(String property) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = format.parse(property); - Snapshot projectSnapshot = finderByDate.findByDate(date); - if (projectSnapshot != null) { - return new PastSnapshot(index, DATE_MODE, projectSnapshot).setModeParameter(property); - } - return null; + return finderByDate.findByDate(date); } catch (ParseException e) { return null; } } - private PastSnapshot findByVersion(int index, String property) { - Snapshot projectSnapshot = finderByVersion.findVersion(property); - if (projectSnapshot != null) { - return new PastSnapshot(index, VERSION_MODE, projectSnapshot).setModeParameter(property); - } - return null; + private PastSnapshot findByVersion(String property) { + return finderByVersion.findByVersion(property); } - private PastSnapshot findByDays(int index, String property) { + private PastSnapshot findByDays(String property) { try { int days = Integer.parseInt(property); - Snapshot projectSnapshot = finderByDays.findInDays(days); - if (projectSnapshot != null) { - return new PastSnapshot(index, DAYS_MODE, projectSnapshot).setModeParameter(String.valueOf(days)); - } - return null; + return finderByDays.findFromDays(days); } catch (NumberFormatException e) { return null; diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDate.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDate.java index 172b8b95843..d33ead7c16b 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDate.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDate.java @@ -23,10 +23,15 @@ import org.sonar.api.BatchExtension; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; public class PastSnapshotFinderByDate implements BatchExtension{ + + public static final String MODE = "date"; + + private Snapshot projectSnapshot; // TODO replace by PersistenceManager private DatabaseSession session; @@ -35,7 +40,7 @@ public class PastSnapshotFinderByDate implements BatchExtension{ this.session = session; } - Snapshot findByDate(Date date) { + PastSnapshot findByDate(Date date) { String hql = "from " + Snapshot.class.getSimpleName() + " where createdAt>=:date AND resourceId=:resourceId AND status=:status order by createdAt asc"; List<Snapshot> snapshots = session.createQuery(hql) .setParameter("date", date) @@ -43,7 +48,12 @@ public class PastSnapshotFinderByDate implements BatchExtension{ .setParameter("status", Snapshot.STATUS_PROCESSED) .setMaxResults(1) .getResultList(); - return snapshots.isEmpty() ? null : snapshots.get(0); + if (snapshots.isEmpty()) { + return null; + } + + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + return new PastSnapshot(MODE, date, snapshots.get(0)).setModeParameter(format.format(date)); } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java index 9a49b236402..618a539307f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java @@ -28,6 +28,9 @@ import java.util.Date; import java.util.List; public class PastSnapshotFinderByDays implements BatchExtension { + + public static final String MODE = "days"; + private Snapshot projectSnapshot; // TODO replace by PersistenceManager private DatabaseSession session; @@ -36,7 +39,7 @@ public class PastSnapshotFinderByDays implements BatchExtension { this.session = session; } - Snapshot findInDays(int days) { + PastSnapshot findFromDays(int days) { Date targetDate = DateUtils.addDays(projectSnapshot.getCreatedAt(), -days); String hql = "from " + Snapshot.class.getSimpleName() + " where resourceId=:resourceId AND status=:status AND createdAt>=:from AND createdAt<:to order by createdAt asc"; List<Snapshot> snapshots = session.createQuery(hql) @@ -47,6 +50,9 @@ public class PastSnapshotFinderByDays implements BatchExtension { .setMaxResults(1) .getResultList(); - return snapshots.isEmpty() ? null : snapshots.get(0); + if (snapshots.isEmpty()) { + return null; + } + return new PastSnapshot(MODE, targetDate, snapshots.get(0)).setModeParameter(String.valueOf(days)); } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByLastAnalysis.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByPreviousAnalysis.java index f50509b0f09..e8c0432fd67 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByLastAnalysis.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByPreviousAnalysis.java @@ -23,18 +23,21 @@ import org.sonar.api.BatchExtension; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; +import java.text.SimpleDateFormat; import java.util.List; -public class PastSnapshotFinderByLastAnalysis implements BatchExtension { +public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension { + public static final String MODE = "previous_analysis"; + private Snapshot projectSnapshot; // TODO replace by PersistenceManager private DatabaseSession session; - public PastSnapshotFinderByLastAnalysis(Snapshot projectSnapshot, DatabaseSession session) { + public PastSnapshotFinderByPreviousAnalysis(Snapshot projectSnapshot, DatabaseSession session) { this.projectSnapshot = projectSnapshot; this.session = session; } - Snapshot findLastAnalysis() { + PastSnapshot findByPreviousAnalysis() { String hql = "from " + Snapshot.class.getSimpleName() + " where createdAt<:date AND resourceId=:resourceId AND status=:status and last=true order by createdAt desc"; List<Snapshot> snapshots = session.createQuery(hql) .setParameter("date", projectSnapshot.getCreatedAt()) @@ -43,7 +46,12 @@ public class PastSnapshotFinderByLastAnalysis implements BatchExtension { .setMaxResults(1) .getResultList(); - return snapshots.isEmpty() ? null : snapshots.get(0); + if (snapshots.isEmpty()) { + return null; + } + Snapshot snapshot = snapshots.get(0); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + return new PastSnapshot(MODE, snapshot.getCreatedAt(), snapshot).setModeParameter(format.format(snapshot.getCreatedAt())); } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java index fc2ec93bf27..bba16f91fad 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java @@ -26,6 +26,9 @@ import org.sonar.api.database.model.Snapshot; import java.util.List; public class PastSnapshotFinderByVersion implements BatchExtension { + + public static final String MODE = "version"; + private Snapshot projectSnapshot; // TODO replace by PersistenceManager private DatabaseSession session; @@ -34,7 +37,7 @@ public class PastSnapshotFinderByVersion implements BatchExtension { this.session = session; } - Snapshot findVersion(String version) { + PastSnapshot findByVersion(String version) { String hql = "from " + Snapshot.class.getSimpleName() + " where version=:version AND resourceId=:resourceId AND status=:status order by createdAt desc"; List<Snapshot> snapshots = session.createQuery(hql) .setParameter("version", version) @@ -43,7 +46,11 @@ public class PastSnapshotFinderByVersion implements BatchExtension { .setMaxResults(1) .getResultList(); - return snapshots.isEmpty() ? null : snapshots.get(0); + if (snapshots.isEmpty()) { + return null; + } + Snapshot snapshot = snapshots.get(0); + return new PastSnapshot(MODE, snapshot.getCreatedAt(), snapshot).setModeParameter(snapshot.getVersion()); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java index 2fd987589aa..84bd2b100eb 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java @@ -67,11 +67,11 @@ public class NewViolationsDecoratorTest { PastSnapshot pastSnapshot = mock(PastSnapshot.class); when(pastSnapshot.getIndex()).thenReturn(1); - when(pastSnapshot.getDate()).thenReturn(fiveDaysAgo); + when(pastSnapshot.getTargetDate()).thenReturn(fiveDaysAgo); PastSnapshot pastSnapshot2 = mock(PastSnapshot.class); when(pastSnapshot2.getIndex()).thenReturn(2); - when(pastSnapshot2.getDate()).thenReturn(tenDaysAgo); + when(pastSnapshot2.getTargetDate()).thenReturn(tenDaysAgo); TimeMachineConfiguration timeMachineConfiguration = mock(TimeMachineConfiguration.class); when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Arrays.asList(pastSnapshot, pastSnapshot2)); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDateTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDateTest.java index 3d0190e1077..5ee48a6595d 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDateTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDateTest.java @@ -42,8 +42,8 @@ public class PastSnapshotFinderByDateTest extends AbstractDbUnitTestCase { Date date = DATE_FORMAT.parse("2008-11-22"); - Snapshot snapshot = finder.findByDate(date); - assertThat(snapshot.getId(), is(1006)); + PastSnapshot pastSnapshot = finder.findByDate(date); + assertThat(pastSnapshot.getProjectSnapshotId(), is(1006)); } @Test @@ -55,7 +55,7 @@ public class PastSnapshotFinderByDateTest extends AbstractDbUnitTestCase { Date date = DATE_FORMAT.parse("2008-11-24"); - Snapshot snapshot = finder.findByDate(date); - assertThat(snapshot.getId(), is(1009)); + PastSnapshot pastSnapshot = finder.findByDate(date); + assertThat(pastSnapshot.getProjectSnapshotId(), is(1009)); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java index a09efeda4dc..226010b327b 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java @@ -27,6 +27,7 @@ import org.sonar.jpa.test.AbstractDbUnitTestCase; import java.text.ParseException; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; public class PastSnapshotFinderByDaysTest extends AbstractDbUnitTestCase { @@ -38,7 +39,7 @@ public class PastSnapshotFinderByDaysTest extends AbstractDbUnitTestCase { Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); // 2008-11-16 PastSnapshotFinderByDays finder = new PastSnapshotFinderByDays(projectSnapshot, getSession()); - assertThat(finder.findInDays(50).getId(), is(1000)); + assertThat(finder.findFromDays(50).getProjectSnapshotId(), is(1000)); } @Test @@ -48,7 +49,7 @@ public class PastSnapshotFinderByDaysTest extends AbstractDbUnitTestCase { Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); // 2008-11-16 PastSnapshotFinderByDays finder = new PastSnapshotFinderByDays(projectSnapshot, getSession()); - assertThat(finder.findInDays(7).getId(), is(1006)); + assertThat(finder.findFromDays(7).getProjectSnapshotId(), is(1006)); } @Test @@ -58,7 +59,7 @@ public class PastSnapshotFinderByDaysTest extends AbstractDbUnitTestCase { Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); // 2008-11-16 PastSnapshotFinderByDays finder = new PastSnapshotFinderByDays(projectSnapshot, getSession()); - assertThat(finder.findInDays(1), IsNull.<Object>nullValue()); + assertThat(finder.findFromDays(1), nullValue()); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByLastAnalysisTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByPreviousAnalysisTest.java index 03ad8599374..992ebc37aa6 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByLastAnalysisTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByPreviousAnalysisTest.java @@ -29,26 +29,26 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; -public class PastSnapshotFinderByLastAnalysisTest extends AbstractDbUnitTestCase { +public class PastSnapshotFinderByPreviousAnalysisTest extends AbstractDbUnitTestCase { @Test - public void shouldFindLastAnalysis() throws ParseException { - setupData("shouldFindLastAnalysis"); + public void shouldFindPreviousAnalysis() throws ParseException { + setupData("shouldFindPreviousAnalysis"); Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); - PastSnapshotFinderByLastAnalysis finder = new PastSnapshotFinderByLastAnalysis(projectSnapshot, getSession()); + PastSnapshotFinderByPreviousAnalysis finder = new PastSnapshotFinderByPreviousAnalysis(projectSnapshot, getSession()); - Snapshot snapshot = finder.findLastAnalysis(); - assertThat(snapshot.getId(), is(1009)); + PastSnapshot pastSnapshot = finder.findByPreviousAnalysis(); + assertThat(pastSnapshot.getProjectSnapshotId(), is(1009)); } @Test - public void shouldNotFindLastAnalysis() throws ParseException { - setupData("shouldNotFindLastAnalysis"); + public void shouldNotFindPreviousAnalysis() throws ParseException { + setupData("shouldNotFindPreviousAnalysis"); Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); - PastSnapshotFinderByLastAnalysis finder = new PastSnapshotFinderByLastAnalysis(projectSnapshot, getSession()); + PastSnapshotFinderByPreviousAnalysis finder = new PastSnapshotFinderByPreviousAnalysis(projectSnapshot, getSession()); - assertNull(finder.findLastAnalysis()); + assertNull(finder.findByPreviousAnalysis()); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java index 8e6d40ef1ea..c051c5d4dae 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java @@ -36,7 +36,7 @@ public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase { Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(currentProjectSnapshot, getSession()); - assertThat(finder.findVersion("1.1").getId(), is(1009)); + assertThat(finder.findByVersion("1.1").getProjectSnapshotId(), is(1009)); } @Test @@ -46,6 +46,6 @@ public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase { Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(currentProjectSnapshot, getSession()); - assertThat(finder.findVersion("1.0"), nullValue()); + assertThat(finder.findByVersion("1.0"), nullValue()); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java index 79ae41629c6..048f34c2b32 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java @@ -30,7 +30,9 @@ import java.text.SimpleDateFormat; import java.util.Date; import static junit.framework.Assert.assertNull; +import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.*; @@ -40,7 +42,7 @@ public class PastSnapshotFinderTest { private PastSnapshotFinderByDays finderByDays; private PastSnapshotFinderByDate finderByDate; private PastSnapshotFinderByVersion finderByVersion; - private PastSnapshotFinderByLastAnalysis finderByLastAnalysis; + private PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis; private PastSnapshotFinder finder; @Before @@ -48,17 +50,17 @@ public class PastSnapshotFinderTest { finderByDays = mock(PastSnapshotFinderByDays.class); finderByDate = mock(PastSnapshotFinderByDate.class); finderByVersion = mock(PastSnapshotFinderByVersion.class); - finderByLastAnalysis = mock(PastSnapshotFinderByLastAnalysis.class); - finder = new PastSnapshotFinder(finderByDays, finderByVersion, finderByDate, finderByLastAnalysis); + finderByPreviousAnalysis = mock(PastSnapshotFinderByPreviousAnalysis.class); + finder = new PastSnapshotFinder(finderByDays, finderByVersion, finderByDate, finderByPreviousAnalysis); } @Test public void shouldFindByNumberOfDays() { - when(finderByDays.findInDays(30)).thenReturn(new Snapshot()); + when(finderByDays.findFromDays(30)).thenReturn(new PastSnapshot("days", null).setModeParameter("30")); PastSnapshot variationSnapshot = finder.find(1, "30"); - verify(finderByDays).findInDays(30); + verify(finderByDays).findFromDays(30); assertNotNull(variationSnapshot); assertThat(variationSnapshot.getIndex(), is(1)); assertThat(variationSnapshot.getMode(), is("days")); @@ -69,7 +71,7 @@ public class PastSnapshotFinderTest { public void shouldNotFindByNumberOfDays() { PastSnapshot variationSnapshot = finder.find(1, "30"); - verify(finderByDays).findInDays(30); + verify(finderByDays).findFromDays(30); assertNull(variationSnapshot); } @@ -77,7 +79,7 @@ public class PastSnapshotFinderTest { public void shouldFindByDate() throws ParseException { final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); final Date date = format.parse("2010-05-18"); - when(finderByDate.findByDate(date)).thenReturn(new Snapshot()); + when(finderByDate.findByDate(date)).thenReturn(new PastSnapshot("date", new Snapshot())); PastSnapshot variationSnapshot = finder.find(2, "2010-05-18"); @@ -92,7 +94,7 @@ public class PastSnapshotFinderTest { })); assertThat(variationSnapshot.getIndex(), is(2)); assertThat(variationSnapshot.getMode(), is("date")); - assertThat(variationSnapshot.getModeParameter(), is("2010-05-18")); + assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue())); } @Test @@ -106,57 +108,57 @@ public class PastSnapshotFinderTest { } @Test - public void shouldFindLastAnalysis() throws ParseException { + public void shouldFindByPreviousAnalysis() throws ParseException { final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); final Date date = format.parse("2010-05-18"); Snapshot snapshot = new Snapshot(); snapshot.setCreatedAt(date); - when(finderByLastAnalysis.findLastAnalysis()).thenReturn(snapshot); + when(finderByPreviousAnalysis.findByPreviousAnalysis()).thenReturn(new PastSnapshot(PastSnapshotFinderByPreviousAnalysis.MODE, snapshot)); - PastSnapshot variationSnapshot = finder.find(2, "last_analysis"); + PastSnapshot variationSnapshot = finder.find(2, PastSnapshotFinderByPreviousAnalysis.MODE); - verify(finderByLastAnalysis).findLastAnalysis(); + verify(finderByPreviousAnalysis).findByPreviousAnalysis(); assertThat(variationSnapshot.getIndex(), is(2)); - assertThat(variationSnapshot.getMode(), is("last_analysis")); - assertThat(variationSnapshot.getModeParameter(), is("2010-05-18")); + assertThat(variationSnapshot.getMode(), is(PastSnapshotFinderByPreviousAnalysis.MODE)); + assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue())); } @Test - public void shouldNotFindLastAnalysis() { - when(finderByLastAnalysis.findLastAnalysis()).thenReturn(null); + public void shouldNotFindPreviousAnalysis() { + when(finderByPreviousAnalysis.findByPreviousAnalysis()).thenReturn(null); - PastSnapshot variationSnapshot = finder.find(2, "last_analysis"); + PastSnapshot variationSnapshot = finder.find(2, PastSnapshotFinderByPreviousAnalysis.MODE); - verify(finderByLastAnalysis).findLastAnalysis(); + verify(finderByPreviousAnalysis).findByPreviousAnalysis(); assertNull(variationSnapshot); } @Test public void shouldFindByVersion() { - when(finderByVersion.findVersion("1.2")).thenReturn(new Snapshot()); + when(finderByVersion.findByVersion("1.2")).thenReturn(new PastSnapshot("version", new Snapshot())); PastSnapshot variationSnapshot = finder.find(2, "1.2"); - verify(finderByVersion).findVersion("1.2"); + verify(finderByVersion).findByVersion("1.2"); assertThat(variationSnapshot.getIndex(), is(2)); assertThat(variationSnapshot.getMode(), is("version")); - assertThat(variationSnapshot.getModeParameter(), is("1.2")); + assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue())); } @Test public void shouldNotFindVersion() { - when(finderByVersion.findVersion("1.2")).thenReturn(null); + when(finderByVersion.findByVersion("1.2")).thenReturn(null); PastSnapshot variationSnapshot = finder.find(2, "1.2"); - verify(finderByVersion).findVersion("1.2"); + verify(finderByVersion).findByVersion("1.2"); assertNull(variationSnapshot); } @Test public void shouldNotFailIfUnknownFormat() { - when(finderByLastAnalysis.findLastAnalysis()).thenReturn(new Snapshot()); // should not be called + when(finderByPreviousAnalysis.findByPreviousAnalysis()).thenReturn(new PastSnapshot(PastSnapshotFinderByPreviousAnalysis.MODE, new Snapshot())); // should not be called assertNull(finder.find(2, "foooo")); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java index 0e25e21d7a3..613747a12ca 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java @@ -35,10 +35,10 @@ public class TimeMachineConfigurationPersisterTest extends AbstractDbUnitTestCas setupData("shared"); TimeMachineConfiguration conf = mock(TimeMachineConfiguration.class); - PastSnapshot vs1 = new PastSnapshot(1, "days", getSession().getSingleResult(Snapshot.class, "id", 100)) - .setModeParameter("30"); - PastSnapshot vs3 = new PastSnapshot(3, "version", getSession().getSingleResult(Snapshot.class, "id", 300)) - .setModeParameter("1.2.3"); + PastSnapshot vs1 = new PastSnapshot("days", getSession().getSingleResult(Snapshot.class, "id", 100)) + .setModeParameter("30").setIndex(1); + PastSnapshot vs3 = new PastSnapshot("version", getSession().getSingleResult(Snapshot.class, "id", 300)) + .setModeParameter("1.2.3").setIndex(3); when(conf.getProjectPastSnapshots()).thenReturn(Arrays.asList(vs1, vs3)); Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1000); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java index 2070bc6b0d3..f9cbfc6cf60 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java @@ -52,8 +52,8 @@ public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase { public void shouldInitVariationSnapshots() throws ParseException { PropertiesConfiguration conf = new PropertiesConfiguration(); PastSnapshotFinder snapshotReferenceFinder = mock(PastSnapshotFinder.class); - when(snapshotReferenceFinder.find(conf, 1)).thenReturn(new PastSnapshot(1, "days", newSnapshot("2010-10-15"))); - when(snapshotReferenceFinder.find(conf, 3)).thenReturn(new PastSnapshot(3, "days", newSnapshot("2010-10-13"))); + when(snapshotReferenceFinder.find(conf, 1)).thenReturn(new PastSnapshot("days", null, newSnapshot("2010-10-15"))); + when(snapshotReferenceFinder.find(conf, 3)).thenReturn(new PastSnapshot("days", null, newSnapshot("2010-10-13"))); TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(conf, snapshotReferenceFinder); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java index a2d21bcb804..0f02f0c2268 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java @@ -58,8 +58,8 @@ public class VariationDecoratorTest extends AbstractDbUnitTestCase { Resource javaPackage = new JavaPackage("org.foo"); PastMeasuresLoader pastMeasuresLoader = mock(PastMeasuresLoader.class); - PastSnapshot pastSnapshot1 = new PastSnapshot(1, "days", new Snapshot()); - PastSnapshot pastSnapshot3 = new PastSnapshot(3, "days", new Snapshot()); + PastSnapshot pastSnapshot1 = new PastSnapshot("days", new Snapshot()).setIndex(1); + PastSnapshot pastSnapshot3 = new PastSnapshot("days", new Snapshot()).setIndex(3); // first past analysis when(pastMeasuresLoader.getPastMeasures(javaPackage, pastSnapshot1)).thenReturn(Arrays.asList( diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByLastAnalysisTest/shouldFindLastAnalysis.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByPreviousAnalysisTest/shouldFindPreviousAnalysis.xml index 0c02bb491c4..0c02bb491c4 100644 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByLastAnalysisTest/shouldFindLastAnalysis.xml +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByPreviousAnalysisTest/shouldFindPreviousAnalysis.xml diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByLastAnalysisTest/shouldNotFindLastAnalysis.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByPreviousAnalysisTest/shouldNotFindPreviousAnalysis.xml index 5eaeff179ff..5eaeff179ff 100644 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByLastAnalysisTest/shouldNotFindLastAnalysis.xml +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByPreviousAnalysisTest/shouldNotFindPreviousAnalysis.xml diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb index d5e095c22ba..2f97370f9db 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb @@ -38,8 +38,8 @@ module DashboardHelper label = "Last %s days" % mode_param elsif mode=='version' label = "Version %s" % mode_param - elsif mode=='last_analysis' - label = "Last analysis (%s)" % localize(Date.parse(mode_param)) + elsif mode=='previous_analysis' + label = "Previous analysis (%s)" % localize(Date.parse(mode_param)) elsif mode=='date' label = localize(Date.parse(mode_param)) end |