]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3887 add unit tests
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 23 Oct 2012 08:57:32 +0000 (10:57 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 23 Oct 2012 08:57:32 +0000 (10:57 +0200)
sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java
sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java

index 288881f9fac2fc49766f3f313ad7b850f2c26ae5..4176925f504ec6da985a9b55a9725b1666c3cae1 100644 (file)
@@ -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 {
index 6abc912b896e209d0e76b0120cdcf05c8bdad20f..5efd621aa302dc7c17b4540a1c9e0c1db99c2eb0 100644 (file)
@@ -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());