From cff65d9bbd2e9ed8542695daed8be62bc3026916 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Mon, 29 Nov 2010 21:01:04 +0000 Subject: [PATCH] SONAR-1450 add the parameter "boolean checkOlder" to ResourcePersister#getLastSnapshot() --- .../java/org/sonar/batch/BatchPluginRepository.java | 9 ++++----- .../sonar/batch/index/DefaultResourcePersister.java | 13 +++++++++---- .../org/sonar/batch/index/ResourcePersister.java | 6 +++++- .../org/sonar/batch/index/ViolationPersister.java | 2 +- .../org/sonar/batch/phases/UpdateStatusJob.java | 2 +- .../sonar/batch/index/ViolationPersisterTest.java | 2 +- ...napshotIsNotUpdatedWhenAnalyzingPastSnapshot.xml | 2 +- 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/BatchPluginRepository.java index f4ef2689516..63e3a957e05 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/BatchPluginRepository.java +++ b/sonar-batch/src/main/java/org/sonar/batch/BatchPluginRepository.java @@ -19,10 +19,7 @@ */ package org.sonar.batch; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.List; - +import com.google.common.collect.Lists; import org.apache.commons.configuration.Configuration; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; @@ -42,7 +39,9 @@ import org.sonar.core.plugin.JpaPlugin; import org.sonar.core.plugin.JpaPluginDao; import org.sonar.core.plugin.JpaPluginFile; -import com.google.common.collect.Lists; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; public class BatchPluginRepository extends AbstractPluginRepository { diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java index 05f5ef18803..fb626dc498a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java @@ -168,11 +168,16 @@ public final class DefaultResourcePersister implements ResourcePersister { return snapshot; } - public Snapshot getPreviousLastSnapshot(Snapshot snapshot) { - Query query = session.createQuery( - "SELECT s FROM " + Snapshot.class.getSimpleName() + " s " + - "WHERE s.last=true AND s.resourceId=:resourceId"); + public Snapshot getLastSnapshot(Snapshot snapshot, boolean onlyOlder) { + String hql = "SELECT s FROM " + Snapshot.class.getSimpleName() + " s WHERE s.last=true AND s.resourceId=:resourceId"; + if (onlyOlder) { + hql += " AND s.createdAt<:date"; + } + Query query = session.createQuery(hql); query.setParameter("resourceId", snapshot.getResourceId()); + if (onlyOlder) { + query.setParameter("date", snapshot.getCreatedAt()); + } return session.getSingleResult(query, null); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java index 6a332562cd3..644c3261b21 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java @@ -30,7 +30,11 @@ public interface ResourcePersister { Snapshot saveResource(Project project, Resource resource); - Snapshot getPreviousLastSnapshot(Snapshot snapshot); + /** + * The current snapshot which is flagged as "last" + * @param onlyOlder true if the result must be anterior to the snapshot parameter + */ + Snapshot getLastSnapshot(Snapshot snapshot, boolean onlyOlder); void clear(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java index 934aaadb7bd..ef085b35680 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java @@ -45,7 +45,7 @@ public final class ViolationPersister { public List getPreviousViolations(Resource resource) { Snapshot snapshot = resourcePersister.getSnapshot(resource); - Snapshot previousLastSnapshot = resourcePersister.getPreviousLastSnapshot(snapshot); + Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true); if (previousLastSnapshot == null) { return Collections.emptyList(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java b/sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java index 80c3368816b..5c04142aadc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java @@ -43,7 +43,7 @@ public class UpdateStatusJob implements BatchComponent { } public void execute() { - Snapshot previousLastSnapshot = resourcePersister.getPreviousLastSnapshot(snapshot); + Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, false); updateFlags(snapshot, previousLastSnapshot); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java index f4b3164345f..d4e60a18baf 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java @@ -54,7 +54,7 @@ public class ViolationPersisterTest extends AbstractDbUnitTestCase { Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", 1000); ResourcePersister resourcePersister = mock(ResourcePersister.class); when(resourcePersister.saveResource((Project) anyObject(), eq(javaFile))).thenReturn(snapshot); - when(resourcePersister.getPreviousLastSnapshot(snapshot)).thenReturn(snapshot); + when(resourcePersister.getLastSnapshot(snapshot, true)).thenReturn(snapshot); when(resourcePersister.getSnapshot(javaFile)).thenReturn(snapshot); violationPersister = new ViolationPersister(getSession(), resourcePersister, new DefaultRuleFinder(getSessionFactory())); } diff --git a/sonar-batch/src/test/resources/org/sonar/batch/phases/UpdateStatusJobTest/lastSnapshotIsNotUpdatedWhenAnalyzingPastSnapshot.xml b/sonar-batch/src/test/resources/org/sonar/batch/phases/UpdateStatusJobTest/lastSnapshotIsNotUpdatedWhenAnalyzingPastSnapshot.xml index 523a66915f4..94f589a471f 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/phases/UpdateStatusJobTest/lastSnapshotIsNotUpdatedWhenAnalyzingPastSnapshot.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/phases/UpdateStatusJobTest/lastSnapshotIsNotUpdatedWhenAnalyzingPastSnapshot.xml @@ -27,7 +27,7 @@ path="[null]"/> - +