Ejemplo n.º 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
   */
  @SuppressWarnings("unchecked")
  @Test
  public void testInternalClose()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
    ConcurrentLinkedQueue<Statement> mockStatementHandles =
        createNiceMock(ConcurrentLinkedQueue.class);
    StatementHandle mockStatement = createNiceMock(StatementHandle.class);

    mockConnection.close();
    expectLastCall().once().andThrow(new SQLException()).once();

    Map<Connection, Reference<ConnectionHandle>> refs =
        new HashMap<Connection, Reference<ConnectionHandle>>();
    expect(mockPool.getFinalizableRefs()).andReturn(refs).anyTimes();
    FinalizableReferenceQueue finalizableRefQueue = new FinalizableReferenceQueue();

    expect(mockPool.getFinalizableRefQueue()).andReturn(finalizableRefQueue).anyTimes();
    expect(mockConnection.getPool()).andReturn(mockPool).anyTimes();

    replay(mockStatement, mockConnection, mockStatementHandles, mockPool);
    testClass.internalClose();
    try {
      testClass.internalClose(); // 2nd time should throw exception
      fail("Should have thrown an exception");
    } catch (Throwable t) {
      // do nothing.
    }

    verify(mockStatement, mockConnection, mockStatementHandles);
  }
Ejemplo n.º 2
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());
  }