2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.jgit
.lib
;
19 import java
.io
.IOException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 public class T0002_Tree
extends RepositoryTestCase
{
24 private static final ObjectId SOME_FAKE_ID
= new ObjectId(
25 "0123456789abcdef0123456789abcdef01234567");
27 public void test001_createEmpty() throws IOException
{
28 final Tree t
= new Tree(db
);
29 assertTrue("isLoaded", t
.isLoaded());
30 assertTrue("isModified", t
.isModified());
31 assertTrue("no parent", t
.getParent() == null);
32 assertTrue("isRoot", t
.isRoot());
33 assertTrue("no name", t
.getName() == null);
34 assertTrue("no nameUTF8", t
.getNameUTF8() == null);
35 assertTrue("has entries array", t
.members() != null);
36 assertTrue("entries is empty", t
.members().length
== 0);
37 assertEquals("full name is empty", "", t
.getFullName());
38 assertTrue("no id", t
.getId() == null);
39 assertTrue("tree is self", t
.getTree() == t
);
40 assertTrue("database is r", t
.getRepository() == db
);
41 assertTrue("no foo child", t
.findTreeMember("foo") == null);
42 assertTrue("no foo child", t
.findBlobMember("foo") == null);
45 public void test002_addFile() throws IOException
{
46 final Tree t
= new Tree(db
);
47 t
.setId(SOME_FAKE_ID
);
48 assertTrue("has id", t
.getId() != null);
49 assertFalse("not modified", t
.isModified());
51 final String n
= "bob";
52 final FileTreeEntry f
= t
.addFile(n
);
53 assertNotNull("have file", f
);
54 assertEquals("name matches", n
, f
.getName());
55 assertEquals("name matches", f
.getName(), new String(f
.getNameUTF8(),
57 assertEquals("full name matches", n
, f
.getFullName());
58 assertTrue("no id", f
.getId() == null);
59 assertTrue("is modified", t
.isModified());
60 assertTrue("has no id", t
.getId() == null);
61 assertTrue("found bob", t
.findBlobMember(f
.getName()) == f
);
63 final TreeEntry
[] i
= t
.members();
64 assertTrue("iterator is not empty", i
!= null && i
.length
> 0);
65 assertTrue("iterator returns file", i
[0] == f
);
66 assertTrue("iterator is empty", i
.length
== 1);
69 public void test004_addTree() throws IOException
{
70 final Tree t
= new Tree(db
);
71 t
.setId(SOME_FAKE_ID
);
72 assertTrue("has id", t
.getId() != null);
73 assertFalse("not modified", t
.isModified());
75 final String n
= "bob";
76 final Tree f
= t
.addTree(n
);
77 assertNotNull("have tree", f
);
78 assertEquals("name matches", n
, f
.getName());
79 assertEquals("name matches", f
.getName(), new String(f
.getNameUTF8(),
81 assertEquals("full name matches", n
, f
.getFullName());
82 assertTrue("no id", f
.getId() == null);
83 assertTrue("parent matches", f
.getParent() == t
);
84 assertTrue("repository matches", f
.getRepository() == db
);
85 assertTrue("isLoaded", f
.isLoaded());
86 assertFalse("has items", f
.members().length
> 0);
87 assertFalse("is root", f
.isRoot());
88 assertTrue("tree is self", f
.getTree() == f
);
89 assertTrue("parent is modified", t
.isModified());
90 assertTrue("parent has no id", t
.getId() == null);
91 assertTrue("found bob child", t
.findTreeMember(f
.getName()) == f
);
93 final TreeEntry
[] i
= t
.members();
94 assertTrue("iterator is not empty", i
.length
> 0);
95 assertTrue("iterator returns file", i
[0] == f
);
96 assertTrue("iterator is empty", i
.length
== 1);
99 public void test005_addRecursiveFile() throws IOException
{
100 final Tree t
= new Tree(db
);
101 final FileTreeEntry f
= t
.addFile("a/b/c");
102 assertNotNull("created f", f
);
103 assertEquals("c", f
.getName());
104 assertEquals("b", f
.getParent().getName());
105 assertEquals("a", f
.getParent().getParent().getName());
106 assertTrue("t is great-grandparent", t
== f
.getParent().getParent()
110 public void test005_addRecursiveTree() throws IOException
{
111 final Tree t
= new Tree(db
);
112 final Tree f
= t
.addTree("a/b/c");
113 assertNotNull("created f", f
);
114 assertEquals("c", f
.getName());
115 assertEquals("b", f
.getParent().getName());
116 assertEquals("a", f
.getParent().getParent().getName());
117 assertTrue("t is great-grandparent", t
== f
.getParent().getParent()
121 public void test006_addDeepTree() throws IOException
{
122 final Tree t
= new Tree(db
);
124 final Tree e
= t
.addTree("e");
125 assertNotNull("have e", e
);
126 assertTrue("e.parent == t", e
.getParent() == t
);
127 final Tree f
= t
.addTree("f");
128 assertNotNull("have f", f
);
129 assertTrue("f.parent == t", f
.getParent() == t
);
130 final Tree g
= f
.addTree("g");
131 assertNotNull("have g", g
);
132 assertTrue("g.parent == f", g
.getParent() == f
);
133 final Tree h
= g
.addTree("h");
134 assertNotNull("have h", h
);
135 assertTrue("h.parent = g", h
.getParent() == g
);
137 h
.setId(SOME_FAKE_ID
);
138 assertTrue("h not modified", !h
.isModified());
139 g
.setId(SOME_FAKE_ID
);
140 assertTrue("g not modified", !g
.isModified());
141 f
.setId(SOME_FAKE_ID
);
142 assertTrue("f not modified", !f
.isModified());
143 e
.setId(SOME_FAKE_ID
);
144 assertTrue("e not modified", !e
.isModified());
145 t
.setId(SOME_FAKE_ID
);
146 assertTrue("t not modified.", !t
.isModified());
148 assertEquals("full path of h ok", "f/g/h", h
.getFullName());
149 assertTrue("Can find h", t
.findTreeMember(h
.getFullName()) == h
);
150 assertTrue("Can't find f/z", t
.findBlobMember("f/z") == null);
151 assertTrue("Can't find y/z", t
.findBlobMember("y/z") == null);
153 final FileTreeEntry i
= h
.addFile("i");
155 assertEquals("full path of i ok", "f/g/h/i", i
.getFullName());
156 assertTrue("Can find i", t
.findBlobMember(i
.getFullName()) == i
);
157 assertTrue("h modified", h
.isModified());
158 assertTrue("g modified", g
.isModified());
159 assertTrue("f modified", f
.isModified());
160 assertTrue("e not modified", !e
.isModified());
161 assertTrue("t modified", t
.isModified());
163 assertTrue("h no id", h
.getId() == null);
164 assertTrue("g no id", g
.getId() == null);
165 assertTrue("f no id", f
.getId() == null);
166 assertTrue("e has id", e
.getId() != null);
167 assertTrue("t no id", t
.getId() == null);
170 public void test007_manyFileLookup() throws IOException
{
171 final Tree t
= new Tree(db
);
172 final List files
= new ArrayList(26 * 26);
173 for (char level1
= 'a'; level1
<= 'z'; level1
++) {
174 for (char level2
= 'a'; level2
<= 'z'; level2
++) {
175 final String n
= "." + level1
+ level2
+ "9";
176 final FileTreeEntry f
= t
.addFile(n
);
177 assertNotNull("File " + n
+ " added.", f
);
178 assertEquals(n
, f
.getName());
182 assertEquals(files
.size(), t
.memberCount());
183 final TreeEntry
[] ents
= t
.members();
185 assertEquals(files
.size(), ents
.length
);
186 for (int k
= 0; k
< ents
.length
; k
++) {
187 assertTrue("File " + ((FileTreeEntry
) files
.get(k
)).getName()
188 + " is at " + k
+ ".", files
.get(k
) == ents
[k
]);
192 public void test008_SubtreeInternalSorting() throws IOException
{
193 final Tree t
= new Tree(db
);
194 final FileTreeEntry e0
= t
.addFile("a-b");
195 final FileTreeEntry e1
= t
.addFile("a-");
196 final FileTreeEntry e2
= t
.addFile("a=b");
197 final Tree e3
= t
.addTree("a");
198 final FileTreeEntry e4
= t
.addFile("a=");
200 final TreeEntry
[] ents
= t
.members();
201 assertSame(e1
, ents
[0]);
202 assertSame(e0
, ents
[1]);
203 assertSame(e3
, ents
[2]);
204 assertSame(e4
, ents
[3]);
205 assertSame(e2
, ents
[4]);