Пример #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
  /**
   * 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.
    }
  }
Пример #4
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);
  }