public void testInitFailure() throws Exception {
    if (log.isDebugEnabled()) {
      log.debug("*** Starting testInitFailure");
    }
    pds.close();

    pds = new PoolingDataSource();
    pds.setMinPoolSize(0);
    pds.setMaxPoolSize(2);
    pds.setMaxIdleTime(1);
    pds.setClassName(MockitoXADataSource.class.getName());
    pds.setUniqueName("pds");
    pds.setAllowLocalTransactions(true);
    pds.setAcquisitionTimeout(1);

    TransactionManagerServices.getTransactionManager().begin();

    MockitoXADataSource.setStaticGetXAConnectionException(new SQLException("not yet started"));
    try {
      pds.init();
      fail("expected ResourceConfigurationException");
    } catch (ResourceConfigurationException ex) {
      Throwable rootCause = ex.getCause().getCause();
      assertEquals(SQLException.class, rootCause.getClass());
      assertEquals("not yet started", rootCause.getMessage());
    }

    MockitoXADataSource.setStaticGetXAConnectionException(null);
    pds.init();

    pds.getConnection().prepareStatement("");

    TransactionManagerServices.getTransactionManager().commit();
  }
  public void testPoolResetErrorHandling() throws Exception {
    if (log.isDebugEnabled()) {
      log.debug("*** Starting testPoolResetErrorHandling");
    }
    Field poolField = pds.getClass().getDeclaredField("pool");
    poolField.setAccessible(true);
    XAPool pool = (XAPool) poolField.get(pds);

    pds.setMinPoolSize(0);
    pds.reset();
    pds.setMinPoolSize(1);
    MockitoXADataSource.setStaticCloseXAConnectionException(
        new SQLException("close fails because datasource broken"));
    pds.reset();

    // the pool is now loaded with one connection which will throw an exception when closed
    pds.reset();

    try {
      MockitoXADataSource.setStaticGetXAConnectionException(
          new SQLException("getXAConnection fails because datasource broken"));
      pds.reset();
      fail("expected SQLException");
    } catch (SQLException ex) {
      assertEquals("getXAConnection fails because datasource broken", ex.getMessage());
      assertEquals(0, pool.inPoolSize());
    }

    MockitoXADataSource.setStaticGetXAConnectionException(null);
    pds.reset();
    assertEquals(1, pool.inPoolSize());
  }
  public void testPoolShrinkErrorHandling() throws Exception {
    if (log.isDebugEnabled()) {
      log.debug("*** Starting testPoolShrinkErrorHandling");
    }

    Field poolField = pds.getClass().getDeclaredField("pool");
    poolField.setAccessible(true);
    XAPool pool = (XAPool) poolField.get(pds);

    pds.setMinPoolSize(0);
    pds.reset();
    pds.setMinPoolSize(1);
    MockitoXADataSource.setStaticCloseXAConnectionException(
        new SQLException("close fails because datasource broken"));
    pds.reset();

    // the pool is now loaded with one connection which will throw an exception when closed
    Thread.sleep(1100); // leave enough time for the ide connections to expire
    TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
    Thread.sleep(100); // leave enough time for the scheduled shrinking task to do its work
    assertEquals(1, pool.inPoolSize());

    MockitoXADataSource.setStaticGetXAConnectionException(
        new SQLException("getXAConnection fails because datasource broken"));
    Thread.sleep(1100); // leave enough time for the ide connections to expire
    TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
    Thread.sleep(100); // leave enough time for the scheduled shrinking task to do its work
    assertEquals(0, pool.inPoolSize());

    MockitoXADataSource.setStaticGetXAConnectionException(null);
    Thread.sleep(1100); // leave enough time for the ide connections to expire
    TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
    Thread.sleep(100); // leave enough time for the scheduled shrinking task to do its work
    assertEquals(1, pool.inPoolSize());
  }
  protected void setUp() throws Exception {
    TransactionManagerServices.getConfiguration().setJournal("null").setGracefulShutdownInterval(2);
    TransactionManagerServices.getTransactionManager();

    MockitoXADataSource.setStaticCloseXAConnectionException(null);
    MockitoXADataSource.setStaticGetXAConnectionException(null);

    pds = new PoolingDataSource();
    pds.setMinPoolSize(1);
    pds.setMaxPoolSize(2);
    pds.setMaxIdleTime(1);
    pds.setClassName(MockitoXADataSource.class.getName());
    pds.setUniqueName("pds");
    pds.setAllowLocalTransactions(true);
    pds.setAcquisitionTimeout(1);
    pds.init();
  }