diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitOnlyTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitOnlyTest.java index 336a33566..5f7434b41 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitOnlyTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitOnlyTest.java @@ -1294,10 +1294,12 @@ public class CommitOnlyTest extends RepositoryTestCase { try { final Repository repo = git.getRepository(); final ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}"); - final TreeWalk tw = TreeWalk.forPath(repo, path, - new RevWalk(repo).parseTree(headId)); - return new String(tw.getObjectReader().open(tw.getObjectId(0)) - .getBytes()); + try (RevWalk rw = new RevWalk(repo)) { + final TreeWalk tw = TreeWalk.forPath(repo, path, + rw.parseTree(headId)); + return new String(tw.getObjectReader().open(tw.getObjectId(0)) + .getBytes()); + } } catch (Exception e) { return ""; } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ConcurrentRepackTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ConcurrentRepackTest.java index 514e00f35..9d7a4822a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ConcurrentRepackTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ConcurrentRepackTest.java @@ -81,15 +81,14 @@ public class ConcurrentRepackTest extends RepositoryTestCase { public void setUp() throws Exception { WindowCacheConfig windowCacheConfig = new WindowCacheConfig(); windowCacheConfig.setPackedGitOpenFiles(1); - WindowCache.reconfigure(windowCacheConfig); + windowCacheConfig.install(); super.setUp(); } @After public void tearDown() throws Exception { super.tearDown(); - WindowCacheConfig windowCacheConfig = new WindowCacheConfig(); - WindowCache.reconfigure(windowCacheConfig); + new WindowCacheConfig().install(); } @Test @@ -206,12 +205,14 @@ public class ConcurrentRepackTest extends RepositoryTestCase { private static void whackCache() { final WindowCacheConfig config = new WindowCacheConfig(); config.setPackedGitOpenFiles(1); - WindowCache.reconfigure(config); + config.install(); } private RevObject parse(final AnyObjectId id) throws MissingObjectException, IOException { - return new RevWalk(db).parseAny(id); + try (RevWalk rw = new RevWalk(db)) { + return rw.parseAny(id); + } } private File[] pack(final Repository src, final RevObject... list) @@ -280,7 +281,6 @@ public class ConcurrentRepackTest extends RepositoryTestCase { private RevObject writeBlob(final Repository repo, final String data) throws IOException { - final RevWalk revWalk = new RevWalk(repo); final byte[] bytes = Constants.encode(data); final ObjectId id; try (ObjectInserter inserter = repo.newObjectInserter()) { @@ -293,6 +293,8 @@ public class ConcurrentRepackTest extends RepositoryTestCase { } catch (MissingObjectException e) { // Ok } - return revWalk.lookupBlob(id); + try (RevWalk revWalk = new RevWalk(repo)) { + return revWalk.lookupBlob(id); + } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileRepositoryBuilderTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileRepositoryBuilderTest.java index dca356434..deffa04b5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileRepositoryBuilderTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileRepositoryBuilderTest.java @@ -72,7 +72,6 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase { .findGitDir(d).getGitDir()); } - @SuppressWarnings("unused") @Test public void emptyRepositoryFormatVersion() throws Exception { Repository r = createWorkRepository(); @@ -81,10 +80,11 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase { ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, ""); config.save(); - new FileRepository(r.getDirectory()); + try (FileRepository repo = new FileRepository(r.getDirectory())) { + // Unused + } } - @SuppressWarnings("unused") @Test public void invalidRepositoryFormatVersion() throws Exception { Repository r = createWorkRepository(); @@ -93,15 +93,13 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase { ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber"); config.save(); - try { - new FileRepository(r.getDirectory()); + try (FileRepository repo = new FileRepository(r.getDirectory())) { fail("IllegalArgumentException not thrown"); } catch (IllegalArgumentException e) { assertNotNull(e.getMessage()); } } - @SuppressWarnings("unused") @Test public void unknownRepositoryFormatVersion() throws Exception { Repository r = createWorkRepository(); @@ -110,75 +108,75 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase { ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999); config.save(); - try { - new FileRepository(r.getDirectory()); + try (FileRepository repo = new FileRepository(r.getDirectory())) { fail("IOException not thrown"); } catch (IOException e) { assertNotNull(e.getMessage()); } } - @SuppressWarnings("resource" /* java 7 */) @Test public void absoluteGitDirRef() throws Exception { Repository repo1 = createWorkRepository(); File dir = createTempDirectory("dir"); File dotGit = new File(dir, Constants.DOT_GIT); - new FileWriter(dotGit).append( - "gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); - FileRepositoryBuilder builder = new FileRepositoryBuilder(); + try (FileWriter writer = new FileWriter(dotGit)) { + writer.append("gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); + FileRepositoryBuilder builder = new FileRepositoryBuilder(); - builder.setWorkTree(dir); - builder.setMustExist(true); - Repository repo2 = builder.build(); + builder.setWorkTree(dir); + builder.setMustExist(true); + Repository repo2 = builder.build(); - assertEquals(repo1.getDirectory().getAbsolutePath(), repo2 - .getDirectory().getAbsolutePath()); - assertEquals(dir, repo2.getWorkTree()); + assertEquals(repo1.getDirectory().getAbsolutePath(), repo2 + .getDirectory().getAbsolutePath()); + assertEquals(dir, repo2.getWorkTree()); + } } - @SuppressWarnings("resource" /* java 7 */) @Test public void relativeGitDirRef() throws Exception { Repository repo1 = createWorkRepository(); File dir = new File(repo1.getWorkTree(), "dir"); assertTrue(dir.mkdir()); File dotGit = new File(dir, Constants.DOT_GIT); - new FileWriter(dotGit).append("gitdir: ../" + Constants.DOT_GIT) - .close(); - - FileRepositoryBuilder builder = new FileRepositoryBuilder(); - builder.setWorkTree(dir); - builder.setMustExist(true); - Repository repo2 = builder.build(); - - // The tmp directory may be a symlink so the actual path - // may not - assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 - .getDirectory().getCanonicalPath()); - assertEquals(dir, repo2.getWorkTree()); + try (FileWriter writer = new FileWriter(dotGit)) { + writer.append("gitdir: ../" + Constants.DOT_GIT).close(); + + FileRepositoryBuilder builder = new FileRepositoryBuilder(); + builder.setWorkTree(dir); + builder.setMustExist(true); + Repository repo2 = builder.build(); + + // The tmp directory may be a symlink so the actual path + // may not + assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 + .getDirectory().getCanonicalPath()); + assertEquals(dir, repo2.getWorkTree()); + } } - @SuppressWarnings("resource" /* java 7 */) @Test public void scanWithGitDirRef() throws Exception { Repository repo1 = createWorkRepository(); File dir = createTempDirectory("dir"); File dotGit = new File(dir, Constants.DOT_GIT); - new FileWriter(dotGit).append( - "gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); - FileRepositoryBuilder builder = new FileRepositoryBuilder(); - - builder.setWorkTree(dir); - builder.findGitDir(dir); - assertEquals(repo1.getDirectory().getAbsolutePath(), builder - .getGitDir().getAbsolutePath()); - builder.setMustExist(true); - Repository repo2 = builder.build(); - - // The tmp directory may be a symlink - assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 - .getDirectory().getCanonicalPath()); - assertEquals(dir, repo2.getWorkTree()); + try (FileWriter writer = new FileWriter(dotGit)) { + writer.append( + "gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); + FileRepositoryBuilder builder = new FileRepositoryBuilder(); + + builder.setWorkTree(dir); + builder.findGitDir(dir); + assertEquals(repo1.getDirectory().getAbsolutePath(), builder + .getGitDir().getAbsolutePath()); + builder.setMustExist(true); + Repository repo2 = builder.build(); + + // The tmp directory may be a symlink + assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 + .getDirectory().getCanonicalPath()); + assertEquals(dir, repo2.getWorkTree()); + } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RepositorySetupWorkDirTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RepositorySetupWorkDirTest.java index 295ef45a7..84c2543c1 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RepositorySetupWorkDirTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RepositorySetupWorkDirTest.java @@ -73,13 +73,14 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { public void testIsBare_CreateRepositoryFromArbitraryGitDir() throws Exception { File gitDir = getFile("workdir"); - assertTrue(new FileRepository(gitDir).isBare()); + Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); + assertTrue(repo.isBare()); } @Test public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception { File gitDir = getFile("workdir", Constants.DOT_GIT); - Repository repo = new FileRepository(gitDir); + Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); assertFalse(repo.isBare()); assertWorkdirPath(repo, "workdir"); assertGitdirPath(repo, "workdir", Constants.DOT_GIT); @@ -89,7 +90,7 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir() throws Exception { File gitDir = getFile("workdir", Constants.DOT_GIT); - Repository repo = new FileRepository(gitDir); + Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); String workdir = repo.getWorkTree().getName(); assertEquals(workdir, "workdir"); } @@ -157,8 +158,8 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { @Test public void testExceptionThrown_BareRepoGetWorkDir() throws Exception { File gitDir = getFile("workdir"); - try { - new FileRepository(gitDir).getWorkTree(); + try (Repository repo = new FileRepository(gitDir)) { + repo.getWorkTree(); fail("Expected NoWorkTreeException missing"); } catch (NoWorkTreeException e) { // expected @@ -168,8 +169,8 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { @Test public void testExceptionThrown_BareRepoGetIndex() throws Exception { File gitDir = getFile("workdir"); - try { - new FileRepository(gitDir).readDirCache(); + try (Repository repo = new FileRepository(gitDir)) { + repo.readDirCache(); fail("Expected NoWorkTreeException missing"); } catch (NoWorkTreeException e) { // expected @@ -179,8 +180,8 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { @Test public void testExceptionThrown_BareRepoGetIndexFile() throws Exception { File gitDir = getFile("workdir"); - try { - new FileRepository(gitDir).getIndexFile(); + try (Repository repo = new FileRepository(gitDir)) { + repo.getIndexFile(); fail("Expected NoWorkTreeException missing"); } catch (NoWorkTreeException e) { // expected diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/UnpackedObjectTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/UnpackedObjectTest.java index 8c8c6c6d0..c6653bfdb 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/UnpackedObjectTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/UnpackedObjectTest.java @@ -143,7 +143,7 @@ public class UnpackedObjectTest extends LocalDiskRepositoryTestCase { public void testStandardFormat_LargeObject() throws Exception { final int type = Constants.OBJ_BLOB; byte[] data = getRng().nextBytes(streamThreshold + 5); - ObjectId id = new ObjectInserter.Formatter().idFor(type, data); + ObjectId id = getId(type, data); write(id, compressStandardFormat(type, data)); ObjectLoader ol; @@ -306,7 +306,7 @@ public class UnpackedObjectTest extends LocalDiskRepositoryTestCase { throws Exception { final int type = Constants.OBJ_BLOB; byte[] data = getRng().nextBytes(streamThreshold + 5); - ObjectId id = new ObjectInserter.Formatter().idFor(type, data); + ObjectId id = getId(type, data); byte[] gz = compressStandardFormat(type, data); gz[gz.length - 1] = 0; gz[gz.length - 2] = 0; @@ -344,7 +344,7 @@ public class UnpackedObjectTest extends LocalDiskRepositoryTestCase { throws Exception { final int type = Constants.OBJ_BLOB; byte[] data = getRng().nextBytes(streamThreshold + 5); - ObjectId id = new ObjectInserter.Formatter().idFor(type, data); + ObjectId id = getId(type, data); byte[] gz = compressStandardFormat(type, data); byte[] tr = new byte[gz.length - 1]; System.arraycopy(gz, 0, tr, 0, tr.length); @@ -379,7 +379,7 @@ public class UnpackedObjectTest extends LocalDiskRepositoryTestCase { throws Exception { final int type = Constants.OBJ_BLOB; byte[] data = getRng().nextBytes(streamThreshold + 5); - ObjectId id = new ObjectInserter.Formatter().idFor(type, data); + ObjectId id = getId(type, data); byte[] gz = compressStandardFormat(type, data); byte[] tr = new byte[gz.length + 1]; System.arraycopy(gz, 0, tr, 0, gz.length); @@ -438,7 +438,7 @@ public class UnpackedObjectTest extends LocalDiskRepositoryTestCase { public void testPackFormat_LargeObject() throws Exception { final int type = Constants.OBJ_BLOB; byte[] data = getRng().nextBytes(streamThreshold + 5); - ObjectId id = new ObjectInserter.Formatter().idFor(type, data); + ObjectId id = getId(type, data); write(id, compressPackFormat(type, data)); ObjectLoader ol; @@ -578,4 +578,10 @@ public class UnpackedObjectTest extends LocalDiskRepositoryTestCase { out.close(); } } + + private ObjectId getId(int type, byte[] data) { + try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) { + return formatter.idFor(type, data); + } + } }