From a582af4ead0750c583aa1708450469a8258397db Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 3 Dec 2012 14:16:44 +0100 Subject: [PATCH] SONAR-3306 Improve unit test to fix issue on it-sonar-persistence tests --- .../sonar/core/persistence/SemaphoreDao.java | 19 +++---- .../core/persistence/SemaphoreDaoTest.java | 54 +++++++++---------- 2 files changed, 31 insertions(+), 42 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java index 9281c78536c..74dd6a3734c 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java @@ -48,7 +48,7 @@ public class SemaphoreDao { createSemaphore(name, lockedAt, session, mapper); boolean isAcquired = doAcquire(name, maxDurationInSeconds, session, mapper); SemaphoreDto semaphore = selectSemaphore(name, session); - return createLock(semaphore, session, isAcquired); + return createLock(semaphore, mapper, isAcquired); } finally { MyBatis.closeQuietly(session); } @@ -63,10 +63,10 @@ public class SemaphoreDao { SemaphoreDto semaphore = selectSemaphore(name, session); Date now = mapper.now(); if (semaphore != null) { - return createLock(semaphore, session, false); + return createLock(semaphore, mapper, false); } else { semaphore = createSemaphore(name, now, session, mapper); - return createLock(semaphore, session, true); + return createLock(semaphore, mapper, true); } } finally { MyBatis.closeQuietly(session); @@ -106,16 +106,16 @@ public class SemaphoreDao { } } - private Lock createLock(SemaphoreDto semaphore, SqlSession session, boolean acquired) { + private Lock createLock(SemaphoreDto semaphore, SemaphoreMapper mapper, boolean acquired) { Lock lock = new Lock(semaphore.getName(), acquired, semaphore.getLockedAt(), semaphore.getCreatedAt(), semaphore.getUpdatedAt()); if (!acquired) { - lock.setDurationSinceLocked(getDurationSinceLocked(semaphore, session)); + lock.setDurationSinceLocked(getDurationSinceLocked(semaphore, mapper)); } return lock; } - private long getDurationSinceLocked(SemaphoreDto semaphore, SqlSession session) { - long now = now(session).getTime(); + private long getDurationSinceLocked(SemaphoreDto semaphore, SemaphoreMapper mapper) { + long now = mapper.now().getTime(); semaphore.getLockedAt(); long locketAt = semaphore.getLockedAt().getTime(); return now - locketAt; @@ -126,9 +126,4 @@ public class SemaphoreDao { return mapper.selectSemaphore(name); } - protected Date now(SqlSession session){ - SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class); - return mapper.now(); - } - } diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java index d73f3cacbb9..3ae10efa307 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java @@ -74,9 +74,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt(), 60)).isTrue(); - assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); - assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getCreatedAt(), 10)).isTrue(); + assertThat(isRecent(semaphore.getUpdatedAt(), 10)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 10)).isTrue(); dao.release("foo"); assertThat(selectSemaphore("foo")).isNull(); @@ -92,9 +92,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt(), 60)).isTrue(); - assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); - assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getCreatedAt(), 10)).isTrue(); + assertThat(isRecent(semaphore.getUpdatedAt(), 10)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 10)).isTrue(); dao.release("foo"); assertThat(selectSemaphore("foo")).isNull(); @@ -110,9 +110,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt(), 60)).isTrue(); - assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); - assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getCreatedAt(), 10)).isTrue(); + assertThat(isRecent(semaphore.getUpdatedAt(), 10)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 10)).isTrue(); dao.release("foo"); assertThat(selectSemaphore("foo")).isNull(); @@ -129,9 +129,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt(), 60)).isFalse(); - assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isFalse(); - assertThat(isRecent(semaphore.getLockedAt(), 60)).isFalse(); + assertThat(isRecent(semaphore.getCreatedAt(), 10)).isFalse(); + assertThat(isRecent(semaphore.getUpdatedAt(), 10)).isFalse(); + assertThat(isRecent(semaphore.getLockedAt(), 10)).isFalse(); } @Test @@ -145,9 +145,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt(), 60)).isFalse(); - assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); - assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getCreatedAt(), 10)).isFalse(); + assertThat(isRecent(semaphore.getUpdatedAt(), 10)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 10)).isTrue(); } @Test @@ -161,9 +161,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt(), 60)).isFalse(); - assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); - assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getCreatedAt(), 10)).isFalse(); + assertThat(isRecent(semaphore.getUpdatedAt(), 10)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 10)).isTrue(); dao.release("foo"); assertThat(selectSemaphore("foo")).isNull(); @@ -180,9 +180,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt(), 60)).isFalse(); - assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isFalse(); - assertThat(isRecent(semaphore.getLockedAt(), 60)).isFalse(); + assertThat(isRecent(semaphore.getCreatedAt(), 10)).isFalse(); + assertThat(isRecent(semaphore.getUpdatedAt(), 10)).isFalse(); + assertThat(isRecent(semaphore.getLockedAt(), 10)).isFalse(); } @Test @@ -229,15 +229,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { } } - private boolean isRecent(Date date, int durationInSeconds) { - SqlSession session = getMyBatis().openSession(); - try { - SemaphoreDao dao = new SemaphoreDao(getMyBatis()); - Date now = dao.now(session); - return date.before(now) && DateUtils.addSeconds(date, durationInSeconds).after(now); - } finally { - MyBatis.closeQuietly(session); - } + private static boolean isRecent(Date date, int durationInMinutes) { + Date now = new Date(); + return date.before(now) && DateUtils.addSeconds(date, durationInMinutes*60).after(now); } private static class Runner extends Thread { -- 2.39.5