]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3306 Improve unit test to fix issue on it-sonar-persistence tests
authorJulien Lancelot <julien.lancelot@gmail.com>
Mon, 3 Dec 2012 14:20:14 +0000 (15:20 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Mon, 3 Dec 2012 14:20:14 +0000 (15:20 +0100)
sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java

index e7d8a5572e019c752dae02130b72054d572122bc..7f7a86d720a87bbdd264e7b496048fb84d70f04f 100644 (file)
@@ -27,6 +27,9 @@ import org.junit.rules.ExpectedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.util.Date;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.CyclicBarrier;
@@ -77,7 +80,7 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
     assertThat(lock.getDurationSinceLocked()).isNull();
 
     SemaphoreDto semaphore = selectSemaphore("foo");
-    LOG.info("semaphore : "+ semaphore);
+    LOG.info("semaphore : " + semaphore);
     assertThat(semaphore).isNotNull();
     assertThat(semaphore.getName()).isEqualTo("foo");
     assertThat(isRecent(semaphore.getCreatedAt())).isTrue();
@@ -235,16 +238,39 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
     }
   }
 
-  private static boolean isRecent(Date date) {
-    LOG.info("**** Date : "+ date);
-    Date now = new Date();
-    LOG.info("**** Now : "+ now);
+  private boolean isRecent(Date date) {
+    LOG.info("**** Date : " + date);
+    Date now = now();
+    LOG.info("**** Now : " + now);
     Date dateInTheFuture = DateUtils.addDays(date, 1);
-    LOG.info("**** DateInTheFuture : "+ dateInTheFuture);
-    LOG.info("date.before(now) -> " + date.before(now) + " && dateInTheFuture.after(now) -> "+ dateInTheFuture.after(now));
+    LOG.info("**** DateInTheFuture : " + dateInTheFuture);
+    LOG.info("date.before(now) -> " + date.before(now) + " && dateInTheFuture.after(date) -> " + dateInTheFuture.after(date));
     return date.before(now) && dateInTheFuture.after(now);
   }
 
+  private Date now() {
+    Connection connection = null;
+    PreparedStatement statement = null;
+    ResultSet rs = null;
+    try {
+      connection = getConnection();
+      rs = null;
+      statement = connection.prepareStatement("select current_timestamp");
+      rs = statement.executeQuery();
+      if (rs.next()) {
+        return new Date();
+      }
+      return null;
+
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    } finally {
+      DatabaseUtils.closeQuietly(rs);
+      DatabaseUtils.closeQuietly(statement);
+      DatabaseUtils.closeQuietly(connection);
+    }
+  }
+
   private static class Runner extends Thread {
     SemaphoreDao dao;
     AtomicInteger locks;