Browse Source

ArchiveCommandTest: Create Git instances in try-with-resource

Change-Id: Icbfd92395db85818736142fd3fb3432385e89ca9
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.2
David Pursehouse 9 years ago
parent
commit
a833fec219
  1. 174
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ArchiveCommandTest.java

174
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ArchiveCommandTest.java

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

Loading…
Cancel
Save