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.

PackIndexTestCase.java 4.4KB

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