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
|
/*******************************************************************************
* Copyright (c) 2005, 2007 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.ListIterator;
import java.util.NoSuchElementException;
import junit.framework.TestCase;
import org.eclipse.jpt.utility.internal.iterators.EmptyListIterator;
public class EmptyListIteratorTests extends TestCase {
public EmptyListIteratorTests(String name) {
super(name);
}
public void testHasNext() {
int i = 0;
for (ListIterator<Object> stream = EmptyListIterator.instance(); stream.hasNext();) {
stream.next();
i++;
}
assertEquals(0, i);
}
public void testNext() {
for (ListIterator<Object> stream = EmptyListIterator.instance(); stream.hasNext();) {
fail("bogus element: " + stream.next());
}
}
public void testNextIndex() {
ListIterator<Object> stream = EmptyListIterator.instance();
assertEquals(0, stream.nextIndex());
}
public void testHasPrevious() {
ListIterator<Object> stream = EmptyListIterator.instance();
int i = 0;
while (stream.hasPrevious()) {
stream.previous();
i++;
}
assertEquals(0, i);
while (stream.hasNext()) {
stream.next();
}
i = 0;
while (stream.hasPrevious()) {
stream.previous();
i++;
}
assertEquals(0, i);
}
public void testPrevious() {
ListIterator<Object> stream = EmptyListIterator.instance();
while (stream.hasPrevious()) {
fail("bogus element: " + stream.previous());
}
while (stream.hasNext()) {
stream.next();
}
while (stream.hasPrevious()) {
fail("bogus element: " + stream.previous());
}
}
public void testPreviousIndex() {
ListIterator<Object> stream = EmptyListIterator.instance();
assertEquals(-1, stream.previousIndex());
}
public void testNoSuchElementException() {
boolean exCaught = false;
ListIterator<Object> stream = EmptyListIterator.instance();
Object element = null;
while (stream.hasNext()) {
element = stream.next();
}
try {
element = stream.next();
} catch (NoSuchElementException ex) {
exCaught = true;
}
assertTrue("NoSuchElementException not thrown (next): " + element, exCaught);
while (stream.hasPrevious()) {
element = stream.previous();
}
try {
element = stream.previous();
} catch (NoSuchElementException ex) {
exCaught = true;
}
assertTrue("NoSuchElementException not thrown (previous): " + element, exCaught);
}
public void testUnsupportedOperationException() {
boolean exCaught = false;
try {
EmptyListIterator.instance().remove();
} catch (UnsupportedOperationException ex) {
exCaught = true;
}
assertTrue("UnsupportedOperationException not thrown (remove)", exCaught);
try {
EmptyListIterator.instance().set(new Object());
} catch (UnsupportedOperationException ex) {
exCaught = true;
}
assertTrue("UnsupportedOperationException not thrown (set)", exCaught);
try {
EmptyListIterator.instance().add(new Object());
} catch (UnsupportedOperationException ex) {
exCaught = true;
}
assertTrue("UnsupportedOperationException not thrown (add)", exCaught);
}
}
|