ReadOnlyEnumerationMap implements Map<String, V> { @SuppressWarnings("unchecked") @Override public boolean equals(Object o) { HasKeys<V> otherRequest = ((ReadOnlyEnumerationMap<V>) o).request; boolean retValue = true; Set<String> otherKeys = new HashSet<String>(); for (Enumeration<String> attribs = otherRequest.getKeys(); attribs .hasMoreElements();) { otherKeys.add(attribs.nextElement()); } for (Enumeration<String> attribs = request.getKeys(); attribs .hasMoreElements() && retValue;) { String parameterName = attribs.nextElement(); retValue = request.getValue(parameterName).equals( otherRequest.getValue(parameterName)); otherKeys.remove(parameterName); } return retValue && otherKeys.isEmpty(); } ReadOnlyEnumerationMap(HasKeys<V> request); void clear(); boolean containsKey(Object key); @SuppressWarnings("unchecked") boolean containsValue(Object value); Set<Map.Entry<String, V>> entrySet(); V get(Object key); boolean isEmpty(); Set<String> keySet(); V put(String key, V value); void putAll(Map<? extends String, ? extends V> map); V remove(Object key); int size(); Collection<V> values(); @SuppressWarnings("unchecked") @Override boolean equals(Object o); @Override int hashCode();  }
@SuppressWarnings("unchecked") @Test public void testEqualsObject() { HasKeys<Integer> otherRequest = createMock(HasKeys.class); ReadOnlyEnumerationMap<Integer> otherMap = createMockBuilder( ReadOnlyEnumerationMap.class).withConstructor(otherRequest) .createMock(); Enumeration<String> keys = createMock(Enumeration.class); Enumeration<String> otherKeys = createMock(Enumeration.class); expect(extractor.getKeys()).andReturn(keys); expect(otherRequest.getKeys()).andReturn(otherKeys); expect(keys.hasMoreElements()).andReturn(true); expect(keys.nextElement()).andReturn("first"); expect(keys.hasMoreElements()).andReturn(true); expect(keys.nextElement()).andReturn("second"); expect(keys.hasMoreElements()).andReturn(false); expect(extractor.getValue("first")).andReturn(1); expect(extractor.getValue("second")).andReturn(2); expect(otherKeys.hasMoreElements()).andReturn(true); expect(otherKeys.nextElement()).andReturn("first"); expect(otherKeys.hasMoreElements()).andReturn(true); expect(otherKeys.nextElement()).andReturn("second"); expect(otherKeys.hasMoreElements()).andReturn(false); expect(otherRequest.getValue("first")).andReturn(1); expect(otherRequest.getValue("second")).andReturn(2); replay(extractor, otherRequest, otherMap, keys, otherKeys); assertTrue(map.equals(otherMap)); verify(extractor, otherRequest, otherMap, keys, otherKeys); }
@SuppressWarnings("unchecked") @Test public void testEqualsObjectFalse() { HasKeys<Integer> otherRequest = createMock(HasKeys.class); ReadOnlyEnumerationMap<Integer> otherMap = createMockBuilder( ReadOnlyEnumerationMap.class).withConstructor(otherRequest) .createMock(); Enumeration<String> keys = createMock(Enumeration.class); Enumeration<String> otherKeys = createMock(Enumeration.class); expect(extractor.getKeys()).andReturn(keys); expect(otherRequest.getKeys()).andReturn(otherKeys); expect(keys.hasMoreElements()).andReturn(true); expect(keys.nextElement()).andReturn("first"); expect(keys.hasMoreElements()).andReturn(true); expect(keys.nextElement()).andReturn("second"); expect(keys.hasMoreElements()).andReturn(false); expect(extractor.getValue("first")).andReturn(1); expect(extractor.getValue("second")).andReturn(2); expect(otherKeys.hasMoreElements()).andReturn(true); expect(otherKeys.nextElement()).andReturn("first"); expect(otherKeys.hasMoreElements()).andReturn(true); expect(otherKeys.nextElement()).andReturn("second"); expect(otherKeys.hasMoreElements()).andReturn(false); expect(otherRequest.getValue("first")).andReturn(1); expect(otherRequest.getValue("second")).andReturn(3); replay(extractor, otherRequest, otherMap, keys, otherKeys); assertFalse(map.equals(otherMap)); verify(extractor, otherRequest, otherMap, keys, otherKeys); }