]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3407 fail analysis if the project version used for comparison does not exist
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 22 May 2012 22:23:45 +0000 (00:23 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 22 May 2012 22:23:45 +0000 (00:23 +0200)
sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java
sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java

index 190d621e4dcb7e218e434189c94d566295694e9c..b3fd56a696ea986235dd47666475cc85c4e31d35 100644 (file)
@@ -30,7 +30,7 @@ import java.util.List;
 
 public class PastSnapshotFinderByVersion implements BatchExtension {
 
-  private DatabaseSession session;
+  private final DatabaseSession session;
 
   public PastSnapshotFinderByVersion(DatabaseSession session) {
     this.session = session;
@@ -47,7 +47,7 @@ public class PastSnapshotFinderByVersion implements BatchExtension {
         .getResultList();
 
     if (snapshots.isEmpty()) {
-      return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION);
+      throw new IllegalStateException("Unknown project version: " + version);
     }
     Snapshot snapshot = snapshots.get(0);
     Date targetDate = snapshot.getCreatedAt();
index d3d0ef92555fe94ae84fad80bd5c4585442a922d..9761c2f06a3017060d2153db3e632b6a15b32af5 100644 (file)
  */
 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.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.sonar.api.database.model.Snapshot;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
+import static org.fest.assertions.Assertions.assertThat;
+
 public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase {
 
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
   @Test
   public void shouldFindByVersion() {
     setupData("shared");
@@ -36,19 +39,19 @@ public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase {
     Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010);
     PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(getSession());
 
-    assertThat(finder.findByVersion(currentProjectSnapshot, "1.1").getProjectSnapshotId(), is(1009));
+    assertThat(finder.findByVersion(currentProjectSnapshot, "1.1").getProjectSnapshotId()).isEqualTo(1009);
   }
 
   @Test
-  public void shouldReturnPastSnapshotEvenWhenNoPreviousAnalysis() {
+  public void failIfUnknownVersion() {
+    thrown.expect(IllegalStateException.class);
+    thrown.expectMessage("Unknown project version: 0.1.2");
+
     setupData("shared");
 
     Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010);
     PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(getSession());
 
-    PastSnapshot pastSnapshot = finder.findByVersion(currentProjectSnapshot, "1.0");
-    assertThat(pastSnapshot.isRelatedToSnapshot(), is(false));
-    assertThat(pastSnapshot.getProjectSnapshot(), nullValue());
-    assertThat(pastSnapshot.getTargetDate(), nullValue());
+    finder.findByVersion(currentProjectSnapshot, "0.1.2");
   }
 }