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

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