You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RefListTest.java 12KB

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