RemovableKeySet extends KeySet { @SuppressWarnings("unchecked") @Override public boolean retainAll(Collection<?> c) { Collection<String> realCollection = (Collection<String>) c; boolean retValue = false; Set<String> keysToRemove = new LinkedHashSet<String>(); for (Enumeration<String> keys = request.getKeys(); keys.hasMoreElements();) { String key = keys.nextElement(); if (!realCollection.contains(key)) { retValue = true; keysToRemove.add(key); } } for (String key : keysToRemove) { request.removeValue(key); } return retValue; } RemovableKeySet(HasRemovableKeys<?> request); @Override boolean remove(Object o); @SuppressWarnings("unchecked") @Override boolean removeAll(Collection<?> c); @SuppressWarnings("unchecked") @Override boolean retainAll(Collection<?> c);  }
@SuppressWarnings("unchecked") @Test public void testRetainAll() { Enumeration<String> keys = createMock(Enumeration.class); expect(extractor.getKeys()).andReturn(keys); expect(keys.hasMoreElements()).andReturn(true); expect(keys.nextElement()).andReturn("one"); expect(keys.hasMoreElements()).andReturn(true); expect(keys.nextElement()).andReturn("two"); expect(keys.hasMoreElements()).andReturn(true); expect(keys.nextElement()).andReturn("three"); expect(keys.hasMoreElements()).andReturn(false); extractor.removeValue("three"); replay(extractor, keys); List<String> coll = new ArrayList<String>(); coll.add("one"); coll.add("two"); assertTrue(entrySet.retainAll(coll)); verify(extractor, keys); }