From f666cc755b35a8f57a7c208c88230f7ab7c1d07b Mon Sep 17 00:00:00 2001 From: Jeff Schumacher Date: Mon, 12 Jul 2010 12:31:18 -0700 Subject: [PATCH] Added more test cases for RenameDetector I added test cases to cover the majority of the code. It's not 100% coverage yet, but the remaining bits are small. Change-Id: Ib534c8e94b13358b8b22cf54e2ff84132bae6d14 --- .../eclipse/jgit/diff/RenameDetectorTest.java | 117 ++++++++++++++++-- 1 file changed, 108 insertions(+), 9 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RenameDetectorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RenameDetectorTest.java index fe0c565d6..86b40e2f4 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RenameDetectorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RenameDetectorTest.java @@ -54,6 +54,7 @@ import org.eclipse.jgit.lib.RepositoryTestCase; public class RenameDetectorTest extends RepositoryTestCase { private static final String PATH_A = "src/A"; + private static final String PATH_B = "src/B"; private static final String PATH_H = "src/H"; private static final String PATH_Q = "src/Q"; @@ -109,8 +110,8 @@ public class RenameDetectorTest extends RepositoryTestCase { DiffEntry a = DiffEntry.add(PATH_A, foo); DiffEntry b = DiffEntry.delete(PATH_Q, foo); - DiffEntry c = DiffEntry.add("README", bar); - DiffEntry d = DiffEntry.delete("REEDME", bar); + DiffEntry c = DiffEntry.add(PATH_H, bar); + DiffEntry d = DiffEntry.delete(PATH_B, bar); rd.add(a); rd.add(b); @@ -119,8 +120,30 @@ public class RenameDetectorTest extends RepositoryTestCase { List entries = rd.compute(); assertEquals(2, entries.size()); - assertRename(d, c, 100, entries.get(0)); - assertRename(b, a, 100, entries.get(1)); + assertRename(b, a, 100, entries.get(0)); + assertRename(d, c, 100, entries.get(1)); + } + + public void testExactRename_MultipleIdenticalDeletes() throws Exception { + ObjectId foo = blob("foo"); + + DiffEntry a = DiffEntry.delete(PATH_A, foo); + DiffEntry b = DiffEntry.delete(PATH_B, foo); + + DiffEntry c = DiffEntry.delete(PATH_H, foo); + DiffEntry d = DiffEntry.add(PATH_Q, foo); + + rd.add(a); + rd.add(b); + rd.add(c); + rd.add(d); + + // Pairs the add with the first delete added + List entries = rd.compute(); + assertEquals(3, entries.size()); + assertEquals(b, entries.get(0)); + assertEquals(c, entries.get(1)); + assertRename(a, d, 100, entries.get(2)); } public void testInexactRename_OnePair() throws Exception { @@ -146,8 +169,8 @@ public class RenameDetectorTest extends RepositoryTestCase { ObjectId cId = blob("some\nsort\nof\ntext\n"); ObjectId dId = blob("completely\nunrelated\ntext\n"); - DiffEntry c = DiffEntry.add("c", cId); - DiffEntry d = DiffEntry.delete("d", dId); + DiffEntry c = DiffEntry.add(PATH_B, cId); + DiffEntry d = DiffEntry.delete(PATH_H, dId); rd.add(a); rd.add(b); @@ -156,9 +179,9 @@ public class RenameDetectorTest extends RepositoryTestCase { List entries = rd.compute(); assertEquals(3, entries.size()); - assertSame(c, entries.get(0)); - assertSame(d, entries.get(1)); - assertRename(b, a, 66, entries.get(2)); + assertRename(b, a, 66, entries.get(0)); + assertSame(c, entries.get(1)); + assertSame(d, entries.get(2)); } public void testInexactRename_LastByteDifferent() throws Exception { @@ -176,6 +199,21 @@ public class RenameDetectorTest extends RepositoryTestCase { assertRename(b, a, 88, entries.get(0)); } + public void testInexactRename_NewlinesOnly() throws Exception { + ObjectId aId = blob("\n\n\n"); + ObjectId bId = blob("\n\n\n\n"); + + DiffEntry a = DiffEntry.add(PATH_A, aId); + DiffEntry b = DiffEntry.delete(PATH_Q, bId); + + rd.add(a); + rd.add(b); + + List entries = rd.compute(); + assertEquals(1, entries.size()); + assertRename(b, a, 74, entries.get(0)); + } + public void testInexactRenames_OnePair2() throws Exception { ObjectId aId = blob("ab\nab\nab\nac\nad\nae\n"); ObjectId bId = blob("ac\nab\nab\nab\naa\na0\na1\n"); @@ -267,6 +305,67 @@ public class RenameDetectorTest extends RepositoryTestCase { assertSame(b, entries.get(1)); } + public void testNoRenames_SymlinkAndFileSamePath() throws Exception { + ObjectId aId = blob("src/dest"); + + DiffEntry a = DiffEntry.delete(PATH_A, aId); + DiffEntry b = DiffEntry.add(PATH_A, aId); + a.oldMode = FileMode.SYMLINK; + + rd.add(a); + rd.add(b); + + // Deletes should be first + List entries = rd.compute(); + assertEquals(2, entries.size()); + assertSame(a, entries.get(0)); + assertSame(b, entries.get(1)); + } + + public void testSetRenameScore_IllegalArgs() throws Exception { + try { + rd.setRenameScore(-1); + fail(); + } catch (IllegalArgumentException e) { + // pass + } + + try { + rd.setRenameScore(101); + fail(); + } catch (IllegalArgumentException e) { + // pass + } + } + + public void testRenameLimit() throws Exception { + ObjectId aId = blob("foo\nbar\nbaz\nblarg\n"); + ObjectId bId = blob("foo\nbar\nbaz\nblah\n"); + DiffEntry a = DiffEntry.add(PATH_A, aId); + DiffEntry b = DiffEntry.delete(PATH_B, bId); + + ObjectId cId = blob("a\nb\nc\nd\n"); + ObjectId dId = blob("a\nb\nc\n"); + DiffEntry c = DiffEntry.add(PATH_H, cId); + DiffEntry d = DiffEntry.delete(PATH_Q, dId); + + rd.add(a); + rd.add(b); + rd.add(c); + rd.add(d); + + rd.setRenameLimit(1); + + assertTrue(rd.isOverRenameLimit()); + + List entries = rd.compute(); + assertEquals(4, entries.size()); + assertSame(a, entries.get(0)); + assertSame(b, entries.get(1)); + assertSame(c, entries.get(2)); + assertSame(d, entries.get(3)); + } + private ObjectId blob(String content) throws Exception { return testDb.blob(content).copy(); }