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

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