Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1ad48b2232afa45c3237153592880bd5b8e922cd (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
/*******************************************************************************
 * Copyright (c) 2005, 2009 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.utility.tests.internal.iterators;

import java.util.Iterator;
import java.util.ListIterator;
import org.eclipse.jpt.utility.internal.iterators.SingleElementListIterator;

@SuppressWarnings("nls")
public class SingleElementListIteratorTests extends SingleElementIteratorTests {

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

	public void testNextIndex() {
		ListIterator<String> stream = this.buildSingleElementListIterator();
		while (stream.hasNext()) {
			assertEquals("bogus index", 0, stream.nextIndex());
			stream.next();
		}
		assertEquals("bogus index", 1, stream.nextIndex());
	}

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

		while (stream.hasPrevious()) {
			stream.previous();
			i++;
		}
		assertEquals(2, i);
	}

	public void testPrevious() {
		ListIterator<String> stream = this.buildSingleElementListIterator();

		while (stream.hasNext()) {
			assertEquals("bogus element", this.singleElement(), stream.next());
		}

		while (stream.hasPrevious()) {
			assertEquals("bogus element", this.singleElement(), stream.previous());
		}
	}

	public void testPreviousIndex() {
		ListIterator<String> stream = this.buildSingleElementListIterator();

		while (stream.hasNext()) {
			assertEquals("bogus index", 0, stream.nextIndex());
			stream.next();
		}

		while (stream.hasPrevious()) {
			assertEquals("bogus index", 0, stream.previousIndex());
			stream.previous();
		}

		assertEquals("bogus index", -1, stream.previousIndex());
	}

	public void testAdd() {
		boolean exCaught = false;
		ListIterator<String> stream = this.buildSingleElementListIterator();

		try {
			stream.add("foo");
		} catch (UnsupportedOperationException ex) {
			exCaught = true;
		}

		assertTrue("UnsupportedOperationException not thrown", exCaught);
	}

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

	@Override
	protected Iterator<String> buildSingleElementIterator() {
		return new SingleElementListIterator<String>(this.singleElement());
	}

	protected ListIterator<String> buildSingleElementListIterator() {
		return (ListIterator<String>) this.buildSingleElementIterator();
	}

}

Back to the top