Пример #1
0
  /**
   * Closing a connection handle should release that connection back in the pool and mark it as
   * closed.
   *
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   * @throws NoSuchMethodException
   * @throws SQLException
   */
  @Test
  public void testClose()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {

    Field field = testClass.getClass().getDeclaredField("doubleCloseCheck");
    field.setAccessible(true);
    field.set(testClass, true);

    testClass.renewConnection();
    mockPool.releaseConnection((Connection) anyObject());
    expectLastCall().once().andThrow(new SQLException()).once();
    replay(mockPool);

    testClass.close();

    // logically mark the connection as closed
    field = testClass.getClass().getDeclaredField("logicallyClosed");

    field.setAccessible(true);
    Assert.assertTrue(field.getBoolean(testClass));
    assertTrue(testClass.isClosed());

    testClass.renewConnection();
    try {
      testClass.close(); // 2nd time should throw an exception
      fail("Should have thrown an exception");
    } catch (Throwable t) {
      // do nothing.
    }

    verify(mockPool);
  }
Пример #2
0
  /**
   * Test renewal of connection.
   *
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   */
  @Test
  public void testRenewConnection()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException {
    Field field = testClass.getClass().getDeclaredField("doubleCloseCheck");
    field.setAccessible(true);
    field.set(testClass, true);

    field = testClass.getClass().getDeclaredField("logicallyClosed");
    field.setAccessible(true);
    field.set(testClass, true);

    testClass.renewConnection();
    assertFalse(field.getBoolean(testClass));
  }
Пример #3
0
  /**
   * Tests various getter/setters.
   *
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws SecurityException
   * @throws NoSuchFieldException
   */
  @SuppressWarnings("deprecation")
  @Test
  public void testSettersGetters()
      throws IllegalArgumentException, IllegalAccessException, SecurityException,
          NoSuchFieldException {
    ConnectionPartition mockPartition = createNiceMock(ConnectionPartition.class);
    testClass.setOriginatingPartition(mockPartition);
    assertEquals(mockPartition, testClass.getOriginatingPartition());

    testClass.setConnectionLastReset(123);
    assertEquals(testClass.getConnectionLastReset(), 123);

    testClass.setConnectionLastUsed(456);
    assertEquals(testClass.getConnectionLastUsed(), 456);

    Field field = testClass.getClass().getDeclaredField("possiblyBroken");
    field.setAccessible(true);
    field.setBoolean(testClass, true);
    assertTrue(testClass.isPossiblyBroken());

    Object debugHandle = new Object();
    testClass.setDebugHandle(debugHandle);
    assertEquals(debugHandle, testClass.getDebugHandle());

    testClass.setInternalConnection(mockConnection);
    assertEquals(mockConnection, testClass.getInternalConnection());
    assertEquals(mockConnection, testClass.getRawConnection());

    field = testClass.getClass().getDeclaredField("logicallyClosed");
    field.setAccessible(true);
    field.setBoolean(testClass, true);
    assertTrue(testClass.isClosed());

    testClass.setLogStatementsEnabled(true);
    assertTrue(testClass.isLogStatementsEnabled());

    assertEquals(testClass.getPool(), mockPool);
    ArrayList<ReplayLog> testLog = new ArrayList<ReplayLog>();
    testClass.setReplayLog(testLog);
    assertEquals(testClass.getReplayLog(), testLog);
    testClass.setInReplayMode(true);
    assertTrue(testClass.isInReplayMode());
    testClass.setInReplayMode(false);

    testClass.threadUsingConnection = Thread.currentThread();
    assertEquals(Thread.currentThread(), testClass.getThreadUsingConnection());
  }
Пример #4
0
  /**
   * Reset everything.
   *
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   */
  @Before
  public void before()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException {
    reset(mockConnection, mockPreparedStatementCache, mockPool, mockCallableStatementCache);

    Field field = testClass.getClass().getDeclaredField("logicallyClosed");
    field.setAccessible(true);
    field.set(testClass, false);
  }
Пример #5
0
  /**
   * Test for check closed routine.
   *
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   * @throws NoSuchMethodException
   */
  @Test
  public void testCheckClosed()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    testClass.renewConnection();

    // call the method (should not throw an exception)
    Method method = testClass.getClass().getDeclaredMethod("checkClosed");
    method.setAccessible(true);
    method.invoke(testClass);

    // logically mark the connection as closed
    Field field = testClass.getClass().getDeclaredField("logicallyClosed");

    field.setAccessible(true);
    field.set(testClass, true);
    try {
      method.invoke(testClass);
      fail("Should have thrown an exception");
    } catch (Throwable t) {
      // do nothing.
    }
  }
Пример #6
0
  /**
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws SQLException
   */
  @Test
  public void testDoubleClose()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException, SQLException {
    Field field = testClass.getClass().getDeclaredField("doubleCloseCheck");
    field.setAccessible(true);
    field.set(testClass, true);

    field = testClass.getClass().getDeclaredField("logicallyClosed");
    field.setAccessible(true);
    field.set(testClass, true);

    field = testClass.getClass().getDeclaredField("doubleCloseException");
    field.setAccessible(true);
    field.set(testClass, "fakeexception");
    mockLogger.error((String) anyObject(), anyObject());
    expectLastCall().once();

    mockPool.releaseConnection((Connection) anyObject());
    expectLastCall().once().andThrow(new SQLException()).once();
    replay(mockLogger, mockPool);

    testClass.close();
  }
