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