]> source.dussan.org Git - jgit.git/commitdiff
DiffFormatterTest: Create auto-closeable instances in try-with-resource 40/64840/1
authorDavid Pursehouse <david.pursehouse@sonymobile.com>
Thu, 21 Jan 2016 08:00:15 +0000 (17:00 +0900)
committerDavid Pursehouse <david.pursehouse@sonymobile.com>
Thu, 21 Jan 2016 08:01:25 +0000 (17:01 +0900)
Git, ByteArrayOutputStream, and DiffFormatter are auto-closeable and
should be managed in try-with-resource.

Change-Id: I83395116acb4b4f7cd4300fd69564355bc07e4bb
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java

index 24b0f8141e93ffe8a198f6890ec40816c95464b5..58fc9454d257805ba8b30d59e871bd8f35a4416c 100644 (file)
@@ -317,30 +317,31 @@ public class DiffFormatterTest extends RepositoryTestCase {
                File folder = new File(db.getDirectory().getParent(), "folder");
                FileUtils.mkdir(folder);
                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");
-
-               ByteArrayOutputStream os = new ByteArrayOutputStream();
-               DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));
-               dfmt.setRepository(db);
-               dfmt.setPathFilter(PathFilter.create("folder"));
-               DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache());
-               FileTreeIterator newTree = new FileTreeIterator(db);
-               dfmt.format(oldTree, newTree);
-               dfmt.flush();
-
-               String actual = os.toString("UTF-8");
-               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, actual);
+               try (Git git = new Git(db);
+                               ByteArrayOutputStream os = new ByteArrayOutputStream();
+                               DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os))) {
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("Initial commit").call();
+                       write(new File(folder, "folder.txt"), "folder change");
+                       dfmt.setRepository(db);
+                       dfmt.setPathFilter(PathFilter.create("folder"));
+                       DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache());
+                       FileTreeIterator newTree = new FileTreeIterator(db);
+
+                       dfmt.format(oldTree, newTree);
+                       dfmt.flush();
+
+                       String actual = os.toString("UTF-8");
+                       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, actual);
+               }
        }
 
        @Test
@@ -349,29 +350,30 @@ public class DiffFormatterTest extends RepositoryTestCase {
                File folder = new File(db.getDirectory().getParent(), "folder");
                FileUtils.mkdir(folder);
                write(new File(folder, "folder.txt"), "folder");
-               Git git = new Git(db);
-               git.add().addFilepattern(".").call();
-               RevCommit commit = git.commit().setMessage("Initial commit").call();
-               write(new File(folder, "folder.txt"), "folder change");
-
-               ByteArrayOutputStream os = new ByteArrayOutputStream();
-               DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));
-               dfmt.setRepository(db);
-               dfmt.setPathFilter(PathFilter.create("folder"));
-               dfmt.format(null, commit.getTree().getId());
-               dfmt.flush();
-
-               String actual = os.toString("UTF-8");
-               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, actual);
+               try (Git git = new Git(db);
+                               ByteArrayOutputStream os = new ByteArrayOutputStream();
+                               DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os))) {
+                       git.add().addFilepattern(".").call();
+                       RevCommit commit = git.commit().setMessage("Initial commit").call();
+                       write(new File(folder, "folder.txt"), "folder change");
+
+                       dfmt.setRepository(db);
+                       dfmt.setPathFilter(PathFilter.create("folder"));
+                       dfmt.format(null, commit.getTree().getId());
+                       dfmt.flush();
+
+                       String actual = os.toString("UTF-8");
+                       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, actual);
+               }
        }
 
        @Test
@@ -380,43 +382,45 @@ public class DiffFormatterTest extends RepositoryTestCase {
                File folder = new File(db.getDirectory().getParent(), "folder");
                FileUtils.mkdir(folder);
                write(new File(folder, "folder.txt"), "folder");
-               Git git = new Git(db);
-               git.add().addFilepattern(".").call();
-               RevCommit commit = git.commit().setMessage("Initial commit").call();
-               write(new File(folder, "folder.txt"), "folder change");
-
-               ByteArrayOutputStream os = new ByteArrayOutputStream();
-               DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));
-               dfmt.setRepository(db);
-               dfmt.setPathFilter(PathFilter.create("folder"));
-               dfmt.format(commit.getTree().getId(), null);
-               dfmt.flush();
-
-               String actual = os.toString("UTF-8");
-               String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
-                               + "deleted file mode 100644\n"
-                               + "index 0119635..0000000\n"
-                               + "--- a/folder/folder.txt\n"
-                               + "+++ /dev/null\n"
-                               + "@@ -1 +0,0 @@\n"
-                               + "-folder\n"
-                               + "\\ No newline at end of file\n";
-
-               assertEquals(expected, actual);
+               try (Git git = new Git(db);
+                               ByteArrayOutputStream os = new ByteArrayOutputStream();
+                               DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));) {
+                       git.add().addFilepattern(".").call();
+                       RevCommit commit = git.commit().setMessage("Initial commit").call();
+                       write(new File(folder, "folder.txt"), "folder change");
+
+                       dfmt.setRepository(db);
+                       dfmt.setPathFilter(PathFilter.create("folder"));
+                       dfmt.format(commit.getTree().getId(), null);
+                       dfmt.flush();
+
+                       String actual = os.toString("UTF-8");
+                       String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+                                       + "deleted file mode 100644\n"
+                                       + "index 0119635..0000000\n"
+                                       + "--- a/folder/folder.txt\n"
+                                       + "+++ /dev/null\n"
+                                       + "@@ -1 +0,0 @@\n"
+                                       + "-folder\n"
+                                       + "\\ No newline at end of file\n";
+
+                       assertEquals(expected, actual);
+               }
        }
 
        @Test
        public void testDiffNullToNull() throws Exception {
-               ByteArrayOutputStream os = new ByteArrayOutputStream();
-               DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));
-               dfmt.setRepository(db);
-               dfmt.format((AnyObjectId) null, null);
-               dfmt.flush();
+               try (ByteArrayOutputStream os = new ByteArrayOutputStream();
+                               DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os))) {
+                       dfmt.setRepository(db);
+                       dfmt.format((AnyObjectId) null, null);
+                       dfmt.flush();
 
-               String actual = os.toString("UTF-8");
-               String expected = "";
+                       String actual = os.toString("UTF-8");
+                       String expected = "";
 
-               assertEquals(expected, actual);
+                       assertEquals(expected, actual);
+               }
        }
 
        private static String makeDiffHeader(String pathA, String pathB,