View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.dircache;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertNotNull;
49  import static org.junit.Assert.assertNotSame;
50  import static org.junit.Assert.assertNull;
51  import static org.junit.Assert.assertSame;
52  
53  import java.io.IOException;
54  
55  import org.eclipse.jgit.errors.CorruptObjectException;
56  import org.eclipse.jgit.junit.RepositoryTestCase;
57  import org.eclipse.jgit.lib.FileMode;
58  import org.junit.Test;
59  
60  public class DirCacheTreeTest extends RepositoryTestCase {
61  	@Test
62  	public void testEmptyCache_NoCacheTree() throws Exception {
63  		final DirCache dc = db.readDirCache();
64  		assertNull(dc.getCacheTree(false));
65  	}
66  
67  	@Test
68  	public void testEmptyCache_CreateEmptyCacheTree() throws Exception {
69  		final DirCache dc = db.readDirCache();
70  		final DirCacheTree tree = dc.getCacheTree(true);
71  		assertNotNull(tree);
72  		assertSame(tree, dc.getCacheTree(false));
73  		assertSame(tree, dc.getCacheTree(true));
74  		assertEquals("", tree.getNameString());
75  		assertEquals("", tree.getPathString());
76  		assertEquals(0, tree.getChildCount());
77  		assertEquals(0, tree.getEntrySpan());
78  		assertFalse(tree.isValid());
79  	}
80  
81  	@Test
82  	public void testEmptyCache_Clear_NoCacheTree() throws Exception {
83  		final DirCache dc = db.readDirCache();
84  		final DirCacheTree tree = dc.getCacheTree(true);
85  		assertNotNull(tree);
86  		dc.clear();
87  		assertNull(dc.getCacheTree(false));
88  		assertNotSame(tree, dc.getCacheTree(true));
89  	}
90  
91  	@Test
92  	public void testSingleSubtree() throws Exception {
93  		final DirCache dc = db.readDirCache();
94  
95  		final String[] paths = { "a-", "a/b", "a/c", "a/d", "a0b" };
96  		final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
97  		for (int i = 0; i < paths.length; i++) {
98  			ents[i] = new DirCacheEntry(paths[i]);
99  			ents[i].setFileMode(FileMode.REGULAR_FILE);
100 		}
101 		final int aFirst = 1;
102 		final int aLast = 3;
103 
104 		final DirCacheBuilder b = dc.builder();
105 		for (DirCacheEntry ent : ents) {
106 			b.add(ent);
107 		}
108 		b.finish();
109 
110 		assertNull(dc.getCacheTree(false));
111 		final DirCacheTree root = dc.getCacheTree(true);
112 		assertNotNull(root);
113 		assertSame(root, dc.getCacheTree(true));
114 		assertEquals("", root.getNameString());
115 		assertEquals("", root.getPathString());
116 		assertEquals(1, root.getChildCount());
117 		assertEquals(dc.getEntryCount(), root.getEntrySpan());
118 		assertFalse(root.isValid());
119 
120 		final DirCacheTree aTree = root.getChild(0);
121 		assertNotNull(aTree);
122 		assertSame(aTree, root.getChild(0));
123 		assertEquals("a", aTree.getNameString());
124 		assertEquals("a/", aTree.getPathString());
125 		assertEquals(0, aTree.getChildCount());
126 		assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
127 		assertFalse(aTree.isValid());
128 	}
129 
130 	@Test
131 	public void testTwoLevelSubtree() throws Exception {
132 		final DirCache dc = db.readDirCache();
133 
134 		final String[] paths = { "a-", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
135 		final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
136 		for (int i = 0; i < paths.length; i++) {
137 			ents[i] = new DirCacheEntry(paths[i]);
138 			ents[i].setFileMode(FileMode.REGULAR_FILE);
139 		}
140 		final int aFirst = 1;
141 		final int aLast = 4;
142 		final int acFirst = 2;
143 		final int acLast = 3;
144 
145 		final DirCacheBuilder b = dc.builder();
146 		for (DirCacheEntry ent : ents) {
147 			b.add(ent);
148 		}
149 		b.finish();
150 
151 		assertNull(dc.getCacheTree(false));
152 		final DirCacheTree root = dc.getCacheTree(true);
153 		assertNotNull(root);
154 		assertSame(root, dc.getCacheTree(true));
155 		assertEquals("", root.getNameString());
156 		assertEquals("", root.getPathString());
157 		assertEquals(1, root.getChildCount());
158 		assertEquals(dc.getEntryCount(), root.getEntrySpan());
159 		assertFalse(root.isValid());
160 
161 		final DirCacheTree aTree = root.getChild(0);
162 		assertNotNull(aTree);
163 		assertSame(aTree, root.getChild(0));
164 		assertEquals("a", aTree.getNameString());
165 		assertEquals("a/", aTree.getPathString());
166 		assertEquals(1, aTree.getChildCount());
167 		assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
168 		assertFalse(aTree.isValid());
169 
170 		final DirCacheTree acTree = aTree.getChild(0);
171 		assertNotNull(acTree);
172 		assertSame(acTree, aTree.getChild(0));
173 		assertEquals("c", acTree.getNameString());
174 		assertEquals("a/c/", acTree.getPathString());
175 		assertEquals(0, acTree.getChildCount());
176 		assertEquals(acLast - acFirst + 1, acTree.getEntrySpan());
177 		assertFalse(acTree.isValid());
178 	}
179 
180 	/**
181 	 * We had bugs related to buffer size in the DirCache. This test creates an
182 	 * index larger than the default BufferedInputStream buffer size. This made
183 	 * the DirCache unable to read the extensions when index size exceeded the
184 	 * buffer size (in some cases at least).
185 	 *
186 	 * @throws CorruptObjectException
187 	 * @throws IOException
188 	 */
189 	@Test
190 	public void testWriteReadTree() throws CorruptObjectException, IOException {
191 		final DirCache dc = db.lockDirCache();
192 
193 		final String A = String.format("a%2000s", "a");
194 		final String B = String.format("b%2000s", "b");
195 		final String[] paths = { A + "-", A + "-" + B, A + "/" + B, A + "0" + B };
196 		final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
197 		for (int i = 0; i < paths.length; i++) {
198 			ents[i] = new DirCacheEntry(paths[i]);
199 			ents[i].setFileMode(FileMode.REGULAR_FILE);
200 		}
201 
202 		final DirCacheBuilder b = dc.builder();
203 		for (DirCacheEntry ent : ents) {
204 			b.add(ent);
205 		}
206 
207 		b.commit();
208 		DirCache read = db.readDirCache();
209 
210 		assertEquals(paths.length, read.getEntryCount());
211 		assertEquals(1, read.getCacheTree(true).getChildCount());
212 	}
213 }