/**
 * Copyright (C) 2012 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.util.async;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.fail;

import java.sql.Connection;
import java.sql.SQLException;

import org.mockito.Mockito;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.testng.annotations.Test;

import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
import com.opengamma.util.test.Timeout;

/**
 * Tests the {@link BoneCPHack} class.
 */
@Test
public class BoneCPHackTest {

  private BoneCPConfig createConfig() {
    final BoneCPConfig config = new BoneCPConfig();
    config.setPartitionCount(1);
    config.setLazyInit(false);
    config.setMinConnectionsPerPartition(3);
    config.setMaxConnectionsPerPartition(3);
    config.setDatasourceBean(new AbstractDataSource() {

      @Override
      public Connection getConnection() throws SQLException {
        return Mockito.mock(Connection.class);
      }

      @Override
      public Connection getConnection(String username, String password) throws SQLException {
        return getConnection();
      }

    });
    config.setConnectionTimeoutInMs(Timeout.standardTimeoutMillis());
    config.setConnectionHook(new BoneCPHack(config.getConnectionHook()));
    return config;
  }

  public void testNonBlocking() throws SQLException {
    final BoneCP bcp = new BoneCP(createConfig());
    BlockingOperation.off();
    try {
      final Connection h1 = bcp.getConnection();
      assertNotNull(h1);
      final Connection h2 = bcp.getConnection();
      assertNotNull(h2);
      final Connection h3 = bcp.getConnection();
      assertNotNull(h3);
      h1.close();
      h2.close();
      h3.close();
    } finally {
      BlockingOperation.on();
    }
  }

  @Test(expectedExceptions = {BlockingOperation.class })
  public void testBlocking() throws SQLException {
    final BoneCP bcp = new BoneCP(createConfig());
    BlockingOperation.off();
    try {
      final Connection h1 = bcp.getConnection();
      assertNotNull(h1);
      final Connection h2 = bcp.getConnection();
      assertNotNull(h2);
      final Connection h3 = bcp.getConnection();
      assertNotNull(h3);
      bcp.getConnection();
      fail();
    } finally {
      BlockingOperation.on();
    }
  }

}

@@ -309,16 +309,49 @@
 ception;
+%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -1252,24 +1252,99 @@
 ();%0A      %7D%0A
+      %0A      public Logger getParentLogger() %7B%0A        return null;%0A      %7D
 %0A    %7D);%0A   

Add stub method to satisfy Java 7 compilation