Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

T0002_Tree.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 java.io.IOException;
  46. import java.io.UnsupportedEncodingException;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. public class T0002_Tree extends SampleDataRepositoryTestCase {
  50. private static final ObjectId SOME_FAKE_ID = ObjectId.fromString(
  51. "0123456789abcdef0123456789abcdef01234567");
  52. private int compareNamesUsingSpecialCompare(String a,String b) throws UnsupportedEncodingException {
  53. char lasta = '\0';
  54. byte[] abytes;
  55. if (a.length() > 0 && a.charAt(a.length()-1) == '/') {
  56. lasta = '/';
  57. a = a.substring(0, a.length() - 1);
  58. }
  59. abytes = a.getBytes("ISO-8859-1");
  60. char lastb = '\0';
  61. byte[] bbytes;
  62. if (b.length() > 0 && b.charAt(b.length()-1) == '/') {
  63. lastb = '/';
  64. b = b.substring(0, b.length() - 1);
  65. }
  66. bbytes = b.getBytes("ISO-8859-1");
  67. return Tree.compareNames(abytes, bbytes, lasta, lastb);
  68. }
  69. public void test000_sort_01() throws UnsupportedEncodingException {
  70. assertEquals(0, compareNamesUsingSpecialCompare("a","a"));
  71. }
  72. public void test000_sort_02() throws UnsupportedEncodingException {
  73. assertEquals(-1, compareNamesUsingSpecialCompare("a","b"));
  74. assertEquals(1, compareNamesUsingSpecialCompare("b","a"));
  75. }
  76. public void test000_sort_03() throws UnsupportedEncodingException {
  77. assertEquals(1, compareNamesUsingSpecialCompare("a:","a"));
  78. assertEquals(1, compareNamesUsingSpecialCompare("a/","a"));
  79. assertEquals(-1, compareNamesUsingSpecialCompare("a","a/"));
  80. assertEquals(-1, compareNamesUsingSpecialCompare("a","a:"));
  81. assertEquals(1, compareNamesUsingSpecialCompare("a:","a/"));
  82. assertEquals(-1, compareNamesUsingSpecialCompare("a/","a:"));
  83. }
  84. public void test000_sort_04() throws UnsupportedEncodingException {
  85. assertEquals(-1, compareNamesUsingSpecialCompare("a.a","a/a"));
  86. assertEquals(1, compareNamesUsingSpecialCompare("a/a","a.a"));
  87. }
  88. public void test000_sort_05() throws UnsupportedEncodingException {
  89. assertEquals(-1, compareNamesUsingSpecialCompare("a.","a/"));
  90. assertEquals(1, compareNamesUsingSpecialCompare("a/","a."));
  91. }
  92. public void test001_createEmpty() throws IOException {
  93. final Tree t = new Tree(db);
  94. assertTrue("isLoaded", t.isLoaded());
  95. assertTrue("isModified", t.isModified());
  96. assertTrue("no parent", t.getParent() == null);
  97. assertTrue("isRoot", t.isRoot());
  98. assertTrue("no name", t.getName() == null);
  99. assertTrue("no nameUTF8", t.getNameUTF8() == null);
  100. assertTrue("has entries array", t.members() != null);
  101. assertTrue("entries is empty", t.members().length == 0);
  102. assertEquals("full name is empty", "", t.getFullName());
  103. assertTrue("no id", t.getId() == null);
  104. assertTrue("tree is self", t.getTree() == t);
  105. assertTrue("database is r", t.getRepository() == db);
  106. assertTrue("no foo child", t.findTreeMember("foo") == null);
  107. assertTrue("no foo child", t.findBlobMember("foo") == null);
  108. }
  109. public void test002_addFile() throws IOException {
  110. final Tree t = new Tree(db);
  111. t.setId(SOME_FAKE_ID);
  112. assertTrue("has id", t.getId() != null);
  113. assertFalse("not modified", t.isModified());
  114. final String n = "bob";
  115. final FileTreeEntry f = t.addFile(n);
  116. assertNotNull("have file", f);
  117. assertEquals("name matches", n, f.getName());
  118. assertEquals("name matches", f.getName(), new String(f.getNameUTF8(),
  119. "UTF-8"));
  120. assertEquals("full name matches", n, f.getFullName());
  121. assertTrue("no id", f.getId() == null);
  122. assertTrue("is modified", t.isModified());
  123. assertTrue("has no id", t.getId() == null);
  124. assertTrue("found bob", t.findBlobMember(f.getName()) == f);
  125. final TreeEntry[] i = t.members();
  126. assertNotNull("members array not null", i);
  127. assertTrue("iterator is not empty", i != null && i.length > 0);
  128. assertTrue("iterator returns file", i != null && i[0] == f);
  129. assertTrue("iterator is empty", i != null && i.length == 1);
  130. }
  131. public void test004_addTree() throws IOException {
  132. final Tree t = new Tree(db);
  133. t.setId(SOME_FAKE_ID);
  134. assertTrue("has id", t.getId() != null);
  135. assertFalse("not modified", t.isModified());
  136. final String n = "bob";
  137. final Tree f = t.addTree(n);
  138. assertNotNull("have tree", f);
  139. assertEquals("name matches", n, f.getName());
  140. assertEquals("name matches", f.getName(), new String(f.getNameUTF8(),
  141. "UTF-8"));
  142. assertEquals("full name matches", n, f.getFullName());
  143. assertTrue("no id", f.getId() == null);
  144. assertTrue("parent matches", f.getParent() == t);
  145. assertTrue("repository matches", f.getRepository() == db);
  146. assertTrue("isLoaded", f.isLoaded());
  147. assertFalse("has items", f.members().length > 0);
  148. assertFalse("is root", f.isRoot());
  149. assertTrue("tree is self", f.getTree() == f);
  150. assertTrue("parent is modified", t.isModified());
  151. assertTrue("parent has no id", t.getId() == null);
  152. assertTrue("found bob child", t.findTreeMember(f.getName()) == f);
  153. final TreeEntry[] i = t.members();
  154. assertTrue("iterator is not empty", i.length > 0);
  155. assertTrue("iterator returns file", i[0] == f);
  156. assertTrue("iterator is empty", i.length == 1);
  157. }
  158. public void test005_addRecursiveFile() throws IOException {
  159. final Tree t = new Tree(db);
  160. final FileTreeEntry f = t.addFile("a/b/c");
  161. assertNotNull("created f", f);
  162. assertEquals("c", f.getName());
  163. assertEquals("b", f.getParent().getName());
  164. assertEquals("a", f.getParent().getParent().getName());
  165. assertTrue("t is great-grandparent", t == f.getParent().getParent()
  166. .getParent());
  167. }
  168. public void test005_addRecursiveTree() throws IOException {
  169. final Tree t = new Tree(db);
  170. final Tree f = t.addTree("a/b/c");
  171. assertNotNull("created f", f);
  172. assertEquals("c", f.getName());
  173. assertEquals("b", f.getParent().getName());
  174. assertEquals("a", f.getParent().getParent().getName());
  175. assertTrue("t is great-grandparent", t == f.getParent().getParent()
  176. .getParent());
  177. }
  178. public void test006_addDeepTree() throws IOException {
  179. final Tree t = new Tree(db);
  180. final Tree e = t.addTree("e");
  181. assertNotNull("have e", e);
  182. assertTrue("e.parent == t", e.getParent() == t);
  183. final Tree f = t.addTree("f");
  184. assertNotNull("have f", f);
  185. assertTrue("f.parent == t", f.getParent() == t);
  186. final Tree g = f.addTree("g");
  187. assertNotNull("have g", g);
  188. assertTrue("g.parent == f", g.getParent() == f);
  189. final Tree h = g.addTree("h");
  190. assertNotNull("have h", h);
  191. assertTrue("h.parent = g", h.getParent() == g);
  192. h.setId(SOME_FAKE_ID);
  193. assertTrue("h not modified", !h.isModified());
  194. g.setId(SOME_FAKE_ID);
  195. assertTrue("g not modified", !g.isModified());
  196. f.setId(SOME_FAKE_ID);
  197. assertTrue("f not modified", !f.isModified());
  198. e.setId(SOME_FAKE_ID);
  199. assertTrue("e not modified", !e.isModified());
  200. t.setId(SOME_FAKE_ID);
  201. assertTrue("t not modified.", !t.isModified());
  202. assertEquals("full path of h ok", "f/g/h", h.getFullName());
  203. assertTrue("Can find h", t.findTreeMember(h.getFullName()) == h);
  204. assertTrue("Can't find f/z", t.findBlobMember("f/z") == null);
  205. assertTrue("Can't find y/z", t.findBlobMember("y/z") == null);
  206. final FileTreeEntry i = h.addFile("i");
  207. assertNotNull(i);
  208. assertEquals("full path of i ok", "f/g/h/i", i.getFullName());
  209. assertTrue("Can find i", t.findBlobMember(i.getFullName()) == i);
  210. assertTrue("h modified", h.isModified());
  211. assertTrue("g modified", g.isModified());
  212. assertTrue("f modified", f.isModified());
  213. assertTrue("e not modified", !e.isModified());
  214. assertTrue("t modified", t.isModified());
  215. assertTrue("h no id", h.getId() == null);
  216. assertTrue("g no id", g.getId() == null);
  217. assertTrue("f no id", f.getId() == null);
  218. assertTrue("e has id", e.getId() != null);
  219. assertTrue("t no id", t.getId() == null);
  220. }
  221. public void test007_manyFileLookup() throws IOException {
  222. final Tree t = new Tree(db);
  223. final List<FileTreeEntry> files = new ArrayList<FileTreeEntry>(26 * 26);
  224. for (char level1 = 'a'; level1 <= 'z'; level1++) {
  225. for (char level2 = 'a'; level2 <= 'z'; level2++) {
  226. final String n = "." + level1 + level2 + "9";
  227. final FileTreeEntry f = t.addFile(n);
  228. assertNotNull("File " + n + " added.", f);
  229. assertEquals(n, f.getName());
  230. files.add(f);
  231. }
  232. }
  233. assertEquals(files.size(), t.memberCount());
  234. final TreeEntry[] ents = t.members();
  235. assertNotNull(ents);
  236. assertEquals(files.size(), ents.length);
  237. for (int k = 0; k < ents.length; k++) {
  238. assertTrue("File " + files.get(k).getName()
  239. + " is at " + k + ".", files.get(k) == ents[k]);
  240. }
  241. }
  242. public void test008_SubtreeInternalSorting() throws IOException {
  243. final Tree t = new Tree(db);
  244. final FileTreeEntry e0 = t.addFile("a-b");
  245. final FileTreeEntry e1 = t.addFile("a-");
  246. final FileTreeEntry e2 = t.addFile("a=b");
  247. final Tree e3 = t.addTree("a");
  248. final FileTreeEntry e4 = t.addFile("a=");
  249. final TreeEntry[] ents = t.members();
  250. assertSame(e1, ents[0]);
  251. assertSame(e0, ents[1]);
  252. assertSame(e3, ents[2]);
  253. assertSame(e4, ents[3]);
  254. assertSame(e2, ents[4]);
  255. }
  256. public void test009_SymlinkAndGitlink() throws IOException {
  257. final Tree symlinkTree = db.mapTree("symlink");
  258. assertTrue("Symlink entry exists", symlinkTree.existsBlob("symlink.txt"));
  259. final Tree gitlinkTree = db.mapTree("gitlink");
  260. assertTrue("Gitlink entry exists", gitlinkTree.existsBlob("submodule"));
  261. }
  262. }