From 66a964aca512b5bced3fae1fffea1e8305754dd6 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 23 May 2012 00:23:45 +0200 Subject: [PATCH] SONAR-3407 fail analysis if the project version used for comparison does not exist --- .../PastSnapshotFinderByVersion.java | 4 ++-- .../PastSnapshotFinderByVersionTest.java | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) 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 190d621e4dc..b3fd56a696e 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 @@ -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(); 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 d3d0ef92555..9761c2f06a3 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 @@ -19,16 +19,19 @@ */ 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"); } } -- 2.39.5