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 5.6KB

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