Skip to main content
summaryrefslogtreecommitdiffstats
blob: 19c45dfd356ee8a2c344ca16ed58d9c668039940 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************************
 * Copyright (c) 2005, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal.iterators;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.eclipse.jpt.common.utility.internal.iterators.CloneIterator;
import org.eclipse.jpt.common.utility.tests.internal.MultiThreadedTestCase;
import org.eclipse.jpt.common.utility.tests.internal.TestTools;

@SuppressWarnings("nls")
public class CloneIteratorTests
	extends MultiThreadedTestCase
{
	Collection<String> originalCollection;

	private Collection<String> concurrentCollection;

	public CloneIteratorTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.originalCollection = this.buildCollection();
	}

	public void testHasNext() {
		int originalSize = this.originalCollection.size();
		int i = 0;
		for (Iterator<String> stream = this.buildCloneIterator(); stream.hasNext();) {
			stream.next();
			// should allow concurrent modification
			this.originalCollection.add("foo");
			i++;
		}
		assertTrue(originalSize != this.originalCollection.size());
		assertEquals(originalSize, i);
	}

	public void testNext() {
		Iterator<String> nestedIterator = this.originalCollection.iterator();
		for (Iterator<String> stream = this.buildCloneIterator(); stream.hasNext();) {
			assertEquals("bogus element", nestedIterator.next(), stream.next());
		}
	}

	public void testNoSuchElementException() {
		boolean exCaught = false;
		Iterator<String> stream = this.buildCloneIterator();
		String string = null;
		while (stream.hasNext()) {
			string = stream.next();
		}
		try {
			string = stream.next();
		} catch (NoSuchElementException ex) {
			exCaught = true;
		}
		assertTrue("NoSuchElementException not thrown: " + string, exCaught);
	}

	public void testRemoveDefault() {
		boolean exCaught = false;
		for (Iterator<String> stream = this.buildCloneIterator(); stream.hasNext();) {
			if (stream.next().equals("three")) {
				try {
					stream.remove();
				} catch (UnsupportedOperationException ex) {
					exCaught = true;
				}
			}
		}
		assertTrue("UnsupportedOperationException not thrown", exCaught);
	}

	public void testRemoveEliminator() {
		CloneIterator.Remover<String> eliminator = new CloneIterator.Remover<String>() {
			public void remove(String element) {
				CloneIteratorTests.this.originalCollection.remove(element);
			}
		};
		this.verifyRemove(new CloneIterator<String>(this.originalCollection, eliminator));
	}

	public void testRemoveSubclass() {
		this.verifyRemove(new CloneIterator<String>(this.originalCollection) {
			@Override
			protected void remove(String current) {
				CloneIteratorTests.this.originalCollection.remove(current);
			}
		});
	}

	/**
	 * Test concurrent access: First build a clone iterator in a separate thread
	 * that hangs momentarily during its construction; then modify the shared
	 * collection in this thread. This would cause a
	 * ConcurrentModificationException in the other thread if the clone iterator
	 * were not synchronized on the original collection.
	 */
	public void testConcurrentAccess() throws Exception {
		SlowCollection<String> slow = new SlowCollection<String>();
		this.populateCollection(slow);
		// using the unsynchronized collection will cause the test to fail
		//		this.originalCollection = slow;
		this.originalCollection = Collections.synchronizedCollection(slow);

		this.concurrentCollection = new ArrayList<String>();
		Thread thread = this.buildThread(this.buildRunnable());
		thread.start();
		while ( ! slow.hasStartedClone()) {
			// wait for the other thread to start the clone...
			Thread.yield();
		}
		// ...then sneak in an extra element
		this.originalCollection.add("seventeen");
		thread.join();
		Collection<String> expected = new ArrayList<String>();
		this.populateCollection(expected);
		assertEquals(expected, this.concurrentCollection);
	}

	private Runnable buildRunnable() {
		return new TestRunnable() {
			@Override
			protected void run_() throws Throwable {
				CloneIteratorTests.this.loopWithCloneIterator();
			}
		};
	}

	/**
	 * use a clone iterator to loop over the "slow" collection and copy its
	 * contents to the concurrent collection
	 */
	void loopWithCloneIterator() {
		for (Iterator<String> stream = this.buildCloneIterator(); stream.hasNext();) {
			this.concurrentCollection.add(stream.next());
		}
	}

	private void verifyRemove(Iterator<String> iterator) {
		Object removed = "three";
		assertTrue(this.originalCollection.contains(removed));
		// try to remove before calling #next()
		boolean exCaught = false;
		try {
			iterator.remove();
		} catch (IllegalStateException ex) {
			exCaught = true;
		}
		assertTrue("IllegalStateException not thrown", exCaught);
		while (iterator.hasNext()) {
			if (iterator.next().equals(removed)) {
				iterator.remove();
				// try to remove twice
				exCaught = false;
				try {
					iterator.remove();
				} catch (IllegalStateException ex) {
					exCaught = true;
				}
				assertTrue("IllegalStateException not thrown", exCaught);
			}
		}
		assertFalse(this.originalCollection.contains(removed));
	}

	private Iterator<String> buildCloneIterator() {
		return this.buildCloneIterator(this.originalCollection);
	}

	private Iterator<String> buildCloneIterator(Collection<String> c) {
		return new CloneIterator<String>(c);
	}

	private Collection<String> buildCollection() {
		Collection<String> c = this.buildEmptyCollection();
		this.populateCollection(c);
		return c;
	}

	protected Collection<String> buildEmptyCollection() {
		return new ArrayList<String>();
	}

	private void populateCollection(Collection<String> c) {
		c.add("one");
		c.add("two");
		c.add("three");
		c.add("four");
		c.add("five");
		c.add("six");
		c.add("seven");
		c.add("eight");
	}

	// ********** custom collection **********
	static class SlowCollection<E> extends ArrayList<E> {
		private static final long serialVersionUID = 1L;
		private boolean hasStartedClone = false;

		public SlowCollection() {
			super();
		}

		@Override
		public Object[] toArray() {
			this.setHasStartedClone(true);
			// take a little snooze before returning the array
			TestTools.sleep(100);
			return super.toArray();
		}

		synchronized void setHasStartedClone(boolean hasStartedClone) {
			this.hasStartedClone = hasStartedClone;
		}

		synchronized boolean hasStartedClone() {
			return this.hasStartedClone;
		}
	}

}

Back to the top