/** * Tests the {@link EMFCompareMap}'s key set. The set's iterator is expected to throw a {@link * ConcurrentModificationException} if its original map is structurally modified via the * put(Object) method. */ public void testConcurrentPut() { testedMethod = "Put(Object)"; final Iterator keyIterator = testedMap.keySet().iterator(); // This entry cannot exist (random value as a key, random key as a value) testedMap.put(VALUE_SET[5], KEY_SET[10]); try { keyIterator.next(); fail(MESSAGE_CONCURRENT + ' ' + testedMethod + '.'); } catch (ConcurrentModificationException e) { // We expected this } catch (NoSuchElementException e) { fail(MESSAGE_CONCURRENT + ' ' + testedMethod + '.'); } }
/** * 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); } } }
/** * {@inheritDoc} * * @see junit.framework.TestCase#setUp() */ @Override protected void setUp() { for (int i = 0; i < KEY_SET.length; i++) { testedMap.put(KEY_SET[i], VALUE_SET[i]); } }