Switch jgit library to the EDL (3-clause BSD)
[jgit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / T0002_Tree.java
blobfcb4d96b078f89a7fe13677e993026d44e19798d
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.lib;
41 import java.io.IOException;
42 import java.io.UnsupportedEncodingException;
43 import java.util.ArrayList;
44 import java.util.List;
46 public class T0002_Tree extends RepositoryTestCase {
47 private static final ObjectId SOME_FAKE_ID = ObjectId.fromString(
48 "0123456789abcdef0123456789abcdef01234567");
50 private int compareNamesUsingSpecialCompare(String a,String b) throws UnsupportedEncodingException {
51 char lasta = '\0';
52 byte[] abytes;
53 if (a.length() > 0 && a.charAt(a.length()-1) == '/') {
54 lasta = '/';
55 a = a.substring(0, a.length() - 1);
57 abytes = a.getBytes("ISO-8859-1");
58 char lastb = '\0';
59 byte[] bbytes;
60 if (b.length() > 0 && b.charAt(b.length()-1) == '/') {
61 lastb = '/';
62 b = b.substring(0, b.length() - 1);
64 bbytes = b.getBytes("ISO-8859-1");
65 return Tree.compareNames(abytes, bbytes, lasta, lastb);
68 public void test000_sort_01() throws UnsupportedEncodingException {
69 assertEquals(0, compareNamesUsingSpecialCompare("a","a"));
71 public void test000_sort_02() throws UnsupportedEncodingException {
72 assertEquals(-1, compareNamesUsingSpecialCompare("a","b"));
73 assertEquals(1, compareNamesUsingSpecialCompare("b","a"));
75 public void test000_sort_03() throws UnsupportedEncodingException {
76 assertEquals(1, compareNamesUsingSpecialCompare("a:","a"));
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:"));
83 public void test000_sort_04() throws UnsupportedEncodingException {
84 assertEquals(-1, compareNamesUsingSpecialCompare("a.a","a/a"));
85 assertEquals(1, compareNamesUsingSpecialCompare("a/a","a.a"));
87 public void test000_sort_05() throws UnsupportedEncodingException {
88 assertEquals(-1, compareNamesUsingSpecialCompare("a.","a/"));
89 assertEquals(1, compareNamesUsingSpecialCompare("a/","a."));
93 public void test001_createEmpty() throws IOException {
94 final Tree t = new Tree(db);
95 assertTrue("isLoaded", t.isLoaded());
96 assertTrue("isModified", t.isModified());
97 assertTrue("no parent", t.getParent() == null);
98 assertTrue("isRoot", t.isRoot());
99 assertTrue("no name", t.getName() == null);
100 assertTrue("no nameUTF8", t.getNameUTF8() == null);
101 assertTrue("has entries array", t.members() != null);
102 assertTrue("entries is empty", t.members().length == 0);
103 assertEquals("full name is empty", "", t.getFullName());
104 assertTrue("no id", t.getId() == null);
105 assertTrue("tree is self", t.getTree() == t);
106 assertTrue("database is r", t.getRepository() == db);
107 assertTrue("no foo child", t.findTreeMember("foo") == null);
108 assertTrue("no foo child", t.findBlobMember("foo") == null);
111 public void test002_addFile() throws IOException {
112 final Tree t = new Tree(db);
113 t.setId(SOME_FAKE_ID);
114 assertTrue("has id", t.getId() != null);
115 assertFalse("not modified", t.isModified());
117 final String n = "bob";
118 final FileTreeEntry f = t.addFile(n);
119 assertNotNull("have file", f);
120 assertEquals("name matches", n, f.getName());
121 assertEquals("name matches", f.getName(), new String(f.getNameUTF8(),
122 "UTF-8"));
123 assertEquals("full name matches", n, f.getFullName());
124 assertTrue("no id", f.getId() == null);
125 assertTrue("is modified", t.isModified());
126 assertTrue("has no id", t.getId() == null);
127 assertTrue("found bob", t.findBlobMember(f.getName()) == f);
129 final TreeEntry[] i = t.members();
130 assertNotNull("members array not null", i);
131 assertTrue("iterator is not empty", i != null && i.length > 0);
132 assertTrue("iterator returns file", i != null && i[0] == f);
133 assertTrue("iterator is empty", i != null && i.length == 1);
136 public void test004_addTree() throws IOException {
137 final Tree t = new Tree(db);
138 t.setId(SOME_FAKE_ID);
139 assertTrue("has id", t.getId() != null);
140 assertFalse("not modified", t.isModified());
142 final String n = "bob";
143 final Tree f = t.addTree(n);
144 assertNotNull("have tree", f);
145 assertEquals("name matches", n, f.getName());
146 assertEquals("name matches", f.getName(), new String(f.getNameUTF8(),
147 "UTF-8"));
148 assertEquals("full name matches", n, f.getFullName());
149 assertTrue("no id", f.getId() == null);
150 assertTrue("parent matches", f.getParent() == t);
151 assertTrue("repository matches", f.getRepository() == db);
152 assertTrue("isLoaded", f.isLoaded());
153 assertFalse("has items", f.members().length > 0);
154 assertFalse("is root", f.isRoot());
155 assertTrue("tree is self", f.getTree() == f);
156 assertTrue("parent is modified", t.isModified());
157 assertTrue("parent has no id", t.getId() == null);
158 assertTrue("found bob child", t.findTreeMember(f.getName()) == f);
160 final TreeEntry[] i = t.members();
161 assertTrue("iterator is not empty", i.length > 0);
162 assertTrue("iterator returns file", i[0] == f);
163 assertTrue("iterator is empty", i.length == 1);
166 public void test005_addRecursiveFile() throws IOException {
167 final Tree t = new Tree(db);
168 final FileTreeEntry f = t.addFile("a/b/c");
169 assertNotNull("created f", f);
170 assertEquals("c", f.getName());
171 assertEquals("b", f.getParent().getName());
172 assertEquals("a", f.getParent().getParent().getName());
173 assertTrue("t is great-grandparent", t == f.getParent().getParent()
174 .getParent());
177 public void test005_addRecursiveTree() throws IOException {
178 final Tree t = new Tree(db);
179 final Tree f = t.addTree("a/b/c");
180 assertNotNull("created f", f);
181 assertEquals("c", f.getName());
182 assertEquals("b", f.getParent().getName());
183 assertEquals("a", f.getParent().getParent().getName());
184 assertTrue("t is great-grandparent", t == f.getParent().getParent()
185 .getParent());
188 public void test006_addDeepTree() throws IOException {
189 final Tree t = new Tree(db);
191 final Tree e = t.addTree("e");
192 assertNotNull("have e", e);
193 assertTrue("e.parent == t", e.getParent() == t);
194 final Tree f = t.addTree("f");
195 assertNotNull("have f", f);
196 assertTrue("f.parent == t", f.getParent() == t);
197 final Tree g = f.addTree("g");
198 assertNotNull("have g", g);
199 assertTrue("g.parent == f", g.getParent() == f);
200 final Tree h = g.addTree("h");
201 assertNotNull("have h", h);
202 assertTrue("h.parent = g", h.getParent() == g);
204 h.setId(SOME_FAKE_ID);
205 assertTrue("h not modified", !h.isModified());
206 g.setId(SOME_FAKE_ID);
207 assertTrue("g not modified", !g.isModified());
208 f.setId(SOME_FAKE_ID);
209 assertTrue("f not modified", !f.isModified());
210 e.setId(SOME_FAKE_ID);
211 assertTrue("e not modified", !e.isModified());
212 t.setId(SOME_FAKE_ID);
213 assertTrue("t not modified.", !t.isModified());
215 assertEquals("full path of h ok", "f/g/h", h.getFullName());
216 assertTrue("Can find h", t.findTreeMember(h.getFullName()) == h);
217 assertTrue("Can't find f/z", t.findBlobMember("f/z") == null);
218 assertTrue("Can't find y/z", t.findBlobMember("y/z") == null);
220 final FileTreeEntry i = h.addFile("i");
221 assertNotNull(i);
222 assertEquals("full path of i ok", "f/g/h/i", i.getFullName());
223 assertTrue("Can find i", t.findBlobMember(i.getFullName()) == i);
224 assertTrue("h modified", h.isModified());
225 assertTrue("g modified", g.isModified());
226 assertTrue("f modified", f.isModified());
227 assertTrue("e not modified", !e.isModified());
228 assertTrue("t modified", t.isModified());
230 assertTrue("h no id", h.getId() == null);
231 assertTrue("g no id", g.getId() == null);
232 assertTrue("f no id", f.getId() == null);
233 assertTrue("e has id", e.getId() != null);
234 assertTrue("t no id", t.getId() == null);
237 public void test007_manyFileLookup() throws IOException {
238 final Tree t = new Tree(db);
239 final List files = new ArrayList(26 * 26);
240 for (char level1 = 'a'; level1 <= 'z'; level1++) {
241 for (char level2 = 'a'; level2 <= 'z'; level2++) {
242 final String n = "." + level1 + level2 + "9";
243 final FileTreeEntry f = t.addFile(n);
244 assertNotNull("File " + n + " added.", f);
245 assertEquals(n, f.getName());
246 files.add(f);
249 assertEquals(files.size(), t.memberCount());
250 final TreeEntry[] ents = t.members();
251 assertNotNull(ents);
252 assertEquals(files.size(), ents.length);
253 for (int k = 0; k < ents.length; k++) {
254 assertTrue("File " + ((FileTreeEntry) files.get(k)).getName()
255 + " is at " + k + ".", files.get(k) == ents[k]);
259 public void test008_SubtreeInternalSorting() throws IOException {
260 final Tree t = new Tree(db);
261 final FileTreeEntry e0 = t.addFile("a-b");
262 final FileTreeEntry e1 = t.addFile("a-");
263 final FileTreeEntry e2 = t.addFile("a=b");
264 final Tree e3 = t.addTree("a");
265 final FileTreeEntry e4 = t.addFile("a=");
267 final TreeEntry[] ents = t.members();
268 assertSame(e1, ents[0]);
269 assertSame(e0, ents[1]);
270 assertSame(e3, ents[2]);
271 assertSame(e4, ents[3]);
272 assertSame(e2, ents[4]);