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.

PackWriterTest.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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.storage.file;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.ByteArrayInputStream;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.File;
  51. import java.io.FileOutputStream;
  52. import java.io.IOException;
  53. import java.util.ArrayList;
  54. import java.util.Arrays;
  55. import java.util.Collection;
  56. import java.util.Collections;
  57. import java.util.Comparator;
  58. import java.util.Iterator;
  59. import java.util.LinkedList;
  60. import java.util.List;
  61. import org.eclipse.jgit.errors.MissingObjectException;
  62. import org.eclipse.jgit.junit.JGitTestUtil;
  63. import org.eclipse.jgit.lib.NullProgressMonitor;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectInserter;
  66. import org.eclipse.jgit.lib.SampleDataRepositoryTestCase;
  67. import org.eclipse.jgit.revwalk.RevObject;
  68. import org.eclipse.jgit.revwalk.RevWalk;
  69. import org.eclipse.jgit.storage.file.PackIndex.MutableEntry;
  70. import org.eclipse.jgit.storage.pack.PackConfig;
  71. import org.eclipse.jgit.storage.pack.PackWriter;
  72. import org.eclipse.jgit.transport.PackParser;
  73. import org.junit.After;
  74. import org.junit.Before;
  75. import org.junit.Test;
  76. public class PackWriterTest extends SampleDataRepositoryTestCase {
  77. private static final List<ObjectId> EMPTY_LIST_OBJECT = Collections
  78. .<ObjectId> emptyList();
  79. private static final List<RevObject> EMPTY_LIST_REVS = Collections
  80. .<RevObject> emptyList();
  81. private PackConfig config;
  82. private PackWriter writer;
  83. private ByteArrayOutputStream os;
  84. private PackFile pack;
  85. private ObjectInserter inserter;
  86. private FileRepository dst;
  87. @Before
  88. public void setUp() throws Exception {
  89. super.setUp();
  90. os = new ByteArrayOutputStream();
  91. config = new PackConfig(db);
  92. dst = createBareRepository();
  93. File alt = new File(dst.getObjectDatabase().getDirectory(), "info/alternates");
  94. alt.getParentFile().mkdirs();
  95. write(alt, db.getObjectDatabase().getDirectory().getAbsolutePath() + "\n");
  96. }
  97. @After
  98. public void tearDown() throws Exception {
  99. if (writer != null) {
  100. writer.release();
  101. writer = null;
  102. }
  103. if (inserter != null) {
  104. inserter.release();
  105. inserter = null;
  106. }
  107. super.tearDown();
  108. }
  109. /**
  110. * Test constructor for exceptions, default settings, initialization.
  111. */
  112. @Test
  113. public void testContructor() {
  114. writer = new PackWriter(config, db.newObjectReader());
  115. assertEquals(false, writer.isDeltaBaseAsOffset());
  116. assertEquals(true, config.isReuseDeltas());
  117. assertEquals(true, config.isReuseObjects());
  118. assertEquals(0, writer.getObjectsNumber());
  119. }
  120. /**
  121. * Change default settings and verify them.
  122. */
  123. @Test
  124. public void testModifySettings() {
  125. config.setReuseDeltas(false);
  126. config.setReuseObjects(false);
  127. config.setDeltaBaseAsOffset(false);
  128. assertEquals(false, config.isReuseDeltas());
  129. assertEquals(false, config.isReuseObjects());
  130. assertEquals(false, config.isDeltaBaseAsOffset());
  131. writer = new PackWriter(config, db.newObjectReader());
  132. writer.setDeltaBaseAsOffset(true);
  133. assertEquals(true, writer.isDeltaBaseAsOffset());
  134. assertEquals(false, config.isDeltaBaseAsOffset());
  135. }
  136. /**
  137. * Write empty pack by providing empty sets of interesting/uninteresting
  138. * objects and check for correct format.
  139. *
  140. * @throws IOException
  141. */
  142. @Test
  143. public void testWriteEmptyPack1() throws IOException {
  144. createVerifyOpenPack(EMPTY_LIST_OBJECT, EMPTY_LIST_OBJECT, false, false);
  145. assertEquals(0, writer.getObjectsNumber());
  146. assertEquals(0, pack.getObjectCount());
  147. assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", writer
  148. .computeName().name());
  149. }
  150. /**
  151. * Write empty pack by providing empty iterator of objects to write and
  152. * check for correct format.
  153. *
  154. * @throws IOException
  155. */
  156. @Test
  157. public void testWriteEmptyPack2() throws IOException {
  158. createVerifyOpenPack(EMPTY_LIST_REVS.iterator());
  159. assertEquals(0, writer.getObjectsNumber());
  160. assertEquals(0, pack.getObjectCount());
  161. }
  162. /**
  163. * Try to pass non-existing object as uninteresting, with non-ignoring
  164. * setting.
  165. *
  166. * @throws IOException
  167. */
  168. @Test
  169. public void testNotIgnoreNonExistingObjects() throws IOException {
  170. final ObjectId nonExisting = ObjectId
  171. .fromString("0000000000000000000000000000000000000001");
  172. try {
  173. createVerifyOpenPack(EMPTY_LIST_OBJECT, Collections.nCopies(1,
  174. nonExisting), false, false);
  175. fail("Should have thrown MissingObjectException");
  176. } catch (MissingObjectException x) {
  177. // expected
  178. }
  179. }
  180. /**
  181. * Try to pass non-existing object as uninteresting, with ignoring setting.
  182. *
  183. * @throws IOException
  184. */
  185. @Test
  186. public void testIgnoreNonExistingObjects() throws IOException {
  187. final ObjectId nonExisting = ObjectId
  188. .fromString("0000000000000000000000000000000000000001");
  189. createVerifyOpenPack(EMPTY_LIST_OBJECT, Collections.nCopies(1,
  190. nonExisting), false, true);
  191. // shouldn't throw anything
  192. }
  193. /**
  194. * Create pack basing on only interesting objects, then precisely verify
  195. * content. No delta reuse here.
  196. *
  197. * @throws IOException
  198. */
  199. @Test
  200. public void testWritePack1() throws IOException {
  201. config.setReuseDeltas(false);
  202. writeVerifyPack1();
  203. }
  204. /**
  205. * Test writing pack without object reuse. Pack content/preparation as in
  206. * {@link #testWritePack1()}.
  207. *
  208. * @throws IOException
  209. */
  210. @Test
  211. public void testWritePack1NoObjectReuse() throws IOException {
  212. config.setReuseDeltas(false);
  213. config.setReuseObjects(false);
  214. writeVerifyPack1();
  215. }
  216. /**
  217. * Create pack basing on both interesting and uninteresting objects, then
  218. * precisely verify content. No delta reuse here.
  219. *
  220. * @throws IOException
  221. */
  222. @Test
  223. public void testWritePack2() throws IOException {
  224. writeVerifyPack2(false);
  225. }
  226. /**
  227. * Test pack writing with deltas reuse, delta-base first rule. Pack
  228. * content/preparation as in {@link #testWritePack2()}.
  229. *
  230. * @throws IOException
  231. */
  232. @Test
  233. public void testWritePack2DeltasReuseRefs() throws IOException {
  234. writeVerifyPack2(true);
  235. }
  236. /**
  237. * Test pack writing with delta reuse. Delta bases referred as offsets. Pack
  238. * configuration as in {@link #testWritePack2DeltasReuseRefs()}.
  239. *
  240. * @throws IOException
  241. */
  242. @Test
  243. public void testWritePack2DeltasReuseOffsets() throws IOException {
  244. config.setDeltaBaseAsOffset(true);
  245. writeVerifyPack2(true);
  246. }
  247. /**
  248. * Test pack writing with delta reuse. Raw-data copy (reuse) is made on a
  249. * pack with CRC32 index. Pack configuration as in
  250. * {@link #testWritePack2DeltasReuseRefs()}.
  251. *
  252. * @throws IOException
  253. */
  254. @Test
  255. public void testWritePack2DeltasCRC32Copy() throws IOException {
  256. final File packDir = new File(db.getObjectDatabase().getDirectory(), "pack");
  257. final File crc32Pack = new File(packDir,
  258. "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.pack");
  259. final File crc32Idx = new File(packDir,
  260. "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.idx");
  261. copyFile(JGitTestUtil.getTestResourceFile(
  262. "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.idxV2"),
  263. crc32Idx);
  264. db.openPack(crc32Pack, crc32Idx);
  265. writeVerifyPack2(true);
  266. }
  267. /**
  268. * Create pack basing on fixed objects list, then precisely verify content.
  269. * No delta reuse here.
  270. *
  271. * @throws IOException
  272. * @throws MissingObjectException
  273. *
  274. */
  275. @Test
  276. public void testWritePack3() throws MissingObjectException, IOException {
  277. config.setReuseDeltas(false);
  278. final ObjectId forcedOrder[] = new ObjectId[] {
  279. ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
  280. ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799"),
  281. ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
  282. ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"),
  283. ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"),
  284. ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3") };
  285. final RevWalk parser = new RevWalk(db);
  286. final RevObject forcedOrderRevs[] = new RevObject[forcedOrder.length];
  287. for (int i = 0; i < forcedOrder.length; i++)
  288. forcedOrderRevs[i] = parser.parseAny(forcedOrder[i]);
  289. createVerifyOpenPack(Arrays.asList(forcedOrderRevs).iterator());
  290. assertEquals(forcedOrder.length, writer.getObjectsNumber());
  291. verifyObjectsOrder(forcedOrder);
  292. assertEquals("ed3f96b8327c7c66b0f8f70056129f0769323d86", writer
  293. .computeName().name());
  294. }
  295. /**
  296. * Another pack creation: basing on both interesting and uninteresting
  297. * objects. No delta reuse possible here, as this is a specific case when we
  298. * write only 1 commit, associated with 1 tree, 1 blob.
  299. *
  300. * @throws IOException
  301. */
  302. @Test
  303. public void testWritePack4() throws IOException {
  304. writeVerifyPack4(false);
  305. }
  306. /**
  307. * Test thin pack writing: 1 blob delta base is on objects edge. Pack
  308. * configuration as in {@link #testWritePack4()}.
  309. *
  310. * @throws IOException
  311. */
  312. @Test
  313. public void testWritePack4ThinPack() throws IOException {
  314. writeVerifyPack4(true);
  315. }
  316. /**
  317. * Compare sizes of packs created using {@link #testWritePack2()} and
  318. * {@link #testWritePack2DeltasReuseRefs()}. The pack using deltas should
  319. * be smaller.
  320. *
  321. * @throws Exception
  322. */
  323. @Test
  324. public void testWritePack2SizeDeltasVsNoDeltas() throws Exception {
  325. testWritePack2();
  326. final long sizePack2NoDeltas = os.size();
  327. tearDown();
  328. setUp();
  329. testWritePack2DeltasReuseRefs();
  330. final long sizePack2DeltasRefs = os.size();
  331. assertTrue(sizePack2NoDeltas > sizePack2DeltasRefs);
  332. }
  333. /**
  334. * Compare sizes of packs created using
  335. * {@link #testWritePack2DeltasReuseRefs()} and
  336. * {@link #testWritePack2DeltasReuseOffsets()}. The pack with delta bases
  337. * written as offsets should be smaller.
  338. *
  339. * @throws Exception
  340. */
  341. @Test
  342. public void testWritePack2SizeOffsetsVsRefs() throws Exception {
  343. testWritePack2DeltasReuseRefs();
  344. final long sizePack2DeltasRefs = os.size();
  345. tearDown();
  346. setUp();
  347. testWritePack2DeltasReuseOffsets();
  348. final long sizePack2DeltasOffsets = os.size();
  349. assertTrue(sizePack2DeltasRefs > sizePack2DeltasOffsets);
  350. }
  351. /**
  352. * Compare sizes of packs created using {@link #testWritePack4()} and
  353. * {@link #testWritePack4ThinPack()}. Obviously, the thin pack should be
  354. * smaller.
  355. *
  356. * @throws Exception
  357. */
  358. @Test
  359. public void testWritePack4SizeThinVsNoThin() throws Exception {
  360. testWritePack4();
  361. final long sizePack4 = os.size();
  362. tearDown();
  363. setUp();
  364. testWritePack4ThinPack();
  365. final long sizePack4Thin = os.size();
  366. assertTrue(sizePack4 > sizePack4Thin);
  367. }
  368. @Test
  369. public void testWriteIndex() throws Exception {
  370. config.setIndexVersion(2);
  371. writeVerifyPack4(false);
  372. File packFile = pack.getPackFile();
  373. String name = packFile.getName();
  374. String base = name.substring(0, name.lastIndexOf('.'));
  375. File indexFile = new File(packFile.getParentFile(), base + ".idx");
  376. // Validate that IndexPack came up with the right CRC32 value.
  377. final PackIndex idx1 = PackIndex.open(indexFile);
  378. assertTrue(idx1 instanceof PackIndexV2);
  379. assertEquals(0x4743F1E4L, idx1.findCRC32(ObjectId
  380. .fromString("82c6b885ff600be425b4ea96dee75dca255b69e7")));
  381. // Validate that an index written by PackWriter is the same.
  382. final File idx2File = new File(indexFile.getAbsolutePath() + ".2");
  383. final FileOutputStream is = new FileOutputStream(idx2File);
  384. try {
  385. writer.writeIndex(is);
  386. } finally {
  387. is.close();
  388. }
  389. final PackIndex idx2 = PackIndex.open(idx2File);
  390. assertTrue(idx2 instanceof PackIndexV2);
  391. assertEquals(idx1.getObjectCount(), idx2.getObjectCount());
  392. assertEquals(idx1.getOffset64Count(), idx2.getOffset64Count());
  393. for (int i = 0; i < idx1.getObjectCount(); i++) {
  394. final ObjectId id = idx1.getObjectId(i);
  395. assertEquals(id, idx2.getObjectId(i));
  396. assertEquals(idx1.findOffset(id), idx2.findOffset(id));
  397. assertEquals(idx1.findCRC32(id), idx2.findCRC32(id));
  398. }
  399. }
  400. // TODO: testWritePackDeltasCycle()
  401. // TODO: testWritePackDeltasDepth()
  402. private void writeVerifyPack1() throws IOException {
  403. final LinkedList<ObjectId> interestings = new LinkedList<ObjectId>();
  404. interestings.add(ObjectId
  405. .fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"));
  406. createVerifyOpenPack(interestings, EMPTY_LIST_OBJECT, false, false);
  407. final ObjectId expectedOrder[] = new ObjectId[] {
  408. ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
  409. ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799"),
  410. ObjectId.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"),
  411. ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
  412. ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"),
  413. ObjectId.fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904"),
  414. ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"),
  415. ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3") };
  416. assertEquals(expectedOrder.length, writer.getObjectsNumber());
  417. verifyObjectsOrder(expectedOrder);
  418. assertEquals("34be9032ac282b11fa9babdc2b2a93ca996c9c2f", writer
  419. .computeName().name());
  420. }
  421. private void writeVerifyPack2(boolean deltaReuse) throws IOException {
  422. config.setReuseDeltas(deltaReuse);
  423. final LinkedList<ObjectId> interestings = new LinkedList<ObjectId>();
  424. interestings.add(ObjectId
  425. .fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"));
  426. final LinkedList<ObjectId> uninterestings = new LinkedList<ObjectId>();
  427. uninterestings.add(ObjectId
  428. .fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"));
  429. createVerifyOpenPack(interestings, uninterestings, false, false);
  430. final ObjectId expectedOrder[] = new ObjectId[] {
  431. ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
  432. ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799"),
  433. ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
  434. ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"),
  435. ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"),
  436. ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3") };
  437. if (deltaReuse) {
  438. // objects order influenced (swapped) by delta-base first rule
  439. ObjectId temp = expectedOrder[4];
  440. expectedOrder[4] = expectedOrder[5];
  441. expectedOrder[5] = temp;
  442. }
  443. assertEquals(expectedOrder.length, writer.getObjectsNumber());
  444. verifyObjectsOrder(expectedOrder);
  445. assertEquals("ed3f96b8327c7c66b0f8f70056129f0769323d86", writer
  446. .computeName().name());
  447. }
  448. private void writeVerifyPack4(final boolean thin) throws IOException {
  449. final LinkedList<ObjectId> interestings = new LinkedList<ObjectId>();
  450. interestings.add(ObjectId
  451. .fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"));
  452. final LinkedList<ObjectId> uninterestings = new LinkedList<ObjectId>();
  453. uninterestings.add(ObjectId
  454. .fromString("c59759f143fb1fe21c197981df75a7ee00290799"));
  455. createVerifyOpenPack(interestings, uninterestings, thin, false);
  456. final ObjectId writtenObjects[] = new ObjectId[] {
  457. ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
  458. ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
  459. ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259") };
  460. assertEquals(writtenObjects.length, writer.getObjectsNumber());
  461. ObjectId expectedObjects[];
  462. if (thin) {
  463. expectedObjects = new ObjectId[4];
  464. System.arraycopy(writtenObjects, 0, expectedObjects, 0,
  465. writtenObjects.length);
  466. expectedObjects[3] = ObjectId
  467. .fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3");
  468. } else {
  469. expectedObjects = writtenObjects;
  470. }
  471. verifyObjectsOrder(expectedObjects);
  472. assertEquals("cded4b74176b4456afa456768b2b5aafb41c44fc", writer
  473. .computeName().name());
  474. }
  475. private void createVerifyOpenPack(final Collection<ObjectId> interestings,
  476. final Collection<ObjectId> uninterestings, final boolean thin,
  477. final boolean ignoreMissingUninteresting)
  478. throws MissingObjectException, IOException {
  479. NullProgressMonitor m = NullProgressMonitor.INSTANCE;
  480. writer = new PackWriter(config, db.newObjectReader());
  481. writer.setThin(thin);
  482. writer.setIgnoreMissingUninteresting(ignoreMissingUninteresting);
  483. writer.preparePack(m, interestings, uninterestings);
  484. writer.writePack(m, m, os);
  485. writer.release();
  486. verifyOpenPack(thin);
  487. }
  488. private void createVerifyOpenPack(final Iterator<RevObject> objectSource)
  489. throws MissingObjectException, IOException {
  490. NullProgressMonitor m = NullProgressMonitor.INSTANCE;
  491. writer = new PackWriter(config, db.newObjectReader());
  492. writer.preparePack(objectSource);
  493. writer.writePack(m, m, os);
  494. writer.release();
  495. verifyOpenPack(false);
  496. }
  497. private void verifyOpenPack(final boolean thin) throws IOException {
  498. final byte[] packData = os.toByteArray();
  499. if (thin) {
  500. PackParser p = index(packData);
  501. try {
  502. p.parse(NullProgressMonitor.INSTANCE);
  503. fail("indexer should grumble about missing object");
  504. } catch (IOException x) {
  505. // expected
  506. }
  507. }
  508. ObjectDirectoryPackParser p = (ObjectDirectoryPackParser) index(packData);
  509. p.setKeepEmpty(true);
  510. p.setAllowThin(thin);
  511. p.setIndexVersion(2);
  512. p.parse(NullProgressMonitor.INSTANCE);
  513. pack = p.getPackFile();
  514. assertNotNull("have PackFile after parsing", pack);
  515. }
  516. private PackParser index(final byte[] packData) throws IOException {
  517. if (inserter == null)
  518. inserter = dst.newObjectInserter();
  519. return inserter.newPackParser(new ByteArrayInputStream(packData));
  520. }
  521. private void verifyObjectsOrder(final ObjectId objectsOrder[]) {
  522. final List<PackIndex.MutableEntry> entries = new ArrayList<PackIndex.MutableEntry>();
  523. for (MutableEntry me : pack) {
  524. entries.add(me.cloneEntry());
  525. }
  526. Collections.sort(entries, new Comparator<PackIndex.MutableEntry>() {
  527. public int compare(MutableEntry o1, MutableEntry o2) {
  528. return Long.signum(o1.getOffset() - o2.getOffset());
  529. }
  530. });
  531. int i = 0;
  532. for (MutableEntry me : entries) {
  533. assertEquals(objectsOrder[i++].toObjectId(), me.toObjectId());
  534. }
  535. }
  536. }