From: Simon Brandhof Date: Fri, 20 Jun 2014 07:48:24 +0000 (+0200) Subject: Fix compatibility of hamcrest with java 6 X-Git-Tag: 4.4-RC1~274 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=59dadf101b95c690312877e2164413a6b2274606;p=sonarqube.git Fix compatibility of hamcrest with java 6 --- diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerTestUtils.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerTestUtils.java index 55e7d54ae1b..944c34b3e95 100644 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerTestUtils.java +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerTestUtils.java @@ -19,8 +19,6 @@ */ package org.sonar.plugins.dbcleaner; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.sonar.api.utils.DateUtils; import org.sonar.core.purge.PurgeableSnapshotDto; @@ -37,25 +35,10 @@ public final class DbCleanerTestUtils { } public static PurgeableSnapshotDto createSnapshotWithDateTime(long snapshotId, String datetime) { - PurgeableSnapshotDto snapshot = new PurgeableSnapshotDto(); - snapshot.setSnapshotId(snapshotId); - snapshot.setDate(DateUtils.parseDateTime(datetime)); - return snapshot; - } - - public static final class SnapshotMatcher extends BaseMatcher { - long snapshotId; - - public SnapshotMatcher(long snapshotId) { - this.snapshotId = snapshotId; - } - - public boolean matches(Object o) { - return ((PurgeableSnapshotDto) o).getSnapshotId() == snapshotId; - } - - public void describeTo(Description description) { - description.appendText("snapshotId").appendValue(snapshotId); - } + PurgeableSnapshotDto snapshot = new PurgeableSnapshotDto(); + snapshot.setSnapshotId(snapshotId); + snapshot.setDate(DateUtils.parseDateTime(datetime)); + return snapshot; } + } diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DeleteAllFilterTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DeleteAllFilterTest.java index 5367c3675b8..12967c16951 100644 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DeleteAllFilterTest.java +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DeleteAllFilterTest.java @@ -27,9 +27,7 @@ import org.sonar.plugins.dbcleaner.DbCleanerTestUtils; import java.util.Arrays; import java.util.List; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.hamcrest.Matchers.hasItem; +import static org.fest.assertions.Assertions.assertThat; public class DeleteAllFilterTest { @@ -37,14 +35,12 @@ public class DeleteAllFilterTest { public void shouldDeleteAllSnapshotsPriorToDate() { Filter filter = new DeleteAllFilter(DateUtils.parseDate("2011-12-25")); - List toDelete = filter.filter(Arrays.asList( + List toDelete = filter.filter(Arrays.asList( DbCleanerTestUtils.createSnapshotWithDate(1L, "2010-01-01"), DbCleanerTestUtils.createSnapshotWithDate(2L, "2010-12-25"), DbCleanerTestUtils.createSnapshotWithDate(3L, "2012-01-01") - )); + )); - assertThat(toDelete.size(), is(2)); - assertThat(toDelete, hasItem(new DbCleanerTestUtils.SnapshotMatcher(1L))); - assertThat(toDelete, hasItem(new DbCleanerTestUtils.SnapshotMatcher(2L))); + assertThat(toDelete).onProperty("snapshotId").containsOnly(1L, 2L); } } diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/KeepOneFilterTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/KeepOneFilterTest.java index cd1624644a5..937133577cf 100644 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/KeepOneFilterTest.java +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/KeepOneFilterTest.java @@ -19,18 +19,15 @@ */ package org.sonar.plugins.dbcleaner.period; -import org.hamcrest.core.IsCollectionContaining; import org.junit.Test; import org.sonar.api.utils.DateUtils; import org.sonar.core.purge.PurgeableSnapshotDto; -import org.sonar.plugins.dbcleaner.DbCleanerTestUtils; import java.util.Arrays; import java.util.Calendar; import java.util.List; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; import static org.sonar.plugins.dbcleaner.DbCleanerTestUtils.createSnapshotWithDate; public class KeepOneFilterTest { @@ -39,7 +36,7 @@ public class KeepOneFilterTest { public void shouldOnlyOneSnapshotPerInterval() { Filter filter = new KeepOneFilter(DateUtils.parseDate("2011-03-25"), DateUtils.parseDate("2011-08-25"), Calendar.MONTH, "month"); - List toDelete = filter.filter(Arrays.asList( + List toDelete = filter.filter(Arrays.asList( createSnapshotWithDate(1L, "2010-01-01"), // out of scope -> keep createSnapshotWithDate(2L, "2011-05-01"), // may -> keep createSnapshotWithDate(3L, "2011-05-02"), // may -> to be deleted @@ -48,9 +45,9 @@ public class KeepOneFilterTest { createSnapshotWithDate(6L, "2012-01-01") // out of scope -> keep )); - assertThat(toDelete.size(), is(2)); - assertThat(toDelete, IsCollectionContaining.hasItem(new DbCleanerTestUtils.SnapshotMatcher(3L))); - assertThat(toDelete, IsCollectionContaining.hasItem(new DbCleanerTestUtils.SnapshotMatcher(4L))); + assertThat(toDelete).hasSize(2); + assertThat(getById(toDelete, 3L)).isNotNull(); + assertThat(getById(toDelete, 4L)).isNotNull(); } @Test @@ -64,15 +61,24 @@ public class KeepOneFilterTest { createSnapshotWithDate(4L, "2011-05-23") // to be deleted )); - assertThat(toDelete.size(), is(2)); - assertThat(toDelete, IsCollectionContaining.hasItem(new DbCleanerTestUtils.SnapshotMatcher(1L))); - assertThat(toDelete, IsCollectionContaining.hasItem(new DbCleanerTestUtils.SnapshotMatcher(4L))); + assertThat(toDelete).hasSize(2); + assertThat(getById(toDelete, 1L)).isNotNull(); + assertThat(getById(toDelete, 4L)).isNotNull(); } @Test public void test_isDeletable() { - assertThat(KeepOneFilter.isDeletable(createSnapshotWithDate(1L, "2011-05-01")), is(true)); - assertThat(KeepOneFilter.isDeletable(createSnapshotWithDate(1L, "2011-05-01").setLast(true)), is(false)); - assertThat(KeepOneFilter.isDeletable(createSnapshotWithDate(1L, "2011-05-01").setHasEvents(true)), is(false)); + assertThat(KeepOneFilter.isDeletable(createSnapshotWithDate(1L, "2011-05-01"))).isTrue(); + assertThat(KeepOneFilter.isDeletable(createSnapshotWithDate(1L, "2011-05-01").setLast(true))).isFalse(); + assertThat(KeepOneFilter.isDeletable(createSnapshotWithDate(1L, "2011-05-01").setHasEvents(true))).isFalse(); + } + + private static PurgeableSnapshotDto getById(List snapshots, long id) { + for (PurgeableSnapshotDto snapshot : snapshots) { + if (snapshot.getSnapshotId() == id) { + return snapshot; + } + } + return null; } } diff --git a/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java b/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java index fc918286a03..fa19f41100a 100644 --- a/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java @@ -19,8 +19,6 @@ */ package org.sonar.core.purge; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.Before; import org.junit.Test; import org.sonar.api.resources.Scopes; @@ -31,9 +29,7 @@ import org.sonar.core.resource.ResourceDao; import java.util.List; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.hamcrest.Matchers.hasItem; +import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -68,7 +64,7 @@ public class PurgeDaoTest extends AbstractDaoTestCase { @Test public void shouldDeleteHistoricalDataOfDirectoriesAndFiles() { setupData("shouldDeleteHistoricalDataOfDirectoriesAndFiles"); - dao.purge(new PurgeConfiguration(1L, new String[]{Scopes.DIRECTORY, Scopes.FILE}, 30)); + dao.purge(new PurgeConfiguration(1L, new String[] {Scopes.DIRECTORY, Scopes.FILE}, 30)); checkTables("shouldDeleteHistoricalDataOfDirectoriesAndFiles", "projects", "snapshots"); } @@ -91,10 +87,22 @@ public class PurgeDaoTest extends AbstractDaoTestCase { setupData("shouldSelectPurgeableSnapshots"); List snapshots = dao.selectPurgeableSnapshots(1L); - assertThat(snapshots, hasItem(new SnapshotMatcher(1L, true, false))); - assertThat(snapshots, hasItem(new SnapshotMatcher(4L, false, false))); - assertThat(snapshots, hasItem(new SnapshotMatcher(5L, false, true))); - assertThat(snapshots.size(), is(3)); + assertThat(snapshots).hasSize(3); + assertThat(getById(snapshots, 1L).isLast()).isTrue(); + assertThat(getById(snapshots, 1L).hasEvents()).isFalse(); + assertThat(getById(snapshots, 4L).isLast()).isFalse(); + assertThat(getById(snapshots, 4L).hasEvents()).isFalse(); + assertThat(getById(snapshots, 5L).isLast()).isFalse(); + assertThat(getById(snapshots, 5L).hasEvents()).isTrue(); + } + + private static PurgeableSnapshotDto getById(List snapshots, long id) { + for (PurgeableSnapshotDto snapshot : snapshots) { + if (snapshot.getSnapshotId() == id) { + return snapshot; + } + } + return null; } @Test @@ -117,28 +125,4 @@ public class PurgeDaoTest extends AbstractDaoTestCase { dao.purge(new PurgeConfiguration(1L, new String[0], 0)); checkTables("should_delete_all_closed_issues", "issues", "issue_changes"); } - - static final class SnapshotMatcher extends BaseMatcher { - long snapshotId; - boolean isLast; - boolean hasEvents; - - SnapshotMatcher(long snapshotId, boolean last, boolean hasEvents) { - this.snapshotId = snapshotId; - this.isLast = last; - this.hasEvents = hasEvents; - } - - public boolean matches(Object o) { - PurgeableSnapshotDto obj = (PurgeableSnapshotDto) o; - return obj.getSnapshotId() == snapshotId && obj.isLast() == isLast && obj.hasEvents() == hasEvents; - } - - public void describeTo(Description description) { - description - .appendText("snapshotId").appendValue(snapshotId) - .appendText("isLast").appendValue(isLast) - .appendText("hasEvents").appendValue(hasEvents); - } - } }