// Path: sane-dbc-hikari/src/main/java/com/novarto/sanedbc/hikari/Hikari.java
// public class Hikari
// {
//     private static final Logger LOGGER = LoggerFactory.getLogger(Hikari.class);
// 
//     public static HikariDataSource createHikari(String url, String user, String pass, Properties dsProps)
//     {
//         HikariConfig config = new HikariConfig();
// 
//         config.setJdbcUrl(url);
//         config.setUsername(user);
//         config.setPassword(pass);
// 
//         config.setDataSourceProperties(dsProps);
// 
//         LOGGER.info("creating hikaricp datasource. using jdbc url {}", config.getJdbcUrl());
// 
//         return new HikariDataSource(config);
//     }
// 
//     public static ExecutorService createExecutorFor(HikariDataSource ds, boolean shutdownOnJvmExit)
//     {
//         return createExecutorFor(ds, shutdownOnJvmExit, Executors::newCachedThreadPool);
// 
//     }
// 
//     public static <A extends ExecutorService> A createExecutorFor(HikariDataSource ds, boolean shutdownOnJvmExit,
//             F0<A> ctor
//     )
//     {
//         A executor = ctor.f();
// 
//         if (shutdownOnJvmExit)
//         {
//             String serviceName = "Shutdown hook for hikari@" + ds.getJdbcUrl();
//             final Thread datasourceShutdownHook = new Thread(() -> gracefulShutdown(executor, ds));
//             datasourceShutdownHook.setName(serviceName);
//             Runtime.getRuntime().addShutdownHook(datasourceShutdownHook);
// 
//         }
// 
//         return executor;
// 
//     }
// 
// 
//     public static void gracefulShutdown(ExecutorService ex, HikariDataSource ds)
//     {
//         ds.close();
//         ConcurrentUtil.shutdownAndAwaitTermination(ex, 5, TimeUnit.SECONDS);
//     }
// 
// }
// 
// Path: sane-dbc-hikari/src/main/java/com/novarto/sanedbc/hikari/Hikari.java
// public static void gracefulShutdown(ExecutorService ex, HikariDataSource ds)
// {
//     ds.close();
//     ConcurrentUtil.shutdownAndAwaitTermination(ex, 5, TimeUnit.SECONDS);
// }


import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.novarto.sanedbc.hikari.Hikari;
import com.zaxxer.hikari.HikariDataSource;
import fj.control.db.DB;
import fj.function.Try1;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import static com.novarto.lang.guava.testutil.FuturesTestUtil.awaitAndGet;
import static com.novarto.lang.guava.testutil.FuturesTestUtil.awaitAndGetFailure;
import static com.novarto.sanedbc.hikari.Hikari.gracefulShutdown;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
package com.novarto.sanedbc.guava;




public class SanityTest
{
    private static GuavaDbInterpreter dbAsync;
    private static HikariDataSource ds;
    private static ListeningExecutorService ex;

    @BeforeClass public static void setupHikari()
    {
        ds = Hikari.createHikari("jdbc:hsqldb:mem:JdbcUtilsTest", "sa", "", new Properties());
        ex = MoreExecutors.listeningDecorator(Hikari.createExecutorFor(ds, false));

        dbAsync = new GuavaDbInterpreter(ds, ex);
    }

    @Test public void sanity()
    {
        ListenableFuture<Integer> success = dbAsync.submit(DB.unit(42));

        SQLException ex = new SQLException("failed i have");
        ListenableFuture<Integer> fail = dbAsync.submit(DB.db((Try1<Connection, Integer, SQLException>) c ->
        {
            assertThat(c, is(notNullValue()));
            throw ex;
        }));

        assertThat(awaitAndGet(success), is(42));

        assertThat(awaitAndGetFailure(fail), is(ex));

    }

