Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PackIndexTestCase.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com> 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.internal.storage.file;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertThrows;
  14. import static org.junit.Assert.fail;
  15. import java.io.File;
  16. import java.io.FileInputStream;
  17. import java.io.IOException;
  18. import java.util.Iterator;
  19. import java.util.NoSuchElementException;
  20. import org.eclipse.jgit.errors.MissingObjectException;
  21. import org.eclipse.jgit.internal.JGitText;
  22. import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
  23. import org.eclipse.jgit.junit.RepositoryTestCase;
  24. import org.junit.Test;
  25. public abstract class PackIndexTestCase extends RepositoryTestCase {
  26. PackIndex smallIdx;
  27. PackIndex denseIdx;
  28. @Override
  29. public void setUp() throws Exception {
  30. super.setUp();
  31. smallIdx = PackIndex.open(getFileForPack34be9032());
  32. denseIdx = PackIndex.open(getFileForPackdf2982f28());
  33. }
  34. /**
  35. * Return file with appropriate index version for prepared pack.
  36. *
  37. * @return file with index
  38. */
  39. public abstract File getFileForPack34be9032();
  40. /**
  41. * Return file with appropriate index version for prepared pack.
  42. *
  43. * @return file with index
  44. */
  45. public abstract File getFileForPackdf2982f28();
  46. /**
  47. * Return file with appropriate index version for bad fanout table test.
  48. *
  49. * @return file with index
  50. */
  51. public abstract File getFileForBadFanoutTable();
  52. /**
  53. * Verify CRC32 support.
  54. *
  55. * @throws MissingObjectException
  56. * @throws UnsupportedOperationException
  57. */
  58. public abstract void testCRC32() throws MissingObjectException,
  59. UnsupportedOperationException;
  60. /**
  61. * Test contracts of Iterator methods and this implementation remove()
  62. * limitations.
  63. */
  64. @Test
  65. public void testIteratorMethodsContract() {
  66. Iterator<PackIndex.MutableEntry> iter = smallIdx.iterator();
  67. while (iter.hasNext()) {
  68. iter.next();
  69. }
  70. try {
  71. iter.next();
  72. fail("next() unexpectedly returned element");
  73. } catch (NoSuchElementException x) {
  74. // expected
  75. }
  76. try {
  77. iter.remove();
  78. fail("remove() shouldn't be implemented");
  79. } catch (UnsupportedOperationException x) {
  80. // expected
  81. }
  82. }
  83. /**
  84. * Test results of iterator comparing to content of well-known (prepared)
  85. * small index.
  86. */
  87. @Test
  88. public void testIteratorReturnedValues1() {
  89. Iterator<PackIndex.MutableEntry> iter = smallIdx.iterator();
  90. assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", iter.next()
  91. .name());
  92. assertEquals("540a36d136cf413e4b064c2b0e0a4db60f77feab", iter.next()
  93. .name());
  94. assertEquals("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259", iter.next()
  95. .name());
  96. assertEquals("6ff87c4664981e4397625791c8ea3bbb5f2279a3", iter.next()
  97. .name());
  98. assertEquals("82c6b885ff600be425b4ea96dee75dca255b69e7", iter.next()
  99. .name());
  100. assertEquals("902d5476fa249b7abc9d84c611577a81381f0327", iter.next()
  101. .name());
  102. assertEquals("aabf2ffaec9b497f0950352b3e582d73035c2035", iter.next()
  103. .name());
  104. assertEquals("c59759f143fb1fe21c197981df75a7ee00290799", iter.next()
  105. .name());
  106. assertFalse(iter.hasNext());
  107. }
  108. /**
  109. * Compare offset from iterator entries with output of findOffset() method.
  110. */
  111. @Test
  112. public void testCompareEntriesOffsetsWithFindOffsets() {
  113. for (MutableEntry me : smallIdx) {
  114. assertEquals(smallIdx.findOffset(me.toObjectId()), me.getOffset());
  115. }
  116. for (MutableEntry me : denseIdx) {
  117. assertEquals(denseIdx.findOffset(me.toObjectId()), me.getOffset());
  118. }
  119. }
  120. /**
  121. * Compare offset from iterator entries with output of getOffset() method.
  122. */
  123. @Test
  124. public void testCompareEntriesOffsetsWithGetOffsets() {
  125. int i = 0;
  126. for (MutableEntry me : smallIdx) {
  127. assertEquals(smallIdx.getOffset(i++), me.getOffset());
  128. }
  129. int j = 0;
  130. for (MutableEntry me : denseIdx) {
  131. assertEquals(denseIdx.getOffset(j++), me.getOffset());
  132. }
  133. }
  134. /**
  135. * Test partial results of iterator comparing to content of well-known
  136. * (prepared) dense index, that may need multi-level indexing.
  137. */
  138. @Test
  139. public void testIteratorReturnedValues2() {
  140. Iterator<PackIndex.MutableEntry> iter = denseIdx.iterator();
  141. while (!iter.next().name().equals(
  142. "0a3d7772488b6b106fb62813c4d6d627918d9181")) {
  143. // just iterating
  144. }
  145. assertEquals("1004d0d7ac26fbf63050a234c9b88a46075719d3", iter.next()
  146. .name()); // same level-1
  147. assertEquals("10da5895682013006950e7da534b705252b03be6", iter.next()
  148. .name()); // same level-1
  149. assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8", iter.next()
  150. .name());
  151. }
  152. @Test
  153. public void testBadFanoutTable() {
  154. IOException ex = assertThrows(IOException.class, () -> {
  155. try (FileInputStream fis = new FileInputStream(
  156. getFileForBadFanoutTable())) {
  157. PackIndex.read(fis);
  158. }
  159. });
  160. assertEquals(JGitText.get().indexFileIsTooLargeForJgit,
  161. ex.getMessage());
  162. }
  163. }