/** * Tests the {@link EMFCompareMap}'s key set. The set is expected to support the * retainAll(Collection) operation and it effectively removes all the map's element not contained * within the given collection. */ public void testRetainAll() { testedMethod = "RetainAll(Collection)"; Set keySet = testedMap.keySet(); final Set retainedElements = new HashSet(); try { keySet.retainAll(retainedElements); } catch (UnsupportedOperationException e) { fail(testedMethod + ' ' + MESSAGE_UNSUPPORTED); } assertEquals("Map should have been emptied by its keySet retainAll().", 0, testedMap.size()); for (int i = 0; i < KEY_SET.length; i++) { // Adds more and more values to the map for each loop for (int j = 0; j < i; j++) { testedMap.put(KEY_SET[j], VALUE_SET[j]); } keySet = testedMap.keySet(); // Create an array of the entries to avoid concurrent modification exception final Object[] array = keySet.toArray(); int currentMapSize = testedMap.size(); for (int j = 0; j < array.length; j++) { for (int k = 0; k < array.length - j; k++) { retainedElements.add(array[k]); } try { keySet.retainAll(retainedElements); } catch (UnsupportedOperationException e) { fail(testedMethod + ' ' + MESSAGE_UNSUPPORTED); } for (int k = 0; k < array.length - j; k++) { assertTrue( "Operation retainAll(Collection) did not retain necessary elements within the map.", testedMap.containsKey(array[k])); } assertEquals( "Unexpected size of the map after retainAll() execution.", currentMapSize--, array.length - j); } } }
/** * Tests the {@link EMFCompareMap}'s key set. The set is expected to support the clear() operation * and it effectively empties the map. */ public void testClear() { testedMethod = "Clear()"; final Set keySet = testedMap.keySet(); try { keySet.clear(); } catch (UnsupportedOperationException e) { fail(testedMethod + ' ' + MESSAGE_UNSUPPORTED); } assertEquals("Clear() operation didn't effectively empty the map.", 0, testedMap.size()); }
/** * Tests the {@link EMFCompareMap}'s key set's iterator. The iterator is expected to support the * remove(Object) operation and this method is expected to effectively remove the given object * from the map. Size of the map afterward should have decreased by 1. */ public void testRemoveViaIterator() { testedMethod = "Iterator's remove(Object)"; int currentMapSize = testedMap.size(); for (final Iterator keyIterator = testedMap.keySet().iterator(); keyIterator.hasNext(); ) { final Object currentKey = keyIterator.next(); try { keyIterator.remove(); } catch (UnsupportedOperationException e) { fail(testedMethod + ' ' + MESSAGE_UNSUPPORTED); } assertFalse( "Operation remove() of the key set's iterator did not remove the element from the map.", testedMap.containsKey(currentKey)); assertEquals( "Unexpected size of the map after removal of an element.", --currentMapSize, testedMap.size()); } }
/** * Tests the {@link EMFCompareMap}'s key set. The set is expected to support the remove(Object) * operation and this method is expected to effectively remove the given object from the map. Size * of the map afterward should have decreased by 1. */ public void testRemoveViaSet() { testedMethod = "Set's remove(Object)"; final Set keySet = testedMap.keySet(); // Create an array of the entries to avoid concurrent modification exception final Object[] array = keySet.toArray(); int currentMapSize = testedMap.size(); for (Object key : array) { try { keySet.remove(key); } catch (UnsupportedOperationException e) { fail(testedMethod + ' ' + MESSAGE_UNSUPPORTED); } assertFalse( "Operation remove() of the key set did not remove the element from the map.", testedMap.containsKey(key)); assertEquals( "Map size didn't decrease by one after removal of one element.", --currentMapSize, testedMap.size()); } }
/** * Tests the creation of the {@link EMFCompareMap}'s key set. * * <p> * * <ul> * Assertions : * <li>Created set isn't <code>null</code> * <li>Created set's size is the same as its original map's. * <li>The set's entries correspond to the original map's keys. * </ul> */ public void testSetCreation() { final Set keySet = testedMap.keySet(); assertNotNull("Key set hasn't been created.", keySet); assertEquals( "Created set hasn't got the same size as its original map.", testedMap.size(), keySet.size()); for (final Iterator keyIterator = keySet.iterator(); keyIterator.hasNext(); ) { final Object currentKey = keyIterator.next(); assertTrue( "The set's iterator contains mappings different than its original map's keys.", testedMap.containsKey(currentKey)); } }