Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Pursehouse2016-01-22 05:30:57 +0000
committerDavid Pursehouse2016-01-22 05:31:50 +0000
commit3ee9e2ac076733298906b92965a27a532c1cd5f5 (patch)
tree41b638f2d1ce9f22f0333dc07b4137b47ed146ad
parentddcbd22cd376754fed73ae1824b924ff5403380d (diff)
downloadjgit-3ee9e2ac076733298906b92965a27a532c1cd5f5.tar.gz
jgit-3ee9e2ac076733298906b92965a27a532c1cd5f5.tar.xz
jgit-3ee9e2ac076733298906b92965a27a532c1cd5f5.zip
FileRepositoryBuilderTest: Use try-with-resource for auto-closeables
Use try-with-resource to create instances of FileRepository and FileWriter. "resource" and "unused" warnings no longer occur, so remove the suppression annotations. Change-Id: I3ad58d4cc2d4c019cd8edda7cb401e9d9f3fb790 Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileRepositoryBuilderTest.java94
1 files changed, 46 insertions, 48 deletions
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 dca356434b..deffa04b54 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());
+ }
}
}

Back to the top