您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DirCacheTreeTest.java 7.2KB

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