aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2012-12-04 09:12:46 +0100
committerJulien Lancelot <julien.lancelot@gmail.com>2012-12-04 09:12:46 +0100
commit1a0966368aeb94d234ea01aab23ab9bedb8ed15a (patch)
tree11f5137dd5bb99cba9047d21d3a2ad4dae82d80d /sonar-core
parente512e8600c652ac7703750d54350846dd32dc816 (diff)
downloadsonarqube-1a0966368aeb94d234ea01aab23ab9bedb8ed15a.tar.gz
sonarqube-1a0966368aeb94d234ea01aab23ab9bedb8ed15a.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.java26
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java47
2 files changed, 28 insertions, 45 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 74dd6a3734c..12100acf19a 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
@@ -45,10 +45,10 @@ public class SemaphoreDao {
try {
SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
Date lockedAt = org.sonar.api.utils.DateUtils.parseDate("2001-01-01");
- createSemaphore(name, lockedAt, session, mapper);
+ createSemaphore(name, lockedAt, session);
boolean isAcquired = doAcquire(name, maxDurationInSeconds, session, mapper);
SemaphoreDto semaphore = selectSemaphore(name, session);
- return createLock(semaphore, mapper, isAcquired);
+ return createLock(semaphore, session, 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, mapper, false);
+ return createLock(semaphore, session, false);
} else {
- semaphore = createSemaphore(name, now, session, mapper);
- return createLock(semaphore, mapper, true);
+ semaphore = createSemaphore(name, now, session);
+ return createLock(semaphore, session, true);
}
} finally {
MyBatis.closeQuietly(session);
@@ -91,8 +91,9 @@ public class SemaphoreDao {
return ok;
}
- private SemaphoreDto createSemaphore(String name, Date lockedAt, SqlSession session, SemaphoreMapper mapper) {
+ private SemaphoreDto createSemaphore(String name, Date lockedAt, SqlSession session) {
try {
+ SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
SemaphoreDto semaphore = new SemaphoreDto()
.setName(name)
.setLockedAt(lockedAt);
@@ -106,16 +107,16 @@ 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;
@@ -126,4 +127,9 @@ 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 4aae0802082..88b3a3c8764 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
@@ -21,15 +21,11 @@ package org.sonar.core.persistence;
import org.apache.commons.lang.time.DateUtils;
import org.apache.ibatis.session.SqlSession;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
@@ -39,7 +35,12 @@ import static org.fest.assertions.Assertions.assertThat;
public class SemaphoreDaoTest extends AbstractDaoTestCase {
- private static final Logger LOG = LoggerFactory.getLogger(SemaphoreDaoTest.class);
+ private SemaphoreDao dao;
+
+ @Before
+ public void before() {
+ dao = new SemaphoreDao(getMyBatis());
+ }
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -73,7 +74,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void create_and_acquire_semaphore() throws Exception {
- SemaphoreDao dao = new SemaphoreDao(getMyBatis());
Lock lock = dao.acquire("foo", 60);
assertThat(lock.isAcquired()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
@@ -91,7 +91,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void create_and_acquire_semaphore_when_timeout_is_zeo() throws Exception {
- SemaphoreDao dao = new SemaphoreDao(getMyBatis());
Lock lock = dao.acquire("foo", 0);
assertThat(lock.isAcquired()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
@@ -109,7 +108,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void create_and_acquire_semaphore_when_no_timeout() throws Exception {
- SemaphoreDao dao = new SemaphoreDao(getMyBatis());
Lock lock = dao.acquire("foo");
assertThat(lock.isAcquired()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
@@ -128,7 +126,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void fail_to_acquire_locked_semaphore() throws Exception {
setupData("old_semaphore");
- SemaphoreDao dao = new SemaphoreDao(getMyBatis());
Lock lock = dao.acquire("foo", Integer.MAX_VALUE);
assertThat(lock.isAcquired()).isFalse();
assertThat(lock.getDurationSinceLocked()).isNotNull();
@@ -144,7 +141,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void acquire_long_locked_semaphore() throws Exception {
setupData("old_semaphore");
- SemaphoreDao dao = new SemaphoreDao(getMyBatis());
Lock lock = dao.acquire("foo", 60);
assertThat(lock.isAcquired()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
@@ -160,7 +156,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void acquire_locked_semaphore_when_timeout_is_zeo() throws Exception {
setupData("old_semaphore");
- SemaphoreDao dao = new SemaphoreDao(getMyBatis());
Lock lock = dao.acquire("foo", 0);
assertThat(lock.isAcquired()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
@@ -179,7 +174,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void fail_to_acquire_locked_semaphore_when_no_timeout() throws Exception {
setupData("old_semaphore");
- SemaphoreDao dao = new SemaphoreDao(getMyBatis());
Lock lock = dao.acquire("foo");
assertThat(lock.isAcquired()).isFalse();
assertThat(lock.getDurationSinceLocked()).isNotNull();
@@ -194,7 +188,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@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());
@@ -207,8 +200,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void test_concurrent_locks() throws Exception {
- SemaphoreDao dao = new SemaphoreDao(getMyBatis());
-
for (int tests = 0; tests < 5; tests++) {
dao.release("my-lock");
int size = 5;
@@ -229,7 +220,6 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
private SemaphoreDto selectSemaphore(String name) throws Exception {
SqlSession session = getMyBatis().openSession();
try {
- SemaphoreDao dao = new SemaphoreDao(getMyBatis());
return dao.selectSemaphore(name, session);
} finally {
MyBatis.closeQuietly(session);
@@ -237,30 +227,17 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
}
private boolean isRecent(Date date) {
- Date futur = DateUtils.addMinutes(now(), 1);
+ Date future = DateUtils.addMinutes(now(), 1);
Date past = DateUtils.addDays(now(), -1);
- return date.after(past) && date.before(futur);
+ return date.after(past) && date.before(future);
}
private Date now() {
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet rs = null;
+ SqlSession session = getMyBatis().openSession();
try {
- connection = getConnection();
- statement = connection.prepareStatement("select current_timestamp");
- rs = statement.executeQuery();
- if (rs.next()) {
- return new Date();
- }
- return null;
-
- } catch (Exception e) {
- throw new RuntimeException(e);
+ return dao.now(session);
} finally {
- DatabaseUtils.closeQuietly(rs);
- DatabaseUtils.closeQuietly(statement);
- DatabaseUtils.closeQuietly(connection);
+ MyBatis.closeQuietly(session);
}
}