Exemplo n.º 1
0
  /**
   * Proper closure test
   *
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws SQLException
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  @Test
  public void testStatementCacheCheckForProperClosure()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException, SQLException {

    ConcurrentMap mockCache = createNiceMock(ConcurrentMap.class);
    List<StatementHandle> mockStatementCollections = createNiceMock(List.class);
    StatementCache testClass = new StatementCache(1, false, null);
    Field field = testClass.getClass().getDeclaredField("cache");
    field.setAccessible(true);
    field.set(testClass, mockCache);

    field = testClass.getClass().getDeclaredField("logger");
    field.setAccessible(true);
    field.set(null, mockLogger);

    Iterator<StatementHandle> mockIterator = createNiceMock(Iterator.class);
    StatementHandle mockStatement = createNiceMock(StatementHandle.class);

    expect(mockCache.values()).andReturn(mockStatementCollections).anyTimes();
    expect(mockStatementCollections.iterator()).andReturn(mockIterator).anyTimes();
    expect(mockIterator.hasNext()).andReturn(true).once().andReturn(false).once();
    expect(mockIterator.next()).andReturn(mockStatement).anyTimes();
    expect(mockStatement.isClosed()).andReturn(false).once();
    mockLogger.error((String) anyObject());
    expectLastCall().once();
    replay(mockCache, mockStatementCollections, mockIterator, mockStatement, mockLogger);

    testClass.checkForProperClosure();
    verify(mockCache, mockStatement, mockLogger);
  }