Browse Source

DiffCommandTest: Open Git and RevWalk in try-with-resource

Change-Id: I966f7e5053651661abc7de63c968e1f5bf2de464
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.2
David Pursehouse 9 years ago
parent
commit
6b66240311
  1. 283
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java

283
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java

@ -70,32 +70,33 @@ public class DiffCommandTest extends RepositoryTestCase {
File folder = new File(db.getWorkTree(), "folder"); File folder = new File(db.getWorkTree(), "folder");
folder.mkdir(); folder.mkdir();
write(new File(folder, "folder.txt"), "folder"); write(new File(folder, "folder.txt"), "folder");
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change"); write(new File(folder, "folder.txt"), "folder change");
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
List<DiffEntry> entries = git.diff().setOutputStream(out).call(); List<DiffEntry> entries = git.diff().setOutputStream(out).call();
assertEquals(1, entries.size()); assertEquals(1, entries.size());
assertEquals(ChangeType.MODIFY, entries.get(0) assertEquals(ChangeType.MODIFY, entries.get(0)
.getChangeType()); .getChangeType());
assertEquals("folder/folder.txt", entries.get(0) assertEquals("folder/folder.txt", entries.get(0)
.getOldPath()); .getOldPath());
assertEquals("folder/folder.txt", entries.get(0) assertEquals("folder/folder.txt", entries.get(0)
.getNewPath()); .getNewPath());
String actual = out.toString(); String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "index 0119635..95c4c65 100644\n" + "index 0119635..95c4c65 100644\n"
+ "--- a/folder/folder.txt\n" + "--- a/folder/folder.txt\n"
+ "+++ b/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
+ "@@ -1 +1 @@\n" + "@@ -1 +1 @@\n"
+ "-folder\n" + "-folder\n"
+ "\\ No newline at end of file\n" + "\\ No newline at end of file\n"
+ "+folder change\n" + "+folder change\n"
+ "\\ No newline at end of file\n"; + "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual); assertEquals(expected.toString(), actual);
}
} }
@Test @Test
@ -103,33 +104,34 @@ public class DiffCommandTest extends RepositoryTestCase {
write(new File(db.getWorkTree(), "test.txt"), "test"); write(new File(db.getWorkTree(), "test.txt"), "test");
File folder = new File(db.getWorkTree(), "folder"); File folder = new File(db.getWorkTree(), "folder");
folder.mkdir(); folder.mkdir();
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder"); write(new File(folder, "folder.txt"), "folder");
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
List<DiffEntry> entries = git.diff().setOutputStream(out) List<DiffEntry> entries = git.diff().setOutputStream(out)
.setCached(true).call(); .setCached(true).call();
assertEquals(1, entries.size()); assertEquals(1, entries.size());
assertEquals(ChangeType.ADD, entries.get(0) assertEquals(ChangeType.ADD, entries.get(0)
.getChangeType()); .getChangeType());
assertEquals("/dev/null", entries.get(0) assertEquals("/dev/null", entries.get(0)
.getOldPath()); .getOldPath());
assertEquals("folder/folder.txt", entries.get(0) assertEquals("folder/folder.txt", entries.get(0)
.getNewPath()); .getNewPath());
String actual = out.toString(); String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "new file mode 100644\n" + "new file mode 100644\n"
+ "index 0000000..0119635\n" + "index 0000000..0119635\n"
+ "--- /dev/null\n" + "--- /dev/null\n"
+ "+++ b/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
+ "@@ -0,0 +1 @@\n" + "@@ -0,0 +1 @@\n"
+ "+folder\n" + "+folder\n"
+ "\\ No newline at end of file\n"; + "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual); assertEquals(expected.toString(), actual);
}
} }
@Test @Test
@ -138,107 +140,109 @@ public class DiffCommandTest extends RepositoryTestCase {
File folder = new File(db.getWorkTree(), "folder"); File folder = new File(db.getWorkTree(), "folder");
folder.mkdir(); folder.mkdir();
write(new File(folder, "folder.txt"), "folder"); write(new File(folder, "folder.txt"), "folder");
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change"); write(new File(folder, "folder.txt"), "folder change");
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("second commit").call(); git.commit().setMessage("second commit").call();
write(new File(folder, "folder.txt"), "second folder change"); write(new File(folder, "folder.txt"), "second folder change");
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("third commit").call(); git.commit().setMessage("third commit").call();
// bad filter // bad filter
DiffCommand diff = git.diff().setShowNameAndStatusOnly(true) DiffCommand diff = git.diff().setShowNameAndStatusOnly(true)
.setPathFilter(PathFilter.create("test.txt")) .setPathFilter(PathFilter.create("test.txt"))
.setOldTree(getTreeIterator("HEAD^^")) .setOldTree(getTreeIterator("HEAD^^"))
.setNewTree(getTreeIterator("HEAD^")); .setNewTree(getTreeIterator("HEAD^"));
List<DiffEntry> entries = diff.call(); List<DiffEntry> entries = diff.call();
assertEquals(0, entries.size()); assertEquals(0, entries.size());
// no filter, two commits // no filter, two commits
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
diff = git.diff().setOutputStream(out) diff = git.diff().setOutputStream(out)
.setOldTree(getTreeIterator("HEAD^^")) .setOldTree(getTreeIterator("HEAD^^"))
.setNewTree(getTreeIterator("HEAD^")); .setNewTree(getTreeIterator("HEAD^"));
entries = diff.call(); entries = diff.call();
assertEquals(1, entries.size()); assertEquals(1, entries.size());
assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType()); assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType());
assertEquals("folder/folder.txt", entries.get(0).getOldPath()); assertEquals("folder/folder.txt", entries.get(0).getOldPath());
assertEquals("folder/folder.txt", entries.get(0).getNewPath()); assertEquals("folder/folder.txt", entries.get(0).getNewPath());
String actual = out.toString(); String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "index 0119635..95c4c65 100644\n" + "index 0119635..95c4c65 100644\n"
+ "--- a/folder/folder.txt\n" + "--- a/folder/folder.txt\n"
+ "+++ b/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
+ "@@ -1 +1 @@\n" + "@@ -1 +1 @@\n"
+ "-folder\n" + "-folder\n"
+ "\\ No newline at end of file\n" + "\\ No newline at end of file\n"
+ "+folder change\n" + "+folder change\n"
+ "\\ No newline at end of file\n"; + "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual); assertEquals(expected.toString(), actual);
}
} }
@Test @Test
public void testDiffWithPrefixes() throws Exception { public void testDiffWithPrefixes() throws Exception {
write(new File(db.getWorkTree(), "test.txt"), "test"); write(new File(db.getWorkTree(), "test.txt"), "test");
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"), "test change"); write(new File(db.getWorkTree(), "test.txt"), "test change");
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setSourcePrefix("old/") git.diff().setOutputStream(out).setSourcePrefix("old/")
.setDestinationPrefix("new/") .setDestinationPrefix("new/").call();
.call();
String actual = out.toString();
String actual = out.toString(); String expected = "diff --git old/test.txt new/test.txt\n"
String expected = "diff --git old/test.txt new/test.txt\n" + "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\n"
+ "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\n" + "+++ new/test.txt\n" + "@@ -1 +1 @@\n" + "-test\n"
+ "+++ new/test.txt\n" + "@@ -1 +1 @@\n" + "-test\n" + "\\ No newline at end of file\n" + "+test change\n"
+ "\\ No newline at end of file\n" + "+test change\n" + "\\ No newline at end of file\n";
+ "\\ No newline at end of file\n"; assertEquals(expected.toString(), actual);
assertEquals(expected.toString(), actual); }
} }
@Test @Test
public void testDiffWithNegativeLineCount() throws Exception { public void testDiffWithNegativeLineCount() throws Exception {
write(new File(db.getWorkTree(), "test.txt"), write(new File(db.getWorkTree(), "test.txt"),
"0\n1\n2\n3\n4\n5\n6\n7\n8\n9"); "0\n1\n2\n3\n4\n5\n6\n7\n8\n9");
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"), write(new File(db.getWorkTree(), "test.txt"),
"0\n1\n2\n3\n4a\n5\n6\n7\n8\n9"); "0\n1\n2\n3\n4a\n5\n6\n7\n8\n9");
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setContextLines(1) git.diff().setOutputStream(out).setContextLines(1).call();
.call();
String actual = out.toString();
String actual = out.toString(); String expected = "diff --git a/test.txt b/test.txt\n"
String expected = "diff --git a/test.txt b/test.txt\n" + "index f55b5c9..c5ec8fd 100644\n" + "--- a/test.txt\n"
+ "index f55b5c9..c5ec8fd 100644\n" + "--- a/test.txt\n" + "+++ b/test.txt\n" + "@@ -4,3 +4,3 @@\n" + " 3\n" + "-4\n"
+ "+++ b/test.txt\n" + "@@ -4,3 +4,3 @@\n" + " 3\n" + "-4\n" + "+4a\n" + " 5\n";
+ "+4a\n" + " 5\n"; assertEquals(expected.toString(), actual);
assertEquals(expected.toString(), actual); }
} }
@Test @Test
public void testNoOutputStreamSet() throws Exception { public void testNoOutputStreamSet() throws Exception {
File file = writeTrashFile("test.txt", "a"); File file = writeTrashFile("test.txt", "a");
assertTrue(file.setLastModified(file.lastModified() - 5000)); assertTrue(file.setLastModified(file.lastModified() - 5000));
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
write(file, "b"); write(file, "b");
List<DiffEntry> diffs = git.diff().call(); List<DiffEntry> diffs = git.diff().call();
assertNotNull(diffs); assertNotNull(diffs);
assertEquals(1, diffs.size()); assertEquals(1, diffs.size());
DiffEntry diff = diffs.get(0); DiffEntry diff = diffs.get(0);
assertEquals(ChangeType.MODIFY, diff.getChangeType()); assertEquals(ChangeType.MODIFY, diff.getChangeType());
assertEquals("test.txt", diff.getOldPath()); assertEquals("test.txt", diff.getOldPath());
assertEquals("test.txt", diff.getNewPath()); assertEquals("test.txt", diff.getNewPath());
}
} }
private AbstractTreeIterator getTreeIterator(String name) private AbstractTreeIterator getTreeIterator(String name)
@ -247,8 +251,9 @@ public class DiffCommandTest extends RepositoryTestCase {
if (id == null) if (id == null)
throw new IllegalArgumentException(name); throw new IllegalArgumentException(name);
final CanonicalTreeParser p = new CanonicalTreeParser(); final CanonicalTreeParser p = new CanonicalTreeParser();
try (ObjectReader or = db.newObjectReader()) { try (ObjectReader or = db.newObjectReader();
p.reset(or, new RevWalk(db).parseTree(id)); RevWalk rw = new RevWalk(db)) {
p.reset(or, rw.parseTree(id));
return p; return p;
} }
} }

Loading…
Cancel
Save