From eca29464574e9551d978c3215f3d981415e31868 Mon Sep 17 00:00:00 2001 From: Christian Halstrick Date: Sat, 5 Jun 2010 00:18:17 +0200 Subject: [PATCH] Refactor ReadTreeTest to allow testing other checkout classes ReadTreeTest contains a lot of useful tests for "checkout" implementations. But ReadTreeTest was hardcoded to test only WorkDirCheckout. This change doesn't add/modify any tests semantically but refactors ReadTreeTest so that a different implementations of checkout can be tested. This was done to allow DirCacheCheckout to be tested without rewriting all these tests. Change-Id: I36e34264482b855ed22c9dde98824f573cf8ae22 Signed-off-by: Christian Halstrick Signed-off-by: Matthias Sohn --- .../org/eclipse/jgit/lib/ReadTreeTest.java | 137 ++++++++++++------ 1 file changed, 96 insertions(+), 41 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java index 481691a44..feef66f9a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java @@ -2,6 +2,7 @@ * Copyright (C) 2007, Dave Watson * Copyright (C) 2008-2009, Robin Rosenberg * Copyright (C) 2008, Shawn O. Pearce + * Copyright (C) 2010, Christian Halstrick * and other copyright owners as documented in the project's IP log. * * This program and the accompanying materials are made available @@ -49,6 +50,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.HashMap; import org.eclipse.jgit.errors.CheckoutConflictException; @@ -58,7 +60,7 @@ public class ReadTreeTest extends RepositoryTestCase { private Tree theHead; private Tree theMerge; private GitIndex theIndex; - private WorkDirCheckout theReadTree; + private Checkout theReadTree; // Each of these rules are from the read-tree manpage // go there to see what they mean. // Rule 0 is left out for obvious reasons :) @@ -71,23 +73,23 @@ public class ReadTreeTest extends RepositoryTestCase { headFile.setId(objectId); Tree merge = new Tree(db); - WorkDirCheckout readTree = new WorkDirCheckout(db, trash, head, index, merge); + Checkout readTree = getCheckoutImpl(head, index, merge); readTree.prescanTwoTrees(); - assertTrue(readTree.removed.contains("foo")); + assertTrue(readTree.removed().contains("foo")); - readTree = new WorkDirCheckout(db, trash, merge, index, head); + readTree = getCheckoutImpl(merge, index, head); readTree.prescanTwoTrees(); - assertEquals(objectId, readTree.updated.get("foo")); + assertEquals(objectId, readTree.updated().get("foo")); ObjectId anotherId = ObjectId.fromString("ba78e065e2c261d4f7b8f42107588051e87e18ee"); merge.addFile("foo").setId(anotherId); - readTree = new WorkDirCheckout(db, trash, head, index, merge); + readTree = getCheckoutImpl(head, index, merge); readTree.prescanTwoTrees(); - assertEquals(anotherId, readTree.updated.get("foo")); + assertEquals(anotherId, readTree.updated().get("foo")); } void setupCase(HashMap headEntries, @@ -134,8 +136,8 @@ public class ReadTreeTest extends RepositoryTestCase { return null; } - private WorkDirCheckout go() throws IOException { - theReadTree = new WorkDirCheckout(db, trash, theHead, theIndex, theMerge); + private Checkout go() throws IOException { + theReadTree = getCheckoutImpl(theHead, theIndex, theMerge); theReadTree.prescanTwoTrees(); return theReadTree; } @@ -152,9 +154,9 @@ public class ReadTreeTest extends RepositoryTestCase { setupCase(null, null, idxMap); theReadTree = go(); - assertTrue(theReadTree.updated.isEmpty()); - assertTrue(theReadTree.removed.isEmpty()); - assertTrue(theReadTree.conflicts.isEmpty()); + assertTrue(theReadTree.updated().isEmpty()); + assertTrue(theReadTree.removed().isEmpty()); + assertTrue(theReadTree.conflicts().isEmpty()); // rules 6 and 7 idxMap = new HashMap(); @@ -172,9 +174,9 @@ public class ReadTreeTest extends RepositoryTestCase { setupCase(null, mergeMap, idxMap); go(); - assertTrue(theReadTree.updated.isEmpty()); - assertTrue(theReadTree.removed.isEmpty()); - assertTrue(theReadTree.conflicts.contains("foo")); + assertTrue(theReadTree.updated().isEmpty()); + assertTrue(theReadTree.removed().isEmpty()); + assertTrue(theReadTree.conflicts().contains("foo")); // rule 10 @@ -183,9 +185,9 @@ public class ReadTreeTest extends RepositoryTestCase { setupCase(headMap, null, idxMap); go(); - assertTrue(theReadTree.removed.contains("foo")); - assertTrue(theReadTree.updated.isEmpty()); - assertTrue(theReadTree.conflicts.isEmpty()); + assertTrue(theReadTree.removed().contains("foo")); + assertTrue(theReadTree.updated().isEmpty()); + assertTrue(theReadTree.conflicts().isEmpty()); // rule 11 setupCase(headMap, null, idxMap); @@ -194,18 +196,18 @@ public class ReadTreeTest extends RepositoryTestCase { theIndex.getMembers()[0].forceRecheck(); go(); - assertTrue(theReadTree.removed.isEmpty()); - assertTrue(theReadTree.updated.isEmpty()); - assertTrue(theReadTree.conflicts.contains("foo")); + assertTrue(theReadTree.removed().isEmpty()); + assertTrue(theReadTree.updated().isEmpty()); + assertTrue(theReadTree.conflicts().contains("foo")); // rule 12 & 13 headMap.put("foo", "head"); setupCase(headMap, null, idxMap); go(); - assertTrue(theReadTree.removed.isEmpty()); - assertTrue(theReadTree.updated.isEmpty()); - assertTrue(theReadTree.conflicts.contains("foo")); + assertTrue(theReadTree.removed().isEmpty()); + assertTrue(theReadTree.updated().isEmpty()); + assertTrue(theReadTree.conflicts().contains("foo")); // rules 14 & 15 setupCase(headMap, headMap, idxMap); @@ -215,7 +217,7 @@ public class ReadTreeTest extends RepositoryTestCase { // rules 16 & 17 setupCase(headMap, mergeMap, idxMap); go(); - assertTrue(theReadTree.conflicts.contains("foo")); + assertTrue(theReadTree.conflicts().contains("foo")); // rules 18 & 19 setupCase(headMap, idxMap, idxMap); go(); @@ -223,7 +225,7 @@ public class ReadTreeTest extends RepositoryTestCase { // rule 20 setupCase(idxMap, mergeMap, idxMap); go(); - assertTrue(theReadTree.updated.containsKey("foo")); + assertTrue(theReadTree.updated().containsKey("foo")); // rules 21 setupCase(idxMap, mergeMap, idxMap); @@ -231,13 +233,13 @@ public class ReadTreeTest extends RepositoryTestCase { writeTrashFile("foo", "bar"); theIndex.getMembers()[0].forceRecheck(); go(); - assertTrue(theReadTree.conflicts.contains("foo")); + assertTrue(theReadTree.conflicts().contains("foo")); } private void assertAllEmpty() { - assertTrue(theReadTree.removed.isEmpty()); - assertTrue(theReadTree.updated.isEmpty()); - assertTrue(theReadTree.conflicts.isEmpty()); + assertTrue(theReadTree.removed().isEmpty()); + assertTrue(theReadTree.updated().isEmpty()); + assertTrue(theReadTree.conflicts().isEmpty()); } public void testDirectoryFileSimple() throws IOException { @@ -254,20 +256,20 @@ public class ReadTreeTest extends RepositoryTestCase { recursiveDelete(new File(trash, "DF")); theIndex.add(trash, writeTrashFile("DF", "DF")); - theReadTree = new WorkDirCheckout(db, trash, treeDF, theIndex, treeDFDF); + theReadTree = getCheckoutImpl(treeDF, theIndex, treeDFDF); theReadTree.prescanTwoTrees(); - assertTrue(theReadTree.removed.contains("DF")); - assertTrue(theReadTree.updated.containsKey("DF/DF")); + assertTrue(theReadTree.removed().contains("DF")); + assertTrue(theReadTree.updated().containsKey("DF/DF")); recursiveDelete(new File(trash, "DF")); theIndex = new GitIndex(db); theIndex.add(trash, writeTrashFile("DF/DF", "DF/DF")); - theReadTree = new WorkDirCheckout(db, trash, treeDFDF, theIndex, treeDF); + theReadTree = getCheckoutImpl(treeDFDF, theIndex, treeDF); theReadTree.prescanTwoTrees(); - assertTrue(theReadTree.removed.contains("DF/DF")); - assertTrue(theReadTree.updated.containsKey("DF")); + assertTrue(theReadTree.removed().contains("DF/DF")); + assertTrue(theReadTree.updated().containsKey("DF")); } /* @@ -473,19 +475,19 @@ public class ReadTreeTest extends RepositoryTestCase { } private void assertConflict(String s) { - assertTrue(theReadTree.conflicts.contains(s)); + assertTrue(theReadTree.conflicts().contains(s)); } private void assertUpdated(String s) { - assertTrue(theReadTree.updated.containsKey(s)); + assertTrue(theReadTree.updated().containsKey(s)); } private void assertRemoved(String s) { - assertTrue(theReadTree.removed.contains(s)); + assertTrue(theReadTree.removed().contains(s)); } private void assertNoConflicts() { - assertTrue(theReadTree.conflicts.isEmpty()); + assertTrue(theReadTree.conflicts().isEmpty()); } private void doit(HashMap h, HashMapm, @@ -551,7 +553,7 @@ public class ReadTreeTest extends RepositoryTestCase { } private void checkout() throws IOException { - theReadTree = new WorkDirCheckout(db, trash, theHead, theIndex, theMerge); + theReadTree = getCheckoutImpl(theHead, theIndex, theMerge); theReadTree.checkout(); } @@ -578,4 +580,57 @@ public class ReadTreeTest extends RepositoryTestCase { // should have thrown } } + + /** + * The interface these tests need from a class implementing a checkout + */ + interface Checkout { + HashMap updated(); + ArrayList conflicts(); + ArrayList removed(); + void prescanTwoTrees() throws IOException; + void checkout() throws IOException; + } + + /** + * Return the current implementation of the {@link Checkout} interface. + *

+ * May be overridden by subclasses which would inherit all tests but can + * specify their own implementation of a Checkout + * + * @param head + * @param index + * @param merge + * @return the current implementation of {@link Checkout} + */ + protected Checkout getCheckoutImpl(Tree head, GitIndex index, + Tree merge) { + return new WorkdirCheckoutImpl(head, index, merge); + } + + /** + * An implementation of the {@link Checkout} interface which uses {@link WorkDirCheckout} + */ + class WorkdirCheckoutImpl extends WorkDirCheckout implements Checkout { + public WorkdirCheckoutImpl(Tree head, GitIndex index, + Tree merge) { + super(db, trash, head, index, merge); + } + + public HashMap updated() { + return updated; + } + + public ArrayList conflicts() { + return conflicts; + } + + public ArrayList removed() { + return removed; + } + + public void prescanTwoTrees() throws IOException { + super.prescanTwoTrees(); + } + } }