From: David Pursehouse Date: Mon, 25 Jan 2016 02:48:12 +0000 (+0900) Subject: DiffCommandTest: Open Git and RevWalk in try-with-resource X-Git-Tag: v4.3.0.201603230630-rc1~37^2~6 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6b662403113c5c696ad46d8afeaa604a0dd3e7f8;p=jgit.git DiffCommandTest: Open Git and RevWalk in try-with-resource Change-Id: I966f7e5053651661abc7de63c968e1f5bf2de464 Signed-off-by: David Pursehouse --- diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java index 4ad01cf6f0..4f5b50dcc1 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java +++ b/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"); folder.mkdir(); write(new File(folder, "folder.txt"), "folder"); - Git git = new Git(db); - git.add().addFilepattern(".").call(); - git.commit().setMessage("Initial commit").call(); - write(new File(folder, "folder.txt"), "folder change"); - - OutputStream out = new ByteArrayOutputStream(); - List entries = git.diff().setOutputStream(out).call(); - assertEquals(1, entries.size()); - assertEquals(ChangeType.MODIFY, entries.get(0) - .getChangeType()); - assertEquals("folder/folder.txt", entries.get(0) - .getOldPath()); - assertEquals("folder/folder.txt", entries.get(0) - .getNewPath()); - - String actual = out.toString(); - String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" - + "index 0119635..95c4c65 100644\n" - + "--- a/folder/folder.txt\n" - + "+++ b/folder/folder.txt\n" - + "@@ -1 +1 @@\n" - + "-folder\n" - + "\\ No newline at end of file\n" - + "+folder change\n" - + "\\ No newline at end of file\n"; - assertEquals(expected.toString(), actual); + try (Git git = new Git(db)) { + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(folder, "folder.txt"), "folder change"); + + OutputStream out = new ByteArrayOutputStream(); + List entries = git.diff().setOutputStream(out).call(); + assertEquals(1, entries.size()); + assertEquals(ChangeType.MODIFY, entries.get(0) + .getChangeType()); + assertEquals("folder/folder.txt", entries.get(0) + .getOldPath()); + assertEquals("folder/folder.txt", entries.get(0) + .getNewPath()); + + String actual = out.toString(); + String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" + + "index 0119635..95c4c65 100644\n" + + "--- a/folder/folder.txt\n" + + "+++ b/folder/folder.txt\n" + + "@@ -1 +1 @@\n" + + "-folder\n" + + "\\ No newline at end of file\n" + + "+folder change\n" + + "\\ No newline at end of file\n"; + assertEquals(expected.toString(), actual); + } } @Test @@ -103,33 +104,34 @@ public class DiffCommandTest extends RepositoryTestCase { write(new File(db.getWorkTree(), "test.txt"), "test"); File folder = new File(db.getWorkTree(), "folder"); folder.mkdir(); - Git git = new Git(db); - git.add().addFilepattern(".").call(); - git.commit().setMessage("Initial commit").call(); - write(new File(folder, "folder.txt"), "folder"); - git.add().addFilepattern(".").call(); - - OutputStream out = new ByteArrayOutputStream(); - List entries = git.diff().setOutputStream(out) - .setCached(true).call(); - assertEquals(1, entries.size()); - assertEquals(ChangeType.ADD, entries.get(0) - .getChangeType()); - assertEquals("/dev/null", entries.get(0) - .getOldPath()); - assertEquals("folder/folder.txt", entries.get(0) - .getNewPath()); - - String actual = out.toString(); - String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" - + "new file mode 100644\n" - + "index 0000000..0119635\n" - + "--- /dev/null\n" - + "+++ b/folder/folder.txt\n" - + "@@ -0,0 +1 @@\n" - + "+folder\n" - + "\\ No newline at end of file\n"; - assertEquals(expected.toString(), actual); + try (Git git = new Git(db)) { + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(folder, "folder.txt"), "folder"); + git.add().addFilepattern(".").call(); + + OutputStream out = new ByteArrayOutputStream(); + List entries = git.diff().setOutputStream(out) + .setCached(true).call(); + assertEquals(1, entries.size()); + assertEquals(ChangeType.ADD, entries.get(0) + .getChangeType()); + assertEquals("/dev/null", entries.get(0) + .getOldPath()); + assertEquals("folder/folder.txt", entries.get(0) + .getNewPath()); + + String actual = out.toString(); + String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" + + "new file mode 100644\n" + + "index 0000000..0119635\n" + + "--- /dev/null\n" + + "+++ b/folder/folder.txt\n" + + "@@ -0,0 +1 @@\n" + + "+folder\n" + + "\\ No newline at end of file\n"; + assertEquals(expected.toString(), actual); + } } @Test @@ -138,107 +140,109 @@ public class DiffCommandTest extends RepositoryTestCase { File folder = new File(db.getWorkTree(), "folder"); folder.mkdir(); write(new File(folder, "folder.txt"), "folder"); - Git git = new Git(db); - git.add().addFilepattern(".").call(); - git.commit().setMessage("Initial commit").call(); - write(new File(folder, "folder.txt"), "folder change"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("second commit").call(); - write(new File(folder, "folder.txt"), "second folder change"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("third commit").call(); - - // bad filter - DiffCommand diff = git.diff().setShowNameAndStatusOnly(true) - .setPathFilter(PathFilter.create("test.txt")) - .setOldTree(getTreeIterator("HEAD^^")) - .setNewTree(getTreeIterator("HEAD^")); - List entries = diff.call(); - assertEquals(0, entries.size()); - - // no filter, two commits - OutputStream out = new ByteArrayOutputStream(); - diff = git.diff().setOutputStream(out) - .setOldTree(getTreeIterator("HEAD^^")) - .setNewTree(getTreeIterator("HEAD^")); - entries = diff.call(); - assertEquals(1, entries.size()); - assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType()); - assertEquals("folder/folder.txt", entries.get(0).getOldPath()); - assertEquals("folder/folder.txt", entries.get(0).getNewPath()); - - String actual = out.toString(); - String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" - + "index 0119635..95c4c65 100644\n" - + "--- a/folder/folder.txt\n" - + "+++ b/folder/folder.txt\n" - + "@@ -1 +1 @@\n" - + "-folder\n" - + "\\ No newline at end of file\n" - + "+folder change\n" - + "\\ No newline at end of file\n"; - assertEquals(expected.toString(), actual); + try (Git git = new Git(db)) { + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(folder, "folder.txt"), "folder change"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("second commit").call(); + write(new File(folder, "folder.txt"), "second folder change"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("third commit").call(); + + // bad filter + DiffCommand diff = git.diff().setShowNameAndStatusOnly(true) + .setPathFilter(PathFilter.create("test.txt")) + .setOldTree(getTreeIterator("HEAD^^")) + .setNewTree(getTreeIterator("HEAD^")); + List entries = diff.call(); + assertEquals(0, entries.size()); + + // no filter, two commits + OutputStream out = new ByteArrayOutputStream(); + diff = git.diff().setOutputStream(out) + .setOldTree(getTreeIterator("HEAD^^")) + .setNewTree(getTreeIterator("HEAD^")); + entries = diff.call(); + assertEquals(1, entries.size()); + assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType()); + assertEquals("folder/folder.txt", entries.get(0).getOldPath()); + assertEquals("folder/folder.txt", entries.get(0).getNewPath()); + + String actual = out.toString(); + String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" + + "index 0119635..95c4c65 100644\n" + + "--- a/folder/folder.txt\n" + + "+++ b/folder/folder.txt\n" + + "@@ -1 +1 @@\n" + + "-folder\n" + + "\\ No newline at end of file\n" + + "+folder change\n" + + "\\ No newline at end of file\n"; + assertEquals(expected.toString(), actual); + } } @Test public void testDiffWithPrefixes() throws Exception { write(new File(db.getWorkTree(), "test.txt"), "test"); - Git git = new Git(db); - git.add().addFilepattern(".").call(); - git.commit().setMessage("Initial commit").call(); - write(new File(db.getWorkTree(), "test.txt"), "test change"); - - OutputStream out = new ByteArrayOutputStream(); - git.diff().setOutputStream(out).setSourcePrefix("old/") - .setDestinationPrefix("new/") - .call(); - - String actual = out.toString(); - String expected = "diff --git old/test.txt new/test.txt\n" - + "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\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"; - assertEquals(expected.toString(), actual); + try (Git git = new Git(db)) { + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(db.getWorkTree(), "test.txt"), "test change"); + + OutputStream out = new ByteArrayOutputStream(); + git.diff().setOutputStream(out).setSourcePrefix("old/") + .setDestinationPrefix("new/").call(); + + String actual = out.toString(); + String expected = "diff --git old/test.txt new/test.txt\n" + + "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\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"; + assertEquals(expected.toString(), actual); + } } @Test public void testDiffWithNegativeLineCount() throws Exception { write(new File(db.getWorkTree(), "test.txt"), "0\n1\n2\n3\n4\n5\n6\n7\n8\n9"); - Git git = new Git(db); - git.add().addFilepattern(".").call(); - git.commit().setMessage("Initial commit").call(); - write(new File(db.getWorkTree(), "test.txt"), - "0\n1\n2\n3\n4a\n5\n6\n7\n8\n9"); - - OutputStream out = new ByteArrayOutputStream(); - git.diff().setOutputStream(out).setContextLines(1) - .call(); - - String actual = out.toString(); - String expected = "diff --git a/test.txt b/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" - + "+4a\n" + " 5\n"; - assertEquals(expected.toString(), actual); + try (Git git = new Git(db)) { + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(db.getWorkTree(), "test.txt"), + "0\n1\n2\n3\n4a\n5\n6\n7\n8\n9"); + + OutputStream out = new ByteArrayOutputStream(); + git.diff().setOutputStream(out).setContextLines(1).call(); + + String actual = out.toString(); + String expected = "diff --git a/test.txt b/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" + + "+4a\n" + " 5\n"; + assertEquals(expected.toString(), actual); + } } @Test public void testNoOutputStreamSet() throws Exception { File file = writeTrashFile("test.txt", "a"); assertTrue(file.setLastModified(file.lastModified() - 5000)); - Git git = new Git(db); - git.add().addFilepattern(".").call(); - write(file, "b"); - - List diffs = git.diff().call(); - assertNotNull(diffs); - assertEquals(1, diffs.size()); - DiffEntry diff = diffs.get(0); - assertEquals(ChangeType.MODIFY, diff.getChangeType()); - assertEquals("test.txt", diff.getOldPath()); - assertEquals("test.txt", diff.getNewPath()); + try (Git git = new Git(db)) { + git.add().addFilepattern(".").call(); + write(file, "b"); + + List diffs = git.diff().call(); + assertNotNull(diffs); + assertEquals(1, diffs.size()); + DiffEntry diff = diffs.get(0); + assertEquals(ChangeType.MODIFY, diff.getChangeType()); + assertEquals("test.txt", diff.getOldPath()); + assertEquals("test.txt", diff.getNewPath()); + } } private AbstractTreeIterator getTreeIterator(String name) @@ -247,8 +251,9 @@ public class DiffCommandTest extends RepositoryTestCase { if (id == null) throw new IllegalArgumentException(name); final CanonicalTreeParser p = new CanonicalTreeParser(); - try (ObjectReader or = db.newObjectReader()) { - p.reset(or, new RevWalk(db).parseTree(id)); + try (ObjectReader or = db.newObjectReader(); + RevWalk rw = new RevWalk(db)) { + p.reset(or, rw.parseTree(id)); return p; } }