]> source.dussan.org Git - sonarqube.git/commitdiff
Fix compatibility of hamcrest with java 6
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 20 Jun 2014 07:48:24 +0000 (09:48 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 20 Jun 2014 07:48:33 +0000 (09:48 +0200)
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerTestUtils.java
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DeleteAllFilterTest.java
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/KeepOneFilterTest.java
sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java

index 55e7d54ae1bd2760649cd1bd638ad568b12ddffc..944c34b3e95821c320ac8dcfd8a51d8275df7bac 100644 (file)
@@ -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<PurgeableSnapshotDto> {
-    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;
   }
+
 }
index 5367c3675b8fa6caa7f6636c7ccd557feb31281a..12967c1695134f847964d862bf438a03542f583a 100644 (file)
@@ -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<PurgeableSnapshotDto> toDelete = filter.filter(Arrays.<PurgeableSnapshotDto>asList(
+    List<PurgeableSnapshotDto> 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);
   }
 }
index cd1624644a5318fed40b07c52197247b869f354c..937133577cf2158f22349e0d89d776ed1917d5bb 100644 (file)
  */
 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<PurgeableSnapshotDto> toDelete = filter.filter(Arrays.<PurgeableSnapshotDto>asList(
+    List<PurgeableSnapshotDto> 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<PurgeableSnapshotDto> snapshots, long id) {
+    for (PurgeableSnapshotDto snapshot : snapshots) {
+      if (snapshot.getSnapshotId() == id) {
+        return snapshot;
+      }
+    }
+    return null;
   }
 }
index fc918286a033179f0ed0288dec3c253212cc38bf..fa19f41100a358cbe58549b3b1759824ddc5fdb3 100644 (file)
@@ -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<PurgeableSnapshotDto> 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<PurgeableSnapshotDto> 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<PurgeableSnapshotDto> {
-    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);
-    }
-  }
 }