From 30c9ec88d18a50b543c5c5d94c6f5f956d538773 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Mon, 15 Feb 2016 17:12:29 +0900 Subject: [PATCH] DirCacheIteratorTest: Open TreeWalk instances in try-with-resource Change-Id: If23597acaebf2295b85411bf87bc0292d5dc789e Signed-off-by: David Pursehouse --- .../jgit/dircache/DirCacheIteratorTest.java | 181 +++++++++--------- 1 file changed, 94 insertions(+), 87 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java index af1c8a356..dd242e5a1 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java @@ -76,9 +76,10 @@ public class DirCacheIteratorTest extends RepositoryTestCase { final DirCache dc = DirCache.newInCore(); assertEquals(0, dc.getEntryCount()); - final TreeWalk tw = new TreeWalk(db); - tw.addTree(new DirCacheIterator(dc)); - assertFalse(tw.next()); + try (final TreeWalk tw = new TreeWalk(db)) { + tw.addTree(new DirCacheIterator(dc)); + assertFalse(tw.next()); + } } @Test @@ -125,19 +126,20 @@ public class DirCacheIteratorTest extends RepositoryTestCase { b.finish(); final DirCacheIterator i = new DirCacheIterator(dc); - final TreeWalk tw = new TreeWalk(db); - tw.addTree(i); - int pathIdx = 0; - while (tw.next()) { - assertSame(i, tw.getTree(0, DirCacheIterator.class)); - assertEquals(pathIdx, i.ptr); - assertSame(ents[pathIdx], i.getDirCacheEntry()); - assertEquals(paths[pathIdx], tw.getPathString()); - assertEquals(modes[pathIdx].getBits(), tw.getRawMode(0)); - assertSame(modes[pathIdx], tw.getFileMode(0)); - pathIdx++; + try (final TreeWalk tw = new TreeWalk(db)) { + tw.addTree(i); + int pathIdx = 0; + while (tw.next()) { + assertSame(i, tw.getTree(0, DirCacheIterator.class)); + assertEquals(pathIdx, i.ptr); + assertSame(ents[pathIdx], i.getDirCacheEntry()); + assertEquals(paths[pathIdx], tw.getPathString()); + assertEquals(modes[pathIdx].getBits(), tw.getRawMode(0)); + assertSame(modes[pathIdx], tw.getFileMode(0)); + pathIdx++; + } + assertEquals(paths.length, pathIdx); } - assertEquals(paths.length, pathIdx); } @Test @@ -162,26 +164,27 @@ public class DirCacheIteratorTest extends RepositoryTestCase { final int expPos[] = { 0, -1, 4 }; final DirCacheIterator i = new DirCacheIterator(dc); - final TreeWalk tw = new TreeWalk(db); - tw.addTree(i); - tw.setRecursive(false); - int pathIdx = 0; - while (tw.next()) { - assertSame(i, tw.getTree(0, DirCacheIterator.class)); - assertEquals(expModes[pathIdx].getBits(), tw.getRawMode(0)); - assertSame(expModes[pathIdx], tw.getFileMode(0)); - assertEquals(expPaths[pathIdx], tw.getPathString()); - - if (expPos[pathIdx] >= 0) { - assertEquals(expPos[pathIdx], i.ptr); - assertSame(ents[expPos[pathIdx]], i.getDirCacheEntry()); - } else { - assertSame(FileMode.TREE, tw.getFileMode(0)); + try (final TreeWalk tw = new TreeWalk(db)) { + tw.addTree(i); + tw.setRecursive(false); + int pathIdx = 0; + while (tw.next()) { + assertSame(i, tw.getTree(0, DirCacheIterator.class)); + assertEquals(expModes[pathIdx].getBits(), tw.getRawMode(0)); + assertSame(expModes[pathIdx], tw.getFileMode(0)); + assertEquals(expPaths[pathIdx], tw.getPathString()); + + if (expPos[pathIdx] >= 0) { + assertEquals(expPos[pathIdx], i.ptr); + assertSame(ents[expPos[pathIdx]], i.getDirCacheEntry()); + } else { + assertSame(FileMode.TREE, tw.getFileMode(0)); + } + + pathIdx++; } - - pathIdx++; + assertEquals(expPaths.length, pathIdx); } - assertEquals(expPaths.length, pathIdx); } @Test @@ -202,21 +205,22 @@ public class DirCacheIteratorTest extends RepositoryTestCase { b.finish(); final DirCacheIterator i = new DirCacheIterator(dc); - final TreeWalk tw = new TreeWalk(db); - tw.addTree(i); - tw.setRecursive(true); - int pathIdx = 0; - while (tw.next()) { - final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class); - assertNotNull(c); - assertEquals(pathIdx, c.ptr); - assertSame(ents[pathIdx], c.getDirCacheEntry()); - assertEquals(paths[pathIdx], tw.getPathString()); - assertEquals(mode.getBits(), tw.getRawMode(0)); - assertSame(mode, tw.getFileMode(0)); - pathIdx++; + try (final TreeWalk tw = new TreeWalk(db)) { + tw.addTree(i); + tw.setRecursive(true); + int pathIdx = 0; + while (tw.next()) { + final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class); + assertNotNull(c); + assertEquals(pathIdx, c.ptr); + assertSame(ents[pathIdx], c.getDirCacheEntry()); + assertEquals(paths[pathIdx], tw.getPathString()); + assertEquals(mode.getBits(), tw.getRawMode(0)); + assertSame(mode, tw.getFileMode(0)); + pathIdx++; + } + assertEquals(paths.length, pathIdx); } - assertEquals(paths.length, pathIdx); } @Test @@ -236,21 +240,22 @@ public class DirCacheIteratorTest extends RepositoryTestCase { b.add(ents[i]); b.finish(); - final TreeWalk tw = new TreeWalk(db); - tw.addTree(new DirCacheIterator(dc)); - tw.setRecursive(true); - int pathIdx = 0; - while (tw.next()) { - final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class); - assertNotNull(c); - assertEquals(pathIdx, c.ptr); - assertSame(ents[pathIdx], c.getDirCacheEntry()); - assertEquals(paths[pathIdx], tw.getPathString()); - assertEquals(mode.getBits(), tw.getRawMode(0)); - assertSame(mode, tw.getFileMode(0)); - pathIdx++; + try (final TreeWalk tw = new TreeWalk(db)) { + tw.addTree(new DirCacheIterator(dc)); + tw.setRecursive(true); + int pathIdx = 0; + while (tw.next()) { + final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class); + assertNotNull(c); + assertEquals(pathIdx, c.ptr); + assertSame(ents[pathIdx], c.getDirCacheEntry()); + assertEquals(paths[pathIdx], tw.getPathString()); + assertEquals(mode.getBits(), tw.getRawMode(0)); + assertSame(mode, tw.getFileMode(0)); + pathIdx++; + } + assertEquals(paths.length, pathIdx); } - assertEquals(paths.length, pathIdx); } @Test @@ -397,22 +402,23 @@ public class DirCacheIteratorTest extends RepositoryTestCase { b.add(ents[i]); b.finish(); - final TreeWalk tw = new TreeWalk(db); - for (int victimIdx = 0; victimIdx < paths.length; victimIdx++) { - tw.reset(); - tw.addTree(new DirCacheIterator(dc)); - tw.setFilter(PathFilterGroup.createFromStrings(Collections - .singleton(paths[victimIdx]))); - tw.setRecursive(tw.getFilter().shouldBeRecursive()); - assertTrue(tw.next()); - final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class); - assertNotNull(c); - assertEquals(victimIdx, c.ptr); - assertSame(ents[victimIdx], c.getDirCacheEntry()); - assertEquals(paths[victimIdx], tw.getPathString()); - assertEquals(mode.getBits(), tw.getRawMode(0)); - assertSame(mode, tw.getFileMode(0)); - assertFalse(tw.next()); + try (final TreeWalk tw = new TreeWalk(db)) { + for (int victimIdx = 0; victimIdx < paths.length; victimIdx++) { + tw.reset(); + tw.addTree(new DirCacheIterator(dc)); + tw.setFilter(PathFilterGroup.createFromStrings(Collections + .singleton(paths[victimIdx]))); + tw.setRecursive(tw.getFilter().shouldBeRecursive()); + assertTrue(tw.next()); + final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class); + assertNotNull(c); + assertEquals(victimIdx, c.ptr); + assertSame(ents[victimIdx], c.getDirCacheEntry()); + assertEquals(paths[victimIdx], tw.getPathString()); + assertEquals(mode.getBits(), tw.getRawMode(0)); + assertSame(mode, tw.getFileMode(0)); + assertFalse(tw.next()); + } } } @@ -424,18 +430,19 @@ public class DirCacheIteratorTest extends RepositoryTestCase { final DirCache dc = DirCache.read(path, FS.DETECTED); assertEquals(2, dc.getEntryCount()); - final TreeWalk tw = new TreeWalk(db); - tw.setRecursive(true); - tw.addTree(new DirCacheIterator(dc)); + try (final TreeWalk tw = new TreeWalk(db)) { + tw.setRecursive(true); + tw.addTree(new DirCacheIterator(dc)); - assertTrue(tw.next()); - assertEquals("a/a", tw.getPathString()); - assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0)); + assertTrue(tw.next()); + assertEquals("a/a", tw.getPathString()); + assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0)); - assertTrue(tw.next()); - assertEquals("q", tw.getPathString()); - assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0)); + assertTrue(tw.next()); + assertEquals("q", tw.getPathString()); + assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0)); - assertFalse(tw.next()); + assertFalse(tw.next()); + } } }