Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3bb881ed1e571ca0e9c7f7fadfd6a9d3fbb259fb (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
/*******************************************************************************
 * Copyright (c) 2009, 2013 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.iterable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import org.eclipse.jpt.common.utility.closure.Closure;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.common.utility.internal.iterator.CloneListIterator;

@SuppressWarnings("nls")
public abstract class CloneIterableTests
	extends TestCase
{
	Iterable<String> iterable;

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

	public void testIterator() {
		List<String> c = new ArrayList<String>();
		c.add("0");
		c.add("1");
		c.add("2");
		c.add("3");
		assertEquals(4, c.size());
		this.iterable = this.buildIterable(c);
		int i = 0;
		for (String s : this.iterable) {
			assertEquals(String.valueOf(i++), s);
			c.remove("3");
		}
		assertEquals(4, i);
		assertEquals(3, c.size());
	}

	public void testRemove() {
		final List<String> collection = this.buildCollection();
		this.iterable = this.buildIterableWithRemover(collection);

		Object removed = "three";
		assertTrue(IterableTools.contains(this.iterable, removed));
		for (Iterator<String> iterator = this.iterable.iterator(); iterator.hasNext(); ) {
			if (iterator.next().equals(removed)) {
				iterator.remove();
			}
		}
		assertFalse(collection.contains(removed));
	}

	public void testMissingRemover() {
		final List<String> collection = this.buildCollection();
		this.iterable = this.buildIterable(collection);
		assertNotNull(this.iterable.toString());

		Object removed = "three";
		assertTrue(IterableTools.contains(this.iterable, removed));
		boolean exCaught = false;
		for (Iterator<String> iterator = this.iterable.iterator(); iterator.hasNext(); ) {
			if (iterator.next().equals(removed)) {
				try {
					iterator.remove();
					fail();
				} catch (RuntimeException ex) {
					exCaught = true;
				}
			}
		}
		assertTrue(exCaught);
	}

	public void testToString() {
		final List<String> collection = this.buildCollection();
		this.iterable = this.buildIterable(collection);
		assertNotNull(this.iterable.toString());
	}

	abstract Iterable<String> buildIterable(List<String> c);

	abstract Iterable<String> buildIterableWithRemover(List<String> c);

	Closure<String> buildRemoveCommand(final Collection<String> c) {
		return new Closure<String>() {
			public void execute(String current) {
				c.remove(current);
			}
		};
	}

	CloneListIterator.Adapter<String> buildMutator(final List<String> list) {
		return new CloneListIterator.Adapter<String>() {
			public void add(int index, String string) {
				list.add(index, string);
			}
			public void set(int index, String string) {
				list.set(index, string);
			}
			public void remove(int index) {
				list.remove(index);
			}
		};
	}

	List<String> buildCollection() {
		List<String> c = new ArrayList<String>();
		c.add("one");
		c.add("two");
		c.add("three");
		c.add("four");
		c.add("five");
		c.add("six");
		c.add("seven");
		c.add("eight");
		return c;
	}

}

Back to the top