From: Simon Brandhof Date: Tue, 23 Oct 2012 08:57:32 +0000 (+0200) Subject: SONAR-3887 add unit tests X-Git-Tag: 3.4~456 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c408b3c0bf41d1b897d58f394a9a7133f53bfd1e;p=sonarqube.git SONAR-3887 add unit 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 288881f9fac..4176925f504 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 @@ -39,7 +39,7 @@ public class SemaphoreDao { public boolean 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"); + Preconditions.checkArgument(maxDurationInSeconds > 0, "Semaphore max duration must be positive: " + maxDurationInSeconds); SqlSession session = mybatis.openSession(); try { 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 6abc912b896..5efd621aa30 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,7 +20,9 @@ package org.sonar.core.persistence; import org.apache.commons.lang.time.DateUtils; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.sql.Connection; import java.sql.PreparedStatement; @@ -35,6 +37,36 @@ import static org.fest.assertions.Assertions.assertThat; public class SemaphoreDaoTest extends AbstractDaoTestCase { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void should_fail_to_acquire_if_blank_semaphore_name() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Semaphore name must not be empty"); + + SemaphoreDao dao = new SemaphoreDao(getMyBatis()); + dao.acquire(null, 5000); + } + + @Test + public void should_fail_to_acquire_if_negative_timeout() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Semaphore max duration must be positive: -5000"); + + SemaphoreDao dao = new SemaphoreDao(getMyBatis()); + dao.acquire("foo", -5000); + } + + @Test + public void should_fail_to_release_if_blank_semaphore_name() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Semaphore name must not be empty"); + + SemaphoreDao dao = new SemaphoreDao(getMyBatis()); + dao.release(null); + } + @Test public void create_and_acquire_semaphore() throws Exception { SemaphoreDao dao = new SemaphoreDao(getMyBatis());