View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.dircache;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertNotSame;
17  import static org.junit.Assert.assertNull;
18  import static org.junit.Assert.assertSame;
19  
20  import java.io.IOException;
21  
22  import org.eclipse.jgit.errors.CorruptObjectException;
23  import org.eclipse.jgit.junit.RepositoryTestCase;
24  import org.eclipse.jgit.lib.FileMode;
25  import org.junit.Test;
26  
27  public class DirCacheTreeTest extends RepositoryTestCase {
28  	@Test
29  	public void testEmptyCache_NoCacheTree() throws Exception {
30  		final DirCache dc = db.readDirCache();
31  		assertNull(dc.getCacheTree(false));
32  	}
33  
34  	@Test
35  	public void testEmptyCache_CreateEmptyCacheTree() throws Exception {
36  		final DirCache dc = db.readDirCache();
37  		final DirCacheTree tree = dc.getCacheTree(true);
38  		assertNotNull(tree);
39  		assertSame(tree, dc.getCacheTree(false));
40  		assertSame(tree, dc.getCacheTree(true));
41  		assertEquals("", tree.getNameString());
42  		assertEquals("", tree.getPathString());
43  		assertEquals(0, tree.getChildCount());
44  		assertEquals(0, tree.getEntrySpan());
45  		assertFalse(tree.isValid());
46  	}
47  
48  	@Test
49  	public void testEmptyCache_Clear_NoCacheTree() throws Exception {
50  		final DirCache dc = db.readDirCache();
51  		final DirCacheTree tree = dc.getCacheTree(true);
52  		assertNotNull(tree);
53  		dc.clear();
54  		assertNull(dc.getCacheTree(false));
55  		assertNotSame(tree, dc.getCacheTree(true));
56  	}
57  
58  	@Test
59  	public void testSingleSubtree() throws Exception {
60  		final DirCache dc = db.readDirCache();
61  
62  		final String[] paths = { "a-", "a/b", "a/c", "a/d", "a0b" };
63  		final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
64  		for (int i = 0; i < paths.length; i++) {
65  			ents[i] = new DirCacheEntry(paths[i]);
66  			ents[i].setFileMode(FileMode.REGULAR_FILE);
67  		}
68  		final int aFirst = 1;
69  		final int aLast = 3;
70  
71  		final DirCacheBuilder b = dc.builder();
72  		for (DirCacheEntry ent : ents) {
73  			b.add(ent);
74  		}
75  		b.finish();
76  
77  		assertNull(dc.getCacheTree(false));
78  		final DirCacheTree root = dc.getCacheTree(true);
79  		assertNotNull(root);
80  		assertSame(root, dc.getCacheTree(true));
81  		assertEquals("", root.getNameString());
82  		assertEquals("", root.getPathString());
83  		assertEquals(1, root.getChildCount());
84  		assertEquals(dc.getEntryCount(), root.getEntrySpan());
85  		assertFalse(root.isValid());
86  
87  		final DirCacheTree aTree = root.getChild(0);
88  		assertNotNull(aTree);
89  		assertSame(aTree, root.getChild(0));
90  		assertEquals("a", aTree.getNameString());
91  		assertEquals("a/", aTree.getPathString());
92  		assertEquals(0, aTree.getChildCount());
93  		assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
94  		assertFalse(aTree.isValid());
95  	}
96  
97  	@Test
98  	public void testTwoLevelSubtree() throws Exception {
99  		final DirCache dc = db.readDirCache();
100 
101 		final String[] paths = { "a-", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
102 		final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
103 		for (int i = 0; i < paths.length; i++) {
104 			ents[i] = new DirCacheEntry(paths[i]);
105 			ents[i].setFileMode(FileMode.REGULAR_FILE);
106 		}
107 		final int aFirst = 1;
108 		final int aLast = 4;
109 		final int acFirst = 2;
110 		final int acLast = 3;
111 
112 		final DirCacheBuilder b = dc.builder();
113 		for (DirCacheEntry ent : ents) {
114 			b.add(ent);
115 		}
116 		b.finish();
117 
118 		assertNull(dc.getCacheTree(false));
119 		final DirCacheTree root = dc.getCacheTree(true);
120 		assertNotNull(root);
121 		assertSame(root, dc.getCacheTree(true));
122 		assertEquals("", root.getNameString());
123 		assertEquals("", root.getPathString());
124 		assertEquals(1, root.getChildCount());
125 		assertEquals(dc.getEntryCount(), root.getEntrySpan());
126 		assertFalse(root.isValid());
127 
128 		final DirCacheTree aTree = root.getChild(0);
129 		assertNotNull(aTree);
130 		assertSame(aTree, root.getChild(0));
131 		assertEquals("a", aTree.getNameString());
132 		assertEquals("a/", aTree.getPathString());
133 		assertEquals(1, aTree.getChildCount());
134 		assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
135 		assertFalse(aTree.isValid());
136 
137 		final DirCacheTree acTree = aTree.getChild(0);
138 		assertNotNull(acTree);
139 		assertSame(acTree, aTree.getChild(0));
140 		assertEquals("c", acTree.getNameString());
141 		assertEquals("a/c/", acTree.getPathString());
142 		assertEquals(0, acTree.getChildCount());
143 		assertEquals(acLast - acFirst + 1, acTree.getEntrySpan());
144 		assertFalse(acTree.isValid());
145 	}
146 
147 	/**
148 	 * We had bugs related to buffer size in the DirCache. This test creates an
149 	 * index larger than the default BufferedInputStream buffer size. This made
150 	 * the DirCache unable to read the extensions when index size exceeded the
151 	 * buffer size (in some cases at least).
152 	 *
153 	 * @throws CorruptObjectException
154 	 * @throws IOException
155 	 */
156 	@Test
157 	public void testWriteReadTree() throws CorruptObjectException, IOException {
158 		final DirCache dc = db.lockDirCache();
159 
160 		final String A = String.format("a%2000s", "a");
161 		final String B = String.format("b%2000s", "b");
162 		final String[] paths = { A + "-", A + "-" + B, A + "/" + B, A + "0" + B };
163 		final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
164 		for (int i = 0; i < paths.length; i++) {
165 			ents[i] = new DirCacheEntry(paths[i]);
166 			ents[i].setFileMode(FileMode.REGULAR_FILE);
167 		}
168 
169 		final DirCacheBuilder b = dc.builder();
170 		for (DirCacheEntry ent : ents) {
171 			b.add(ent);
172 		}
173 
174 		b.commit();
175 		DirCache read = db.readDirCache();
176 
177 		assertEquals(paths.length, read.getEntryCount());
178 		assertEquals(1, read.getCacheTree(true).getChildCount());
179 	}
180 }