diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-06-17 14:55:40 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-06-17 17:18:45 +0400 |
commit | c973498cfec4ff8e59ac33e9c9fb150a71ef828a (patch) | |
tree | 276e8ac2d6ba96b6edeb4622c6702ae19f988251 /sonar-batch | |
parent | 58c0a485ceade20933b5bc52b914ef2b34cadbd5 (diff) | |
download | sonarqube-c973498cfec4ff8e59ac33e9c9fb150a71ef828a.tar.gz sonarqube-c973498cfec4ff8e59ac33e9c9fb150a71ef828a.zip |
SONAR-2428 Detection new violations, when new module added to project
Diffstat (limited to 'sonar-batch')
6 files changed, 50 insertions, 31 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java index 97886481de5..843304af53b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java @@ -44,6 +44,13 @@ public class PastSnapshot { this(mode, targetDate, null); } + /** + * See SONAR-2428 : even if previous analysis does not exist (no snapshot and no target date), we should perform comparison. + */ + public PastSnapshot(String mode) { + this(mode, null, null); + } + public PastSnapshot setIndex(int index) { this.index = index; return this; @@ -93,7 +100,11 @@ public class PastSnapshot { @Override public String toString() { if (StringUtils.equals(mode, PastSnapshotFinderByVersion.MODE)) { - return String.format("Compare to version %s (%s)", modeParameter, DateUtils.formatDate(getTargetDate())); + String label = String.format("Compare to version %s", modeParameter); + if (getTargetDate() != null) { + label += String.format(" (%s)", DateUtils.formatDate(getTargetDate())); + } + return label; } if (StringUtils.equals(mode, PastSnapshotFinderByDays.MODE)) { String label = String.format("Compare over %s days (%s", modeParameter, DateUtils.formatDate(getTargetDate())); diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java index 5f8ca55592c..77d890f9ea4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java @@ -38,12 +38,6 @@ public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension { this.session = session; } - /** - * See SONAR-2428 : even if past snapshot does not exist, we should perform comparison. - * In this case as a value for target date we use moment just before current snapshot - * to be able to perform strict comparison of dates. - * Also note that ruby side (resource viewer) has precision in one minute. - */ PastSnapshot findByPreviousAnalysis(Snapshot projectSnapshot) { String hql = "from " + Snapshot.class.getSimpleName() + " where createdAt<:date AND resourceId=:resourceId AND status=:status and last=:last and qualifier<>:lib order by createdAt desc"; @@ -56,15 +50,11 @@ public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension { .setMaxResults(1) .getResultList(); - Snapshot snapshot; - Date targetDate; if (snapshots.isEmpty()) { - snapshot = null; - targetDate = new Date(projectSnapshot.getCreatedAt().getTime() - 1000 * 60); - } else { - snapshot = snapshots.get(0); - targetDate = snapshot.getCreatedAt(); + return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); } + Snapshot snapshot = snapshots.get(0); + Date targetDate = snapshot.getCreatedAt(); SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT); return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, targetDate, snapshot).setModeParameter(format.format(targetDate)); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java index ed353e975d7..c4cc272c5db 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java @@ -37,9 +37,6 @@ public class PastSnapshotFinderByVersion implements BatchExtension { this.session = session; } - /** - * See comments in {@link PastSnapshotFinderByPreviousAnalysis#findByPreviousAnalysis(Snapshot)} - */ PastSnapshot findByVersion(Snapshot projectSnapshot, String version) { String hql = "from " + Snapshot.class.getSimpleName() + " where version=:version AND resourceId=:resourceId AND status=:status AND qualifier<>:lib order by createdAt desc"; List<Snapshot> snapshots = session.createQuery(hql) @@ -50,15 +47,11 @@ public class PastSnapshotFinderByVersion implements BatchExtension { .setMaxResults(1) .getResultList(); - Snapshot snapshot; - Date targetDate; if (snapshots.isEmpty()) { - snapshot = null; - targetDate = new Date(projectSnapshot.getCreatedAt().getTime() - 1000 * 60); - } else { - snapshot = snapshots.get(0); - targetDate = snapshot.getCreatedAt(); + return new PastSnapshot(MODE); } + Snapshot snapshot = snapshots.get(0); + Date targetDate = snapshot.getCreatedAt(); return new PastSnapshot(MODE, targetDate, snapshot).setModeParameter(version); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java index 9e6cb1b98b7..f36e6fea13d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java @@ -52,6 +52,6 @@ public class PastSnapshotFinderByPreviousAnalysisTest extends AbstractDbUnitTest PastSnapshot pastSnapshot = finder.findByPreviousAnalysis(projectSnapshot); assertThat(pastSnapshot.isRelatedToSnapshot(), is(false)); assertThat(pastSnapshot.getProjectSnapshot(), nullValue()); - assertThat(projectSnapshot.getCreatedAt().getTime() - pastSnapshot.getTargetDate().getTime(), is(1000L * 60)); + assertThat(pastSnapshot.getTargetDate(), nullValue()); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java index 046610f5e0b..5ad41e4c808 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java @@ -49,6 +49,6 @@ public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase { PastSnapshot pastSnapshot = finder.findByVersion(currentProjectSnapshot, "1.0"); assertThat(pastSnapshot.isRelatedToSnapshot(), is(false)); assertThat(pastSnapshot.getProjectSnapshot(), nullValue()); - assertThat(currentProjectSnapshot.getCreatedAt().getTime() - pastSnapshot.getTargetDate().getTime(), is(1000L * 60)); + assertThat(pastSnapshot.getTargetDate(), nullValue()); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java index 48fd131f125..8bd305e85e2 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java @@ -19,15 +19,16 @@ */ package org.sonar.batch.components; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.Assert.assertThat; + import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.database.model.Snapshot; import java.util.Date; -import static org.hamcrest.Matchers.startsWith; -import static org.junit.Assert.assertThat; - public class PastSnapshotTest { @Test @@ -37,9 +38,21 @@ public class PastSnapshotTest { } @Test + public void testToStringForVersionWithoutDate() { + PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByVersion.MODE).setModeParameter("2.3"); + assertThat(pastSnapshot.toString(), equalTo("Compare to version 2.3")); + } + + @Test public void testToStringForNumberOfDays() { PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDays.MODE, new Date()).setModeParameter("30"); - assertThat(pastSnapshot.toString(), startsWith("Compare over 30 days")); + assertThat(pastSnapshot.toString(), startsWith("Compare over 30 days (")); + } + + @Test + public void testToStringForNumberOfDaysWithSnapshot() { + PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDays.MODE, new Date(), new Snapshot().setCreatedAt(new Date())).setModeParameter("30"); + assertThat(pastSnapshot.toString(), startsWith("Compare over 30 days (")); } @Test @@ -49,8 +62,20 @@ public class PastSnapshotTest { } @Test + public void testToStringForDateWithSnapshot() { + PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDate.MODE, new Date(), new Snapshot().setCreatedAt(new Date())); + assertThat(pastSnapshot.toString(), startsWith("Compare to date ")); + } + + @Test public void testToStringForPreviousAnalysis() { PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, new Date(), new Snapshot().setCreatedAt(new Date())); - assertThat(pastSnapshot.toString(), startsWith("Compare to previous analysis")); + assertThat(pastSnapshot.toString(), startsWith("Compare to previous analysis ")); + } + + @Test + public void testToStringForPreviousAnalysisWithoutDate() { + PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); + assertThat(pastSnapshot.toString(), equalTo("Compare to previous analysis")); } } |