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

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