    @AfterClass public static void shutdownHikari()
    {
// Path: sane-dbc-hikari/src/main/java/com/novarto/sanedbc/hikari/Hikari.java
// public class Hikari
// {
//     private static final Logger LOGGER = LoggerFactory.getLogger(Hikari.class);
// 
//     public static HikariDataSource createHikari(String url, String user, String pass, Properties dsProps)
//     {
//         HikariConfig config = new HikariConfig();
// 
//         config.setJdbcUrl(url);
//         config.setUsername(user);
//         config.setPassword(pass);
// 
//         config.setDataSourceProperties(dsProps);
// 
//         LOGGER.info("creating hikaricp datasource. using jdbc url {}", config.getJdbcUrl());
// 
//         return new HikariDataSource(config);
//     }
// 
//     public static ExecutorService createExecutorFor(HikariDataSource ds, boolean shutdownOnJvmExit)
//     {
//         return createExecutorFor(ds, shutdownOnJvmExit, Executors::newCachedThreadPool);
// 
//     }
// 
//     public static <A extends ExecutorService> A createExecutorFor(HikariDataSource ds, boolean shutdownOnJvmExit,
//             F0<A> ctor
//     )
//     {
//         A executor = ctor.f();
// 
//         if (shutdownOnJvmExit)
//         {
//             String serviceName = "Shutdown hook for hikari@" + ds.getJdbcUrl();
//             final Thread datasourceShutdownHook = new Thread(() -> gracefulShutdown(executor, ds));
//             datasourceShutdownHook.setName(serviceName);
//             Runtime.getRuntime().addShutdownHook(datasourceShutdownHook);
// 
//         }
// 
//         return executor;
// 
//     }
// 
// 
//     public static void gracefulShutdown(ExecutorService ex, HikariDataSource ds)
//     {
//         ds.close();
//         ConcurrentUtil.shutdownAndAwaitTermination(ex, 5, TimeUnit.SECONDS);
//     }
// 
// }
// 
// Path: sane-dbc-hikari/src/main/java/com/novarto/sanedbc/hikari/Hikari.java
// public static void gracefulShutdown(ExecutorService ex, HikariDataSource ds)
// {
//     ds.close();
//     ConcurrentUtil.shutdownAndAwaitTermination(ex, 5, TimeUnit.SECONDS);
// }


// Path: sane-dbc-guava/src/test/java/com/novarto/sanedbc/guava/SanityTest.java
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.novarto.sanedbc.hikari.Hikari;
import com.zaxxer.hikari.HikariDataSource;
import fj.control.db.DB;
import fj.function.Try1;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import static com.novarto.lang.guava.testutil.FuturesTestUtil.awaitAndGet;
import static com.novarto.lang.guava.testutil.FuturesTestUtil.awaitAndGetFailure;
import static com.novarto.sanedbc.hikari.Hikari.gracefulShutdown;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

package com.novarto.sanedbc.guava;




public class SanityTest
{
    private static GuavaDbInterpreter dbAsync;
    private static HikariDataSource ds;
    private static ListeningExecutorService ex;

    @BeforeClass public static void setupHikari()
    {
        ds = Hikari.createHikari("jdbc:hsqldb:mem:JdbcUtilsTest", "sa", "", new Properties());
        ex = MoreExecutors.listeningDecorator(Hikari.createExecutorFor(ds, false));

        dbAsync = new GuavaDbInterpreter(ds, ex);
    }

    @Test public void sanity()
    {
        ListenableFuture<Integer> success = dbAsync.submit(DB.unit(42));

        SQLException ex = new SQLException("failed i have");
        ListenableFuture<Integer> fail = dbAsync.submit(DB.db((Try1<Connection, Integer, SQLException>) c ->
        {
            assertThat(c, is(notNullValue()));
            throw ex;
        }));

        assertThat(awaitAndGet(success), is(42));

        assertThat(awaitAndGetFailure(fail), is(ex));

    }

    @AfterClass public static void shutdownHikari()
    {
        gracefulShutdown(ex, ds);