Example #1
0
  /**
   * Prepared statement tests.
   *
   * @throws SQLException
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   */
  @Test
  public void testPreparedStatement()
      throws SQLException, SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException {
    BoneCP dsb = null;
    CommonTestUtils.logTestInfo("Tests that prepared statements are obtained from cache when set.");
    config.setMinConnectionsPerPartition(10);
    config.setMaxConnectionsPerPartition(20);
    config.setAcquireIncrement(5);
    config.setPartitionCount(1);
    config.setStatementsCacheSize(1);
    config.setLogStatementsEnabled(true);
    config.setStatementReleaseHelperThreads(0);
    dsb = new BoneCP(config);

    ConnectionHandle con = (ConnectionHandle) dsb.getConnection();
    Field preparedStatement = con.getClass().getDeclaredField("preparedStatementCache");
    preparedStatement.setAccessible(true);
    // switch to our mock
    preparedStatement.set(con, mockCache);
    expect(mockCache.get(isA(String.class))).andReturn(null);
    //		mockCache.put(isA(String.class), isA(PreparedStatement.class));

    replay(mockCache);
    Statement statement = con.prepareStatement(CommonTestUtils.TEST_QUERY);
    statement.close();
    verify(mockCache);

    reset(mockCache);
    expect(mockCache.get(isA(String.class))).andReturn(null);
    replay(mockCache);

    con.prepareStatement(CommonTestUtils.TEST_QUERY);
    statement.close();
    verify(mockCache);
    dsb.shutdown();
    statement.close();
    con.close();
    CommonTestUtils.logPass();
  }