aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-03-08 18:15:49 +0100
committersimonbrandhof <simon.brandhof@gmail.com>2011-03-08 18:15:49 +0100
commit4be8b53738bd6dd2a02c0bb9f1952d3cf6d9a361 (patch)
tree49043b6f26f9fb01eef6dee28cdff5ece5390191 /sonar-batch
parent06770b3ba35667e66028fd969e356bbdae44d55d (diff)
downloadsonarqube-4be8b53738bd6dd2a02c0bb9f1952d3cf6d9a361.tar.gz
sonarqube-4be8b53738bd6dd2a02c0bb9f1952d3cf6d9a361.zip
SONAR-2218 coverage of new code must be computed even if there are no previous analysis
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java2
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java41
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java24
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java8
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java3
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java4
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java5
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByDaysTest.java2
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java8
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java8
10 files changed, 62 insertions, 43 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java
index 8c9ab0e9fde..19efcadd46d 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java
@@ -62,7 +62,7 @@ public class PastMeasuresLoader implements BatchExtension {
}
public List<MeasureModel> getPastMeasures(Resource resource, Snapshot projectSnapshot) {
- if (isPersisted(resource)) {
+ if (isPersisted(resource) && projectSnapshot!=null) {
return getPastMeasures(resource.getId(), projectSnapshot);
}
return Collections.emptyList();
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 00f05439bd7..152dacce0f1 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
@@ -23,6 +23,7 @@ import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.sonar.api.CoreProperties;
import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.utils.DateUtils;
import java.util.Date;
@@ -39,9 +40,8 @@ public class PastSnapshot {
this.projectSnapshot = projectSnapshot;
}
- public PastSnapshot(String mode, Snapshot projectSnapshot) {
- this.mode = mode;
- this.projectSnapshot = projectSnapshot;
+ public PastSnapshot(String mode, Date targetDate) {
+ this(mode, targetDate, null);
}
public PastSnapshot setIndex(int index) {
@@ -53,12 +53,16 @@ public class PastSnapshot {
return index;
}
+ public boolean isRelatedToSnapshot() {
+ return projectSnapshot!=null;
+ }
+
public Snapshot getProjectSnapshot() {
return projectSnapshot;
}
public Date getDate() {
- return projectSnapshot.getCreatedAt();
+ return (projectSnapshot!=null ? projectSnapshot.getCreatedAt() : null);
}
public String getMode() {
@@ -74,33 +78,44 @@ public class PastSnapshot {
return this;
}
- public Integer getProjectSnapshotId() {
+ Integer getProjectSnapshotId() {
return (projectSnapshot != null ? projectSnapshot.getId() : null);
}
+ public String getQualifier() {
+ return (projectSnapshot != null ? projectSnapshot.getQualifier() : null);
+ }
+
+
public Date getTargetDate() {
return targetDate;
}
- public PastSnapshot setTargetDate(Date d) {
- this.targetDate = d;
- return this;
- }
@Override
public String toString() {
if (StringUtils.equals(mode, PastSnapshotFinderByVersion.MODE)) {
- return String.format("Compare to version " + modeParameter + "(" + getDate() + ")");
+ return String.format("Compare to version %s (%s)", modeParameter, DateUtils.formatDate(getTargetDate()));
}
if (StringUtils.equals(mode, PastSnapshotFinderByDays.MODE)) {
- return String.format("Compare over " + modeParameter + " days (" + getDate() + ")");
+ String label = String.format("Compare over %s days (%s", modeParameter, DateUtils.formatDate(getTargetDate()));
+ if (isRelatedToSnapshot()) {
+ label += ", analysis of " + getDate();
+ }
+ label+=")";
+ return label;
}
if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS)) {
- return String.format("Compare to previous analysis " + " (" + getDate() + ")");
+ return String.format("Compare to previous analysis (%s)", DateUtils.formatDate(getDate()));
}
if (StringUtils.equals(mode, PastSnapshotFinderByDate.MODE)) {
- return String.format("Compare to date " + getDate());
+ String label = "Compare to date " + DateUtils.formatDate(getTargetDate());
+ if (isRelatedToSnapshot()) {
+ label += String.format("(analysis of %s)", DateUtils.formatDate(getDate()));
+ }
+ return label;
}
return ReflectionToStringBuilder.toString(this);
}
+
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java
index 71c95af82b8..4340ac6c4b3 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java
@@ -22,7 +22,8 @@ package org.sonar.batch.components;
import org.sonar.api.BatchExtension;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.utils.DateUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -39,20 +40,25 @@ public class PastSnapshotFinderByDate implements BatchExtension {
}
PastSnapshot findByDate(Snapshot projectSnapshot, Date date) {
+ Snapshot snapshot = null;
+ if (projectSnapshot != null) {
+ snapshot = findSnapshot(projectSnapshot, date);
+ }
+ SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT);
+ return new PastSnapshot(MODE, date, snapshot).setModeParameter(format.format(date));
+ }
+
+
+ private Snapshot findSnapshot(Snapshot projectSnapshot, Date date) {
String hql = "from " + Snapshot.class.getSimpleName() + " where createdAt>=:date AND resourceId=:resourceId AND status=:status AND qualifier<>:lib order by createdAt asc";
List<Snapshot> snapshots = session.createQuery(hql)
.setParameter("date", date)
.setParameter("resourceId", projectSnapshot.getResourceId())
.setParameter("status", Snapshot.STATUS_PROCESSED)
- .setParameter("lib", Project.QUALIFIER_LIB)
+ .setParameter("lib", Qualifiers.LIBRARY)
.setMaxResults(1)
.getResultList();
- if (snapshots.isEmpty()) {
- return null;
- }
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- return new PastSnapshot(MODE, date, snapshots.get(0)).setModeParameter(format.format(date));
+ return (snapshots.isEmpty() ? null : snapshots.get(0));
}
-
-}
+} \ No newline at end of file
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java
index ddb0d59c970..b3763107e9c 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java
@@ -23,7 +23,7 @@ import org.apache.commons.lang.time.DateUtils;
import org.sonar.api.BatchExtension;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Qualifiers;
import java.util.Date;
import java.util.List;
@@ -45,13 +45,9 @@ public class PastSnapshotFinderByDays implements BatchExtension {
.setParameter("date", projectSnapshot.getCreatedAt())
.setParameter("resourceId", projectSnapshot.getResourceId())
.setParameter("status", Snapshot.STATUS_PROCESSED)
- .setParameter("lib", Project.QUALIFIER_LIB)
+ .setParameter("lib", Qualifiers.LIBRARY)
.getResultList();
- if (snapshots.isEmpty()) {
- return null;
- }
-
Snapshot snapshot = getNearestToTarget(snapshots, targetDate);
return new PastSnapshot(MODE, targetDate, snapshot).setModeParameter(String.valueOf(days));
}
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 6993ca0b78d..c036530682c 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
@@ -24,6 +24,7 @@ import org.sonar.api.CoreProperties;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Project;
+import org.sonar.api.utils.DateUtils;
import java.text.SimpleDateFormat;
import java.util.List;
@@ -51,7 +52,7 @@ public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension {
return null;
}
Snapshot snapshot = snapshots.get(0);
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+ SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT);
return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, snapshot.getCreatedAt(), snapshot).setModeParameter(format.format(snapshot.getCreatedAt()));
}
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 e81019f9021..9bf15e1414c 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
@@ -22,7 +22,7 @@ package org.sonar.batch.components;
import org.sonar.api.BatchExtension;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Qualifiers;
import java.util.List;
@@ -42,7 +42,7 @@ public class PastSnapshotFinderByVersion implements BatchExtension {
.setParameter("version", version)
.setParameter("resourceId", projectSnapshot.getResourceId())
.setParameter("status", Snapshot.STATUS_PROCESSED)
- .setParameter("lib", Project.QUALIFIER_LIB)
+ .setParameter("lib", Qualifiers.LIBRARY)
.setMaxResults(1)
.getResultList();
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java
index 5a1dfbda48f..fb8d071f5d2 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java
@@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory;
import org.sonar.api.BatchExtension;
import org.sonar.api.CoreProperties;
import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
import org.sonar.api.utils.Logs;
@@ -56,9 +57,9 @@ public class TimeMachineConfiguration implements BatchExtension {
}
private void log(PastSnapshot pastSnapshot) {
- String qualifier = pastSnapshot.getProjectSnapshot().getQualifier();
+ String qualifier = pastSnapshot.getQualifier();
// hack to avoid too many logs when the views plugin is installed
- if (StringUtils.equals(Resource.QUALIFIER_VIEW, qualifier) || StringUtils.equals(Resource.QUALIFIER_SUBVIEW, qualifier)) {
+ if (StringUtils.equals(Qualifiers.VIEW, qualifier) || StringUtils.equals(Qualifiers.SUBVIEW, qualifier)) {
LoggerFactory.getLogger(getClass()).debug(pastSnapshot.toString());
} else {
Logs.INFO.info(pastSnapshot.toString());
diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByDaysTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByDaysTest.java
index c4fc0d34063..ff293fdda77 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByDaysTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByDaysTest.java
@@ -66,7 +66,7 @@ public class PastSnapshotFinderByDaysTest extends AbstractDbUnitTestCase {
Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); // 2008-11-16
PastSnapshotFinderByDays finder = new PastSnapshotFinderByDays(getSession());
- assertThat(finder.findFromDays(projectSnapshot, 1), nullValue());
+ assertThat(finder.findFromDays(projectSnapshot, 1).getProjectSnapshot(), nullValue());
}
@Test
diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java
index d524ce8f232..7f24bf1fe5e 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java
@@ -81,7 +81,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(null, date)).thenReturn(new PastSnapshot("date", new Snapshot()));
+ when(finderByDate.findByDate(null, date)).thenReturn(new PastSnapshot("date", date, new Snapshot()));
PastSnapshot variationSnapshot = finder.find(null, 2, "2010-05-18");
@@ -115,7 +115,7 @@ public class PastSnapshotFinderTest {
final Date date = format.parse("2010-05-18");
Snapshot snapshot = new Snapshot();
snapshot.setCreatedAt(date);
- when(finderByPreviousAnalysis.findByPreviousAnalysis(null)).thenReturn(new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, snapshot));
+ when(finderByPreviousAnalysis.findByPreviousAnalysis(null)).thenReturn(new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, date, snapshot));
PastSnapshot variationSnapshot = finder.find(null, 2, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
@@ -138,7 +138,7 @@ public class PastSnapshotFinderTest {
@Test
public void shouldFindByVersion() {
- when(finderByVersion.findByVersion(null, "1.2")).thenReturn(new PastSnapshot("version", new Snapshot()));
+ when(finderByVersion.findByVersion(null, "1.2")).thenReturn(new PastSnapshot("version", new Date(), new Snapshot()));
PastSnapshot variationSnapshot = finder.find(null, 2, "1.2");
@@ -160,7 +160,7 @@ public class PastSnapshotFinderTest {
@Test
public void shouldNotFailIfUnknownFormat() {
- when(finderByPreviousAnalysis.findByPreviousAnalysis(null)).thenReturn(new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, new Snapshot())); // should not be called
+ when(finderByPreviousAnalysis.findByPreviousAnalysis(null)).thenReturn(new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, new Date(), new Snapshot())); // should not be called
assertNull(finder.find(null, 2, "foooo"));
}
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 b504b0a44b7..48fd131f125 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
@@ -32,25 +32,25 @@ public class PastSnapshotTest {
@Test
public void testToStringForVersion() {
- PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByVersion.MODE, new Snapshot()).setModeParameter("2.3");
+ PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByVersion.MODE, new Date()).setModeParameter("2.3");
assertThat(pastSnapshot.toString(), startsWith("Compare to version 2.3"));
}
@Test
public void testToStringForNumberOfDays() {
- PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDays.MODE, new Snapshot()).setModeParameter("30");
+ PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDays.MODE, new Date()).setModeParameter("30");
assertThat(pastSnapshot.toString(), startsWith("Compare over 30 days"));
}
@Test
public void testToStringForDate() {
- PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDate.MODE, new Snapshot()).setTargetDate(new Date());
+ PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDate.MODE, new Date());
assertThat(pastSnapshot.toString(), startsWith("Compare to date "));
}
@Test
public void testToStringForPreviousAnalysis() {
- PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, new Snapshot()).setTargetDate(new Date());
+ PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, new Date(), new Snapshot().setCreatedAt(new Date()));
assertThat(pastSnapshot.toString(), startsWith("Compare to previous analysis"));
}
}