aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-12-14 18:27:29 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-12-14 18:27:29 +0100
commitaf07212b764549c16cd252a9311bb68c642e8179 (patch)
tree2276df7ea25c510e83da3c70e90d93f75e07e4b2 /sonar-core/src
parent189a12a09eb4fd34b632fb2582eb479e15a08125 (diff)
downloadsonarqube-af07212b764549c16cd252a9311bb68c642e8179.tar.gz
sonarqube-af07212b764549c16cd252a9311bb68c642e8179.zip
SONAR-3887 refactor semaphore API
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java34
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/SemaphoresImpl.java (renamed from sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java)10
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java32
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/SemaphoresImplTest.java (renamed from sonar-core/src/test/java/org/sonar/core/persistence/DatabaseSemaphoreImplTest.java)10
4 files changed, 45 insertions, 41 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 471315c2d40..3e36545012b 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
@@ -23,11 +23,10 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.apache.commons.lang.time.DateUtils;
import org.apache.ibatis.session.SqlSession;
+import org.sonar.api.utils.Semaphores;
import java.util.Date;
-import static org.sonar.api.utils.DatabaseSemaphore.Lock;
-
/**
* @since 3.4
*/
@@ -39,7 +38,7 @@ public class SemaphoreDao {
this.mybatis = mybatis;
}
- public Lock acquire(String name, int maxDurationInSeconds) {
+ public Semaphores.Semaphore acquire(String name, int maxDurationInSeconds) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Semaphore name must not be empty");
Preconditions.checkArgument(maxDurationInSeconds >= 0, "Semaphore max duration must be positive: " + maxDurationInSeconds);
@@ -47,7 +46,7 @@ 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);
+ createDto(name, lockedAt, session);
boolean isAcquired = doAcquire(name, maxDurationInSeconds, session, mapper);
SemaphoreDto semaphore = selectSemaphore(name, session);
return createLock(semaphore, session, isAcquired);
@@ -56,7 +55,7 @@ public class SemaphoreDao {
}
}
- public Lock acquire(String name) {
+ public Semaphores.Semaphore acquire(String name) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Semaphore name must not be empty");
SqlSession session = mybatis.openSession();
@@ -67,7 +66,7 @@ public class SemaphoreDao {
if (semaphore != null) {
return createLock(semaphore, session, false);
} else {
- semaphore = createSemaphore(name, now, session);
+ semaphore = createDto(name, now, session);
return createLock(semaphore, session, true);
}
} finally {
@@ -93,12 +92,12 @@ public class SemaphoreDao {
return ok;
}
- private SemaphoreDto createSemaphore(String name, Date lockedAt, SqlSession session) {
+ private SemaphoreDto createDto(String name, Date lockedAt, SqlSession session) {
try {
SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
SemaphoreDto semaphore = new SemaphoreDto()
- .setName(name)
- .setLockedAt(lockedAt);
+ .setName(name)
+ .setLockedAt(lockedAt);
mapper.initialize(semaphore);
session.commit();
return semaphore;
@@ -109,12 +108,17 @@ public class SemaphoreDao {
}
}
- private Lock createLock(SemaphoreDto semaphore, SqlSession session, boolean acquired) {
- Lock lock = new Lock(semaphore.getName(), acquired, semaphore.getLockedAt(), semaphore.getCreatedAt(), semaphore.getUpdatedAt());
+ private Semaphores.Semaphore createLock(SemaphoreDto dto, SqlSession session, boolean acquired) {
+ Semaphores.Semaphore semaphore = new Semaphores.Semaphore()
+ .setName(dto.getName())
+ .setLocked(acquired)
+ .setLocketAt(dto.getLockedAt())
+ .setCreatedAt(dto.getCreatedAt())
+ .setUpdatedAt(dto.getUpdatedAt());
if (!acquired) {
- lock.setDurationSinceLocked(getDurationSinceLocked(semaphore, session));
+ semaphore.setDurationSinceLocked(getDurationSinceLocked(dto, session));
}
- return lock;
+ return semaphore;
}
private long getDurationSinceLocked(SemaphoreDto semaphore, SqlSession session) {
@@ -124,12 +128,12 @@ public class SemaphoreDao {
return now - locketAt;
}
- protected SemaphoreDto selectSemaphore(String name, SqlSession session){
+ protected SemaphoreDto selectSemaphore(String name, SqlSession session) {
SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
return mapper.selectSemaphore(name);
}
- protected Date now(SqlSession session){
+ protected Date now(SqlSession session) {
SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
return mapper.now();
}
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoresImpl.java
index 75a5bb7e2ed..b69cff90448 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoresImpl.java
@@ -19,24 +19,24 @@
*/
package org.sonar.core.persistence;
-import org.sonar.api.utils.DatabaseSemaphore;
+import org.sonar.api.utils.Semaphores;
/**
* @since 3.4
*/
-public class DatabaseSemaphoreImpl implements DatabaseSemaphore {
+public class SemaphoresImpl implements Semaphores {
private SemaphoreDao dao;
- public DatabaseSemaphoreImpl(SemaphoreDao dao) {
+ public SemaphoresImpl(SemaphoreDao dao) {
this.dao = dao;
}
- public Lock acquire(String name, int maxDurationInSeconds) {
+ public Semaphore acquire(String name, int maxDurationInSeconds) {
return dao.acquire(name, maxDurationInSeconds);
}
- public Lock acquire(String name) {
+ public Semaphore acquire(String name) {
return dao.acquire(name);
}
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 948afed4a59..ccfcb6336c3 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
@@ -25,6 +25,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.Semaphores;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
@@ -32,7 +33,6 @@ import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;
import static org.fest.assertions.Assertions.assertThat;
-import static org.sonar.api.utils.DatabaseSemaphore.Lock;
public class SemaphoreDaoTest extends AbstractDaoTestCase {
@@ -75,8 +75,8 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void create_and_acquire_semaphore() throws Exception {
- Lock lock = dao.acquire("foo", 60);
- assertThat(lock.isAcquired()).isTrue();
+ Semaphores.Semaphore lock = dao.acquire("foo", 60);
+ assertThat(lock.isLocked()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
SemaphoreDto semaphore = selectSemaphore("foo");
@@ -92,8 +92,8 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void create_and_acquire_semaphore_when_timeout_is_zeo() throws Exception {
- Lock lock = dao.acquire("foo", 0);
- assertThat(lock.isAcquired()).isTrue();
+ Semaphores.Semaphore lock = dao.acquire("foo", 0);
+ assertThat(lock.isLocked()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
SemaphoreDto semaphore = selectSemaphore("foo");
@@ -109,8 +109,8 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void create_and_acquire_semaphore_when_no_timeout() throws Exception {
- Lock lock = dao.acquire("foo");
- assertThat(lock.isAcquired()).isTrue();
+ Semaphores.Semaphore lock = dao.acquire("foo");
+ assertThat(lock.isLocked()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
SemaphoreDto semaphore = selectSemaphore("foo");
@@ -127,8 +127,8 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void fail_to_acquire_locked_semaphore() throws Exception {
setupData("old_semaphore");
- Lock lock = dao.acquire("foo", Integer.MAX_VALUE);
- assertThat(lock.isAcquired()).isFalse();
+ Semaphores.Semaphore lock = dao.acquire("foo", Integer.MAX_VALUE);
+ assertThat(lock.isLocked()).isFalse();
assertThat(lock.getDurationSinceLocked()).isNotNull();
SemaphoreDto semaphore = selectSemaphore("foo");
@@ -142,8 +142,8 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void acquire_long_locked_semaphore() throws Exception {
setupData("old_semaphore");
- Lock lock = dao.acquire("foo", 60);
- assertThat(lock.isAcquired()).isTrue();
+ Semaphores.Semaphore lock = dao.acquire("foo", 60);
+ assertThat(lock.isLocked()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
SemaphoreDto semaphore = selectSemaphore("foo");
@@ -157,8 +157,8 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void acquire_locked_semaphore_when_timeout_is_zeo() throws Exception {
setupData("old_semaphore");
- Lock lock = dao.acquire("foo", 0);
- assertThat(lock.isAcquired()).isTrue();
+ Semaphores.Semaphore lock = dao.acquire("foo", 0);
+ assertThat(lock.isLocked()).isTrue();
assertThat(lock.getDurationSinceLocked()).isNull();
SemaphoreDto semaphore = selectSemaphore("foo");
@@ -175,8 +175,8 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
public void fail_to_acquire_locked_semaphore_when_no_timeout() throws Exception {
setupData("old_semaphore");
- Lock lock = dao.acquire("foo");
- assertThat(lock.isAcquired()).isFalse();
+ Semaphores.Semaphore lock = dao.acquire("foo");
+ assertThat(lock.isLocked()).isFalse();
assertThat(lock.getDurationSinceLocked()).isNotNull();
SemaphoreDto semaphore = selectSemaphore("foo");
@@ -259,7 +259,7 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
try {
barrier.await();
for (int i = 0; i < 100; i++) {
- if (dao.acquire("my-lock", 60 * 5).isAcquired()) {
+ if (dao.acquire("my-lock", 60 * 5).isLocked()) {
locks.incrementAndGet();
}
}
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseSemaphoreImplTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoresImplTest.java
index 73a7d61396b..a09b49a3048 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseSemaphoreImplTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoresImplTest.java
@@ -20,23 +20,23 @@
package org.sonar.core.persistence;
import org.junit.Test;
+import org.sonar.api.utils.Semaphores;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import static org.sonar.api.utils.DatabaseSemaphore.Lock;
-public class DatabaseSemaphoreImplTest {
+public class SemaphoresImplTest {
@Test
public void should_be_a_bridge_over_dao() {
- Lock lock = mock(Lock.class);
SemaphoreDao dao = mock(SemaphoreDao.class);
- when(dao.acquire(anyString(), anyInt())).thenReturn(lock);
+ Semaphores.Semaphore semaphore = new Semaphores.Semaphore();
+ when(dao.acquire(anyString(), anyInt())).thenReturn(semaphore);
- DatabaseSemaphoreImpl impl = new DatabaseSemaphoreImpl(dao);
+ SemaphoresImpl impl = new SemaphoresImpl(dao);
impl.acquire("do-xxx", 50000);
verify(dao).acquire("do-xxx", 50000);