StatementCache implements IStatementCache { public void clear() { for (StatementHandle statement: this.cache.values()){ try { if (!statement.isClosed()){ statement.close(); } } catch (SQLException e) { } } this.cache.clear(); } StatementCache(int size, boolean maintainStats, Statistics statistics); String calculateCacheKey(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability); String calculateCacheKey(String sql, int resultSetType, int resultSetConcurrency); String calculateCacheKey(String sql, int autoGeneratedKeys); String calculateCacheKey(String sql, int[] columnIndexes); String calculateCacheKey(String sql, String[] columnNames); StatementHandle get(String key); StatementHandle get(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability); StatementHandle get(String sql, int resultSetType, int resultSetConcurrency); StatementHandle get(String sql, int autoGeneratedKeys); StatementHandle get(String sql, int[] columnIndexes); StatementHandle get(String sql, String[] columnNames); int size(); void clear(); void checkForProperClosure(); void putIfAbsent(String key, StatementHandle handle);  }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testStatementCacheClear() 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); 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).times(2).andReturn(false).once(); expect(mockIterator.next()).andReturn(mockStatement).anyTimes(); mockStatement.close(); expectLastCall().once().andThrow(new SQLException()).once(); mockCache.clear(); expectLastCall().once(); replay(mockCache, mockStatementCollections, mockIterator,mockStatement); testClass.clear(); verify(mockCache, mockStatement); }