diff options
11 files changed, 74 insertions, 45 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewCoverageFileAnalyzer.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewCoverageFileAnalyzer.java index 5686c6d4ecc..d44ffd056e1 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewCoverageFileAnalyzer.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewCoverageFileAnalyzer.java @@ -60,7 +60,6 @@ public final class NewCoverageFileAnalyzer implements Decorator { this.structs = structs; } - public boolean shouldExecuteOnProject(Project project) { return project.isLatestAnalysis() && !structs.isEmpty(); } @@ -146,7 +145,6 @@ public final class NewCoverageFileAnalyzer implements Decorator { context.saveMeasure(newUncoveredConditions); } - private Map<Integer, Integer> parseCountByLine(Measure measure) { if (measure != null && measure.hasData()) { return KeyValueFormat.parseIntInt(measure.getData()); @@ -154,7 +152,6 @@ public final class NewCoverageFileAnalyzer implements Decorator { return Maps.newHashMap(); } - public static final class PeriodStruct { int index; Date date; @@ -179,9 +176,9 @@ public final class NewCoverageFileAnalyzer implements Decorator { 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); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java index 358c3394edc..86a150e28cd 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java @@ -173,7 +173,10 @@ public class NewViolationsDecorator implements Decorator { } 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) { 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")); } } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/resource_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/resource_controller.rb index 87384d9fdd5..8cdc48be703 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/resource_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/resource_controller.rb @@ -137,7 +137,7 @@ class ResourceController < ApplicationController 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 @@ -201,8 +201,6 @@ class ResourceController < ApplicationController if date conditions+=' AND created_at>?' values<<date.advance(:minutes => 1) - else - conditions+=' AND id=-1' end end @@ -228,9 +226,9 @@ class ResourceController < ApplicationController 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 diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 3704dbf3e2c..9eddc51e56a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -90,9 +90,17 @@ module ApplicationHelper 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 diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb index 8b880481bde..9e258c7e108 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb @@ -50,7 +50,11 @@ module DashboardHelper 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 |