}
public boolean isRelatedToSnapshot() {
- return projectSnapshot!=null;
+ return projectSnapshot != null;
}
public Snapshot getProjectSnapshot() {
}
public Date getDate() {
- return (projectSnapshot!=null ? projectSnapshot.getCreatedAt() : null);
+ return (projectSnapshot != null ? projectSnapshot.getCreatedAt() : null);
}
public String getMode() {
return (projectSnapshot != null ? projectSnapshot.getQualifier() : null);
}
-
public Date getTargetDate() {
return targetDate;
}
-
@Override
public String toString() {
if (StringUtils.equals(mode, PastSnapshotFinderByVersion.MODE)) {
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;
}
import org.sonar.api.utils.DateUtils;
import java.text.SimpleDateFormat;
+import java.util.Date;
import java.util.List;
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())
.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));
}
-
}
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 {
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()) {
- 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);
}
}
*/
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
}
@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));
}
}
*/
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
}
@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));
}
}