Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 12245e93ebd8e8cd3b60877b1a9a232fc8ef4f4e (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
/*******************************************************************************
 * Copyright (c) 2005, 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.iterator;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import org.eclipse.jpt.common.utility.internal.iterator.ArrayListIterator;
import org.eclipse.jpt.common.utility.internal.iterator.IteratorTools;

@SuppressWarnings("nls")
public class ArrayListIteratorTests extends ArrayIteratorTests {

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

	public void testHasPrevious() {
		ListIterator<String> stream = this.buildListIterator();
		while (stream.hasNext()) {
			stream.next();
		}
		int i = 0;
		while (stream.hasPrevious()) {
			stream.previous();
			i++;
		}
		assertEquals(this.buildArray().length, i);
	}

	public void testPrevious() {
		ListIterator<String> stream = this.buildListIterator();
		while (stream.hasNext()) {
			stream.next();
		}
		int i = this.buildArray().length;
		while (stream.hasPrevious()) {
			assertEquals("bogus element", i--, Integer.parseInt(stream.previous()));
		}
	}

	public void testNextIndex() {
		int i = 0;
		ListIterator<String> stream = this.buildListIterator();
		while (stream.hasNext()) {
			assertEquals(i, stream.nextIndex());
			stream.next();
			i++;
		}
		assertEquals(i, stream.nextIndex());
	}

	public void testPreviousIndex() {
		int i = 0;
		ListIterator<String> stream = this.buildListIterator();
		while (stream.hasNext()) {
			assertEquals(i - 1, stream.previousIndex());
			stream.next();
			i++;
		}
		assertEquals(i - 1, stream.previousIndex());
	}

	@Override
	public void testNoSuchElementException() {
		boolean exCaught = false;
		ListIterator<String> stream = this.buildListIterator();
		String string = null;
		try {
			string = stream.previous();
		} catch (NoSuchElementException ex) {
			exCaught = true;
		}
		assertTrue("NoSuchElementException not thrown: " + string, exCaught);
	}

	public void testUnsupportedOperationExceptionAdd() {
		boolean exCaught = false;
		for (ListIterator<String> stream = this.buildListIterator(); stream.hasNext();) {
			if (stream.next().equals("3")) {
				try {
					stream.add("3.5");
				} catch (UnsupportedOperationException ex) {
					exCaught = true;
				}
			}
		}
		assertTrue("UnsupportedOperationException not thrown", exCaught);
	}

	public void testUnsupportedOperationExceptionSet() {
		boolean exCaught = false;
		for (ListIterator<String> stream = this.buildListIterator(); stream.hasNext();) {
			if (stream.next().equals("3")) {
				try {
					stream.set("three");
				} catch (UnsupportedOperationException ex) {
					exCaught = true;
				}
			}
		}
		assertTrue("UnsupportedOperationException not thrown", exCaught);
	}

	@Override
	Iterator<Number> buildGenericIterator(Integer[] integers) {
		return IteratorTools.<Number>listIterator(integers);
	}

	@Override
	Iterator<Number> buildVarArgIterator() {
		return IteratorTools.<Number>listIterator(new Integer(0), new Integer(1), new Integer(2));
	}

	private ListIterator<String> buildListIterator() {
		return this.buildListIterator(this.buildArray());
	}

	private ListIterator<String> buildListIterator(String[] array) {
		return IteratorTools.listIterator(array);
	}

	@Override
	Iterator<String> buildIterator(String[] array) {
		return IteratorTools.listIterator(array);
	}

	@Override
	Iterator<String> buildIterator(String[] array, int start, int end) {
		return new ArrayListIterator<String>(array, start, end);
	}
}

Back to the top