Пример #7
0
  /**
   * Test marking of possibly broken status.
   *
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   */
  @Test
  public void testMarkPossiblyBroken()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException {
    Field field = testClass.getClass().getDeclaredField("possiblyBroken");
    field.setAccessible(true);
    field.set(testClass, false);
    testClass.markPossiblyBroken(null);
    Assert.assertTrue(field.getBoolean(testClass));

    // Test that a db fatal error will lead to the pool being instructed to terminate all
    // connections (+ log)
    mockPool.terminateAllConnections();
    mockLogger.error((String) anyObject(), anyObject());
    replay(mockPool);
    testClass.markPossiblyBroken(new SQLException("test", "08001"));
    verify(mockPool);
  }
Пример #8
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();
  }
Пример #9
0
  /**
   * Mock setup.
   *
   * @throws Exception
   */
  @BeforeClass
  public static void setUp() throws Exception {
    config = CommonTestUtils.getConfigClone();
    mockConnection = createNiceMock(ConnectionHandle.class);
    mockPreparedStatementCache = createNiceMock(IStatementCache.class);
    mockCallableStatementCache = createNiceMock(IStatementCache.class);

    mockLogger = createNiceMock(Logger.class);
    makeThreadSafe(mockLogger, true);
    mockPool = createNiceMock(BoneCP.class);
    mockPool.closeConnectionWatch = true;
    expect(mockPool.getConfig()).andReturn(config).anyTimes();
    config.setTransactionRecoveryEnabled(false);
    config.setStatementsCacheSize(1);
    replay(mockPool);
    testClass =
        new ConnectionHandle(
            mockConnection, mockPreparedStatementCache, mockCallableStatementCache, mockPool);
    testStatementCache = new StatementCache(100);
    Field field = testClass.getClass().getDeclaredField("logger");
    field.setAccessible(true);
    field.set(null, mockLogger);
    config.setReleaseHelperThreads(0);
  }
Пример #10
0
  /**
   * Test routine for callable statements.
   *
   * @param args
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws NoSuchMethodException
   * @throws InvocationTargetException
   */
  @SuppressWarnings("unchecked")
  private void callableStatementTest(Class... args)
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    Object[] params = new Object[args.length];
    for (int i = 0; i < args.length; i++) {
      params[i] = CommonTestUtils.instanceMap.get(args[i]);
    }

    Method prepCallMethod = testClass.getClass().getMethod("prepareCall", args);

    CallableStatementHandle mockStatement = createNiceMock(CallableStatementHandle.class);
    ConcurrentLinkedQueue<Statement> mockStatementHandles =
        createNiceMock(ConcurrentLinkedQueue.class);

    testClass.renewConnection(); // logically open the connection

    // fetching a statement that is found in cache. Statement should be returned and marked as being
    // (logically) open
    doStatementMock(mockCallableStatementCache, mockStatement, params, args);

    //
    //	expect(mockCallableStatementCache.get((String)anyObject())).andReturn(mockStatement).anyTimes();

    ((StatementHandle) mockStatement).setLogicallyOpen();
    expectLastCall();
    replay(mockStatement, mockCallableStatementCache);
    prepCallMethod.invoke(testClass, params);
    verify(mockStatement, mockCallableStatementCache);

    reset(mockStatement, mockCallableStatementCache, mockStatementHandles);

    // test for a cache miss
    doStatementMock(mockCallableStatementCache, null, params, args);

    // we should be creating the preparedStatement because it's not in the cache
    expect(prepCallMethod.invoke(mockConnection, params)).andReturn(mockStatement);
    // we should be tracking this statement
    expect(mockStatementHandles.add(mockStatement)).andReturn(true);

    replay(mockStatement, mockCallableStatementCache, mockConnection, mockStatementHandles);
    prepCallMethod.invoke(testClass, params);
    verify(mockStatement, mockCallableStatementCache, mockConnection);

    // test for cache miss + sql exception
    reset(mockStatement, mockCallableStatementCache, mockConnection);

    Method mockConnectionPrepareCallMethod =
        mockConnection.getClass().getMethod("prepareCall", args);

    //		expect(mockCallableStatementCache.get((String)anyObject())).andReturn(null).once();
    doStatementMock(mockCallableStatementCache, null, params, args);

    // we should be creating the preparedStatement because it's not in the cache
    expect(mockConnectionPrepareCallMethod.invoke(mockConnection, params))
        .andThrow(new SQLException("test", "Z"));

    replay(mockStatement, mockCallableStatementCache, mockConnection);
    try {
      prepCallMethod.invoke(testClass, params);
      fail("Should have thrown an exception");
    } catch (Throwable t) {
      // do nothing
    }

    verify(mockStatement, mockCallableStatementCache, mockConnection);

    // test for no cache defined
    reset(mockStatement, mockCallableStatementCache, mockConnection);
    boolean oldState = testClass.statementCachingEnabled;

    testClass.statementCachingEnabled = false;

    // we should be creating the preparedStatement because it's not in the cache
    expect(mockConnectionPrepareCallMethod.invoke(mockConnection, params)).andReturn(mockStatement);

    replay(mockStatement, mockCallableStatementCache, mockConnection);
    prepCallMethod.invoke(testClass, params);
    verify(mockStatement, mockCallableStatementCache, mockConnection);
    // restore sanity
    testClass.statementCachingEnabled = oldState;

    reset(mockStatement, mockCallableStatementCache, mockConnection);
  }