1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.iterators;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertSame;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.NoSuchElementException;
31 import java.util.Set;
32
33 import org.apache.commons.collections4.OrderedMapIterator;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39
40
41
42
43
44 public abstract class AbstractOrderedMapIteratorTest<K, V> extends AbstractMapIteratorTest<K, V> {
45
46
47
48
49
50
51 public AbstractOrderedMapIteratorTest(final String testName) {
52 super(testName);
53 }
54
55 @Override
56 public abstract OrderedMapIterator<K, V> makeEmptyIterator();
57
58 @Override
59 public abstract OrderedMapIterator<K, V> makeObject();
60
61
62
63
64 @Test
65 @Override
66 public void testEmptyMapIterator() {
67 if (!supportsEmptyIterator()) {
68 return;
69 }
70
71 super.testEmptyMapIterator();
72
73 final OrderedMapIterator<K, V> it = makeEmptyIterator();
74 assertFalse(it.hasPrevious());
75
76 assertThrows(NoSuchElementException.class, () -> it.previous());
77 }
78
79
80
81
82 @Test
83 @Override
84 public void testFullMapIterator() {
85 if (!supportsFullIterator()) {
86 return;
87 }
88
89 super.testFullMapIterator();
90
91 final OrderedMapIterator<K, V> it = makeObject();
92 final Map<K, V> map = getMap();
93
94 assertTrue(it.hasNext());
95 assertFalse(it.hasPrevious());
96 final Set<K> set = new HashSet<>();
97 while (it.hasNext()) {
98
99 final K key = it.next();
100 assertSame(key, it.getKey(), "it.next() should equals getKey()");
101 assertTrue(map.containsKey(key), "Key must be in map");
102 assertTrue(set.add(key), "Key must be unique");
103
104
105 final V value = it.getValue();
106 if (!isGetStructuralModify()) {
107 assertSame(map.get(key), value, "Value must be mapped to key");
108 }
109 assertTrue(map.containsValue(value), "Value must be in map");
110
111 assertTrue(it.hasPrevious());
112
113 verify();
114 }
115 while (it.hasPrevious()) {
116
117 final Object key = it.previous();
118 assertSame(key, it.getKey(), "it.previous() should equals getKey()");
119 assertTrue(map.containsKey(key), "Key must be in map");
120 assertTrue(set.remove(key), "Key must be unique");
121
122
123 final Object value = it.getValue();
124 if (!isGetStructuralModify()) {
125 assertSame(map.get(key), value, "Value must be mapped to key");
126 }
127 assertTrue(map.containsValue(value), "Value must be in map");
128
129 assertTrue(it.hasNext());
130
131 verify();
132 }
133 }
134
135
136
137
138 @Test
139 public void testMapIteratorOrder() {
140 if (!supportsFullIterator()) {
141 return;
142 }
143
144 final OrderedMapIterator<K, V> it = makeObject();
145 final Map<K, V> map = getMap();
146
147 assertEquals(new ArrayList<>(map.keySet()), new ArrayList<>(map.keySet()), "keySet() not consistent");
148
149 final Iterator<K> it2 = map.keySet().iterator();
150 assertTrue(it.hasNext());
151 assertTrue(it2.hasNext());
152 final List<K> list = new ArrayList<>();
153 while (it.hasNext()) {
154 final K key = it.next();
155 assertEquals(it2.next(), key);
156 list.add(key);
157 }
158 assertEquals(map.size(), list.size());
159 while (it.hasPrevious()) {
160 final K key = it.previous();
161 assertEquals(list.get(list.size() - 1), key);
162 list.remove(list.size() - 1);
163 }
164 assertEquals(0, list.size());
165 }
166
167 }