aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0002_TreeTest.java
blob: 651e62c9ca7390e4912879fd800118be9c0656ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
 * and other copyright owners as documented in the project's IP log.
 *
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Distribution License v1.0 which
 * accompanies this distribution, is reproduced below, and is
 * available at http://www.eclipse.org/org/documents/edl-v10.php
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials provided
 *   with the distribution.
 *
 * - Neither the name of the Eclipse Foundation, Inc. nor the
 *   names of its contributors may be used to endorse or promote
 *   products derived from this software without specific prior
 *   written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.eclipse.jgit.lib;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
import org.junit.Test;

@SuppressWarnings("deprecation")
public class T0002_TreeTest extends SampleDataRepositoryTestCase {
	private static final ObjectId SOME_FAKE_ID = ObjectId.fromString(
			"0123456789abcdef0123456789abcdef01234567");

	private static int compareNamesUsingSpecialCompare(String a, String b)
			throws UnsupportedEncodingException {
		char lasta = '\0';
		byte[] abytes;
		if (a.length() > 0 && a.charAt(a.length()-1) == '/') {
			lasta = '/';
			a = a.substring(0, a.length() - 1);
		}
		abytes = a.getBytes("ISO-8859-1");
		char lastb = '\0';
		byte[] bbytes;
		if (b.length() > 0 && b.charAt(b.length()-1) == '/') {
			lastb = '/';
			b = b.substring(0, b.length() - 1);
		}
		bbytes = b.getBytes("ISO-8859-1");
		return Tree.compareNames(abytes, bbytes, lasta, lastb);
	}

	@Test
	public void test000_sort_01() throws UnsupportedEncodingException {
		assertEquals(0, compareNamesUsingSpecialCompare("a","a"));
	}

	@Test
	public void test000_sort_02() throws UnsupportedEncodingException {
		assertEquals(-1, compareNamesUsingSpecialCompare("a","b"));
		assertEquals(1, compareNamesUsingSpecialCompare("b","a"));
	}

	@Test
	public void test000_sort_03() throws UnsupportedEncodingException {
		assertEquals(1, compareNamesUsingSpecialCompare("a:","a"));
		assertEquals(1, compareNamesUsingSpecialCompare("a/","a"));
		assertEquals(-1, compareNamesUsingSpecialCompare("a","a/"));
		assertEquals(-1, compareNamesUsingSpecialCompare("a","a:"));
		assertEquals(1, compareNamesUsingSpecialCompare("a:","a/"));
		assertEquals(-1, compareNamesUsingSpecialCompare("a/","a:"));
	}

	@Test
	public void test000_sort_04() throws UnsupportedEncodingException {
		assertEquals(-1, compareNamesUsingSpecialCompare("a.a","a/a"));
		assertEquals(1, compareNamesUsingSpecialCompare("a/a","a.a"));
	}

	@Test
	public void test000_sort_05() throws UnsupportedEncodingException {
		assertEquals(-1, compareNamesUsingSpecialCompare("a.","a/"));
		assertEquals(1, compareNamesUsingSpecialCompare("a/","a."));

	}

	@Test
	public void test001_createEmpty() throws IOException {
		final Tree t = new Tree(db);
		assertTrue("isLoaded", t.isLoaded());
		assertTrue("isModified", t.isModified());
		assertTrue("no parent", t.getParent() == null);
		assertTrue("isRoot", t.isRoot());
		assertTrue("no name", t.getName() == null);
		assertTrue("no nameUTF8", t.getNameUTF8() == null);
		assertTrue("has entries array", t.members() != null);
		assertEquals("entries is empty", 0, t.members().length);
		assertEquals("full name is empty", "", t.getFullName());
		assertTrue("no id", t.getId() == null);
		assertTrue("database is r", t.getRepository() == db);
		assertTrue("no foo child", t.findTreeMember("foo") == null);
		assertTrue("no foo child", t.findBlobMember("foo") == null);
	}

	@Test
	public void test002_addFile() throws IOException {
		final Tree t = new Tree(db);
		t.setId(SOME_FAKE_ID);
		assertTrue("has id", t.getId() != null);
		assertFalse("not modified", t.isModified());

		final String n = "bob";
		final FileTreeEntry f = t.addFile(n);
		assertNotNull("have file", f);
		assertEquals("name matches", n, f.getName());
		assertEquals("name matches", f.getName(), new String(f.getNameUTF8(),
				"UTF-8"));
		assertEquals("full name matches", n, f.getFullName());
		assertTrue("no id", f.getId() == null);
		assertTrue("is modified", t.isModified());
		assertTrue("has no id", t.getId() == null);
		assertTrue("found bob", t.findBlobMember(f.getName()) == f);

		final TreeEntry[] i = t.members();
		assertNotNull("members array not null", i);
		assertTrue("iterator is not empty", i != null && i.length > 0);
		assertTrue("iterator returns file", i != null && i[0] == f);
		assertTrue("iterator is empty", i != null && i.length == 1);
	}

	@Test
	public void test004_addTree() throws IOException {
		final Tree t = new Tree(db);
		t.setId(SOME_FAKE_ID);
		assertTrue("has id", t.getId() != null);
		assertFalse("not modified", t.isModified());

		final String n = "bob";
		final Tree f = t.addTree(n);
		assertNotNull("have tree", f);
		assertEquals("name matches", n, f.getName());
		assertEquals("name matches", f.getName(), new String(f.getNameUTF8(),
				"UTF-8"));
		assertEquals("full name matches", n, f.getFullName());
		assertTrue("no id", f.getId() == null);
		assertTrue("parent matches", f.getParent() == t);
		assertTrue("repository matches", f.getRepository() == db);
		assertTrue("isLoaded", f.isLoaded());
		assertFalse("has items", f.members().length > 0);
		assertFalse("is root", f.isRoot());
		assertTrue("parent is modified", t.isModified());
		assertTrue("parent has no id", t.getId() == null);
		assertTrue("found bob child", t.findTreeMember(f.getName()) == f);

		final TreeEntry[] i = t.members();
		assertTrue("iterator is not empty", i.length > 0);
		assertTrue("iterator returns file", i[0] == f);
		assertEquals("iterator is empty", 1, i.length);
	}

