diff options
author | Shawn O. Pearce <spearce@spearce.org> | 2010-01-12 12:30:42 -0800 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2010-01-12 12:30:42 -0800 |
commit | f5eb0d93660786213b98dadde7d93c5605454495 (patch) | |
tree | 4730c52648d239fc45e483f388be1bd1e874d106 /org.eclipse.jgit.junit | |
parent | d5bc8be743a6308509f36a2bd2f9414030eea9ea (diff) | |
download | jgit-f5eb0d93660786213b98dadde7d93c5605454495.tar.gz jgit-f5eb0d93660786213b98dadde7d93c5605454495.zip |
Add JUnit tests for HTTP transport
No Eclipse support for this project is provided, because the
Jetty project does not publish a complete P2 repository.
Change-Id: Ic5fe2e79bb216e36920fd4a70ec15dd6ccfd1468
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'org.eclipse.jgit.junit')
-rw-r--r-- | org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java | 12 | ||||
-rw-r--r-- | org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java | 162 |
2 files changed, 161 insertions, 13 deletions
diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java index ec9b1d7ac3..ddace0df2e 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009, Google Inc. + * Copyright (C) 2009-2010, Google Inc. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com> * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org> * and other copyright owners as documented in the project's IP log. @@ -57,8 +57,10 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import junit.framework.Assert; import junit.framework.TestCase; +import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileBasedConfig; import org.eclipse.jgit.lib.PersonIdent; @@ -404,6 +406,14 @@ public abstract class LocalDiskRepositoryTestCase extends TestCase { return new String(body, 0, body.length, "UTF-8"); } + protected static void assertEquals(AnyObjectId exp, AnyObjectId act) { + if (exp != null) + exp = exp.copy(); + if (act != null) + act = act.copy(); + Assert.assertEquals(exp, act); + } + private static String[] toEnvArray(final Map<String, String> env) { final String[] envp = new String[env.size()]; int i = 0; diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java index ce8b3e6ee6..e738276bd8 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java @@ -44,11 +44,15 @@ package org.eclipse.jgit.junit; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.security.MessageDigest; import java.util.ArrayList; import java.util.Collections; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; import junit.framework.Assert; import junit.framework.AssertionFailedError; @@ -60,21 +64,30 @@ import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheEditor.DeletePath; import org.eclipse.jgit.dircache.DirCacheEditor.DeleteTree; import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit; +import org.eclipse.jgit.errors.IncorrectObjectTypeException; +import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.ObjectWritingException; import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.Commit; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.LockFile; +import org.eclipse.jgit.lib.NullProgressMonitor; +import org.eclipse.jgit.lib.ObjectChecker; +import org.eclipse.jgit.lib.ObjectDatabase; import org.eclipse.jgit.lib.ObjectDirectory; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectWriter; +import org.eclipse.jgit.lib.PackFile; +import org.eclipse.jgit.lib.PackWriter; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefWriter; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Tag; +import org.eclipse.jgit.lib.PackIndex.MutableEntry; +import org.eclipse.jgit.revwalk.ObjectWalk; import org.eclipse.jgit.revwalk.RevBlob; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevObject; @@ -428,26 +441,25 @@ public class TestRepository { * @throws Exception */ public void updateServerInfo() throws Exception { - if (db.getObjectDatabase() instanceof ObjectDirectory) { + final ObjectDatabase odb = db.getObjectDatabase(); + if (odb instanceof ObjectDirectory) { RefWriter rw = new RefWriter(db.getAllRefs().values()) { @Override protected void writeFile(final String name, final byte[] bin) throws IOException { - final File p = new File(db.getDirectory(), name); - final LockFile lck = new LockFile(p); - if (!lck.lock()) - throw new ObjectWritingException("Can't write " + p); - try { - lck.write(bin); - } catch (IOException ioe) { - throw new ObjectWritingException("Can't write " + p); - } - if (!lck.commit()) - throw new ObjectWritingException("Can't write " + p); + TestRepository.this.writeFile(name, bin); } }; rw.writePackedRefs(); rw.writeInfoRefs(); + + final StringBuilder w = new StringBuilder(); + for (PackFile p : ((ObjectDirectory) odb).getPacks()) { + w.append("P "); + w.append(p.getPackFile().getName()); + w.append('\n'); + } + writeFile("objects/info/packs", Constants.encodeASCII(w.toString())); } } @@ -484,6 +496,132 @@ public class TestRepository { return new BranchBuilder(ref); } + /** + * Run consistency checks against the object database. + * <p> + * This method completes silently if the checks pass. A temporary revision + * pool is constructed during the checking. + * + * @param tips + * the tips to start checking from; if not supplied the refs of + * the repository are used instead. + * @throws MissingObjectException + * @throws IncorrectObjectTypeException + * @throws IOException + */ + public void fsck(RevObject... tips) throws MissingObjectException, + IncorrectObjectTypeException, IOException { + ObjectWalk ow = new ObjectWalk(db); + if (tips.length != 0) { + for (RevObject o : tips) + ow.markStart(ow.parseAny(o)); + } else { + for (Ref r : db.getAllRefs().values()) + ow.markStart(ow.parseAny(r.getObjectId())); + } + + ObjectChecker oc = new ObjectChecker(); + for (;;) { + final RevCommit o = ow.next(); + if (o == null) + break; + + final byte[] bin = db.openObject(o).getCachedBytes(); + oc.checkCommit(bin); + assertHash(o, bin); + } + + for (;;) { + final RevObject o = ow.nextObject(); + if (o == null) + break; + + final byte[] bin = db.openObject(o).getCachedBytes(); + oc.check(o.getType(), bin); + assertHash(o, bin); + } + } + + private static void assertHash(RevObject id, byte[] bin) { + MessageDigest md = Constants.newMessageDigest(); + md.update(Constants.encodedTypeString(id.getType())); + md.update((byte) ' '); + md.update(Constants.encodeASCII(bin.length)); + md.update((byte) 0); + md.update(bin); + Assert.assertEquals(id.copy(), ObjectId.fromRaw(md.digest())); + } + + /** + * Pack all reachable objects in the repository into a single pack file. + * <p> + * All loose objects are automatically pruned. Existing packs however are + * not removed. + * + * @throws Exception + */ + public void packAndPrune() throws Exception { + final ObjectDirectory odb = (ObjectDirectory) db.getObjectDatabase(); + final PackWriter pw = new PackWriter(db, NullProgressMonitor.INSTANCE); + + Set<ObjectId> all = new HashSet<ObjectId>(); + for (Ref r : db.getAllRefs().values()) + all.add(r.getObjectId()); + pw.preparePack(all, Collections.<ObjectId> emptySet()); + + final ObjectId name = pw.computeName(); + FileOutputStream out; + + final File pack = nameFor(odb, name, ".pack"); + out = new FileOutputStream(pack); + try { + pw.writePack(out); + } finally { + out.close(); + } + pack.setReadOnly(); + + final File idx = nameFor(odb, name, ".idx"); + out = new FileOutputStream(idx); + try { + pw.writeIndex(out); + } finally { + out.close(); + } + idx.setReadOnly(); + + odb.openPack(pack, idx); + updateServerInfo(); + prunePacked(odb); + } + + private void prunePacked(ObjectDirectory odb) { + for (PackFile p : odb.getPacks()) { + for (MutableEntry e : p) + odb.fileFor(e.toObjectId()).delete(); + } + } + + private static File nameFor(ObjectDirectory odb, ObjectId name, String t) { + File packdir = new File(odb.getDirectory(), "pack"); + return new File(packdir, "pack-" + name.name() + t); + } + + private void writeFile(final String name, final byte[] bin) + throws IOException, ObjectWritingException { + final File p = new File(db.getDirectory(), name); + final LockFile lck = new LockFile(p); + if (!lck.lock()) + throw new ObjectWritingException("Can't write " + p); + try { + lck.write(bin); + } catch (IOException ioe) { + throw new ObjectWritingException("Can't write " + p); + } + if (!lck.commit()) + throw new ObjectWritingException("Can't write " + p); + } + /** Helper to build a branch with one or more commits */ public class BranchBuilder { private final String ref; |