Browse Source

Refactor ResolveMergerTest to allow testing multiple strategies

By making use of JUnit Theories and Datapoints ResolveMergerTests is now
capable to run the tests against multiple Merge strategies.

Change-Id: Ifa0075e0a2aca7576ef268291b73fa2f4d79b591
stable-2.2
Christian Halstrick 12 years ago
parent
commit
37ab3ffdaf
  1. 102
      org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/ResolveMergerTest.java

102
org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/ResolveMergerTest.java

@ -61,12 +61,20 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.FileUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
@RunWith(Theories.class)
public class ResolveMergerTest extends RepositoryTestCase { public class ResolveMergerTest extends RepositoryTestCase {
@Test @DataPoint
public void failingPathsShouldNotResultInOKReturnValue() throws Exception { public static MergeStrategy resolve = MergeStrategy.RESOLVE;
@Theory
public void failingPathsShouldNotResultInOKReturnValue(
MergeStrategy strategy) throws Exception {
File folder1 = new File(db.getWorkTree(), "folder1"); File folder1 = new File(db.getWorkTree(), "folder1");
FileUtils.mkdir(folder1); FileUtils.mkdir(folder1);
File file = new File(folder1, "file1.txt"); File file = new File(folder1, "file1.txt");
@ -96,7 +104,7 @@ public class ResolveMergerTest extends RepositoryTestCase {
file = new File(folder1, "file3.txt"); file = new File(folder1, "file3.txt");
write(file, "folder1--file3.txt"); write(file, "folder1--file3.txt");
ResolveMerger merger = new ResolveMerger(db, false); ResolveMerger merger = (ResolveMerger) strategy.newMerger(db, false);
merger.setCommitNames(new String[] { "BASE", "HEAD", "other" }); merger.setCommitNames(new String[] { "BASE", "HEAD", "other" });
merger.setWorkingTreeIterator(new FileTreeIterator(db)); merger.setWorkingTreeIterator(new FileTreeIterator(db));
boolean ok = merger.merge(head.getId(), other.getId()); boolean ok = merger.merge(head.getId(), other.getId());
@ -109,10 +117,12 @@ public class ResolveMergerTest extends RepositoryTestCase {
* Merging two conflicting subtrees when the index does not contain any file * Merging two conflicting subtrees when the index does not contain any file
* in that subtree should lead to a conflicting state. * in that subtree should lead to a conflicting state.
* *
* @param strategy
* @throws Exception * @throws Exception
*/ */
@Test @Theory
public void checkMergeConflictingTreesWithoutIndex() throws Exception { public void checkMergeConflictingTreesWithoutIndex(MergeStrategy strategy)
throws Exception {
Git git = Git.wrap(db); Git git = Git.wrap(db);
writeTrashFile("d/1", "orig"); writeTrashFile("d/1", "orig");
@ -130,7 +140,8 @@ public class ResolveMergerTest extends RepositoryTestCase {
git.rm().addFilepattern("d/1").call(); git.rm().addFilepattern("d/1").call();
git.rm().addFilepattern("d").call(); git.rm().addFilepattern("d").call();
MergeResult mergeRes = git.merge().include(masterCommit).call(); MergeResult mergeRes = git.merge().setStrategy(strategy)
.include(masterCommit).call();
assertEquals(MergeStatus.CONFLICTING, mergeRes.getMergeStatus()); assertEquals(MergeStatus.CONFLICTING, mergeRes.getMergeStatus());
assertEquals( assertEquals(
"[d/1, mode:100644, stage:1, content:orig][d/1, mode:100644, stage:2, content:side][d/1, mode:100644, stage:3, content:master]", "[d/1, mode:100644, stage:1, content:orig][d/1, mode:100644, stage:2, content:side][d/1, mode:100644, stage:3, content:master]",
@ -141,10 +152,12 @@ public class ResolveMergerTest extends RepositoryTestCase {
* Merging two different but mergeable subtrees when the index does not * Merging two different but mergeable subtrees when the index does not
* contain any file in that subtree should lead to a merged state. * contain any file in that subtree should lead to a merged state.
* *
* @param strategy
* @throws Exception * @throws Exception
*/ */
@Test @Theory
public void checkMergeMergeableTreesWithoutIndex() throws Exception { public void checkMergeMergeableTreesWithoutIndex(MergeStrategy strategy)
throws Exception {
Git git = Git.wrap(db); Git git = Git.wrap(db);
writeTrashFile("d/1", "1\n2\n3"); writeTrashFile("d/1", "1\n2\n3");
@ -162,7 +175,8 @@ public class ResolveMergerTest extends RepositoryTestCase {
git.rm().addFilepattern("d/1").call(); git.rm().addFilepattern("d/1").call();
git.rm().addFilepattern("d").call(); git.rm().addFilepattern("d").call();
MergeResult mergeRes = git.merge().include(masterCommit).call(); MergeResult mergeRes = git.merge().setStrategy(strategy)
.include(masterCommit).call();
assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus()); assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
assertEquals("[d/1, mode:100644, content:1master\n2\n3side\n]", assertEquals("[d/1, mode:100644, content:1master\n2\n3side\n]",
indexState(CONTENT)); indexState(CONTENT));
@ -172,10 +186,12 @@ public class ResolveMergerTest extends RepositoryTestCase {
* Merging two equal subtrees when the index does not contain any file in * Merging two equal subtrees when the index does not contain any file in
* that subtree should lead to a merged state. * that subtree should lead to a merged state.
* *
* @param strategy
* @throws Exception * @throws Exception
*/ */
@Test @Theory
public void checkMergeEqualTreesWithoutIndex() throws Exception { public void checkMergeEqualTreesWithoutIndex(MergeStrategy strategy)
throws Exception {
Git git = Git.wrap(db); Git git = Git.wrap(db);
writeTrashFile("d/1", "orig"); writeTrashFile("d/1", "orig");
@ -193,7 +209,8 @@ public class ResolveMergerTest extends RepositoryTestCase {
git.rm().addFilepattern("d/1").call(); git.rm().addFilepattern("d/1").call();
git.rm().addFilepattern("d").call(); git.rm().addFilepattern("d").call();
MergeResult mergeRes = git.merge().include(masterCommit).call(); MergeResult mergeRes = git.merge().setStrategy(strategy)
.include(masterCommit).call();
assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus()); assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
assertEquals("[d/1, mode:100644, content:modified]", assertEquals("[d/1, mode:100644, content:modified]",
indexState(CONTENT)); indexState(CONTENT));
@ -203,10 +220,12 @@ public class ResolveMergerTest extends RepositoryTestCase {
* Merging two equal subtrees with an incore merger should lead to a merged * Merging two equal subtrees with an incore merger should lead to a merged
* state (The 'Gerrit' use case). * state (The 'Gerrit' use case).
* *
* @param strategy
* @throws Exception * @throws Exception
*/ */
@Test @Theory
public void checkMergeEqualTreesInCore() throws Exception { public void checkMergeEqualTreesInCore(MergeStrategy strategy)
throws Exception {
Git git = Git.wrap(db); Git git = Git.wrap(db);
writeTrashFile("d/1", "orig"); writeTrashFile("d/1", "orig");
@ -226,8 +245,8 @@ public class ResolveMergerTest extends RepositoryTestCase {
git.rm().addFilepattern("d/1").call(); git.rm().addFilepattern("d/1").call();
git.rm().addFilepattern("d").call(); git.rm().addFilepattern("d").call();
ThreeWayMerger resolveMerger = MergeStrategy.RESOLVE ThreeWayMerger resolveMerger = (ThreeWayMerger) strategy.newMerger(db,
.newMerger(db, true); true);
boolean noProblems = resolveMerger.merge(masterCommit, sideCommit); boolean noProblems = resolveMerger.merge(masterCommit, sideCommit);
assertTrue(noProblems); assertTrue(noProblems);
} }
@ -236,10 +255,12 @@ public class ResolveMergerTest extends RepositoryTestCase {
* Merging two equal subtrees when the index and HEAD does not contain any * Merging two equal subtrees when the index and HEAD does not contain any
* file in that subtree should lead to a merged state. * file in that subtree should lead to a merged state.
* *
* @param strategy
* @throws Exception * @throws Exception
*/ */
@Test @Theory
public void checkMergeEqualNewTrees() throws Exception { public void checkMergeEqualNewTrees(MergeStrategy strategy)
throws Exception {
Git git = Git.wrap(db); Git git = Git.wrap(db);
writeTrashFile("2", "orig"); writeTrashFile("2", "orig");
@ -259,7 +280,8 @@ public class ResolveMergerTest extends RepositoryTestCase {
git.rm().addFilepattern("d/1").call(); git.rm().addFilepattern("d/1").call();
git.rm().addFilepattern("d").call(); git.rm().addFilepattern("d").call();
MergeResult mergeRes = git.merge().include(masterCommit).call(); MergeResult mergeRes = git.merge().setStrategy(strategy)
.include(masterCommit).call();
assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus()); assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
assertEquals( assertEquals(
"[2, mode:100644, content:orig][d/1, mode:100644, content:orig]", "[2, mode:100644, content:orig][d/1, mode:100644, content:orig]",
@ -270,10 +292,12 @@ public class ResolveMergerTest extends RepositoryTestCase {
* Merging two conflicting subtrees when the index and HEAD does not contain * Merging two conflicting subtrees when the index and HEAD does not contain
* any file in that subtree should lead to a conflicting state. * any file in that subtree should lead to a conflicting state.
* *
* @param strategy
* @throws Exception * @throws Exception
*/ */
@Test @Theory
public void checkMergeConflictingNewTrees() throws Exception { public void checkMergeConflictingNewTrees(MergeStrategy strategy)
throws Exception {
Git git = Git.wrap(db); Git git = Git.wrap(db);
writeTrashFile("2", "orig"); writeTrashFile("2", "orig");
@ -293,7 +317,8 @@ public class ResolveMergerTest extends RepositoryTestCase {
git.rm().addFilepattern("d/1").call(); git.rm().addFilepattern("d/1").call();
git.rm().addFilepattern("d").call(); git.rm().addFilepattern("d").call();
MergeResult mergeRes = git.merge().include(masterCommit).call(); MergeResult mergeRes = git.merge().setStrategy(strategy)
.include(masterCommit).call();
assertEquals(MergeStatus.CONFLICTING, mergeRes.getMergeStatus()); assertEquals(MergeStatus.CONFLICTING, mergeRes.getMergeStatus());
assertEquals( assertEquals(
"[2, mode:100644, content:orig][d/1, mode:100644, stage:2, content:side][d/1, mode:100644, stage:3, content:master]", "[2, mode:100644, content:orig][d/1, mode:100644, stage:2, content:side][d/1, mode:100644, stage:3, content:master]",
@ -304,10 +329,12 @@ public class ResolveMergerTest extends RepositoryTestCase {
* Merging two conflicting files when the index contains a tree for that * Merging two conflicting files when the index contains a tree for that
* path should lead to a failed state. * path should lead to a failed state.
* *
* @param strategy
* @throws Exception * @throws Exception
*/ */
@Test @Theory
public void checkMergeConflictingFilesWithTreeInIndex() throws Exception { public void checkMergeConflictingFilesWithTreeInIndex(MergeStrategy strategy)
throws Exception {
Git git = Git.wrap(db); Git git = Git.wrap(db);
writeTrashFile("0", "orig"); writeTrashFile("0", "orig");
@ -326,7 +353,8 @@ public class ResolveMergerTest extends RepositoryTestCase {
git.rm().addFilepattern("0").call(); git.rm().addFilepattern("0").call();
writeTrashFile("0/0", "side"); writeTrashFile("0/0", "side");
git.add().addFilepattern("0/0").call(); git.add().addFilepattern("0/0").call();
MergeResult mergeRes = git.merge().include(masterCommit).call(); MergeResult mergeRes = git.merge().setStrategy(strategy)
.include(masterCommit).call();
assertEquals(MergeStatus.FAILED, mergeRes.getMergeStatus()); assertEquals(MergeStatus.FAILED, mergeRes.getMergeStatus());
} }
@ -334,10 +362,12 @@ public class ResolveMergerTest extends RepositoryTestCase {
* Merging two equal files when the index contains a tree for that path * Merging two equal files when the index contains a tree for that path
* should lead to a failed state. * should lead to a failed state.
* *
* @param strategy
* @throws Exception * @throws Exception
*/ */
@Test @Theory
public void checkMergeMergeableFilesWithTreeInIndex() throws Exception { public void checkMergeMergeableFilesWithTreeInIndex(MergeStrategy strategy)
throws Exception {
Git git = Git.wrap(db); Git git = Git.wrap(db);
writeTrashFile("0", "orig"); writeTrashFile("0", "orig");
@ -358,7 +388,7 @@ public class ResolveMergerTest extends RepositoryTestCase {
writeTrashFile("0/0", "modified"); writeTrashFile("0/0", "modified");
git.add().addFilepattern("0/0").call(); git.add().addFilepattern("0/0").call();
try { try {
git.merge().include(masterCommit).call(); git.merge().setStrategy(strategy).include(masterCommit).call();
Assert.fail("Didn't get the expected exception"); Assert.fail("Didn't get the expected exception");
} catch (CheckoutConflictException e) { } catch (CheckoutConflictException e) {
assertEquals(1, e.getConflictingPaths().size()); assertEquals(1, e.getConflictingPaths().size());
@ -366,8 +396,9 @@ public class ResolveMergerTest extends RepositoryTestCase {
} }
} }
@Test @Theory
public void checkLockedFilesToBeDeleted() throws Exception { public void checkLockedFilesToBeDeleted(MergeStrategy strategy)
throws Exception {
Git git = Git.wrap(db); Git git = Git.wrap(db);
writeTrashFile("a.txt", "orig"); writeTrashFile("a.txt", "orig");
@ -392,7 +423,8 @@ public class ResolveMergerTest extends RepositoryTestCase {
// Get a handle to the the file so on windows it can't be deleted. // Get a handle to the the file so on windows it can't be deleted.
FileInputStream fis = new FileInputStream(new File(db.getWorkTree(), FileInputStream fis = new FileInputStream(new File(db.getWorkTree(),
"b.txt")); "b.txt"));
MergeResult mergeRes = git.merge().include(masterCommit).call(); MergeResult mergeRes = git.merge().setStrategy(strategy)
.include(masterCommit).call();
if (mergeRes.getMergeStatus().equals(MergeStatus.FAILED)) { if (mergeRes.getMergeStatus().equals(MergeStatus.FAILED)) {
// probably windows // probably windows
assertEquals(1, mergeRes.getFailingPaths().size()); assertEquals(1, mergeRes.getFailingPaths().size());
@ -404,8 +436,8 @@ public class ResolveMergerTest extends RepositoryTestCase {
fis.close(); fis.close();
} }
@Test @Theory
public void checkForCorrectIndex() throws Exception { public void checkForCorrectIndex(MergeStrategy strategy) throws Exception {
File f; File f;
long lastTs4, lastTsIndex; long lastTs4, lastTsIndex;
Git git = Git.wrap(db); Git git = Git.wrap(db);
@ -484,7 +516,7 @@ public class ResolveMergerTest extends RepositoryTestCase {
// merge master and side. Should only touch "0," "2" and "3" // merge master and side. Should only touch "0," "2" and "3"
fsTick(indexFile); fsTick(indexFile);
git.merge().include(masterCommit).call(); git.merge().setStrategy(strategy).include(masterCommit).call();
checkConsistentLastModified("0", "1", "2", "4"); checkConsistentLastModified("0", "1", "2", "4");
checkModificationTimeStampOrder("4", "*" + lastTs4, "<1", "<*" checkModificationTimeStampOrder("4", "*" + lastTs4, "<1", "<*"
+ lastTsIndex, "<0", "2", "3", ".git/index"); + lastTsIndex, "<0", "2", "3", ".git/index");

Loading…
Cancel
Save