diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2012-12-04 14:35:07 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2012-12-04 15:04:30 +0100 |
commit | cdf6262009b0245c719ffefd746a996f2f48a289 (patch) | |
tree | ae450bf79559cc92e9ae35e403400c1140398565 /sonar-core | |
parent | eb8e412ce6d8c0b38fcda7ee4e077bddf79cb06c (diff) | |
download | sonarqube-cdf6262009b0245c719ffefd746a996f2f48a289.tar.gz sonarqube-cdf6262009b0245c719ffefd746a996f2f48a289.zip |
SONAR-3306 Refactoring to use DatabaseSemaphore instead of SemaphoreDao
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java (renamed from sonar-core/src/main/java/org/sonar/core/persistence/Lock.java) | 48 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java | 2 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/persistence/DatabaseSemaphoreImplTest.java | 50 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java | 1 |
4 files changed, 64 insertions, 37 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/Lock.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java index 1e47fbcba2e..75a5bb7e2ed 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/Lock.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java @@ -17,56 +17,30 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ - package org.sonar.core.persistence; -import java.util.Date; +import org.sonar.api.utils.DatabaseSemaphore; /** * @since 3.4 */ -public class Lock { - - private String name; - private boolean acquired; - private Date locketAt; - private Date createdAt; - private Date updatedAt; - private Long durationSinceLocked; +public class DatabaseSemaphoreImpl implements DatabaseSemaphore { - public Lock(String name, boolean acquired, Date locketAt, Date createdAt, Date updatedAt) { - this.name = name; - this.acquired = acquired; - this.locketAt = locketAt; - this.createdAt = createdAt; - this.updatedAt = updatedAt; - } - - public String getName() { - return name; - } - - public Date getLocketAt() { - return locketAt; - } - - public Date getCreatedAt() { - return createdAt; - } + private SemaphoreDao dao; - public Date getUpdatedAt() { - return updatedAt; + public DatabaseSemaphoreImpl(SemaphoreDao dao) { + this.dao = dao; } - public boolean isAcquired() { - return acquired; + public Lock acquire(String name, int maxDurationInSeconds) { + return dao.acquire(name, maxDurationInSeconds); } - public Long getDurationSinceLocked() { - return durationSinceLocked; + public Lock acquire(String name) { + return dao.acquire(name); } - public void setDurationSinceLocked(Long durationSinceLocked) { - this.durationSinceLocked = durationSinceLocked; + public void release(String name) { + dao.release(name); } } 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 12100acf19a..471315c2d40 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 @@ -26,6 +26,8 @@ import org.apache.ibatis.session.SqlSession; import java.util.Date; +import static org.sonar.api.utils.DatabaseSemaphore.Lock; + /** * @since 3.4 */ diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseSemaphoreImplTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseSemaphoreImplTest.java new file mode 100644 index 00000000000..73a7d61396b --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseSemaphoreImplTest.java @@ -0,0 +1,50 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.persistence; + +import org.junit.Test; + +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 { + + @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); + + DatabaseSemaphoreImpl impl = new DatabaseSemaphoreImpl(dao); + + impl.acquire("do-xxx", 50000); + verify(dao).acquire("do-xxx", 50000); + + impl.acquire("do-xxx"); + verify(dao).acquire("do-xxx"); + + impl.release("do-xxx"); + verify(dao).release("do-xxx"); + } +} 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 75582e650b1..948afed4a59 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 @@ -32,6 +32,7 @@ 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 { |