aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-06-15 03:26:35 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-06-15 20:42:21 +0400
commit86c4e9f9605139802806dbda3dd2714949c98684 (patch)
tree7d2e42ad88be838c7114ea476313017e847a4b01 /sonar-batch
parentb1a5b67aaf3419158483703ded7a4662eeefad68 (diff)
downloadsonarqube-86c4e9f9605139802806dbda3dd2714949c98684.tar.gz
sonarqube-86c4e9f9605139802806dbda3dd2714949c98684.zip
SONAR-2428 Detect new violations, when new module added to project
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java16
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java22
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java15
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java15
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java15
5 files changed, 56 insertions, 27 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 152dacce0f1..97886481de5 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
@@ -54,7 +54,7 @@ public class PastSnapshot {
}
public boolean isRelatedToSnapshot() {
- return projectSnapshot!=null;
+ return projectSnapshot != null;
}
public Snapshot getProjectSnapshot() {
@@ -62,7 +62,7 @@ public class PastSnapshot {
}
public Date getDate() {
- return (projectSnapshot!=null ? projectSnapshot.getCreatedAt() : null);
+ return (projectSnapshot != null ? projectSnapshot.getCreatedAt() : null);
}
public String getMode() {
@@ -86,12 +86,10 @@ public class PastSnapshot {
return (projectSnapshot != null ? projectSnapshot.getQualifier() : null);
}
-
public Date getTargetDate() {
return targetDate;
}
-
@Override
public String toString() {
if (StringUtils.equals(mode, PastSnapshotFinderByVersion.MODE)) {
@@ -102,16 +100,20 @@ public class PastSnapshot {
if (isRelatedToSnapshot()) {
label += ", analysis of " + getDate();
}
- label+=")";
+ label += ")";
return label;
}
if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS)) {
- return String.format("Compare to previous analysis (%s)", DateUtils.formatDate(getDate()));
+ String label = "Compare to previous analysis";
+ if (isRelatedToSnapshot()) {
+ label += String.format(" (%s)", DateUtils.formatDate(getDate()));
+ }
+ return label;
}
if (StringUtils.equals(mode, PastSnapshotFinderByDate.MODE)) {
String label = "Compare to date " + DateUtils.formatDate(getTargetDate());
if (isRelatedToSnapshot()) {
- label += String.format("(analysis of %s)", DateUtils.formatDate(getDate()));
+ label += String.format(" (analysis of %s)", DateUtils.formatDate(getDate()));
}
return label;
}
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 d3eb23519e9..5f8ca55592c 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
@@ -27,6 +27,7 @@ import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.DateUtils;
import java.text.SimpleDateFormat;
+import java.util.Date;
import java.util.List;
public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension {
@@ -37,8 +38,15 @@ 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";
+ 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";
List<Snapshot> snapshots = session.createQuery(hql)
.setParameter("date", projectSnapshot.getCreatedAt())
.setParameter("resourceId", projectSnapshot.getResourceId())
@@ -48,12 +56,16 @@ public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension {
.setMaxResults(1)
.getResultList();
+ Snapshot snapshot;
+ Date targetDate;
if (snapshots.isEmpty()) {
- return null;
+ snapshot = null;
+ targetDate = new Date(projectSnapshot.getCreatedAt().getTime() - 1000 * 60);
+ } else {
+ snapshot = snapshots.get(0);
+ targetDate = snapshot.getCreatedAt();
}
- Snapshot snapshot = snapshots.get(0);
SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT);
- return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, snapshot.getCreatedAt(), snapshot).setModeParameter(format.format(snapshot.getCreatedAt()));
+ 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 9bf15e1414c..ed353e975d7 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
@@ -24,6 +24,7 @@ import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Qualifiers;
+import java.util.Date;
import java.util.List;
public class PastSnapshotFinderByVersion implements BatchExtension {
@@ -36,6 +37,9 @@ 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)
@@ -46,11 +50,16 @@ public class PastSnapshotFinderByVersion implements BatchExtension {
.setMaxResults(1)
.getResultList();
+ Snapshot snapshot;
+ Date targetDate;
if (snapshots.isEmpty()) {
- return null;
+ snapshot = null;
+ targetDate = new Date(projectSnapshot.getCreatedAt().getTime() - 1000 * 60);
+ } else {
+ snapshot = snapshots.get(0);
+ targetDate = snapshot.getCreatedAt();
}
- Snapshot snapshot = snapshots.get(0);
- return new PastSnapshot(MODE, snapshot.getCreatedAt(), snapshot).setModeParameter(snapshot.getVersion());
+ 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 62a10db48e7..9e6cb1b98b7 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
@@ -19,16 +19,16 @@
*/
package org.sonar.batch.components;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.assertThat;
+
import org.junit.Test;
import org.sonar.api.database.model.Snapshot;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import java.text.ParseException;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-
public class PastSnapshotFinderByPreviousAnalysisTest extends AbstractDbUnitTestCase {
@Test
@@ -43,12 +43,15 @@ public class PastSnapshotFinderByPreviousAnalysisTest extends AbstractDbUnitTest
}
@Test
- public void shouldNotFindPreviousAnalysis() throws ParseException {
+ public void shouldReturnPastSnapshotEvenWhenNoPreviousAnalysis() throws ParseException {
setupData("shouldNotFindPreviousAnalysis");
Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010);
PastSnapshotFinderByPreviousAnalysis finder = new PastSnapshotFinderByPreviousAnalysis(getSession());
- assertNull(finder.findByPreviousAnalysis(projectSnapshot));
+ PastSnapshot pastSnapshot = finder.findByPreviousAnalysis(projectSnapshot);
+ assertThat(pastSnapshot.isRelatedToSnapshot(), is(false));
+ assertThat(pastSnapshot.getProjectSnapshot(), nullValue());
+ assertThat(projectSnapshot.getCreatedAt().getTime() - pastSnapshot.getTargetDate().getTime(), is(1000L * 60));
}
}
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 29f23920f0d..046610f5e0b 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
@@ -19,14 +19,14 @@
*/
package org.sonar.batch.components;
-import org.junit.Test;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
-
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
+import org.junit.Test;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
+
public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase {
@Test
@@ -40,12 +40,15 @@ public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase {
}
@Test
- public void shouldNotFindVersion() {
+ public void shouldReturnPastSnapshotEvenWhenNoPreviousAnalysis() {
setupData("shared");
Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010);
PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(getSession());
- assertThat(finder.findByVersion(currentProjectSnapshot, "1.0"), nullValue());
+ 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));
}
}