View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  			// expected
87  		}
88  	}
89  
90  	@Test
91  	public void testEmptyBuilder() {
92  		RefList<Ref> list = new RefList.Builder<>().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 		// default array capacity should be 16, with no bounds checking.
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 			// expected
109 		}
110 	}
111 
112 	@Test
113 	public void testBuilder_AddThenSort() {
114 		RefList.Builder<Ref> builder = new RefList.Builder<>(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_AddThenDedupe() {
132 		RefList.Builder<Ref> builder = new RefList.Builder<>(1);
133 		builder.add(REF_B);
134 		builder.add(REF_A);
135 		builder.add(REF_A);
136 		builder.add(REF_B);
137 		builder.add(REF_c);
138 
139 		builder.sort();
140 		builder.dedupe((a, b) -> b);
141 		RefList<Ref> list = builder.toRefList();
142 
143 		assertEquals(3, list.size());
144 		assertSame(REF_A, list.get(0));
145 		assertSame(REF_B, list.get(1));
146 		assertSame(REF_c, list.get(2));
147 	}
148 
149 	@Test
150 	public void testBuilder_AddThenDedupe_Border() {
151 		RefList.Builder<Ref> builder = new RefList.Builder<>(1);
152 		builder.sort();
153 		builder.dedupe((a, b) -> b);
154 		RefList<Ref> list = builder.toRefList();
155 		assertTrue(list.isEmpty());
156 
157 		builder = new RefList.Builder<>(1);
158 		builder.add(REF_A);
159 		builder.sort();
160 		builder.dedupe((a, b) -> b);
161 		list = builder.toRefList();
162 		assertEquals(1, list.size());
163 	}
164 
165 	@Test
166 	public void testBuilder_AddAll() {
167 		RefList.Builder<Ref> builder = new RefList.Builder<>(1);
168 		Ref[] src = { REF_A, REF_B, REF_c, REF_A };
169 		builder.addAll(src, 1, 2);
170 
171 		RefList<Ref> list = builder.toRefList();
172 		assertEquals(2, list.size());
173 		assertSame(REF_B, list.get(0));
174 		assertSame(REF_c, list.get(1));
175 	}
176 
177 	@Test
178 	public void testBuilder_Set() {
179 		RefList.Builder<Ref> builder = new RefList.Builder<>();
180 		builder.add(REF_A);
181 		builder.add(REF_A);
182 
183 		assertEquals(2, builder.size());
184 		assertSame(REF_A, builder.get(0));
185 		assertSame(REF_A, builder.get(1));
186 
187 		RefList<Ref> list = builder.toRefList();
188 		assertEquals(2, list.size());
189 		assertSame(REF_A, list.get(0));
190 		assertSame(REF_A, list.get(1));
191 		builder.set(1, REF_B);
192 
193 		list = builder.toRefList();
194 		assertEquals(2, list.size());
195 		assertSame(REF_A, list.get(0));
196 		assertSame(REF_B, list.get(1));
197 	}
198 
199 	@Test
200 	public void testBuilder_Remove() {
201 		RefList.Builder<Ref> builder = new RefList.Builder<>();
202 		builder.add(REF_A);
203 		builder.add(REF_B);
204 		builder.remove(0);
205 
206 		assertEquals(1, builder.size());
207 		assertSame(REF_B, builder.get(0));
208 	}
209 
210 	@Test
211 	public void testSet() {
212 		RefList<Ref> one = toList(REF_A, REF_A);
213 		RefList<Ref> two = one.set(1, REF_B);
214 		assertNotSame(one, two);
215 
216 		// one is not modified
217 		assertEquals(2, one.size());
218 		assertSame(REF_A, one.get(0));
219 		assertSame(REF_A, one.get(1));
220 
221 		// but two is
222 		assertEquals(2, two.size());
223 		assertSame(REF_A, one.get(0));
224 		assertSame(REF_B, two.get(1));
225 	}
226 
227 	@Test
228 	public void testAddToEmptyList() {
229 		RefList<Ref> one = toList();
230 		RefList<Ref> two = one.add(0, REF_B);
231 		assertNotSame(one, two);
232 
233 		// one is not modified, but two is
234 		assertEquals(0, one.size());
235 		assertEquals(1, two.size());
236 		assertFalse(two.isEmpty());
237 		assertSame(REF_B, two.get(0));
238 	}
239 
240 	@Test
241 	public void testAddToFrontOfList() {
242 		RefList<Ref> one = toList(REF_A);
243 		RefList<Ref> two = one.add(0, REF_B);
244 		assertNotSame(one, two);
245 
246 		// one is not modified, but two is
247 		assertEquals(1, one.size());
248 		assertSame(REF_A, one.get(0));
249 		assertEquals(2, two.size());
250 		assertSame(REF_B, two.get(0));
251 		assertSame(REF_A, two.get(1));
252 	}
253 
254 	@Test
255 	public void testAddToEndOfList() {
256 		RefList<Ref> one = toList(REF_A);
257 		RefList<Ref> two = one.add(1, REF_B);
258 		assertNotSame(one, two);
259 
260 		// one is not modified, but two is
261 		assertEquals(1, one.size());
262 		assertSame(REF_A, one.get(0));
263 		assertEquals(2, two.size());
264 		assertSame(REF_A, two.get(0));
265 		assertSame(REF_B, two.get(1));
266 	}
267 
268 	@Test
269 	public void testAddToMiddleOfListByInsertionPosition() {
270 		RefList<Ref> one = toList(REF_A, REF_c);
271 
272 		assertEquals(-2, one.find(REF_B.getName()));
273 
274 		RefList<Ref> two = one.add(one.find(REF_B.getName()), REF_B);
275 		assertNotSame(one, two);
276 
277 		// one is not modified, but two is
278 		assertEquals(2, one.size());
279 		assertSame(REF_A, one.get(0));
280 		assertSame(REF_c, one.get(1));
281 
282 		assertEquals(3, two.size());
283 		assertSame(REF_A, two.get(0));
284 		assertSame(REF_B, two.get(1));
285 		assertSame(REF_c, two.get(2));
286 	}
287 
288 	@Test
289 	public void testPutNewEntry() {
290 		RefList<Ref> one = toList(REF_A, REF_c);
291 		RefList<Ref> two = one.put(REF_B);
292 		assertNotSame(one, two);
293 
294 		// one is not modified, but two is
295 		assertEquals(2, one.size());
296 		assertSame(REF_A, one.get(0));
297 		assertSame(REF_c, one.get(1));
298 
299 		assertEquals(3, two.size());
300 		assertSame(REF_A, two.get(0));
301 		assertSame(REF_B, two.get(1));
302 		assertSame(REF_c, two.get(2));
303 	}
304 
305 	@Test
306 	public void testPutReplaceEntry() {
307 		Ref otherc = newRef(REF_c.getName());
308 		assertNotSame(REF_c, otherc);
309 
310 		RefList<Ref> one = toList(REF_A, REF_c);
311 		RefList<Ref> two = one.put(otherc);
312 		assertNotSame(one, two);
313 
314 		// one is not modified, but two is
315 		assertEquals(2, one.size());
316 		assertSame(REF_A, one.get(0));
317 		assertSame(REF_c, one.get(1));
318 
319 		assertEquals(2, two.size());
320 		assertSame(REF_A, two.get(0));
321 		assertSame(otherc, two.get(1));
322 	}
323 
324 	@Test
325 	public void testRemoveFrontOfList() {
326 		RefList<Ref> one = toList(REF_A, REF_B, REF_c);
327 		RefList<Ref> two = one.remove(0);
328 		assertNotSame(one, two);
329 
330 		assertEquals(3, one.size());
331 		assertSame(REF_A, one.get(0));
332 		assertSame(REF_B, one.get(1));
333 		assertSame(REF_c, one.get(2));
334 
335 		assertEquals(2, two.size());
336 		assertSame(REF_B, two.get(0));
337 		assertSame(REF_c, two.get(1));
338 	}
339 
340 	@Test
341 	public void testRemoveMiddleOfList() {
342 		RefList<Ref> one = toList(REF_A, REF_B, REF_c);
343 		RefList<Ref> two = one.remove(1);
344 		assertNotSame(one, two);
345 
346 		assertEquals(3, one.size());
347 		assertSame(REF_A, one.get(0));
348 		assertSame(REF_B, one.get(1));
349 		assertSame(REF_c, one.get(2));
350 
351 		assertEquals(2, two.size());
352 		assertSame(REF_A, two.get(0));
353 		assertSame(REF_c, two.get(1));
354 	}
355 
356 	@Test
357 	public void testRemoveEndOfList() {
358 		RefList<Ref> one = toList(REF_A, REF_B, REF_c);
359 		RefList<Ref> two = one.remove(2);
360 		assertNotSame(one, two);
361 
362 		assertEquals(3, one.size());
363 		assertSame(REF_A, one.get(0));
364 		assertSame(REF_B, one.get(1));
365 		assertSame(REF_c, one.get(2));
366 
367 		assertEquals(2, two.size());
368 		assertSame(REF_A, two.get(0));
369 		assertSame(REF_B, two.get(1));
370 	}
371 
372 	@Test
373 	public void testRemoveMakesEmpty() {
374 		RefList<Ref> one = toList(REF_A);
375 		RefList<Ref> two = one.remove(1);
376 		assertNotSame(one, two);
377 		assertSame(two, RefList.emptyList());
378 	}
379 
380 	@Test
381 	public void testToString() {
382 		StringBuilder exp = new StringBuilder();
383 		exp.append("[");
384 		exp.append(REF_A);
385 		exp.append(", ");
386 		exp.append(REF_B);
387 		exp.append("]");
388 
389 		RefList<Ref> list = toList(REF_A, REF_B);
390 		assertEquals(exp.toString(), list.toString());
391 	}
392 
393 	@Test
394 	public void testBuilder_ToString() {
395 		StringBuilder exp = new StringBuilder();
396 		exp.append("[");
397 		exp.append(REF_A);
398 		exp.append(", ");
399 		exp.append(REF_B);
400 		exp.append("]");
401 
402 		RefList.Builder<Ref> list = new RefList.Builder<>();
403 		list.add(REF_A);
404 		list.add(REF_B);
405 		assertEquals(exp.toString(), list.toString());
406 	}
407 
408 	@Test
409 	public void testFindContainsGet() {
410 		RefList<Ref> list = toList(REF_A, REF_B, REF_c);
411 
412 		assertEquals(0, list.find("A"));
413 		assertEquals(1, list.find("B"));
414 		assertEquals(2, list.find("c"));
415 
416 		assertEquals(-1, list.find("0"));
417 		assertEquals(-2, list.find("AB"));
418 		assertEquals(-3, list.find("a"));
419 		assertEquals(-4, list.find("z"));
420 
421 		assertSame(REF_A, list.get("A"));
422 		assertSame(REF_B, list.get("B"));
423 		assertSame(REF_c, list.get("c"));
424 		assertNull(list.get("AB"));
425 		assertNull(list.get("z"));
426 
427 		assertTrue(list.contains("A"));
428 		assertTrue(list.contains("B"));
429 		assertTrue(list.contains("c"));
430 		assertFalse(list.contains("AB"));
431 		assertFalse(list.contains("z"));
432 	}
433 
434 	@Test
435 	public void testIterable() {
436 		RefList<Ref> list = toList(REF_A, REF_B, REF_c);
437 
438 		int idx = 0;
439 		for (Ref ref : list)
440 			assertSame(list.get(idx++), ref);
441 		assertEquals(3, idx);
442 
443 		Iterator<Ref> i = RefList.emptyList().iterator();
444 		try {
445 			i.next();
446 			fail("did not throw NoSuchElementException");
447 		} catch (NoSuchElementException err) {
448 			// expected
449 		}
450 
451 		i = list.iterator();
452 		assertTrue(i.hasNext());
453 		assertSame(REF_A, i.next());
454 		try {
455 			i.remove();
456 			fail("did not throw UnsupportedOperationException");
457 		} catch (UnsupportedOperationException err) {
458 			// expected
459 		}
460 	}
461 
462 	@Test
463 	public void testCopyLeadingPrefix() {
464 		RefList<Ref> one = toList(REF_A, REF_B, REF_c);
465 		RefList<Ref> two = one.copy(2).toRefList();
466 		assertNotSame(one, two);
467 
468 		assertEquals(3, one.size());
469 		assertSame(REF_A, one.get(0));
470 		assertSame(REF_B, one.get(1));
471 		assertSame(REF_c, one.get(2));
472 
473 		assertEquals(2, two.size());
474 		assertSame(REF_A, two.get(0));
475 		assertSame(REF_B, two.get(1));
476 	}
477 
478 	@Test
479 	public void testCopyConstructorReusesArray() {
480 		RefList.Builder<Ref> one = new RefList.Builder<>();
481 		one.add(REF_A);
482 
483 		RefList<Ref> two = new RefList<>(one.toRefList());
484 		one.set(0, REF_B);
485 		assertSame(REF_B, two.get(0));
486 	}
487 
488 	private static RefList<Ref> toList(Ref... refs) {
489 		RefList.Builder<Ref> b = new RefList.Builder<>(refs.length);
490 		b.addAll(refs, 0, refs.length);
491 		return b.toRefList();
492 	}
493 
494 	private static Ref newRef(String name) {
495 		return new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID);
496 	}
497 }