* This is analagous to Jalview's consensus thread modifying entries in the * AlignmentAnnotation.annotations array of Annotation[] while it is being * read. */ @Test public void testEnhancedForLoop_arrayModified() { int j = 0; for (int i : intArray) { if (j == 5) { intArray[5] = -1; intArray[6] = -2; } /* * the value 'just read' by the for loop is not affected; * the next value read is affected */ int expected = j == 6 ? -2 : j; assertEquals(i, expected); j++; } assertEquals(j, MAX); } /** * Test for the case where a list entry is added during the iteration. */ @Test public void testEnhancedForLoop_listExtended() { int j = 0; try { for (int i : intList) { if (j == 5) { intList.add(MAX + 1); } assertEquals(i, j); j++; } } catch (ConcurrentModificationException e) { /* * exception occurs on next loop iteration after 'concurrent' * modification */ assertEquals(j, 6); return; } fail("Expected exception"); } /** * Test for the case where a list entry is modified during the iteration. No * exception occurs. */ @Test public void testEnhancedForLoop_listModified() { int j = 0; for (int i : intList) { if (j == 5) { intList.set(5, -1); intList.set(6, -2); } /* * the value 'just read' is not affected, the next value * is read as modified, no exception */ int expected = j == 6 ? -2 : j; assertEquals(i, expected); j++; } assertEquals(j, MAX); } /** * Test for the case where the list is recreated during the iteration. */ @Test public void testEnhancedForLoop_listRenewed() { Object theList = intList; int j = 0; for (int i : intList) { if (j == 5) { /* * recreate a new List object */ setUp(); assertNotSame(theList, intList); } assertEquals(i, j); j++; } /* * no exception in the for loop; changing the object intList refers to * does not affect the loop's iteration over the original object */ assertEquals(j, MAX); } }