this.structs = structs;
}
-
public boolean shouldExecuteOnProject(Project project) {
return project.isLatestAnalysis() && !structs.isEmpty();
}
context.saveMeasure(newUncoveredConditions);
}
-
private Map<Integer, Integer> parseCountByLine(Measure measure) {
if (measure != null && measure.hasData()) {
return KeyValueFormat.parseIntInt(measure.getData());
return Maps.newHashMap();
}
-
public static final class PeriodStruct {
int index;
Date date;
void analyze(Date lineDate, int hits, int conditions, int coveredConditions) {
if (lineDate == null) {
- //TODO warning
+ // TODO warning
- } else if (lineDate.after(date)) {
+ } else if (date == null || lineDate.after(date)) {
// TODO test if string comparison is faster or not
addLine(hits > 0);
addConditions(conditions, coveredConditions);
}
private boolean isAfter(Violation violation, Date date) {
- return violation.getCreatedAt()!= null && violation.getCreatedAt().after(date);
+ if (date == null) {
+ return true;
+ }
+ return violation.getCreatedAt() != null && violation.getCreatedAt().after(date);
}
private Metric getMetricForSeverity(RulePriority severity) {
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;
@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()));
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";
.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));
}
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)
.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);
}
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());
}
}
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());
}
}
*/
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
assertThat(pastSnapshot.toString(), startsWith("Compare to version 2.3"));
}
+ @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
assertThat(pastSnapshot.toString(), startsWith("Compare to date "));
}
+ @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"));
}
}
end
end
- to=(@period ? Java::JavaUtil::Date.new(@snapshot.period_datetime(@period).to_f * 1000) : nil)
+ to=(@period && @snapshot.period_datetime(@period) ? Java::JavaUtil::Date.new(@snapshot.period_datetime(@period).to_f * 1000) : nil)
metric=Metric.by_key(params[:coverage_filter]||params[:metric])
@coverage_filter=(metric ? metric.key : 'coverage')
@filtered=true
if date
conditions+=' AND created_at>?'
values<<date.advance(:minutes => 1)
- else
- conditions+=' AND id=-1'
end
end
render :action => 'index', :layout => !request.xhr?
end
-
+
def filter_lines_by_date
- if @period
+ if @period && @snapshot.period_datetime(@period)
@filtered=true
to=Java::JavaUtil::Date.new(@snapshot.period_datetime(@period).to_f * 1000)
if to
if mode=='days'
label = "over %s days" % mode_param
elsif mode=='version'
- label = "since version %s (%s)" % [mode_param, (date.strftime("%Y %b %d"))]
+ if !date.nil?
+ label = "since version %s (%s)" % [mode_param]
+ else
+ label = "since version %s" % [mode_param, (date.strftime("%Y %b %d"))]
+ end
elsif mode=='previous_analysis'
- label = "since previous analysis (%s)" % (date.strftime("%Y %b %d"))
+ if !date.nil?
+ label = "since previous analysis (%s)" % (date.strftime("%Y %b %d"))
+ else
+ label = "since previous analysis"
+ end
elsif mode=='date'
label = "since #{date.strftime("%Y %b %d")}"
end
elsif mode=='version'
label = "Added since version %s" % mode_param
elsif mode=='previous_analysis'
- label = "Added since previous analysis (%s)" % date.strftime("%Y %b. %d")
+ if !date.nil?
+ label = "Added since previous analysis (%s)" % date.strftime("%Y %b. %d")
+ else
+ label = "Added since previous analysis"
+ end
elsif mode=='date'
label = "Added since #{date.strftime("%Y %b %d")}"
end