Browse Source

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>
stable-4.2
David Pursehouse 9 years ago
parent
commit
3ee9e2ac07
  1. 94
      org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileRepositoryBuilderTest.java

94
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()); .findGitDir(d).getGitDir());
} }
@SuppressWarnings("unused")
@Test @Test
public void emptyRepositoryFormatVersion() throws Exception { public void emptyRepositoryFormatVersion() throws Exception {
Repository r = createWorkRepository(); Repository r = createWorkRepository();
@ -81,10 +80,11 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, ""); ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "");
config.save(); config.save();
new FileRepository(r.getDirectory()); try (FileRepository repo = new FileRepository(r.getDirectory())) {
// Unused
}
} }
@SuppressWarnings("unused")
@Test @Test
public void invalidRepositoryFormatVersion() throws Exception { public void invalidRepositoryFormatVersion() throws Exception {
Repository r = createWorkRepository(); Repository r = createWorkRepository();
@ -93,15 +93,13 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber"); ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber");
config.save(); config.save();
try { try (FileRepository repo = new FileRepository(r.getDirectory())) {
new FileRepository(r.getDirectory());
fail("IllegalArgumentException not thrown"); fail("IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
} }
} }
@SuppressWarnings("unused")
@Test @Test
public void unknownRepositoryFormatVersion() throws Exception { public void unknownRepositoryFormatVersion() throws Exception {
Repository r = createWorkRepository(); Repository r = createWorkRepository();
@ -110,75 +108,75 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999); ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999);
config.save(); config.save();
try { try (FileRepository repo = new FileRepository(r.getDirectory())) {
new FileRepository(r.getDirectory());
fail("IOException not thrown"); fail("IOException not thrown");
} catch (IOException e) { } catch (IOException e) {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
} }
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void absoluteGitDirRef() throws Exception { public void absoluteGitDirRef() throws Exception {
Repository repo1 = createWorkRepository(); Repository repo1 = createWorkRepository();
File dir = createTempDirectory("dir"); File dir = createTempDirectory("dir");
File dotGit = new File(dir, Constants.DOT_GIT); File dotGit = new File(dir, Constants.DOT_GIT);
new FileWriter(dotGit).append( try (FileWriter writer = new FileWriter(dotGit)) {
"gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); writer.append("gitdir: " + repo1.getDirectory().getAbsolutePath()).close();
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
builder.setWorkTree(dir); builder.setWorkTree(dir);
builder.setMustExist(true); builder.setMustExist(true);
Repository repo2 = builder.build(); Repository repo2 = builder.build();
assertEquals(repo1.getDirectory().getAbsolutePath(), repo2 assertEquals(repo1.getDirectory().getAbsolutePath(), repo2
.getDirectory().getAbsolutePath()); .getDirectory().getAbsolutePath());
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());
}
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void relativeGitDirRef() throws Exception { public void relativeGitDirRef() throws Exception {
Repository repo1 = createWorkRepository(); Repository repo1 = createWorkRepository();
File dir = new File(repo1.getWorkTree(), "dir"); File dir = new File(repo1.getWorkTree(), "dir");
assertTrue(dir.mkdir()); assertTrue(dir.mkdir());
File dotGit = new File(dir, Constants.DOT_GIT); File dotGit = new File(dir, Constants.DOT_GIT);
new FileWriter(dotGit).append("gitdir: ../" + Constants.DOT_GIT) try (FileWriter writer = new FileWriter(dotGit)) {
.close(); writer.append("gitdir: ../" + Constants.DOT_GIT).close();
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
builder.setWorkTree(dir); builder.setWorkTree(dir);
builder.setMustExist(true); builder.setMustExist(true);
Repository repo2 = builder.build(); Repository repo2 = builder.build();
// The tmp directory may be a symlink so the actual path // The tmp directory may be a symlink so the actual path
// may not // may not
assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 assertEquals(repo1.getDirectory().getCanonicalPath(), repo2
.getDirectory().getCanonicalPath()); .getDirectory().getCanonicalPath());
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());
}
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void scanWithGitDirRef() throws Exception { public void scanWithGitDirRef() throws Exception {
Repository repo1 = createWorkRepository(); Repository repo1 = createWorkRepository();
File dir = createTempDirectory("dir"); File dir = createTempDirectory("dir");
File dotGit = new File(dir, Constants.DOT_GIT); File dotGit = new File(dir, Constants.DOT_GIT);
new FileWriter(dotGit).append( try (FileWriter writer = new FileWriter(dotGit)) {
"gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); writer.append(
FileRepositoryBuilder builder = new FileRepositoryBuilder(); "gitdir: " + repo1.getDirectory().getAbsolutePath()).close();
FileRepositoryBuilder builder = new FileRepositoryBuilder();
builder.setWorkTree(dir);
builder.findGitDir(dir); builder.setWorkTree(dir);
assertEquals(repo1.getDirectory().getAbsolutePath(), builder builder.findGitDir(dir);
.getGitDir().getAbsolutePath()); assertEquals(repo1.getDirectory().getAbsolutePath(), builder
builder.setMustExist(true); .getGitDir().getAbsolutePath());
Repository repo2 = builder.build(); builder.setMustExist(true);
Repository repo2 = builder.build();
// The tmp directory may be a symlink
assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 // The tmp directory may be a symlink
.getDirectory().getCanonicalPath()); assertEquals(repo1.getDirectory().getCanonicalPath(), repo2
assertEquals(dir, repo2.getWorkTree()); .getDirectory().getCanonicalPath());
assertEquals(dir, repo2.getWorkTree());
}
} }
} }

Loading…
Cancel
Save