Browse Source

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

Change-Id: I6e68c4b028e0aa01964eace10307372060371df7
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.2
David Pursehouse 9 years ago
parent
commit
bebd30f364
  1. 39
      org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

39
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

@ -255,10 +255,11 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test
public void testDirCacheMatchingId() throws Exception {
File f = writeTrashFile("file", "content");
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("file", "content");
fsTick(f);
git.add().addFilepattern("file").call();
}
DirCacheEntry dce = db.readDirCache().getEntry("file");
TreeWalk tw = new TreeWalk(db);
FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
@ -282,11 +283,12 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test
public void testIsModifiedSymlinkAsFile() throws Exception {
writeTrashFile("symlink", "content");
Git git = new Git(db);
try (Git git = new Git(db)) {
db.getConfig().setString(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_SYMLINKS, "false");
git.add().addFilepattern("symlink").call();
git.commit().setMessage("commit").call();
}
// Modify previously committed DirCacheEntry and write it back to disk
DirCacheEntry dce = db.readDirCache().getEntry("symlink");
@ -305,7 +307,7 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test
public void testIsModifiedFileSmudged() throws Exception {
File f = writeTrashFile("file", "content");
Git git = new Git(db);
try (Git git = new Git(db)) {
// The idea of this test is to check the smudged handling
// Hopefully fsTick will make sure our entry gets smudged
fsTick(f);
@ -319,6 +321,7 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
// resolution, so we force the index to have the
// same timestamp as the file we look at.
db.getIndexFile().setLastModified(lastModified);
}
DirCacheEntry dce = db.readDirCache().getEntry("file");
FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
.getConfig().get(WorkingTreeOptions.KEY));
@ -334,7 +337,8 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test
public void submoduleHeadMatchesIndex() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
@ -354,7 +358,6 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
@ -364,10 +367,12 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter));
}
}
@Test
public void submoduleWithNoGitDirectory() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
@ -387,7 +392,6 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
assertTrue(submoduleRoot.mkdir());
assertTrue(new File(submoduleRoot, Constants.DOT_GIT).mkdir());
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
@ -398,10 +402,12 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
assertFalse(indexIter.idEqual(workTreeIter));
assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
}
}
@Test
public void submoduleWithNoHead() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
@ -420,7 +426,6 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
assertNotNull(Git.init().setDirectory(new File(db.getWorkTree(), path))
.call().getRepository());
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
@ -431,10 +436,12 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
assertFalse(indexIter.idEqual(workTreeIter));
assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
}
}
@Test
public void submoduleDirectoryIterator() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
@ -454,7 +461,6 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db.getWorkTree(),
db.getFS(), db.getConfig().get(WorkingTreeOptions.KEY));
@ -465,10 +471,12 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter));
}
}
@Test
public void submoduleNestedWithHeadMatchingIndex() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
@ -488,7 +496,6 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
@ -498,16 +505,17 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter));
}
}
@Test
public void idOffset() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db);
TreeWalk tw = new TreeWalk(db)) {
writeTrashFile("fileAinfsonly", "A");
File fileBinindex = writeTrashFile("fileBinindex", "B");
fsTick(fileBinindex);
git.add().addFilepattern("fileBinindex").call();
writeTrashFile("fileCinfsonly", "C");
TreeWalk tw = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
tw.addTree(indexIter);
@ -527,6 +535,7 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
"fileCinfsonly", tw);
assertFalse(tw.next());
}
}
private static void assertEntry(String sha1string, String path, TreeWalk tw)
throws MissingObjectException, IncorrectObjectTypeException,

Loading…
Cancel
Save