]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1450 add the parameter "boolean checkOlder" to ResourcePersister#getLastSnapshot()
authorsimonbrandhof <simon.brandhof@gmail.com>
Mon, 29 Nov 2010 21:01:04 +0000 (21:01 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Mon, 29 Nov 2010 21:01:04 +0000 (21:01 +0000)
sonar-batch/src/main/java/org/sonar/batch/BatchPluginRepository.java
sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java
sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java
sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java
sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java
sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java
sonar-batch/src/test/resources/org/sonar/batch/phases/UpdateStatusJobTest/lastSnapshotIsNotUpdatedWhenAnalyzingPastSnapshot.xml

index f4ef2689516fd1b3550b8f3652cb62bc316b9f22..63e3a957e05ec8fba9295803f59ecb63e70328ad 100644 (file)
  */
 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 {
 
index 05f5ef188031a7571ab87b84e87e56a05ae1a924..fb626dc498ac9c3931477f2619472cbd6b7e3334 100644 (file)
@@ -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);
   }
 
index 6a332562cd3e96606984c098c4c6335b6a2bb237..644c3261b2103f0cdf256f19291d5d2e30f09ec8 100644 (file)
@@ -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();
 }
index 934aaadb7bdef2ade21fa63aef07bb8caa381634..ef085b35680289669564c02fa96e8968fbf6702f 100644 (file)
@@ -45,7 +45,7 @@ public final class ViolationPersister {
 
   public List<RuleFailureModel> 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();
     }
index 80c3368816b006a37d7804ac3a01147cb1e44069..5c04142aadc5370c588e16121c13a6c76c31a3cd 100644 (file)
@@ -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);
   }
 
index f4b3164345f8766e3cb921a7d294821ddeaf6617..d4e60a18baf1cfea93f74e912f421658f981d02e 100644 (file)
@@ -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()));
   }
index 523a66915f4f5f9587b54e0215a6e2710088cc8a..94f589a471f921243edf3d74aa63ed95d5e1dc70 100644 (file)
@@ -27,7 +27,7 @@
              path="[null]"/>
 
 
-  <!-- the snapshot to process -->
+  <!-- the snapshot to process : before last snapshot -->
   <snapshots depth="[null]" id="6" scope="PRJ" qualifier="TRK" created_at="2005-10-01 00:00:00.00" version="[null]"
              project_id="1"
               parent_snapshot_id="[null]" root_project_id="[null]" root_snapshot_id="[null]" status="[null]"