]> source.dussan.org Git - jgit.git/commitdiff
ArchiveCommandTest: Create Git instances in try-with-resource 24/64824/1
authorDavid Pursehouse <david.pursehouse@sonymobile.com>
Thu, 21 Jan 2016 00:45:58 +0000 (09:45 +0900)
committerDavid Pursehouse <david.pursehouse@sonymobile.com>
Thu, 21 Jan 2016 01:22:18 +0000 (10:22 +0900)
Change-Id: Icbfd92395db85818736142fd3fb3432385e89ca9
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ArchiveCommandTest.java

index 44e70885ba3c4a28566312ddba9205f1cd7bfe0e..fc8df42e2694d87d3a2a707702911234aa9f1491 100644 (file)
@@ -85,103 +85,107 @@ public class ArchiveCommandTest extends RepositoryTestCase {
 
        @Test
        public void archiveHeadAllFiles() throws IOException, GitAPIException {
-               Git git = new Git(db);
-               writeTrashFile("file_1.txt", "content_1_1");
-               git.add().addFilepattern("file_1.txt").call();
-               git.commit().setMessage("create file").call();
-
-               writeTrashFile("file_1.txt", "content_1_2");
-               writeTrashFile("file_2.txt", "content_2_2");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("updated file").call();
-
-               git.archive().setOutputStream(new MockOutputStream())
-                               .setFormat(format.SUFFIXES.get(0))
-                               .setTree(git.getRepository().resolve("HEAD")).call();
-
-               assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
-               assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath("file_1.txt"));
-               assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath("file_2.txt"));
+               try (Git git = new Git(db)) {
+                       writeTrashFile("file_1.txt", "content_1_1");
+                       git.add().addFilepattern("file_1.txt").call();
+                       git.commit().setMessage("create file").call();
+
+                       writeTrashFile("file_1.txt", "content_1_2");
+                       writeTrashFile("file_2.txt", "content_2_2");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("updated file").call();
+
+                       git.archive().setOutputStream(new MockOutputStream())
+                                       .setFormat(format.SUFFIXES.get(0))
+                                       .setTree(git.getRepository().resolve("HEAD")).call();
+
+                       assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
+                       assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath("file_1.txt"));
+                       assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath("file_2.txt"));
+               }
        }
 
        @Test
        public void archiveHeadSpecificPath() throws IOException, GitAPIException {
-               Git git = new Git(db);
-               writeTrashFile("file_1.txt", "content_1_1");
-               git.add().addFilepattern("file_1.txt").call();
-               git.commit().setMessage("create file").call();
-
-               writeTrashFile("file_1.txt", "content_1_2");
-               String expectedFilePath = "some_directory/file_2.txt";
-               writeTrashFile(expectedFilePath, "content_2_2");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("updated file").call();
-
-               git.archive().setOutputStream(new MockOutputStream())
-                               .setFormat(format.SUFFIXES.get(0))
-                               .setTree(git.getRepository().resolve("HEAD"))
-                               .setPaths(expectedFilePath).call();
-
-               assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
-               assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath));
-               assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
+               try (Git git = new Git(db)) {
+                       writeTrashFile("file_1.txt", "content_1_1");
+                       git.add().addFilepattern("file_1.txt").call();
+                       git.commit().setMessage("create file").call();
+
+                       writeTrashFile("file_1.txt", "content_1_2");
+                       String expectedFilePath = "some_directory/file_2.txt";
+                       writeTrashFile(expectedFilePath, "content_2_2");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("updated file").call();
+
+                       git.archive().setOutputStream(new MockOutputStream())
+                                       .setFormat(format.SUFFIXES.get(0))
+                                       .setTree(git.getRepository().resolve("HEAD"))
+                                       .setPaths(expectedFilePath).call();
+
+                       assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
+                       assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath));
+                       assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
+               }
        }
 
        @Test
        public void archiveByIdSpecificFile() throws IOException, GitAPIException {
-               Git git = new Git(db);
-               writeTrashFile("file_1.txt", "content_1_1");
-               git.add().addFilepattern("file_1.txt").call();
-               RevCommit first = git.commit().setMessage("create file").call();
-
-               writeTrashFile("file_1.txt", "content_1_2");
-               String expectedFilePath = "some_directory/file_2.txt";
-               writeTrashFile(expectedFilePath, "content_2_2");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("updated file").call();
-
-               Map<String, Object> options = new HashMap<>();
-               Integer opt = Integer.valueOf(42);
-               options.put("foo", opt);
-               MockOutputStream out = new MockOutputStream();
-               git.archive().setOutputStream(out)
-                               .setFormat(format.SUFFIXES.get(0))
-                               .setFormatOptions(options)
-                               .setTree(first)
-                               .setPaths("file_1.txt").call();
-
-               assertEquals(opt.intValue(), out.getFoo());
-               assertEquals(UNEXPECTED_ARCHIVE_SIZE, 1, format.size());
-               assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_1", format.getByPath("file_1.txt"));
+               try (Git git = new Git(db)) {
+                       writeTrashFile("file_1.txt", "content_1_1");
+                       git.add().addFilepattern("file_1.txt").call();
+                       RevCommit first = git.commit().setMessage("create file").call();
+
+                       writeTrashFile("file_1.txt", "content_1_2");
+                       String expectedFilePath = "some_directory/file_2.txt";
+                       writeTrashFile(expectedFilePath, "content_2_2");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("updated file").call();
+
+                       Map<String, Object> options = new HashMap<>();
+                       Integer opt = Integer.valueOf(42);
+                       options.put("foo", opt);
+                       MockOutputStream out = new MockOutputStream();
+                       git.archive().setOutputStream(out)
+                                       .setFormat(format.SUFFIXES.get(0))
+                                       .setFormatOptions(options)
+                                       .setTree(first)
+                                       .setPaths("file_1.txt").call();
+
+                       assertEquals(opt.intValue(), out.getFoo());
+                       assertEquals(UNEXPECTED_ARCHIVE_SIZE, 1, format.size());
+                       assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_1", format.getByPath("file_1.txt"));
+               }
        }
 
        @Test
        public void archiveByDirectoryPath() throws GitAPIException, IOException {
-               Git git = new Git(db);
-               writeTrashFile("file_0.txt", "content_0_1");
-               git.add().addFilepattern("file_0.txt").call();
-               git.commit().setMessage("commit_1").call();
-
-               writeTrashFile("file_0.txt", "content_0_2");
-               String expectedFilePath1 = "some_directory/file_1.txt";
-               writeTrashFile(expectedFilePath1, "content_1_2");
-               String expectedFilePath2 = "some_directory/file_2.txt";
-               writeTrashFile(expectedFilePath2, "content_2_2");
-               String expectedFilePath3 = "some_directory/nested_directory/file_3.txt";
-               writeTrashFile(expectedFilePath3, "content_3_2");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("commit_2").call();
-               git.archive().setOutputStream(new MockOutputStream())
-                               .setFormat(format.SUFFIXES.get(0))
-                               .setTree(git.getRepository().resolve("HEAD"))
-                               .setPaths("some_directory/").call();
-
-               assertEquals(UNEXPECTED_ARCHIVE_SIZE, 5, format.size());
-               assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath(expectedFilePath1));
-               assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath2));
-               assertEquals(UNEXPECTED_FILE_CONTENTS, "content_3_2", format.getByPath(expectedFilePath3));
-               assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
-               assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory/nested_directory"));
+               try (Git git = new Git(db)) {
+                       writeTrashFile("file_0.txt", "content_0_1");
+                       git.add().addFilepattern("file_0.txt").call();
+                       git.commit().setMessage("commit_1").call();
+
+                       writeTrashFile("file_0.txt", "content_0_2");
+                       String expectedFilePath1 = "some_directory/file_1.txt";
+                       writeTrashFile(expectedFilePath1, "content_1_2");
+                       String expectedFilePath2 = "some_directory/file_2.txt";
+                       writeTrashFile(expectedFilePath2, "content_2_2");
+                       String expectedFilePath3 = "some_directory/nested_directory/file_3.txt";
+                       writeTrashFile(expectedFilePath3, "content_3_2");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("commit_2").call();
+                       git.archive().setOutputStream(new MockOutputStream())
+                                       .setFormat(format.SUFFIXES.get(0))
+                                       .setTree(git.getRepository().resolve("HEAD"))
+                                       .setPaths("some_directory/").call();
+
+                       assertEquals(UNEXPECTED_ARCHIVE_SIZE, 5, format.size());
+                       assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath(expectedFilePath1));
+                       assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath2));
+                       assertEquals(UNEXPECTED_FILE_CONTENTS, "content_3_2", format.getByPath(expectedFilePath3));
+                       assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
+                       assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory/nested_directory"));
+               }
        }
 
        private class MockFormat implements ArchiveCommand.Format<MockOutputStream> {