	@Test
	public void test005_addRecursiveFile() throws IOException {
		final Tree t = new Tree(db);
		final FileTreeEntry f = t.addFile("a/b/c");
		assertNotNull("created f", f);
		assertEquals("c", f.getName());
		assertEquals("b", f.getParent().getName());
		assertEquals("a", f.getParent().getParent().getName());
		assertTrue("t is great-grandparent", t == f.getParent().getParent()
				.getParent());
	}

	@Test
	public void test005_addRecursiveTree() throws IOException {
		final Tree t = new Tree(db);
		final Tree f = t.addTree("a/b/c");
		assertNotNull("created f", f);
		assertEquals("c", f.getName());
		assertEquals("b", f.getParent().getName());
		assertEquals("a", f.getParent().getParent().getName());
		assertTrue("t is great-grandparent", t == f.getParent().getParent()
				.getParent());
	}

	@Test
	public void test006_addDeepTree() throws IOException {
		final Tree t = new Tree(db);

		final Tree e = t.addTree("e");
		assertNotNull("have e", e);
		assertTrue("e.parent == t", e.getParent() == t);
		final Tree f = t.addTree("f");
		assertNotNull("have f", f);
		assertTrue("f.parent == t", f.getParent() == t);
		final Tree g = f.addTree("g");
		assertNotNull("have g", g);
		assertTrue("g.parent == f", g.getParent() == f);
		final Tree h = g.addTree("h");
		assertNotNull("have h", h);
		assertTrue("h.parent = g", h.getParent() == g);

		h.setId(SOME_FAKE_ID);
		assertTrue("h not modified", !h.isModified());
		g.setId(SOME_FAKE_ID);
		assertTrue("g not modified", !g.isModified());
		f.setId(SOME_FAKE_ID);
		assertTrue("f not modified", !f.isModified());
		e.setId(SOME_FAKE_ID);
		assertTrue("e not modified", !e.isModified());
		t.setId(SOME_FAKE_ID);
		assertTrue("t not modified.", !t.isModified());

		assertEquals("full path of h ok", "f/g/h", h.getFullName());
		assertTrue("Can find h", t.findTreeMember(h.getFullName()) == h);
		assertTrue("Can't find f/z", t.findBlobMember("f/z") == null);
		assertTrue("Can't find y/z", t.findBlobMember("y/z") == null);

		final FileTreeEntry i = h.addFile("i");
		assertNotNull(i);
		assertEquals("full path of i ok", "f/g/h/i", i.getFullName());
		assertTrue("Can find i", t.findBlobMember(i.getFullName()) == i);
		assertTrue("h modified", h.isModified());
		assertTrue("g modified", g.isModified());
		assertTrue("f modified", f.isModified());
		assertTrue("e not modified", !e.isModified());
		assertTrue("t modified", t.isModified());

		assertTrue("h no id", h.getId() == null);
		assertTrue("g no id", g.getId() == null);
		assertTrue("f no id", f.getId() == null);
		assertTrue("e has id", e.getId() != null);
		assertTrue("t no id", t.getId() == null);
	}

	@Test
	public void test007_manyFileLookup() throws IOException {
		final Tree t = new Tree(db);
		final List<FileTreeEntry> files = new ArrayList<FileTreeEntry>(26 * 26);
		for (char level1 = 'a'; level1 <= 'z'; level1++) {
			for (char level2 = 'a'; level2 <= 'z'; level2++) {
				final String n = "." + level1 + level2 + "9";
				final FileTreeEntry f = t.addFile(n);
				assertNotNull("File " + n + " added.", f);
				assertEquals(n, f.getName());
				files.add(f);
			}
		}
		assertEquals(files.size(), t.memberCount());
		final TreeEntry[] ents = t.members();
		assertNotNull(ents);
		assertEquals(files.size(), ents.length);
		for (int k = 0; k < ents.length; k++) {
			assertTrue("File " + files.get(k).getName()
					+ " is at " + k + ".", files.get(k) == ents[k]);
		}
	}

	@Test
	public void test008_SubtreeInternalSorting() throws IOException {
		final Tree t = new Tree(db);
		final FileTreeEntry e0 = t.addFile("a-b");
		final FileTreeEntry e1 = t.addFile("a-");
		final FileTreeEntry e2 = t.addFile("a=b");
		final Tree e3 = t.addTree("a");
		final FileTreeEntry e4 = t.addFile("a=");

		final TreeEntry[] ents = t.members();
		assertSame(e1, ents[0]);
		assertSame(e0, ents[1]);
		assertSame(e3, ents[2]);
		assertSame(e4, ents[3]);
		assertSame(e2, ents[4]);
	}

	@Test
	public void test009_SymlinkAndGitlink() throws IOException {
		final Tree symlinkTree = mapTree("symlink");
		assertTrue("Symlink entry exists", symlinkTree.existsBlob("symlink.txt"));
		final Tree gitlinkTree = mapTree("gitlink");
		assertTrue("Gitlink entry exists", gitlinkTree.existsBlob("submodule"));
	}

	private Tree mapTree(String name) throws IOException {
		ObjectId id = db.resolve(name + "^{tree}");
		return new Tree(db, id, db.open(id).getCachedBytes());
	}
}