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.

T0002_TreeTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertSame;
  49. import static org.junit.Assert.assertTrue;
  50. import java.io.IOException;
  51. import java.io.UnsupportedEncodingException;
  52. import java.util.ArrayList;
  53. import java.util.List;
  54. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  55. import org.junit.Test;
  56. @SuppressWarnings("deprecation")
  57. public class T0002_TreeTest extends SampleDataRepositoryTestCase {
  58. private static final ObjectId SOME_FAKE_ID = ObjectId.fromString(
  59. "0123456789abcdef0123456789abcdef01234567");
  60. private static int compareNamesUsingSpecialCompare(String a, String b)
  61. throws UnsupportedEncodingException {
  62. char lasta = '\0';
  63. byte[] abytes;
  64. if (a.length() > 0 && a.charAt(a.length()-1) == '/') {
  65. lasta = '/';
  66. a = a.substring(0, a.length() - 1);
  67. }
  68. abytes = a.getBytes("ISO-8859-1");
  69. char lastb = '\0';
  70. byte[] bbytes;
  71. if (b.length() > 0 && b.charAt(b.length()-1) == '/') {
  72. lastb = '/';
  73. b = b.substring(0, b.length() - 1);
  74. }
  75. bbytes = b.getBytes("ISO-8859-1");
  76. return Tree.compareNames(abytes, bbytes, lasta, lastb);
  77. }
  78. @Test
  79. public void test000_sort_01() throws UnsupportedEncodingException {
  80. assertEquals(0, compareNamesUsingSpecialCompare("a","a"));
  81. }
  82. @Test
  83. public void test000_sort_02() throws UnsupportedEncodingException {
  84. assertEquals(-1, compareNamesUsingSpecialCompare("a","b"));
  85. assertEquals(1, compareNamesUsingSpecialCompare("b","a"));
  86. }
  87. @Test
  88. public void test000_sort_03() throws UnsupportedEncodingException {
  89. assertEquals(1, compareNamesUsingSpecialCompare("a:","a"));
  90. assertEquals(1, compareNamesUsingSpecialCompare("a/","a"));
  91. assertEquals(-1, compareNamesUsingSpecialCompare("a","a/"));
  92. assertEquals(-1, compareNamesUsingSpecialCompare("a","a:"));
  93. assertEquals(1, compareNamesUsingSpecialCompare("a:","a/"));
  94. assertEquals(-1, compareNamesUsingSpecialCompare("a/","a:"));
  95. }
  96. @Test
  97. public void test000_sort_04() throws UnsupportedEncodingException {
  98. assertEquals(-1, compareNamesUsingSpecialCompare("a.a","a/a"));
  99. assertEquals(1, compareNamesUsingSpecialCompare("a/a","a.a"));
  100. }
  101. @Test
  102. public void test000_sort_05() throws UnsupportedEncodingException {
  103. assertEquals(-1, compareNamesUsingSpecialCompare("a.","a/"));
  104. assertEquals(1, compareNamesUsingSpecialCompare("a/","a."));
  105. }
  106. @Test
  107. public void test001_createEmpty() throws IOException {
  108. final Tree t = new Tree(db);
  109. assertTrue("isLoaded", t.isLoaded());
  110. assertTrue("isModified", t.isModified());
  111. assertTrue("no parent", t.getParent() == null);
  112. assertTrue("isRoot", t.isRoot());
  113. assertTrue("no name", t.getName() == null);
  114. assertTrue("no nameUTF8", t.getNameUTF8() == null);
  115. assertTrue("has entries array", t.members() != null);
  116. assertEquals("entries is empty", 0, t.members().length);
  117. assertEquals("full name is empty", "", t.getFullName());
  118. assertTrue("no id", t.getId() == null);
  119. assertTrue("database is r", t.getRepository() == db);
  120. assertTrue("no foo child", t.findTreeMember("foo") == null);
  121. assertTrue("no foo child", t.findBlobMember("foo") == null);
  122. }
  123. @Test
  124. public void test002_addFile() throws IOException {
  125. final Tree t = new Tree(db);
  126. t.setId(SOME_FAKE_ID);
  127. assertTrue("has id", t.getId() != null);
  128. assertFalse("not modified", t.isModified());
  129. final String n = "bob";
  130. final FileTreeEntry f = t.addFile(n);
  131. assertNotNull("have file", f);
  132. assertEquals("name matches", n, f.getName());
  133. assertEquals("name matches", f.getName(), new String(f.getNameUTF8(),
  134. "UTF-8"));
  135. assertEquals("full name matches", n, f.getFullName());
  136. assertTrue("no id", f.getId() == null);
  137. assertTrue("is modified", t.isModified());
  138. assertTrue("has no id", t.getId() == null);
  139. assertTrue("found bob", t.findBlobMember(f.getName()) == f);
  140. final TreeEntry[] i = t.members();
  141. assertNotNull("members array not null", i);
  142. assertTrue("iterator is not empty", i != null && i.length > 0);
  143. assertTrue("iterator returns file", i != null && i[0] == f);
  144. assertTrue("iterator is empty", i != null && i.length == 1);
  145. }
  146. @Test
  147. public void test004_addTree() throws IOException {
  148. final Tree t = new Tree(db);
  149. t.setId(SOME_FAKE_ID);
  150. assertTrue("has id", t.getId() != null);
  151. assertFalse("not modified", t.isModified());
  152. final String n = "bob";
  153. final Tree f = t.addTree(n);
  154. assertNotNull("have tree", f);
  155. assertEquals("name matches", n, f.getName());
  156. assertEquals("name matches", f.getName(), new String(f.getNameUTF8(),
  157. "UTF-8"));
  158. assertEquals("full name matches", n, f.getFullName());
  159. assertTrue("no id", f.getId() == null);
  160. assertTrue("parent matches", f.getParent() == t);
  161. assertTrue("repository matches", f.getRepository() == db);
  162. assertTrue("isLoaded", f.isLoaded());
  163. assertFalse("has items", f.members().length > 0);
  164. assertFalse("is root", f.isRoot());
  165. assertTrue("parent is modified", t.isModified());
  166. assertTrue("parent has no id", t.getId() == null);
  167. assertTrue("found bob child", t.findTreeMember(f.getName()) == f);
  168. final TreeEntry[] i = t.members();
  169. assertTrue("iterator is not empty", i.length > 0);
  170. assertTrue("iterator returns file", i[0] == f);
  171. assertEquals("iterator is empty", 1, i.length);
  172. }
  173. @Test
  174. public void test005_addRecursiveFile() throws IOException {
  175. final Tree t = new Tree(db);
  176. final FileTreeEntry f = t.addFile("a/b/c");
  177. assertNotNull("created f", f);
  178. assertEquals("c", f.getName());
  179. assertEquals("b", f.getParent().getName());
  180. assertEquals("a", f.getParent().getParent().getName());
  181. assertTrue("t is great-grandparent", t == f.getParent().getParent()
  182. .getParent());
  183. }
  184. @Test
  185. public void test005_addRecursiveTree() throws IOException {
  186. final Tree t = new Tree(db);
  187. final Tree f = t.addTree("a/b/c");
  188. assertNotNull("created f", f);
  189. assertEquals("c", f.getName());
  190. assertEquals("b", f.getParent().getName());
  191. assertEquals("a", f.getParent().getParent().getName());
  192. assertTrue("t is great-grandparent", t == f.getParent().getParent()
  193. .getParent());
  194. }
  195. @Test
  196. public void test006_addDeepTree() throws IOException {
  197. final Tree t = new Tree(db);
  198. final Tree e = t.addTree("e");
  199. assertNotNull("have e", e);
  200. assertTrue("e.parent == t", e.getParent() == t);
  201. final Tree f = t.addTree("f");
  202. assertNotNull("have f", f);
  203. assertTrue("f.parent == t", f.getParent() == t);
  204. final Tree g = f.addTree("g");
  205. assertNotNull("have g", g);
  206. assertTrue("g.parent == f", g.getParent() == f);
  207. final Tree h = g.addTree("h");
  208. assertNotNull("have h", h);
  209. assertTrue("h.parent = g", h.getParent() == g);
  210. h.setId(SOME_FAKE_ID);
  211. assertTrue("h not modified", !h.isModified());
  212. g.setId(SOME_FAKE_ID);
  213. assertTrue("g not modified", !g.isModified());
  214. f.setId(SOME_FAKE_ID);
  215. assertTrue("f not modified", !f.isModified());
  216. e.setId(SOME_FAKE_ID);
  217. assertTrue("e not modified", !e.isModified());
  218. t.setId(SOME_FAKE_ID);
  219. assertTrue("t not modified.", !t.isModified());
  220. assertEquals("full path of h ok", "f/g/h", h.getFullName());
  221. assertTrue("Can find h", t.findTreeMember(h.getFullName()) == h);
  222. assertTrue("Can't find f/z", t.findBlobMember("f/z") == null);
  223. assertTrue("Can't find y/z", t.findBlobMember("y/z") == null);
  224. final FileTreeEntry i = h.addFile("i");
  225. assertNotNull(i);
  226. assertEquals("full path of i ok", "f/g/h/i", i.getFullName());
  227. assertTrue("Can find i", t.findBlobMember(i.getFullName()) == i);
  228. assertTrue("h modified", h.isModified());
  229. assertTrue("g modified", g.isModified());
  230. assertTrue("f modified", f.isModified());
  231. assertTrue("e not modified", !e.isModified());
  232. assertTrue("t modified", t.isModified());
  233. assertTrue("h no id", h.getId() == null);
  234. assertTrue("g no id", g.getId() == null);
  235. assertTrue("f no id", f.getId() == null);
  236. assertTrue("e has id", e.getId() != null);
  237. assertTrue("t no id", t.getId() == null);
  238. }
  239. @Test
  240. public void test007_manyFileLookup() throws IOException {
  241. final Tree t = new Tree(db);
  242. final List<FileTreeEntry> files = new ArrayList<FileTreeEntry>(26 * 26);
  243. for (char level1 = 'a'; level1 <= 'z'; level1++) {
  244. for (char level2 = 'a'; level2 <= 'z'; level2++) {
  245. final String n = "." + level1 + level2 + "9";
  246. final FileTreeEntry f = t.addFile(n);
  247. assertNotNull("File " + n + " added.", f);
  248. assertEquals(n, f.getName());
  249. files.add(f);
  250. }
  251. }
  252. assertEquals(files.size(), t.memberCount());
  253. final TreeEntry[] ents = t.members();
  254. assertNotNull(ents);
  255. assertEquals(files.size(), ents.length);
  256. for (int k = 0; k < ents.length; k++) {
  257. assertTrue("File " + files.get(k).getName()
  258. + " is at " + k + ".", files.get(k) == ents[k]);
  259. }
  260. }
  261. @Test
  262. public void test008_SubtreeInternalSorting() throws IOException {
  263. final Tree t = new Tree(db);
  264. final FileTreeEntry e0 = t.addFile("a-b");
  265. final FileTreeEntry e1 = t.addFile("a-");
  266. final FileTreeEntry e2 = t.addFile("a=b");
  267. final Tree e3 = t.addTree("a");
  268. final FileTreeEntry e4 = t.addFile("a=");
  269. final TreeEntry[] ents = t.members();
  270. assertSame(e1, ents[0]);
  271. assertSame(e0, ents[1]);
  272. assertSame(e3, ents[2]);
  273. assertSame(e4, ents[3]);
  274. assertSame(e2, ents[4]);
  275. }
  276. @Test
  277. public void test009_SymlinkAndGitlink() throws IOException {
  278. final Tree symlinkTree = mapTree("symlink");
  279. assertTrue("Symlink entry exists", symlinkTree.existsBlob("symlink.txt"));
  280. final Tree gitlinkTree = mapTree("gitlink");
  281. assertTrue("Gitlink entry exists", gitlinkTree.existsBlob("submodule"));
  282. }
  283. private Tree mapTree(String name) throws IOException {
  284. ObjectId id = db.resolve(name + "^{tree}");
  285. return new Tree(db, id, db.open(id).getCachedBytes());
  286. }
  287. }