@@ -0,0 +1,125 @@
+package org.spearce.jgit.lib;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.spearce.jgit.errors.IncorrectObjectTypeException;
+import org.spearce.jgit.errors.MissingObjectException;
+import org.spearce.jgit.revwalk.RevObject;
+import org.spearce.jgit.revwalk.RevWalk;
+
+public class WindowCacheTest extends RepositoryTestCase {
+
+ public void setUp() throws Exception {
+ WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
+ windowCacheConfig.setPackedGitOpenFiles(1);
+ WindowCache.reconfigure(windowCacheConfig);
+ super.setUp();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
+ WindowCache.reconfigure(windowCacheConfig);
+ }
+
+ /*
+ * Add pack, find objects in new pack Replace packs, find objects in another
+ * pack than original, don't choke on lost pack
+ */
+ public void testObjectInNewPack() throws IncorrectObjectTypeException,
+ IOException {
+ Repository extra = createNewEmptyRepo();
+ RevObject o1 = writeBlob(extra, db,
+ "nihuioijoisuoiudyosyudoiuyusyuoiyewData");
+ PackWriter packWriter = new PackWriter(extra, new TextProgressMonitor());
+ packWriter.addObject(o1);
+ ObjectId name = packWriter.computeName();
+ File packFileName = fullPackFileName(db, name, ".pack");
+ FileOutputStream pos = new FileOutputStream(packFileName);
+ packWriter.writePack(pos);
+ pos.close();
+ File idxfname = fullPackFileName(db, name, ".idx");
+ FileOutputStream ios = new FileOutputStream(idxfname);
+ packWriter.writeIndex(ios);
+ ios.close();
+ assertEquals(o1.name(), new RevWalk(db).parseAny(o1).name());
+ }
+
+ /*
+ * Add pack, find objects in new pack Replace packs, find objects in another
+ * pack than original, don't choke on lost pack
+ */
+ public void testObjectMovedToNewPack() throws IncorrectObjectTypeException,
+ IOException {
+ Repository extra = createNewEmptyRepo();
+ RevObject o1 = writeBlob(extra, db,
+ "nihuioijoisuoiudyosyudoiuyusyuoiyewData1");
+ PackWriter packWriter1 = new PackWriter(extra,
+ new TextProgressMonitor());
+ packWriter1.addObject(o1);
+ ObjectId name1 = packWriter1.computeName();
+ File packFileName1 = fullPackFileName(db, name1, ".pack");
+ FileOutputStream pos1 = new FileOutputStream(packFileName1);
+ packWriter1.writePack(pos1);
+ pos1.close();
+ File idxfname1 = fullPackFileName(db, name1, ".idx");
+ FileOutputStream ios1 = new FileOutputStream(idxfname1);
+ packWriter1.writeIndex(ios1);
+ ios1.close();
+ assertEquals(o1.name(), new RevWalk(db).parseAny(o1).name());
+
+ // Ok, lets repack this repo
+ // create an additional object
+ RevObject o2 = writeBlob(extra, db,
+ "nihuioijoisuoiudyosyudoiuyusyuoiyewData2");
+ PackWriter packWriter2 = new PackWriter(extra,
+ new TextProgressMonitor());
+ packWriter2.addObject(o1);
+ packWriter2.addObject(o2);
+ ObjectId name2 = packWriter2.computeName();
+ File packFileName2 = fullPackFileName(db, name2, ".pack");
+ FileOutputStream pos2 = new FileOutputStream(packFileName2);
+ packWriter2.writePack(pos2);
+ pos2.close();
+ File idxfname2 = fullPackFileName(db, name2, ".idx");
+ FileOutputStream ios2 = new FileOutputStream(idxfname2);
+ packWriter2.writeIndex(ios2);
+ ios2.close();
+ assertEquals(o2.name(), new RevWalk(db).parseAny(o2).name());
+
+ WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
+ windowCacheConfig.setPackedGitOpenFiles(1);
+ WindowCache.reconfigure(windowCacheConfig);
+ packFileName1.delete();
+ idxfname1.delete();
+
+ // Now here is the interesting thing. Will git figure the new
+ // object exists in the new pack, and not the old one.
+ assertEquals(o1.name(), new RevWalk(db).parseAny(o1).name());
+ }
+
+ private File fullPackFileName(Repository repository, ObjectId hash,
+ String suffix) {
+ return new File(new File(repository.getObjectsDirectory(), "pack"),
+ "pack-" + hash.name() + suffix);
+ }
+
+ private RevObject writeBlob(final Repository repo, final Repository notIn,
+ final String data) throws IOException {
+ RevWalk revWalk = new RevWalk(repo);
+ byte[] bytes = data.getBytes(Constants.CHARACTER_ENCODING);
+ ObjectId id = new ObjectWriter(repo).writeBlob(bytes.length,
+ new ByteArrayInputStream(bytes));
+ try {
+ assertNull(
+ "Oops, We want new objects that we do not have yet, for this test!",
+ new RevWalk(notIn).parseAny(id));
+ } catch (MissingObjectException e) {
+ // Ok
+ }
+ return revWalk.lookupBlob(id);
+ }
+}