1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotSame;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertSame;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20
21 import java.util.Iterator;
22 import java.util.NoSuchElementException;
23
24 import org.eclipse.jgit.lib.ObjectId;
25 import org.eclipse.jgit.lib.ObjectIdRef;
26 import org.eclipse.jgit.lib.Ref;
27 import org.junit.Test;
28
29 public class RefListTest {
30 private static final ObjectId ID = ObjectId
31 .fromString("41eb0d88f833b558bddeb269b7ab77399cdf98ed");
32
33 private static final Ref REF_A = newRef("A");
34
35 private static final Ref REF_B = newRef("B");
36
37 private static final Ref REF_c = newRef("c");
38
39 @Test
40 public void testEmpty() {
41 RefList<Ref> list = RefList.emptyList();
42 assertEquals(0, list.size());
43 assertTrue(list.isEmpty());
44 assertFalse(list.iterator().hasNext());
45 assertEquals(-1, list.find("a"));
46 assertEquals(-1, list.find("z"));
47 assertFalse(list.contains("a"));
48 assertNull(list.get("a"));
49 try {
50 list.get(0);
51 fail("RefList.emptyList should have 0 element array");
52 } catch (ArrayIndexOutOfBoundsException err) {
53
54 }
55 }
56
57 @Test
58 public void testEmptyBuilder() {
59 RefList<Ref> list = new RefList.Builder<>().toRefList();
60 assertEquals(0, list.size());
61 assertFalse(list.iterator().hasNext());
62 assertEquals(-1, list.find("a"));
63 assertEquals(-1, list.find("z"));
64 assertFalse(list.contains("a"));
65 assertNull(list.get("a"));
66 assertTrue(list.asList().isEmpty());
67 assertEquals("[]", list.toString());
68
69
70 assertNull(list.get(16 - 1));
71 try {
72 list.get(16);
73 fail("default RefList should have 16 element array");
74 } catch (ArrayIndexOutOfBoundsException err) {
75
76 }
77 }
78
79 @Test
80 public void testBuilder_AddThenSort() {
81 RefList.Builder<Ref> builder = new RefList.Builder<>(1);
82 builder.add(REF_B);
83 builder.add(REF_A);
84
85 RefList<Ref> list = builder.toRefList();
86 assertEquals(2, list.size());
87 assertSame(REF_B, list.get(0));
88 assertSame(REF_A, list.get(1));
89
90 builder.sort();
91 list = builder.toRefList();
92 assertEquals(2, list.size());
93 assertSame(REF_A, list.get(0));
94 assertSame(REF_B, list.get(1));
95 }
96
97 @Test
98 public void testBuilder_AddThenDedupe() {
99 RefList.Builder<Ref> builder = new RefList.Builder<>(1);
100 builder.add(REF_B);
101 builder.add(REF_A);
102 builder.add(REF_A);
103 builder.add(REF_B);
104 builder.add(REF_c);
105
106 builder.sort();
107 builder.dedupe((a, b) -> b);
108 RefList<Ref> list = builder.toRefList();
109
110 assertEquals(3, list.size());
111 assertSame(REF_A, list.get(0));
112 assertSame(REF_B, list.get(1));
113 assertSame(REF_c, list.get(2));
114 }
115
116 @Test
117 public void testBuilder_AddThenDedupe_Border() {
118 RefList.Builder<Ref> builder = new RefList.Builder<>(1);
119 builder.sort();
120 builder.dedupe((a, b) -> b);
121 RefList<Ref> list = builder.toRefList();
122 assertTrue(list.isEmpty());
123
124 builder = new RefList.Builder<>(1);
125 builder.add(REF_A);
126 builder.sort();
127 builder.dedupe((a, b) -> b);
128 list = builder.toRefList();
129 assertEquals(1, list.size());
130 }
131
132 @Test
133 public void testBuilder_AddAll() {
134 RefList.Builder<Ref> builder = new RefList.Builder<>(1);
135 Ref[] src = { REF_A, REF_B, REF_c, REF_A };
136 builder.addAll(src, 1, 2);
137
138 RefList<Ref> list = builder.toRefList();
139 assertEquals(2, list.size());
140 assertSame(REF_B, list.get(0));
141 assertSame(REF_c, list.get(1));
142 }
143
144 @Test
145 public void testBuilder_Set() {
146 RefList.Builder<Ref> builder = new RefList.Builder<>();
147 builder.add(REF_A);
148 builder.add(REF_A);
149
150 assertEquals(2, builder.size());
151 assertSame(REF_A, builder.get(0));
152 assertSame(REF_A, builder.get(1));
153
154 RefList<Ref> list = builder.toRefList();
155 assertEquals(2, list.size());
156 assertSame(REF_A, list.get(0));
157 assertSame(REF_A, list.get(1));
158 builder.set(1, REF_B);
159
160 list = builder.toRefList();
161 assertEquals(2, list.size());
162 assertSame(REF_A, list.get(0));
163 assertSame(REF_B, list.get(1));
164 }
165
166 @Test
167 public void testBuilder_Remove() {
168 RefList.Builder<Ref> builder = new RefList.Builder<>();
169 builder.add(REF_A);
170 builder.add(REF_B);
171 builder.remove(0);
172
173 assertEquals(1, builder.size());
174 assertSame(REF_B, builder.get(0));
175 }
176
177 @Test
178 public void testSet() {
179 RefList<Ref> one = toList(REF_A, REF_A);
180 RefList<Ref> two = one.set(1, REF_B);
181 assertNotSame(one, two);
182
183
184 assertEquals(2, one.size());
185 assertSame(REF_A, one.get(0));
186 assertSame(REF_A, one.get(1));
187
188
189 assertEquals(2, two.size());
190 assertSame(REF_A, one.get(0));
191 assertSame(REF_B, two.get(1));
192 }
193
194 @Test
195 public void testAddToEmptyList() {
196 RefList<Ref> one = toList();
197 RefList<Ref> two = one.add(0, REF_B);
198 assertNotSame(one, two);
199
200
201 assertEquals(0, one.size());
202 assertEquals(1, two.size());
203 assertFalse(two.isEmpty());
204 assertSame(REF_B, two.get(0));
205 }
206
207 @Test
208 public void testAddToFrontOfList() {
209 RefList<Ref> one = toList(REF_A);
210 RefList<Ref> two = one.add(0, REF_B);
211 assertNotSame(one, two);
212
213
214 assertEquals(1, one.size());
215 assertSame(REF_A, one.get(0));
216 assertEquals(2, two.size());
217 assertSame(REF_B, two.get(0));
218 assertSame(REF_A, two.get(1));
219 }
220
221 @Test
222 public void testAddToEndOfList() {
223 RefList<Ref> one = toList(REF_A);
224 RefList<Ref> two = one.add(1, REF_B);
225 assertNotSame(one, two);
226
227
228 assertEquals(1, one.size());
229 assertSame(REF_A, one.get(0));
230 assertEquals(2, two.size());
231 assertSame(REF_A, two.get(0));
232 assertSame(REF_B, two.get(1));
233 }
234
235 @Test
236 public void testAddToMiddleOfListByInsertionPosition() {
237 RefList<Ref> one = toList(REF_A, REF_c);
238
239 assertEquals(-2, one.find(REF_B.getName()));
240
241 RefList<Ref> two = one.add(one.find(REF_B.getName()), REF_B);
242 assertNotSame(one, two);
243
244
245 assertEquals(2, one.size());
246 assertSame(REF_A, one.get(0));
247 assertSame(REF_c, one.get(1));
248
249 assertEquals(3, two.size());
250 assertSame(REF_A, two.get(0));
251 assertSame(REF_B, two.get(1));
252 assertSame(REF_c, two.get(2));
253 }
254
255 @Test
256 public void testPutNewEntry() {
257 RefList<Ref> one = toList(REF_A, REF_c);
258 RefList<Ref> two = one.put(REF_B);
259 assertNotSame(one, two);
260
261
262 assertEquals(2, one.size());
263 assertSame(REF_A, one.get(0));
264 assertSame(REF_c, one.get(1));
265
266 assertEquals(3, two.size());
267 assertSame(REF_A, two.get(0));
268 assertSame(REF_B, two.get(1));
269 assertSame(REF_c, two.get(2));
270 }
271
272 @Test
273 public void testPutReplaceEntry() {
274 Ref otherc = newRef(REF_c.getName());
275 assertNotSame(REF_c, otherc);
276
277 RefList<Ref> one = toList(REF_A, REF_c);
278 RefList<Ref> two = one.put(otherc);
279 assertNotSame(one, two);
280
281
282 assertEquals(2, one.size());
283 assertSame(REF_A, one.get(0));
284 assertSame(REF_c, one.get(1));
285
286 assertEquals(2, two.size());
287 assertSame(REF_A, two.get(0));
288 assertSame(otherc, two.get(1));
289 }
290
291 @Test
292 public void testRemoveFrontOfList() {
293 RefList<Ref> one = toList(REF_A, REF_B, REF_c);
294 RefList<Ref> two = one.remove(0);
295 assertNotSame(one, two);
296
297 assertEquals(3, one.size());
298 assertSame(REF_A, one.get(0));
299 assertSame(REF_B, one.get(1));
300 assertSame(REF_c, one.get(2));
301
302 assertEquals(2, two.size());
303 assertSame(REF_B, two.get(0));
304 assertSame(REF_c, two.get(1));
305 }
306
307 @Test
308 public void testRemoveMiddleOfList() {
309 RefList<Ref> one = toList(REF_A, REF_B, REF_c);
310 RefList<Ref> two = one.remove(1);
311 assertNotSame(one, two);
312
313 assertEquals(3, one.size());
314 assertSame(REF_A, one.get(0));
315 assertSame(REF_B, one.get(1));
316 assertSame(REF_c, one.get(2));
317
318 assertEquals(2, two.size());
319 assertSame(REF_A, two.get(0));
320 assertSame(REF_c, two.get(1));
321 }
322
323 @Test
324 public void testRemoveEndOfList() {
325 RefList<Ref> one = toList(REF_A, REF_B, REF_c);
326 RefList<Ref> two = one.remove(2);
327 assertNotSame(one, two);
328
329 assertEquals(3, one.size());
330 assertSame(REF_A, one.get(0));
331 assertSame(REF_B, one.get(1));
332 assertSame(REF_c, one.get(2));
333
334 assertEquals(2, two.size());
335 assertSame(REF_A, two.get(0));
336 assertSame(REF_B, two.get(1));
337 }
338
339 @Test
340 public void testRemoveMakesEmpty() {
341 RefList<Ref> one = toList(REF_A);
342 RefList<Ref> two = one.remove(1);
343 assertNotSame(one, two);
344 assertSame(two, RefList.emptyList());
345 }
346
347 @Test
348 public void testToString() {
349 StringBuilder exp = new StringBuilder();
350 exp.append("[");
351 exp.append(REF_A);
352 exp.append(", ");
353 exp.append(REF_B);
354 exp.append("]");
355
356 RefList<Ref> list = toList(REF_A, REF_B);
357 assertEquals(exp.toString(), list.toString());
358 }
359
360 @Test
361 public void testBuilder_ToString() {
362 StringBuilder exp = new StringBuilder();
363 exp.append("[");
364 exp.append(REF_A);
365 exp.append(", ");
366 exp.append(REF_B);
367 exp.append("]");
368
369 RefList.Builder<Ref> list = new RefList.Builder<>();
370 list.add(REF_A);
371 list.add(REF_B);
372 assertEquals(exp.toString(), list.toString());
373 }
374
375 @Test
376 public void testFindContainsGet() {
377 RefList<Ref> list = toList(REF_A, REF_B, REF_c);
378
379 assertEquals(0, list.find("A"));
380 assertEquals(1, list.find("B"));
381 assertEquals(2, list.find("c"));
382
383 assertEquals(-1, list.find("0"));
384 assertEquals(-2, list.find("AB"));
385 assertEquals(-3, list.find("a"));
386 assertEquals(-4, list.find("z"));
387
388 assertSame(REF_A, list.get("A"));
389 assertSame(REF_B, list.get("B"));
390 assertSame(REF_c, list.get("c"));
391 assertNull(list.get("AB"));
392 assertNull(list.get("z"));
393
394 assertTrue(list.contains("A"));
395 assertTrue(list.contains("B"));
396 assertTrue(list.contains("c"));
397 assertFalse(list.contains("AB"));
398 assertFalse(list.contains("z"));
399 }
400
401 @Test
402 public void testIterable() {
403 RefList<Ref> list = toList(REF_A, REF_B, REF_c);
404
405 int idx = 0;
406 for (Ref ref : list)
407 assertSame(list.get(idx++), ref);
408 assertEquals(3, idx);
409
410 Iterator<Ref> i = RefList.emptyList().iterator();
411 try {
412 i.next();
413 fail("did not throw NoSuchElementException");
414 } catch (NoSuchElementException err) {
415
416 }
417
418 i = list.iterator();
419 assertTrue(i.hasNext());
420 assertSame(REF_A, i.next());
421 try {
422 i.remove();
423 fail("did not throw UnsupportedOperationException");
424 } catch (UnsupportedOperationException err) {
425
426 }
427 }
428
429 @Test
430 public void testCopyLeadingPrefix() {
431 RefList<Ref> one = toList(REF_A, REF_B, REF_c);
432 RefList<Ref> two = one.copy(2).toRefList();
433 assertNotSame(one, two);
434
435 assertEquals(3, one.size());
436 assertSame(REF_A, one.get(0));
437 assertSame(REF_B, one.get(1));
438 assertSame(REF_c, one.get(2));
439
440 assertEquals(2, two.size());
441 assertSame(REF_A, two.get(0));
442 assertSame(REF_B, two.get(1));
443 }
444
445 @Test
446 public void testCopyConstructorReusesArray() {
447 RefList.Builder<Ref> one = new RefList.Builder<>();
448 one.add(REF_A);
449
450 RefList<Ref> two = new RefList<>(one.toRefList());
451 one.set(0, REF_B);
452 assertSame(REF_B, two.get(0));
453 }
454
455 private static RefList<Ref> toList(Ref... refs) {
456 RefList.Builder<Ref> b = new RefList.Builder<>(refs.length);
457 b.addAll(refs, 0, refs.length);
458 return b.toRefList();
459 }
460
461 private static Ref newRef(String name) {
462 return new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID);
463 }
464 }