Browse Source

DirCacheIteratorTest: Open TreeWalk instances in try-with-resource

Change-Id: If23597acaebf2295b85411bf87bc0292d5dc789e
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.2
David Pursehouse 9 years ago
parent
commit
30c9ec88d1
  1. 181
      org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java

181
org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java vendored

@ -76,9 +76,10 @@ public class DirCacheIteratorTest extends RepositoryTestCase {
final DirCache dc = DirCache.newInCore(); final DirCache dc = DirCache.newInCore();
assertEquals(0, dc.getEntryCount()); assertEquals(0, dc.getEntryCount());
final TreeWalk tw = new TreeWalk(db); try (final TreeWalk tw = new TreeWalk(db)) {
tw.addTree(new DirCacheIterator(dc)); tw.addTree(new DirCacheIterator(dc));
assertFalse(tw.next()); assertFalse(tw.next());
}
} }
@Test @Test
@ -125,19 +126,20 @@ public class DirCacheIteratorTest extends RepositoryTestCase {
b.finish(); b.finish();
final DirCacheIterator i = new DirCacheIterator(dc); final DirCacheIterator i = new DirCacheIterator(dc);
final TreeWalk tw = new TreeWalk(db); try (final TreeWalk tw = new TreeWalk(db)) {
tw.addTree(i); tw.addTree(i);
int pathIdx = 0; int pathIdx = 0;
while (tw.next()) { while (tw.next()) {
assertSame(i, tw.getTree(0, DirCacheIterator.class)); assertSame(i, tw.getTree(0, DirCacheIterator.class));
assertEquals(pathIdx, i.ptr); assertEquals(pathIdx, i.ptr);
assertSame(ents[pathIdx], i.getDirCacheEntry()); assertSame(ents[pathIdx], i.getDirCacheEntry());
assertEquals(paths[pathIdx], tw.getPathString()); assertEquals(paths[pathIdx], tw.getPathString());
assertEquals(modes[pathIdx].getBits(), tw.getRawMode(0)); assertEquals(modes[pathIdx].getBits(), tw.getRawMode(0));
assertSame(modes[pathIdx], tw.getFileMode(0)); assertSame(modes[pathIdx], tw.getFileMode(0));
pathIdx++; pathIdx++;
}
assertEquals(paths.length, pathIdx);
} }
assertEquals(paths.length, pathIdx);
} }
@Test @Test
@ -162,26 +164,27 @@ public class DirCacheIteratorTest extends RepositoryTestCase {
final int expPos[] = { 0, -1, 4 }; final int expPos[] = { 0, -1, 4 };
final DirCacheIterator i = new DirCacheIterator(dc); final DirCacheIterator i = new DirCacheIterator(dc);
final TreeWalk tw = new TreeWalk(db); try (final TreeWalk tw = new TreeWalk(db)) {
tw.addTree(i); tw.addTree(i);
tw.setRecursive(false); tw.setRecursive(false);
int pathIdx = 0; int pathIdx = 0;
while (tw.next()) { while (tw.next()) {
assertSame(i, tw.getTree(0, DirCacheIterator.class)); assertSame(i, tw.getTree(0, DirCacheIterator.class));
assertEquals(expModes[pathIdx].getBits(), tw.getRawMode(0)); assertEquals(expModes[pathIdx].getBits(), tw.getRawMode(0));
assertSame(expModes[pathIdx], tw.getFileMode(0)); assertSame(expModes[pathIdx], tw.getFileMode(0));
assertEquals(expPaths[pathIdx], tw.getPathString()); assertEquals(expPaths[pathIdx], tw.getPathString());
if (expPos[pathIdx] >= 0) { if (expPos[pathIdx] >= 0) {
assertEquals(expPos[pathIdx], i.ptr); assertEquals(expPos[pathIdx], i.ptr);
assertSame(ents[expPos[pathIdx]], i.getDirCacheEntry()); assertSame(ents[expPos[pathIdx]], i.getDirCacheEntry());
} else { } else {
assertSame(FileMode.TREE, tw.getFileMode(0)); assertSame(FileMode.TREE, tw.getFileMode(0));
}
pathIdx++;
} }
assertEquals(expPaths.length, pathIdx);
pathIdx++;
} }
assertEquals(expPaths.length, pathIdx);
} }
@Test @Test
@ -202,21 +205,22 @@ public class DirCacheIteratorTest extends RepositoryTestCase {
b.finish(); b.finish();
final DirCacheIterator i = new DirCacheIterator(dc); final DirCacheIterator i = new DirCacheIterator(dc);
final TreeWalk tw = new TreeWalk(db); try (final TreeWalk tw = new TreeWalk(db)) {
tw.addTree(i); tw.addTree(i);
tw.setRecursive(true); tw.setRecursive(true);
int pathIdx = 0; int pathIdx = 0;
while (tw.next()) { while (tw.next()) {
final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class); final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
assertNotNull(c); assertNotNull(c);
assertEquals(pathIdx, c.ptr); assertEquals(pathIdx, c.ptr);
assertSame(ents[pathIdx], c.getDirCacheEntry()); assertSame(ents[pathIdx], c.getDirCacheEntry());
assertEquals(paths[pathIdx], tw.getPathString()); assertEquals(paths[pathIdx], tw.getPathString());
assertEquals(mode.getBits(), tw.getRawMode(0)); assertEquals(mode.getBits(), tw.getRawMode(0));
assertSame(mode, tw.getFileMode(0)); assertSame(mode, tw.getFileMode(0));
pathIdx++; pathIdx++;
}
assertEquals(paths.length, pathIdx);
} }
assertEquals(paths.length, pathIdx);
} }
@Test @Test
@ -236,21 +240,22 @@ public class DirCacheIteratorTest extends RepositoryTestCase {
b.add(ents[i]); b.add(ents[i]);
b.finish(); b.finish();
final TreeWalk tw = new TreeWalk(db); try (final TreeWalk tw = new TreeWalk(db)) {
tw.addTree(new DirCacheIterator(dc)); tw.addTree(new DirCacheIterator(dc));
tw.setRecursive(true); tw.setRecursive(true);
int pathIdx = 0; int pathIdx = 0;
while (tw.next()) { while (tw.next()) {
final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class); final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
assertNotNull(c); assertNotNull(c);
assertEquals(pathIdx, c.ptr); assertEquals(pathIdx, c.ptr);
assertSame(ents[pathIdx], c.getDirCacheEntry()); assertSame(ents[pathIdx], c.getDirCacheEntry());
assertEquals(paths[pathIdx], tw.getPathString()); assertEquals(paths[pathIdx], tw.getPathString());
assertEquals(mode.getBits(), tw.getRawMode(0)); assertEquals(mode.getBits(), tw.getRawMode(0));
assertSame(mode, tw.getFileMode(0)); assertSame(mode, tw.getFileMode(0));
pathIdx++; pathIdx++;
}
assertEquals(paths.length, pathIdx);
} }
assertEquals(paths.length, pathIdx);
} }
@Test @Test
@ -397,22 +402,23 @@ public class DirCacheIteratorTest extends RepositoryTestCase {
b.add(ents[i]); b.add(ents[i]);
b.finish(); b.finish();
final TreeWalk tw = new TreeWalk(db); try (final TreeWalk tw = new TreeWalk(db)) {
for (int victimIdx = 0; victimIdx < paths.length; victimIdx++) { for (int victimIdx = 0; victimIdx < paths.length; victimIdx++) {
tw.reset(); tw.reset();
tw.addTree(new DirCacheIterator(dc)); tw.addTree(new DirCacheIterator(dc));
tw.setFilter(PathFilterGroup.createFromStrings(Collections tw.setFilter(PathFilterGroup.createFromStrings(Collections
.singleton(paths[victimIdx]))); .singleton(paths[victimIdx])));
tw.setRecursive(tw.getFilter().shouldBeRecursive()); tw.setRecursive(tw.getFilter().shouldBeRecursive());
assertTrue(tw.next()); assertTrue(tw.next());
final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class); final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
assertNotNull(c); assertNotNull(c);
assertEquals(victimIdx, c.ptr); assertEquals(victimIdx, c.ptr);
assertSame(ents[victimIdx], c.getDirCacheEntry()); assertSame(ents[victimIdx], c.getDirCacheEntry());
assertEquals(paths[victimIdx], tw.getPathString()); assertEquals(paths[victimIdx], tw.getPathString());
assertEquals(mode.getBits(), tw.getRawMode(0)); assertEquals(mode.getBits(), tw.getRawMode(0));
assertSame(mode, tw.getFileMode(0)); assertSame(mode, tw.getFileMode(0));
assertFalse(tw.next()); assertFalse(tw.next());
}
} }
} }
@ -424,18 +430,19 @@ public class DirCacheIteratorTest extends RepositoryTestCase {
final DirCache dc = DirCache.read(path, FS.DETECTED); final DirCache dc = DirCache.read(path, FS.DETECTED);
assertEquals(2, dc.getEntryCount()); assertEquals(2, dc.getEntryCount());
final TreeWalk tw = new TreeWalk(db); try (final TreeWalk tw = new TreeWalk(db)) {
tw.setRecursive(true); tw.setRecursive(true);
tw.addTree(new DirCacheIterator(dc)); tw.addTree(new DirCacheIterator(dc));
assertTrue(tw.next()); assertTrue(tw.next());
assertEquals("a/a", tw.getPathString()); assertEquals("a/a", tw.getPathString());
assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0)); assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0));
assertTrue(tw.next()); assertTrue(tw.next());
assertEquals("q", tw.getPathString()); assertEquals("q", tw.getPathString());
assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0)); assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0));
assertFalse(tw.next()); assertFalse(tw.next());
}
} }
} }

Loading…
Cancel
Save