Browse Source

Merge "Refactor ReadTreeTest to allow testing other checkout classes"

stable-0.9
Shawn Pearce 15 years ago committed by Code Review
parent
commit
92eedd667d
  1. 137
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java

137
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java

@ -2,6 +2,7 @@
* Copyright (C) 2007, Dave Watson <dwatson@mimvista.com> * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
* Copyright (C) 2008-2009, Robin Rosenberg <robin.rosenberg@dewire.com> * Copyright (C) 2008-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
* Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
@ -49,6 +50,7 @@ import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import org.eclipse.jgit.errors.CheckoutConflictException; import org.eclipse.jgit.errors.CheckoutConflictException;
@ -58,7 +60,7 @@ public class ReadTreeTest extends RepositoryTestCase {
private Tree theHead; private Tree theHead;
private Tree theMerge; private Tree theMerge;
private GitIndex theIndex; private GitIndex theIndex;
private WorkDirCheckout theReadTree; private Checkout theReadTree;
// Each of these rules are from the read-tree manpage // Each of these rules are from the read-tree manpage
// go there to see what they mean. // go there to see what they mean.
// Rule 0 is left out for obvious reasons :) // Rule 0 is left out for obvious reasons :)
@ -71,23 +73,23 @@ public class ReadTreeTest extends RepositoryTestCase {
headFile.setId(objectId); headFile.setId(objectId);
Tree merge = new Tree(db); Tree merge = new Tree(db);
WorkDirCheckout readTree = new WorkDirCheckout(db, trash, head, index, merge); Checkout readTree = getCheckoutImpl(head, index, merge);
readTree.prescanTwoTrees(); 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(); readTree.prescanTwoTrees();
assertEquals(objectId, readTree.updated.get("foo")); assertEquals(objectId, readTree.updated().get("foo"));
ObjectId anotherId = ObjectId.fromString("ba78e065e2c261d4f7b8f42107588051e87e18ee"); ObjectId anotherId = ObjectId.fromString("ba78e065e2c261d4f7b8f42107588051e87e18ee");
merge.addFile("foo").setId(anotherId); merge.addFile("foo").setId(anotherId);
readTree = new WorkDirCheckout(db, trash, head, index, merge); readTree = getCheckoutImpl(head, index, merge);
readTree.prescanTwoTrees(); readTree.prescanTwoTrees();
assertEquals(anotherId, readTree.updated.get("foo")); assertEquals(anotherId, readTree.updated().get("foo"));
} }
void setupCase(HashMap<String, String> headEntries, void setupCase(HashMap<String, String> headEntries,
@ -134,8 +136,8 @@ public class ReadTreeTest extends RepositoryTestCase {
return null; return null;
} }
private WorkDirCheckout go() throws IOException { private Checkout go() throws IOException {
theReadTree = new WorkDirCheckout(db, trash, theHead, theIndex, theMerge); theReadTree = getCheckoutImpl(theHead, theIndex, theMerge);
theReadTree.prescanTwoTrees(); theReadTree.prescanTwoTrees();
return theReadTree; return theReadTree;
} }
@ -152,9 +154,9 @@ public class ReadTreeTest extends RepositoryTestCase {
setupCase(null, null, idxMap); setupCase(null, null, idxMap);
theReadTree = go(); theReadTree = go();
assertTrue(theReadTree.updated.isEmpty()); assertTrue(theReadTree.updated().isEmpty());
assertTrue(theReadTree.removed.isEmpty()); assertTrue(theReadTree.removed().isEmpty());
assertTrue(theReadTree.conflicts.isEmpty()); assertTrue(theReadTree.conflicts().isEmpty());
// rules 6 and 7 // rules 6 and 7
idxMap = new HashMap<String, String>(); idxMap = new HashMap<String, String>();
@ -172,9 +174,9 @@ public class ReadTreeTest extends RepositoryTestCase {
setupCase(null, mergeMap, idxMap); setupCase(null, mergeMap, idxMap);
go(); go();
assertTrue(theReadTree.updated.isEmpty()); assertTrue(theReadTree.updated().isEmpty());
assertTrue(theReadTree.removed.isEmpty()); assertTrue(theReadTree.removed().isEmpty());
assertTrue(theReadTree.conflicts.contains("foo")); assertTrue(theReadTree.conflicts().contains("foo"));
// rule 10 // rule 10
@ -183,9 +185,9 @@ public class ReadTreeTest extends RepositoryTestCase {
setupCase(headMap, null, idxMap); setupCase(headMap, null, idxMap);
go(); go();
assertTrue(theReadTree.removed.contains("foo")); assertTrue(theReadTree.removed().contains("foo"));
assertTrue(theReadTree.updated.isEmpty()); assertTrue(theReadTree.updated().isEmpty());
assertTrue(theReadTree.conflicts.isEmpty()); assertTrue(theReadTree.conflicts().isEmpty());
// rule 11 // rule 11
setupCase(headMap, null, idxMap); setupCase(headMap, null, idxMap);
@ -194,18 +196,18 @@ public class ReadTreeTest extends RepositoryTestCase {
theIndex.getMembers()[0].forceRecheck(); theIndex.getMembers()[0].forceRecheck();
go(); go();
assertTrue(theReadTree.removed.isEmpty()); assertTrue(theReadTree.removed().isEmpty());
assertTrue(theReadTree.updated.isEmpty()); assertTrue(theReadTree.updated().isEmpty());
assertTrue(theReadTree.conflicts.contains("foo")); assertTrue(theReadTree.conflicts().contains("foo"));
// rule 12 & 13 // rule 12 & 13
headMap.put("foo", "head"); headMap.put("foo", "head");
setupCase(headMap, null, idxMap); setupCase(headMap, null, idxMap);
go(); go();
assertTrue(theReadTree.removed.isEmpty()); assertTrue(theReadTree.removed().isEmpty());
assertTrue(theReadTree.updated.isEmpty()); assertTrue(theReadTree.updated().isEmpty());
assertTrue(theReadTree.conflicts.contains("foo")); assertTrue(theReadTree.conflicts().contains("foo"));
// rules 14 & 15 // rules 14 & 15
setupCase(headMap, headMap, idxMap); setupCase(headMap, headMap, idxMap);
@ -215,7 +217,7 @@ public class ReadTreeTest extends RepositoryTestCase {
// rules 16 & 17 // rules 16 & 17
setupCase(headMap, mergeMap, idxMap); go(); setupCase(headMap, mergeMap, idxMap); go();
assertTrue(theReadTree.conflicts.contains("foo")); assertTrue(theReadTree.conflicts().contains("foo"));
// rules 18 & 19 // rules 18 & 19
setupCase(headMap, idxMap, idxMap); go(); setupCase(headMap, idxMap, idxMap); go();
@ -223,7 +225,7 @@ public class ReadTreeTest extends RepositoryTestCase {
// rule 20 // rule 20
setupCase(idxMap, mergeMap, idxMap); go(); setupCase(idxMap, mergeMap, idxMap); go();
assertTrue(theReadTree.updated.containsKey("foo")); assertTrue(theReadTree.updated().containsKey("foo"));
// rules 21 // rules 21
setupCase(idxMap, mergeMap, idxMap); setupCase(idxMap, mergeMap, idxMap);
@ -231,13 +233,13 @@ public class ReadTreeTest extends RepositoryTestCase {
writeTrashFile("foo", "bar"); writeTrashFile("foo", "bar");
theIndex.getMembers()[0].forceRecheck(); theIndex.getMembers()[0].forceRecheck();
go(); go();
assertTrue(theReadTree.conflicts.contains("foo")); assertTrue(theReadTree.conflicts().contains("foo"));
} }
private void assertAllEmpty() { private void assertAllEmpty() {
assertTrue(theReadTree.removed.isEmpty()); assertTrue(theReadTree.removed().isEmpty());
assertTrue(theReadTree.updated.isEmpty()); assertTrue(theReadTree.updated().isEmpty());
assertTrue(theReadTree.conflicts.isEmpty()); assertTrue(theReadTree.conflicts().isEmpty());
} }
public void testDirectoryFileSimple() throws IOException { public void testDirectoryFileSimple() throws IOException {
@ -254,20 +256,20 @@ public class ReadTreeTest extends RepositoryTestCase {
recursiveDelete(new File(trash, "DF")); recursiveDelete(new File(trash, "DF"));
theIndex.add(trash, writeTrashFile("DF", "DF")); theIndex.add(trash, writeTrashFile("DF", "DF"));
theReadTree = new WorkDirCheckout(db, trash, treeDF, theIndex, treeDFDF); theReadTree = getCheckoutImpl(treeDF, theIndex, treeDFDF);
theReadTree.prescanTwoTrees(); theReadTree.prescanTwoTrees();
assertTrue(theReadTree.removed.contains("DF")); assertTrue(theReadTree.removed().contains("DF"));
assertTrue(theReadTree.updated.containsKey("DF/DF")); assertTrue(theReadTree.updated().containsKey("DF/DF"));
recursiveDelete(new File(trash, "DF")); recursiveDelete(new File(trash, "DF"));
theIndex = new GitIndex(db); theIndex = new GitIndex(db);
theIndex.add(trash, writeTrashFile("DF/DF", "DF/DF")); theIndex.add(trash, writeTrashFile("DF/DF", "DF/DF"));
theReadTree = new WorkDirCheckout(db, trash, treeDFDF, theIndex, treeDF); theReadTree = getCheckoutImpl(treeDFDF, theIndex, treeDF);
theReadTree.prescanTwoTrees(); theReadTree.prescanTwoTrees();
assertTrue(theReadTree.removed.contains("DF/DF")); assertTrue(theReadTree.removed().contains("DF/DF"));
assertTrue(theReadTree.updated.containsKey("DF")); assertTrue(theReadTree.updated().containsKey("DF"));
} }
/* /*
@ -473,19 +475,19 @@ public class ReadTreeTest extends RepositoryTestCase {
} }
private void assertConflict(String s) { private void assertConflict(String s) {
assertTrue(theReadTree.conflicts.contains(s)); assertTrue(theReadTree.conflicts().contains(s));
} }
private void assertUpdated(String s) { private void assertUpdated(String s) {
assertTrue(theReadTree.updated.containsKey(s)); assertTrue(theReadTree.updated().containsKey(s));
} }
private void assertRemoved(String s) { private void assertRemoved(String s) {
assertTrue(theReadTree.removed.contains(s)); assertTrue(theReadTree.removed().contains(s));
} }
private void assertNoConflicts() { private void assertNoConflicts() {
assertTrue(theReadTree.conflicts.isEmpty()); assertTrue(theReadTree.conflicts().isEmpty());
} }
private void doit(HashMap<String, String> h, HashMap<String, String>m, private void doit(HashMap<String, String> h, HashMap<String, String>m,
@ -551,7 +553,7 @@ public class ReadTreeTest extends RepositoryTestCase {
} }
private void checkout() throws IOException { private void checkout() throws IOException {
theReadTree = new WorkDirCheckout(db, trash, theHead, theIndex, theMerge); theReadTree = getCheckoutImpl(theHead, theIndex, theMerge);
theReadTree.checkout(); theReadTree.checkout();
} }
@ -578,4 +580,57 @@ public class ReadTreeTest extends RepositoryTestCase {
// should have thrown // should have thrown
} }
} }
/**
* The interface these tests need from a class implementing a checkout
*/
interface Checkout {
HashMap<String, ObjectId> updated();
ArrayList<String> conflicts();
ArrayList<String> removed();
void prescanTwoTrees() throws IOException;
void checkout() throws IOException;
}
/**
* Return the current implementation of the {@link Checkout} interface.
* <p>
* 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<String, ObjectId> updated() {
return updated;
}
public ArrayList<String> conflicts() {
return conflicts;
}
public ArrayList<String> removed() {
return removed;
}
public void prescanTwoTrees() throws IOException {
super.prescanTwoTrees();
}
}
} }

Loading…
Cancel
Save