From 6997c809ef8f6e89dac065d693185286d0af83f6 Mon Sep 17 00:00:00 2001 From: Carsten Hammer Date: Sun, 12 May 2019 16:24:41 +0200 Subject: Use try-with-resources Convert try finally block to try-with-resources Change-Id: I29a482e2bbfe5e9029b41c1fe7a5bf45308905c2 Signed-off-by: Carsten Hammer --- .../src/org/eclipse/egit/ui/test/TestUtil.java | 9 ++------- .../ui/test/team/actions/BranchAndResetActionTest.java | 5 +---- .../egit/ui/test/team/actions/CompareActionsTest.java | 12 ++++++------ .../SynchronizeViewGitChangeSetModelTest.java | 17 ++++++++--------- .../synchronize/SynchronizeViewWorkspaceModelTest.java | 16 ++++++++-------- 5 files changed, 25 insertions(+), 34 deletions(-) (limited to 'org.eclipse.egit.ui.test/src') diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java index 17e62768b0..a586299bc4 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java @@ -292,14 +292,9 @@ public class TestUtil { */ public static void appendFileContent(File file, String content, boolean append) throws IOException { - Writer fw = null; - try { - fw = new OutputStreamWriter(new FileOutputStream(file, append), - "UTF-8"); + try (Writer fw = new OutputStreamWriter(new FileOutputStream(file, append), + "UTF-8")) { fw.append(content); - } finally { - if (fw != null) - fw.close(); } } diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/BranchAndResetActionTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/BranchAndResetActionTest.java index 27bae49a95..980ba7663d 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/BranchAndResetActionTest.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/BranchAndResetActionTest.java @@ -202,8 +202,7 @@ public class BranchAndResetActionTest extends LocalRepositoryTestCase { "Add to stable"); op.execute(null); - InputStream is = toBeDeleted.getContents(); - try { + try (InputStream is = toBeDeleted.getContents()) { checkout(new String[] { LOCAL_BRANCHES, "master" }); final SWTBotShell showUndeleted = bot .shell(UIText.NonDeletedFilesDialog_NonDeletedFilesTitle); @@ -282,8 +281,6 @@ public class BranchAndResetActionTest extends LocalRepositoryTestCase { UIText.NonDeletedFilesDialog_RetryDeleteButton).click(); showUndeleted.bot().waitUntil(treeEmpty, 1000, 100); showUndeleted.close(); - } finally { - is.close(); } } diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CompareActionsTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CompareActionsTest.java index 6be393b081..4229466dd1 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CompareActionsTest.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CompareActionsTest.java @@ -204,12 +204,12 @@ public class CompareActionsTest extends LocalRepositoryTestCase { Ref newBranch = git.checkout().setCreateBranch(true) .setStartPoint(commitOfTag.name()).setName("toMerge") .call(); - ByteArrayInputStream bis = new ByteArrayInputStream( - "Modified".getBytes("UTF-8")); - ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1) - .getFolder(FOLDER).getFile(FILE2) - .setContents(bis, false, false, null); - bis.close(); + try (ByteArrayInputStream bis = new ByteArrayInputStream( + "Modified".getBytes("UTF-8"))) { + ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1) + .getFolder(FOLDER).getFile(FILE2) + .setContents(bis, false, false, null); + } git.commit().setAll(true).setMessage("To be merged").call(); git.merge().include(masterId).call(); String menuLabel = util diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewGitChangeSetModelTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewGitChangeSetModelTest.java index 6afb055d1f..c59708f2d2 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewGitChangeSetModelTest.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewGitChangeSetModelTest.java @@ -256,11 +256,10 @@ public class SynchronizeViewGitChangeSetModelTest extends String name = "non-workspace.txt"; File root = new File(getTestDirectory(), REPO1); File nonWorkspace = new File(root, name); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( - Files.newOutputStream(nonWorkspace.toPath()), "UTF-8")); - - writer.append("file content"); - writer.close(); + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"))) { + writer.append("file content"); + } // TODO Synchronize currently shows "No changes" when the only thing that has // changed is a non-workspace file, so add another change so that this // does not happen. When the bug is fixed, this should be removed. @@ -282,10 +281,10 @@ public class SynchronizeViewGitChangeSetModelTest extends String name = "non-workspace.txt"; File root = new File(getTestDirectory(), REPO1); File nonWorkspace = new File(root, name); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( - Files.newOutputStream(nonWorkspace.toPath()), "UTF-8")); - writer.append(content); - writer.close(); + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"))) { + writer.append(content); + } // TODO Synchronize currently shows "No changes" when the only thing that has // changed is a non-workspace file, so add another change so that this // does not happen. When the bug is fixed, this should be removed. diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewWorkspaceModelTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewWorkspaceModelTest.java index 0d247919a5..4b2cbb8145 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewWorkspaceModelTest.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewWorkspaceModelTest.java @@ -296,10 +296,10 @@ public class SynchronizeViewWorkspaceModelTest extends AbstractSynchronizeViewTe String name = "non-workspace.txt"; File root = new File(getTestDirectory(), REPO1); File nonWorkspace = new File(root, name); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( - Files.newOutputStream(nonWorkspace.toPath()), "UTF-8")); - writer.append("file content"); - writer.close(); + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"))) { + writer.append("file content"); + } // when launchSynchronization(INITIAL_TAG, HEAD, true); @@ -319,10 +319,10 @@ public class SynchronizeViewWorkspaceModelTest extends AbstractSynchronizeViewTe String name = "non-workspace.txt"; File root = new File(getTestDirectory(), REPO1); File nonWorkspace = new File(root, name); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( - Files.newOutputStream(nonWorkspace.toPath()), "UTF-8")); - writer.append(content); - writer.close(); + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"))) { + writer.append(content); + } // when launchSynchronization(INITIAL_TAG, HEAD, true); -- cgit v1.2.3