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 (int i = 0; i < ents.length; i++)
106 			b.add(ents[i]);
107 		b.finish();
108 
109 		assertNull(dc.getCacheTree(false));
110 		final DirCacheTree root = dc.getCacheTree(true);
111 		assertNotNull(root);
112 		assertSame(root, dc.getCacheTree(true));
113 		assertEquals("", root.getNameString());
114 		assertEquals("", root.getPathString());
115 		assertEquals(1, root.getChildCount());
116 		assertEquals(dc.getEntryCount(), root.getEntrySpan());
117 		assertFalse(root.isValid());
118 
119 		final DirCacheTree aTree = root.getChild(0);
120 		assertNotNull(aTree);
121 		assertSame(aTree, root.getChild(0));
122 		assertEquals("a", aTree.getNameString());
123 		assertEquals("a/", aTree.getPathString());
124 		assertEquals(0, aTree.getChildCount());
125 		assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
126 		assertFalse(aTree.isValid());
127 	}
128 
129 	@Test
130 	public void testTwoLevelSubtree() throws Exception {
131 		final DirCache dc = db.readDirCache();
132 
133 		final String[] paths = { "a-", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
134 		final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
135 		for (int i = 0; i < paths.length; i++) {
136 			ents[i] = new DirCacheEntry(paths[i]);
137 			ents[i].setFileMode(FileMode.REGULAR_FILE);
138 		}
139 		final int aFirst = 1;
140 		final int aLast = 4;
141 		final int acFirst = 2;
142 		final int acLast = 3;
143 
144 		final DirCacheBuilder b = dc.builder();
145 		for (int i = 0; i < ents.length; i++)
146 			b.add(ents[i]);
147 		b.finish();
148 
149 		assertNull(dc.getCacheTree(false));
150 		final DirCacheTree root = dc.getCacheTree(true);
151 		assertNotNull(root);
152 		assertSame(root, dc.getCacheTree(true));
153 		assertEquals("", root.getNameString());
154 		assertEquals("", root.getPathString());
155 		assertEquals(1, root.getChildCount());
156 		assertEquals(dc.getEntryCount(), root.getEntrySpan());
157 		assertFalse(root.isValid());
158 
159 		final DirCacheTree aTree = root.getChild(0);
160 		assertNotNull(aTree);
161 		assertSame(aTree, root.getChild(0));
162 		assertEquals("a", aTree.getNameString());
163 		assertEquals("a/", aTree.getPathString());
164 		assertEquals(1, aTree.getChildCount());
165 		assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
166 		assertFalse(aTree.isValid());
167 
168 		final DirCacheTree acTree = aTree.getChild(0);
169 		assertNotNull(acTree);
170 		assertSame(acTree, aTree.getChild(0));
171 		assertEquals("c", acTree.getNameString());
172 		assertEquals("a/c/", acTree.getPathString());
173 		assertEquals(0, acTree.getChildCount());
174 		assertEquals(acLast - acFirst + 1, acTree.getEntrySpan());
175 		assertFalse(acTree.isValid());
176 	}
177 
178 	/**
179 	 * We had bugs related to buffer size in the DirCache. This test creates an
180 	 * index larger than the default BufferedInputStream buffer size. This made
181 	 * the DirCache unable to read the extensions when index size exceeded the
182 	 * buffer size (in some cases at least).
183 	 *
184 	 * @throws CorruptObjectException
185 	 * @throws IOException
186 	 */
187 	@Test
188 	public void testWriteReadTree() throws CorruptObjectException, IOException {
189 		final DirCache dc = db.lockDirCache();
190 
191 		final String A = String.format("a%2000s", "a");
192 		final String B = String.format("b%2000s", "b");
193 		final String[] paths = { A + "-", A + "-" + B, A + "/" + B, A + "0" + B };
194 		final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
195 		for (int i = 0; i < paths.length; i++) {
196 			ents[i] = new DirCacheEntry(paths[i]);
197 			ents[i].setFileMode(FileMode.REGULAR_FILE);
198 		}
199 
200 		final DirCacheBuilder b = dc.builder();
201 		for (int i = 0; i < ents.length; i++)
202 			b.add(ents[i]);
203 
204 		b.commit();
205 		DirCache read = db.readDirCache();
206 
207 		assertEquals(paths.length, read.getEntryCount());
208 		assertEquals(1, read.getCacheTree(true).getChildCount());
209 	}
210 }