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 {
    EventRecorder.clear();

    // change disk journal into mock journal
    Field field = TransactionManagerServices.class.getDeclaredField("journalRef");
    field.setAccessible(true);
    AtomicReference<Journal> journalRef =
        (AtomicReference<Journal>) field.get(TransactionManagerServices.class);
    journalRef.set(new MockJournal());

    poolingDataSource1 = new PoolingDataSource();
    poolingDataSource1.setClassName(MockitoXADataSource.class.getName());
    poolingDataSource1.setUniqueName("pds1");
    poolingDataSource1.setMinPoolSize(5);
    poolingDataSource1.setMaxPoolSize(5);
    poolingDataSource1.setAutomaticEnlistingEnabled(true);
    poolingDataSource1.init();

    poolingDataSource2 = new PoolingDataSource();
    poolingDataSource2.setClassName(MockitoXADataSource.class.getName());
    poolingDataSource2.setUniqueName("pds2");
    poolingDataSource2.setMinPoolSize(5);
    poolingDataSource2.setMaxPoolSize(5);
    poolingDataSource2.setAutomaticEnlistingEnabled(true);
    poolingDataSource2.init();

    btm = TransactionManagerServices.getTransactionManager();
  }
  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 testPoolNotStartingTransactionManager() throws Exception {
    if (log.isDebugEnabled()) {
      log.debug("*** Starting testPoolNotStartingTransactionManager");
    }
    // make sure TM is not running
    TransactionManagerServices.getTransactionManager().shutdown();

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

    assertFalse(TransactionManagerServices.isTransactionManagerRunning());

    Connection c = pds.getConnection();
    Statement stmt = c.createStatement();
    stmt.close();
    c.close();

    assertFalse(TransactionManagerServices.isTransactionManagerRunning());

    pds.close();

    assertFalse(TransactionManagerServices.isTransactionManagerRunning());
  }
  public void testObjectProperties() throws Exception {
    pds.close();

    pds = new PoolingDataSource();
    pds.setUniqueName("pds");
    pds.setClassName(MockitoXADataSource.class.getName());
    pds.setMinPoolSize(1);
    pds.setMaxPoolSize(1);
    pds.getDriverProperties().put("uselessThing", new Object());
    pds.init();
  }
  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();
  }