From af07212b764549c16cd252a9311bb68c642e8179 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 14 Dec 2012 18:27:29 +0100 Subject: SONAR-3887 refactor semaphore API --- .../core/persistence/DatabaseSemaphoreImpl.java | 46 -------------------- .../org/sonar/core/persistence/SemaphoreDao.java | 34 ++++++++------- .../org/sonar/core/persistence/SemaphoresImpl.java | 46 ++++++++++++++++++++ .../persistence/DatabaseSemaphoreImplTest.java | 50 ---------------------- .../sonar/core/persistence/SemaphoreDaoTest.java | 32 +++++++------- .../sonar/core/persistence/SemaphoresImplTest.java | 50 ++++++++++++++++++++++ 6 files changed, 131 insertions(+), 127 deletions(-) delete mode 100644 sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java create mode 100644 sonar-core/src/main/java/org/sonar/core/persistence/SemaphoresImpl.java delete mode 100644 sonar-core/src/test/java/org/sonar/core/persistence/DatabaseSemaphoreImplTest.java create mode 100644 sonar-core/src/test/java/org/sonar/core/persistence/SemaphoresImplTest.java (limited to 'sonar-core/src') diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java deleted file mode 100644 index 75a5bb7e2ed..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.sonar.api.utils.DatabaseSemaphore; - -/** - * @since 3.4 - */ -public class DatabaseSemaphoreImpl implements DatabaseSemaphore { - - private SemaphoreDao dao; - - public DatabaseSemaphoreImpl(SemaphoreDao dao) { - this.dao = dao; - } - - public Lock acquire(String name, int maxDurationInSeconds) { - return dao.acquire(name, maxDurationInSeconds); - } - - public Lock acquire(String name) { - return dao.acquire(name); - } - - 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 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/SemaphoresImpl.java b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoresImpl.java new file mode 100644 index 00000000000..b69cff90448 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoresImpl.java @@ -0,0 +1,46 @@ +/* + * 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.sonar.api.utils.Semaphores; + +/** + * @since 3.4 + */ +public class SemaphoresImpl implements Semaphores { + + private SemaphoreDao dao; + + public SemaphoresImpl(SemaphoreDao dao) { + this.dao = dao; + } + + public Semaphore acquire(String name, int maxDurationInSeconds) { + return dao.acquire(name, maxDurationInSeconds); + } + + public Semaphore acquire(String name) { + return dao.acquire(name); + } + + public void release(String name) { + dao.release(name); + } +} 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 deleted file mode 100644 index 73a7d61396b..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseSemaphoreImplTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 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/SemaphoresImplTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoresImplTest.java new file mode 100644 index 00000000000..a09b49a3048 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoresImplTest.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 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; + +public class SemaphoresImplTest { + + @Test + public void should_be_a_bridge_over_dao() { + SemaphoreDao dao = mock(SemaphoreDao.class); + Semaphores.Semaphore semaphore = new Semaphores.Semaphore(); + when(dao.acquire(anyString(), anyInt())).thenReturn(semaphore); + + SemaphoresImpl impl = new SemaphoresImpl(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"); + } +} -- cgit v1.2.3