From aa7da2d32a3fc19b092d0eefd17d13d0a7b2791e Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Fri, 25 May 2012 09:46:38 +0200 Subject: [PATCH] SONAR-3308 Revert back changes => Too many side effects => Still need to handle the original exception --- .../core/sensors/VersionEventsSensor.java | 16 +----- .../core/sensors/VersionEventsSensorTest.java | 55 +++++-------------- 2 files changed, 16 insertions(+), 55 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/VersionEventsSensor.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/VersionEventsSensor.java index 39bb7264db3..0c4825fc77a 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/VersionEventsSensor.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/VersionEventsSensor.java @@ -31,8 +31,6 @@ import java.util.Iterator; @NotDryRun public class VersionEventsSensor implements Sensor { - private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT"; - public boolean shouldExecuteOnProject(Project project) { return true; } @@ -47,19 +45,11 @@ public class VersionEventsSensor implements Sensor { private void deleteDeprecatedEvents(Project project, SensorContext context) { String version = project.getAnalysisVersion(); - boolean isReleaseVersion = !version.endsWith(SNAPSHOT_SUFFIX); - String snapshotVersionToDelete = isReleaseVersion ? version + SNAPSHOT_SUFFIX : ""; for (Iterator it = context.getEvents(project).iterator(); it.hasNext();) { Event event = it.next(); - if (event.isVersionCategory()) { - if (snapshotVersionToDelete.equals(event.getName()) || (version.equals(event.getName()) && !isReleaseVersion)) { - it.remove(); - context.deleteEvent(event); - } else if (version.equals(event.getName()) && isReleaseVersion) { - // we try to delete a released version that already exists in the project history => this shouldn't happen - throw new IllegalStateException("A Sonar analysis can't delete a released version that already exists in the project history (version " - + version + "). Please change the version of the project or clean its history first."); - } + if (event.isVersionCategory() && version.equals(event.getName())) { + it.remove(); + context.deleteEvent(event); } } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/VersionEventsSensorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/VersionEventsSensorTest.java index b92dbf587cf..c1dbd67d4f1 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/VersionEventsSensorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/VersionEventsSensorTest.java @@ -30,6 +30,8 @@ import org.sonar.api.resources.Resource; import java.util.Date; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; @@ -44,6 +46,16 @@ public class VersionEventsSensorTest { @Rule public ExpectedException thrown = ExpectedException.none(); + @Test + public void shouldExecuteOnProject() throws Exception { + assertThat(new VersionEventsSensor().shouldExecuteOnProject(null), is(true)); + } + + @Test + public void testToString() throws Exception { + assertThat(new VersionEventsSensor().toString(), is("VersionEventsSensor")); + } + @Test public void shouldDoNothingIfNoVersion() { VersionEventsSensor sensor = new VersionEventsSensor(); @@ -90,52 +102,11 @@ public class VersionEventsSensorTest { verify(context).createEvent(eq(project), eq("1.5-SNAPSHOT"), (String) isNull(), eq(Event.CATEGORY_VERSION), (Date) isNull()); } - @Test - public void shouldDeleteAssociatedSnapshotWhenReleasing() { - Event snapshotVersion = mockVersionEvent("1.5-SNAPSHOT"); - Event otherEvent = mockVersionEvent("1.4"); - - SensorContext context = mock(SensorContext.class); - Project project = mock(Project.class); - when(project.getAnalysisVersion()).thenReturn("1.5"); - when(context.getEvents(project)).thenReturn(Lists.newArrayList(snapshotVersion, otherEvent)); - - VersionEventsSensor sensor = new VersionEventsSensor(); - sensor.analyse(project, context); - - verify(context).deleteEvent(snapshotVersion); - verify(context).createEvent(eq(project), eq("1.5"), (String) isNull(), eq(Event.CATEGORY_VERSION), (Date) isNull()); - } - - @Test - public void shouldFailIfTryingToDeleteReleasedVersion() { - // Given - Event snapshotEvent1 = mockVersionEvent("1.5-SNAPSHOT"); - Event releaseEvent2 = mockVersionEvent("1.4"); - Event snapshotEvent2 = mockVersionEvent("1.3-SNAPSHOT"); - - VersionEventsSensor sensor = new VersionEventsSensor(); - SensorContext context = mock(SensorContext.class); - - Project project = mock(Project.class); - when(project.getAnalysisVersion()).thenReturn("1.4"); - - when(context.getEvents(project)).thenReturn(Lists.newArrayList(snapshotEvent1, releaseEvent2, snapshotEvent2)); - - // Expect - thrown.expect(IllegalStateException.class); - thrown.expectMessage("A Sonar analysis can't delete a released version that already exists in the project history (version 1.4). " + - "Please change the version of the project or clean its history first."); - - // When - sensor.analyse(project, context); - - } - private Event mockVersionEvent(String version) { Event event = mock(Event.class); when(event.isVersionCategory()).thenReturn(true); when(event.getName()).thenReturn(version); return event; } + } -- 2.39.5