1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.map;
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
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.commons.collections4.BulkTest;
28 import org.apache.commons.collections4.MapIterator;
29 import org.apache.commons.collections4.list.AbstractListTest;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35
36
37
38 public class ListOrderedMap2Test<K, V> extends AbstractOrderedMapTest<K, V> {
39
40 public class TestListView extends AbstractListTest<K> {
41
42 @Override
43 public K[] getFullElements() {
44 return getSampleKeys();
45 }
46
47 @Override
48 public boolean isAddSupported() {
49 return false;
50 }
51
52 @Override
53 public boolean isNullSupported() {
54 return isAllowNullKey();
55 }
56
57 @Override
58 public boolean isRemoveSupported() {
59 return false;
60 }
61
62 @Override
63 public boolean isSetSupported() {
64 return false;
65 }
66
67 @Override
68 public boolean isTestSerialization() {
69 return false;
70 }
71
72 @Override
73 public List<K> makeFullCollection() {
74 return ListOrderedMap2Test.this.makeFullMap().asList();
75 }
76
77 @Override
78 public List<K> makeObject() {
79 return ListOrderedMap2Test.this.makeObject().asList();
80 }
81 }
82
83 public BulkTest bulkTestListView() {
84 return new TestListView();
85 }
86
87 @Override
88 public String getCompatibilityVersion() {
89 return "4";
90 }
91
92
93
94
95 @Override
96 public ListOrderedMap<K, V> getMap() {
97 return (ListOrderedMap<K, V>) super.getMap();
98 }
99
100
101
102
103 @Override
104 public ListOrderedMap<K, V> makeFullMap() {
105 return (ListOrderedMap<K, V>) super.makeFullMap();
106 }
107
108 @Override
109 public ListOrderedMap<K, V> makeObject() {
110 return new ListOrderedMap<>();
111 }
112
113 @Test
114 public void testGetByIndex() {
115 resetEmpty();
116 assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(0));
117 assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(-1));
118 resetFull();
119 final ListOrderedMap<K, V> lom = getMap();
120 assertThrows(IndexOutOfBoundsException.class, () -> lom.get(-1));
121 assertThrows(IndexOutOfBoundsException.class, () -> lom.get(lom.size()));
122 int i = 0;
123 for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
124 assertSame(it.next(), lom.get(i));
125 }
126 }
127
128 @Test
129 public void testGetValueByIndex() {
130 resetEmpty();
131 assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(0));
132 assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(-1));
133 resetFull();
134 final ListOrderedMap<K, V> lom = getMap();
135 assertThrows(IndexOutOfBoundsException.class, () -> lom.getValue(-1));
136 assertThrows(IndexOutOfBoundsException.class, () -> lom.getValue(lom.size()));
137 int i = 0;
138 for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
139 it.next();
140 assertSame(it.getValue(), lom.getValue(i));
141 }
142 }
143
144 @Test
145 public void testIndexOf() {
146 resetEmpty();
147 ListOrderedMap<K, V> lom = getMap();
148 assertEquals(-1, lom.indexOf(getOtherKeys()));
149
150 resetFull();
151 lom = getMap();
152 final List<K> list = new ArrayList<>();
153 for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext();) {
154 list.add(it.next());
155 }
156 for (int i = 0; i < list.size(); i++) {
157 assertEquals(i, lom.indexOf(list.get(i)));
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172 @Test
173 public void testRemoveByIndex() {
174 resetEmpty();
175 assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(0));
176 assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(-1));
177 resetFull();
178 final ListOrderedMap<K, V> lom = getMap();
179 assertThrows(IndexOutOfBoundsException.class, () -> lom.remove(-1));
180 assertThrows(IndexOutOfBoundsException.class, () -> lom.remove(lom.size()));
181 final List<K> list = new ArrayList<>();
182 for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext();) {
183 list.add(it.next());
184 }
185 for (int i = 0; i < list.size(); i++) {
186 final Object key = list.get(i);
187 final Object value = lom.get(key);
188 assertEquals(value, lom.remove(i));
189 list.remove(i);
190 assertFalse(lom.containsKey(key));
191 }
192 }
193 }