2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertNotSame;
25 import static org.testng.Assert.fail;
27 import java.util.ArrayList;
28 import java.util.ConcurrentModificationException;
29 import java.util.List;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.Test;
35 * Not a test of code specific to Jalview, but some tests to verify Java
36 * behaviour under certain scenarios of concurrent modification of iterated
39 public class ConcurrentModificationTest
45 List<Integer> intList;
48 * Setup: populate array and list with values 0,...,9
53 intArray = new int[MAX];
54 intList = new ArrayList<Integer>();
55 for (int i = 0; i < MAX; i++)
63 * Sanity check of values if no 'interference'
66 public void test_nullCase()
72 for (int i : intArray)
90 * Test for the case where the array is reallocated and enlarged during the
91 * iteration. The for loop iteration is not affected.
94 public void testEnhancedForLoop_arrayExtended()
97 for (int i : intArray)
101 intArray = new int[MAX + 1];
106 assertEquals(j, MAX);
110 * Test for the case where the array is nulled during the iteration. The for
111 * loop iteration is not affected.
114 public void testEnhancedForLoop_arrayNulled()
117 for (int i : intArray)
126 assertEquals(j, MAX);
130 * Test for the case where a value is changed before the iteration reaches it.
131 * The iteration reads the new value.
133 * This is analagous to Jalview's consensus thread modifying entries in the
134 * AlignmentAnnotation.annotations array of Annotation[] while it is being
138 public void testEnhancedForLoop_arrayModified()
141 for (int i : intArray)
149 * the value 'just read' by the for loop is not affected;
150 * the next value read is affected
152 int expected = j == 6 ? -2 : j;
153 assertEquals(i, expected);
156 assertEquals(j, MAX);
160 * Test for the case where a list entry is added during the iteration.
163 public void testEnhancedForLoop_listExtended()
168 for (int i : intList)
172 intList.add(MAX + 1);
177 } catch (ConcurrentModificationException e)
180 * exception occurs on next loop iteration after 'concurrent'
186 fail("Expected exception");
190 * Test for the case where a list entry is modified during the iteration. No
194 public void testEnhancedForLoop_listModified()
197 for (int i : intList)
206 * the value 'just read' is not affected, the next value
207 * is read as modified, no exception
209 int expected = j == 6 ? -2 : j;
210 assertEquals(i, expected);
213 assertEquals(j, MAX);
217 * Test for the case where the list is recreated during the iteration.
220 public void testEnhancedForLoop_listRenewed()
222 Object theList = intList;
224 for (int i : intList)
229 * recreate a new List object
232 assertNotSame(theList, intList);
239 * no exception in the for loop; changing the object intList refers to
240 * does not affect the loop's iteration over the original object
242 assertEquals(j, MAX);