You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirCacheTreeTest.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. package org.eclipse.jgit.dircache;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNotSame;
  48. import static org.junit.Assert.assertNull;
  49. import static org.junit.Assert.assertSame;
  50. import java.io.IOException;
  51. import org.eclipse.jgit.errors.CorruptObjectException;
  52. import org.eclipse.jgit.junit.RepositoryTestCase;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.junit.Test;
  55. public class DirCacheTreeTest extends RepositoryTestCase {
  56. @Test
  57. public void testEmptyCache_NoCacheTree() throws Exception {
  58. final DirCache dc = db.readDirCache();
  59. assertNull(dc.getCacheTree(false));
  60. }
  61. @Test
  62. public void testEmptyCache_CreateEmptyCacheTree() throws Exception {
  63. final DirCache dc = db.readDirCache();
  64. final DirCacheTree tree = dc.getCacheTree(true);
  65. assertNotNull(tree);
  66. assertSame(tree, dc.getCacheTree(false));
  67. assertSame(tree, dc.getCacheTree(true));
  68. assertEquals("", tree.getNameString());
  69. assertEquals("", tree.getPathString());
  70. assertEquals(0, tree.getChildCount());
  71. assertEquals(0, tree.getEntrySpan());
  72. assertFalse(tree.isValid());
  73. }
  74. @Test
  75. public void testEmptyCache_Clear_NoCacheTree() throws Exception {
  76. final DirCache dc = db.readDirCache();
  77. final DirCacheTree tree = dc.getCacheTree(true);
  78. assertNotNull(tree);
  79. dc.clear();
  80. assertNull(dc.getCacheTree(false));
  81. assertNotSame(tree, dc.getCacheTree(true));
  82. }
  83. @Test
  84. public void testSingleSubtree() throws Exception {
  85. final DirCache dc = db.readDirCache();
  86. final String[] paths = { "a-", "a/b", "a/c", "a/d", "a0b" };
  87. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  88. for (int i = 0; i < paths.length; i++) {
  89. ents[i] = new DirCacheEntry(paths[i]);
  90. ents[i].setFileMode(FileMode.REGULAR_FILE);
  91. }
  92. final int aFirst = 1;
  93. final int aLast = 3;
  94. final DirCacheBuilder b = dc.builder();
  95. for (DirCacheEntry ent : ents) {
  96. b.add(ent);
  97. }
  98. b.finish();
  99. assertNull(dc.getCacheTree(false));
  100. final DirCacheTree root = dc.getCacheTree(true);
  101. assertNotNull(root);
  102. assertSame(root, dc.getCacheTree(true));
  103. assertEquals("", root.getNameString());
  104. assertEquals("", root.getPathString());
  105. assertEquals(1, root.getChildCount());
  106. assertEquals(dc.getEntryCount(), root.getEntrySpan());
  107. assertFalse(root.isValid());
  108. final DirCacheTree aTree = root.getChild(0);
  109. assertNotNull(aTree);
  110. assertSame(aTree, root.getChild(0));
  111. assertEquals("a", aTree.getNameString());
  112. assertEquals("a/", aTree.getPathString());
  113. assertEquals(0, aTree.getChildCount());
  114. assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
  115. assertFalse(aTree.isValid());
  116. }
  117. @Test
  118. public void testTwoLevelSubtree() throws Exception {
  119. final DirCache dc = db.readDirCache();
  120. final String[] paths = { "a-", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
  121. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  122. for (int i = 0; i < paths.length; i++) {
  123. ents[i] = new DirCacheEntry(paths[i]);
  124. ents[i].setFileMode(FileMode.REGULAR_FILE);
  125. }
  126. final int aFirst = 1;
  127. final int aLast = 4;
  128. final int acFirst = 2;
  129. final int acLast = 3;
  130. final DirCacheBuilder b = dc.builder();
  131. for (DirCacheEntry ent : ents) {
  132. b.add(ent);
  133. }
  134. b.finish();
  135. assertNull(dc.getCacheTree(false));
  136. final DirCacheTree root = dc.getCacheTree(true);
  137. assertNotNull(root);
  138. assertSame(root, dc.getCacheTree(true));
  139. assertEquals("", root.getNameString());
  140. assertEquals("", root.getPathString());
  141. assertEquals(1, root.getChildCount());
  142. assertEquals(dc.getEntryCount(), root.getEntrySpan());
  143. assertFalse(root.isValid());
  144. final DirCacheTree aTree = root.getChild(0);
  145. assertNotNull(aTree);
  146. assertSame(aTree, root.getChild(0));
  147. assertEquals("a", aTree.getNameString());
  148. assertEquals("a/", aTree.getPathString());
  149. assertEquals(1, aTree.getChildCount());
  150. assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
  151. assertFalse(aTree.isValid());
  152. final DirCacheTree acTree = aTree.getChild(0);
  153. assertNotNull(acTree);
  154. assertSame(acTree, aTree.getChild(0));
  155. assertEquals("c", acTree.getNameString());
  156. assertEquals("a/c/", acTree.getPathString());
  157. assertEquals(0, acTree.getChildCount());
  158. assertEquals(acLast - acFirst + 1, acTree.getEntrySpan());
  159. assertFalse(acTree.isValid());
  160. }
  161. /**
  162. * We had bugs related to buffer size in the DirCache. This test creates an
  163. * index larger than the default BufferedInputStream buffer size. This made
  164. * the DirCache unable to read the extensions when index size exceeded the
  165. * buffer size (in some cases at least).
  166. *
  167. * @throws CorruptObjectException
  168. * @throws IOException
  169. */
  170. @Test
  171. public void testWriteReadTree() throws CorruptObjectException, IOException {
  172. final DirCache dc = db.lockDirCache();
  173. final String A = String.format("a%2000s", "a");
  174. final String B = String.format("b%2000s", "b");
  175. final String[] paths = { A + "-", A + "-" + B, A + "/" + B, A + "0" + B };
  176. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  177. for (int i = 0; i < paths.length; i++) {
  178. ents[i] = new DirCacheEntry(paths[i]);
  179. ents[i].setFileMode(FileMode.REGULAR_FILE);
  180. }
  181. final DirCacheBuilder b = dc.builder();
  182. for (DirCacheEntry ent : ents) {
  183. b.add(ent);
  184. }
  185. b.commit();
  186. DirCache read = db.readDirCache();
  187. assertEquals(paths.length, read.getEntryCount());
  188. assertEquals(1, read.getCacheTree(true).getChildCount());
  189. }
  190. }