1 package jalview.datamodel;
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertNotSame;
5 import static org.testng.Assert.fail;
7 import java.util.ArrayList;
8 import java.util.ConcurrentModificationException;
11 import org.testng.annotations.BeforeMethod;
12 import org.testng.annotations.Test;
15 * Not a test of code specific to Jalview, but some tests to verify Java
16 * behaviour under certain scenarios of concurrent modification of iterated
19 public class ConcurrentModificationTest
25 List<Integer> intList;
28 * Setup: populate array and list with values 0,...,9
33 intArray = new int[MAX];
34 intList = new ArrayList<Integer>();
35 for (int i = 0; i < MAX; i++)
43 * Sanity check of values if no 'interference'
46 public void test_nullCase()
52 for (int i : intArray)
70 * Test for the case where the array is reallocated and enlarged during the
71 * iteration. The for loop iteration is not affected.
74 public void testEnhancedForLoop_arrayExtended()
77 for (int i : intArray)
81 intArray = new int[MAX + 1];
90 * Test for the case where the array is nulled during the iteration. The for
91 * loop iteration is not affected.
94 public void testEnhancedForLoop_arrayNulled()
97 for (int i : intArray)
106 assertEquals(j, MAX);
110 * Test for the case where a value is changed before the iteration reaches it.
111 * The iteration reads the new value.
113 * This is analagous to Jalview's consensus thread modifying entries in the
114 * AlignmentAnnotation.annotations array of Annotation[] while it is being
118 public void testEnhancedForLoop_arrayModified()
121 for (int i : intArray)
129 * the value 'just read' by the for loop is not affected;
130 * the next value read is affected
132 int expected = j == 6 ? -2 : j;
133 assertEquals(i, expected);
136 assertEquals(j, MAX);
140 * Test for the case where a list entry is added during the iteration.
143 public void testEnhancedForLoop_listExtended()
148 for (int i : intList)
152 intList.add(MAX + 1);
157 } catch (ConcurrentModificationException e)
160 * exception occurs on next loop iteration after 'concurrent'
166 fail("Expected exception");
170 * Test for the case where a list entry is modified during the iteration. No
174 public void testEnhancedForLoop_listModified()
177 for (int i : intList)
186 * the value 'just read' is not affected, the next value
187 * is read as modified, no exception
189 int expected = j == 6 ? -2 : j;
190 assertEquals(i, expected);
193 assertEquals(j, MAX);
197 * Test for the case where the list is recreated during the iteration.
200 public void testEnhancedForLoop_listRenewed()
202 Object theList = intList;
204 for (int i : intList)
209 * recreate a new List object
212 assertNotSame(theList, intList);
219 * no exception in the for loop; changing the object intList refers to
220 * does not affect the loop's iteration over the original object
222 assertEquals(j, MAX);