diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2012-12-03 14:16:44 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2012-12-03 14:16:50 +0100 |
commit | a582af4ead0750c583aa1708450469a8258397db (patch) | |
tree | 29e51b5f8444e4cd3bbf575368cff191d5848e19 /sonar-core | |
parent | 7c48d7d02be54043f6ec236abf422fe265fc5a69 (diff) | |
download | sonarqube-a582af4ead0750c583aa1708450469a8258397db.tar.gz sonarqube-a582af4ead0750c583aa1708450469a8258397db.zip |
SONAR-3306 Improve unit test to fix issue on it-sonar-persistence tests
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java | 19 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/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 { |