From: Julien Lancelot Date: Mon, 3 Dec 2012 13:04:04 +0000 (+0100) Subject: SONAR-3306 Improve unit test to fix issue on it-sonar-persistence tests X-Git-Tag: 3.4~210 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f629d8a518be51db00d1abc84ff043676ed51582;p=sonarqube.git SONAR-3306 Improve unit test to fix issue on it-sonar-persistence tests --- 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 28e13c08d16..9281c78536c 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 @@ -47,8 +47,8 @@ public class SemaphoreDao { Date lockedAt = org.sonar.api.utils.DateUtils.parseDate("2001-01-01"); createSemaphore(name, lockedAt, session, mapper); boolean isAcquired = doAcquire(name, maxDurationInSeconds, session, mapper); - SemaphoreDto semaphore = mapper.selectSemaphore(name); - return createLock(semaphore, mapper, isAcquired); + SemaphoreDto semaphore = selectSemaphore(name, session); + return createLock(semaphore, session, isAcquired); } finally { MyBatis.closeQuietly(session); } @@ -60,13 +60,13 @@ public class SemaphoreDao { SqlSession session = mybatis.openSession(); try { SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class); - SemaphoreDto semaphore = mapper.selectSemaphore(name); + SemaphoreDto semaphore = selectSemaphore(name, session); Date now = mapper.now(); if (semaphore != null) { - return createLock(semaphore, mapper, false); + return createLock(semaphore, session, false); } else { semaphore = createSemaphore(name, now, session, mapper); - return createLock(semaphore, mapper, true); + return createLock(semaphore, session, true); } } finally { MyBatis.closeQuietly(session); @@ -106,18 +106,29 @@ public class SemaphoreDao { } } - private Lock createLock(SemaphoreDto semaphore, SemaphoreMapper mapper, boolean acquired) { + private Lock createLock(SemaphoreDto semaphore, SqlSession session, boolean acquired) { Lock lock = new Lock(semaphore.getName(), acquired, semaphore.getLockedAt(), semaphore.getCreatedAt(), semaphore.getUpdatedAt()); if (!acquired) { - lock.setDurationSinceLocked(getDurationSinceLocked(semaphore, mapper)); + lock.setDurationSinceLocked(getDurationSinceLocked(semaphore, session)); } return lock; } - private long getDurationSinceLocked(SemaphoreDto semaphore, SemaphoreMapper mapper) { - long now = mapper.now().getTime(); + private long getDurationSinceLocked(SemaphoreDto semaphore, SqlSession session) { + long now = now(session).getTime(); semaphore.getLockedAt(); long locketAt = semaphore.getLockedAt().getTime(); return now - locketAt; } + + protected SemaphoreDto selectSemaphore(String name, SqlSession session){ + SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class); + 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 d0a97e07655..d73f3cacbb9 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 @@ -20,14 +20,11 @@ package org.sonar.core.persistence; import org.apache.commons.lang.time.DateUtils; +import org.apache.ibatis.session.SqlSession; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Timestamp; import java.util.Date; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; @@ -74,12 +71,12 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { assertThat(lock.isAcquired()).isTrue(); assertThat(lock.getDurationSinceLocked()).isNull(); - Semaphore semaphore = selectSemaphore("foo"); + SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); - assertThat(semaphore.name).isEqualTo("foo"); - assertThat(isRecent(semaphore.createdAt, 60)).isTrue(); - assertThat(isRecent(semaphore.updatedAt, 60)).isTrue(); - assertThat(isRecent(semaphore.lockedAt, 60)).isTrue(); + assertThat(semaphore.getName()).isEqualTo("foo"); + assertThat(isRecent(semaphore.getCreatedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); dao.release("foo"); assertThat(selectSemaphore("foo")).isNull(); @@ -92,12 +89,12 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { assertThat(lock.isAcquired()).isTrue(); assertThat(lock.getDurationSinceLocked()).isNull(); - Semaphore semaphore = selectSemaphore("foo"); + SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); - assertThat(semaphore.name).isEqualTo("foo"); - assertThat(isRecent(semaphore.createdAt, 60)).isTrue(); - assertThat(isRecent(semaphore.updatedAt, 60)).isTrue(); - assertThat(isRecent(semaphore.lockedAt, 60)).isTrue(); + assertThat(semaphore.getName()).isEqualTo("foo"); + assertThat(isRecent(semaphore.getCreatedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); dao.release("foo"); assertThat(selectSemaphore("foo")).isNull(); @@ -110,12 +107,12 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { assertThat(lock.isAcquired()).isTrue(); assertThat(lock.getDurationSinceLocked()).isNull(); - Semaphore semaphore = selectSemaphore("foo"); + SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); - assertThat(semaphore.name).isEqualTo("foo"); - assertThat(isRecent(semaphore.createdAt, 60)).isTrue(); - assertThat(isRecent(semaphore.updatedAt, 60)).isTrue(); - assertThat(isRecent(semaphore.lockedAt, 60)).isTrue(); + assertThat(semaphore.getName()).isEqualTo("foo"); + assertThat(isRecent(semaphore.getCreatedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); dao.release("foo"); assertThat(selectSemaphore("foo")).isNull(); @@ -129,12 +126,12 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { assertThat(lock.isAcquired()).isFalse(); assertThat(lock.getDurationSinceLocked()).isNotNull(); - Semaphore semaphore = selectSemaphore("foo"); + SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); - assertThat(semaphore.name).isEqualTo("foo"); - assertThat(isRecent(semaphore.createdAt, 60)).isFalse(); - assertThat(isRecent(semaphore.updatedAt, 60)).isFalse(); - assertThat(isRecent(semaphore.lockedAt, 60)).isFalse(); + assertThat(semaphore.getName()).isEqualTo("foo"); + assertThat(isRecent(semaphore.getCreatedAt(), 60)).isFalse(); + assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isFalse(); + assertThat(isRecent(semaphore.getLockedAt(), 60)).isFalse(); } @Test @@ -145,12 +142,12 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { assertThat(lock.isAcquired()).isTrue(); assertThat(lock.getDurationSinceLocked()).isNull(); - Semaphore semaphore = selectSemaphore("foo"); + SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); - assertThat(semaphore.name).isEqualTo("foo"); - assertThat(isRecent(semaphore.createdAt, 60)).isFalse(); - assertThat(isRecent(semaphore.updatedAt, 60)).isTrue(); - assertThat(isRecent(semaphore.lockedAt, 60)).isTrue(); + assertThat(semaphore.getName()).isEqualTo("foo"); + assertThat(isRecent(semaphore.getCreatedAt(), 60)).isFalse(); + assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); } @Test @@ -161,12 +158,12 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { assertThat(lock.isAcquired()).isTrue(); assertThat(lock.getDurationSinceLocked()).isNull(); - Semaphore semaphore = selectSemaphore("foo"); + SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); - assertThat(semaphore.name).isEqualTo("foo"); - assertThat(isRecent(semaphore.createdAt, 60)).isFalse(); - assertThat(isRecent(semaphore.updatedAt, 60)).isTrue(); - assertThat(isRecent(semaphore.lockedAt, 60)).isTrue(); + assertThat(semaphore.getName()).isEqualTo("foo"); + assertThat(isRecent(semaphore.getCreatedAt(), 60)).isFalse(); + assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isTrue(); + assertThat(isRecent(semaphore.getLockedAt(), 60)).isTrue(); dao.release("foo"); assertThat(selectSemaphore("foo")).isNull(); @@ -180,12 +177,25 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { assertThat(lock.isAcquired()).isFalse(); assertThat(lock.getDurationSinceLocked()).isNotNull(); - Semaphore semaphore = selectSemaphore("foo"); + SemaphoreDto semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); - assertThat(semaphore.name).isEqualTo("foo"); - assertThat(isRecent(semaphore.createdAt, 60)).isFalse(); - assertThat(isRecent(semaphore.updatedAt, 60)).isFalse(); - assertThat(isRecent(semaphore.lockedAt, 60)).isFalse(); + assertThat(semaphore.getName()).isEqualTo("foo"); + assertThat(isRecent(semaphore.getCreatedAt(), 60)).isFalse(); + assertThat(isRecent(semaphore.getUpdatedAt(), 60)).isFalse(); + assertThat(isRecent(semaphore.getLockedAt(), 60)).isFalse(); + } + + @Test + public void should_select_semaphore_return_current_semaphore_when_acquiring() throws Exception { + SemaphoreDao dao = new SemaphoreDao(getMyBatis()); + dao.acquire("foo"); + + SemaphoreDto semaphore = dao.selectSemaphore("foo", getMyBatis().openSession()); + assertThat(semaphore).isNotNull(); + assertThat(semaphore.getName()).isEqualTo("foo"); + assertThat(semaphore.getCreatedAt()).isNotNull(); + assertThat(semaphore.getUpdatedAt()).isNotNull(); + assertThat(semaphore.getLockedAt()).isNotNull(); } @Test @@ -209,41 +219,27 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { } } - private Semaphore selectSemaphore(String name) throws Exception { - Connection connection = getConnection(); - PreparedStatement statement = null; - ResultSet rs = null; + private SemaphoreDto selectSemaphore(String name) throws Exception { + SqlSession session = getMyBatis().openSession(); try { - statement = connection.prepareStatement("SELECT * FROM semaphores WHERE name='" + name + "'"); - rs = statement.executeQuery(); - if (rs.next()) { - return new Semaphore(rs.getString("name"), rs.getTimestamp("created_at"), rs.getTimestamp("updated_at"), rs.getTimestamp("locked_at")); - } - return null; + SemaphoreDao dao = new SemaphoreDao(getMyBatis()); + return dao.selectSemaphore(name, session); } finally { - DatabaseUtils.closeQuietly(rs); - DatabaseUtils.closeQuietly(statement); - DatabaseUtils.closeQuietly(connection); + MyBatis.closeQuietly(session); } } - private static class Semaphore { - String name; - Date createdAt, updatedAt, lockedAt; - - private Semaphore(String name, Timestamp createdAt, Timestamp updatedAt, Timestamp lockedAt) { - this.name = name; - this.createdAt = createdAt; - this.updatedAt = updatedAt; - this.lockedAt = lockedAt; + 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 durationInSeconds) { - Date now = new Date(); - return date.before(now) && DateUtils.addSeconds(date, durationInSeconds).after(now); - } - private static class Runner extends Thread { SemaphoreDao dao; AtomicInteger locks;