Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn2015-05-20 23:45:20 +0000
committerMatthias Sohn2015-05-21 08:22:35 +0000
commitae24740051491a4aa5d02ef4f94ca8d3399d90f2 (patch)
treea5371f29cc7fa8b0e714cbf6f773addb365eb011 /org.eclipse.egit.ui.test
parent8b8c71f2cfbe2a44aa4e617d7b1ded714905aab3 (diff)
downloadegit-ae24740051491a4aa5d02ef4f94ca8d3399d90f2.tar.gz
egit-ae24740051491a4aa5d02ef4f94ca8d3399d90f2.tar.xz
egit-ae24740051491a4aa5d02ef4f94ca8d3399d90f2.zip
Use try-with-resource to avoid leaks with RevWalk and TreeWalk
Change-Id: I1ea48039ea38766bb07f079e5ef4569de034d577 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.egit.ui.test')
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java59
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/FetchAndMergeActionTest.java7
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/PushActionTest.java70
3 files changed, 73 insertions, 63 deletions
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 36b5c27886..1bf1e586df 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
@@ -366,14 +366,16 @@ public class TestUtil {
Set<String> expectedfiles = new HashSet<String>();
for (String path : paths)
expectedfiles.add(path);
- TreeWalk treeWalk = new TreeWalk(repository);
- treeWalk.addTree(repository.resolve("HEAD^{tree}"));
- treeWalk.setRecursive(true);
- while (treeWalk.next()) {
- String path = treeWalk.getPathString();
- if (!expectedfiles.contains(path))
- fail("Repository contains unexpected expected file " + path);
- expectedfiles.remove(path);
+ try (TreeWalk treeWalk = new TreeWalk(repository)) {
+ treeWalk.addTree(repository.resolve("HEAD^{tree}"));
+ treeWalk.setRecursive(true);
+ while (treeWalk.next()) {
+ String path = treeWalk.getPathString();
+ if (!expectedfiles.contains(path))
+ fail("Repository contains unexpected expected file "
+ + path);
+ expectedfiles.remove(path);
+ }
}
if (expectedfiles.size() > 0) {
StringBuilder message = new StringBuilder(
@@ -401,22 +403,24 @@ public class TestUtil {
public static void assertRepositoryContainsFilesWithContent(Repository repository,
String... args) throws Exception {
HashMap<String, String> expectedfiles = mkmap(args);
- TreeWalk treeWalk = new TreeWalk(repository);
- treeWalk.addTree(repository.resolve("HEAD^{tree}"));
- treeWalk.setRecursive(true);
- while (treeWalk.next()) {
- String path = treeWalk.getPathString();
- assertTrue(expectedfiles.containsKey(path));
- ObjectId objectId = treeWalk.getObjectId(0);
- byte[] expectedContent = expectedfiles.get(path).getBytes("UTF-8");
- byte[] repoContent = treeWalk.getObjectReader().open(objectId)
- .getBytes();
- if (!Arrays.equals(repoContent, expectedContent))
- fail("File " + path + " has repository content "
- + new String(repoContent, "UTF-8")
- + " instead of expected content "
- + new String(expectedContent, "UTF-8"));
- expectedfiles.remove(path);
+ try (TreeWalk treeWalk = new TreeWalk(repository)) {
+ treeWalk.addTree(repository.resolve("HEAD^{tree}"));
+ treeWalk.setRecursive(true);
+ while (treeWalk.next()) {
+ String path = treeWalk.getPathString();
+ assertTrue(expectedfiles.containsKey(path));
+ ObjectId objectId = treeWalk.getObjectId(0);
+ byte[] expectedContent = expectedfiles.get(path)
+ .getBytes("UTF-8");
+ byte[] repoContent = treeWalk.getObjectReader().open(objectId)
+ .getBytes();
+ if (!Arrays.equals(repoContent, expectedContent))
+ fail("File " + path + " has repository content "
+ + new String(repoContent, "UTF-8")
+ + " instead of expected content "
+ + new String(expectedContent, "UTF-8"));
+ expectedfiles.remove(path);
+ }
}
if (expectedfiles.size() > 0) {
StringBuilder message = new StringBuilder(
@@ -510,8 +514,11 @@ public class TestUtil {
throws Exception {
RevCommit headCommit = null;
ObjectId parentId = repository.resolve(Constants.HEAD);
- if (parentId != null)
- headCommit = new RevWalk(repository).parseCommit(parentId);
+ if (parentId != null) {
+ try (RevWalk rw = new RevWalk(repository)) {
+ headCommit = rw.parseCommit(parentId);
+ }
+ }
return headCommit;
}
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/FetchAndMergeActionTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/FetchAndMergeActionTest.java
index 1ef9507f17..22f2ce6eee 100644
--- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/FetchAndMergeActionTest.java
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/FetchAndMergeActionTest.java
@@ -140,9 +140,10 @@ public class FetchAndMergeActionTest extends LocalRepositoryTestCase {
private RevCommit getCommitForHead() throws Exception {
Repository repo = lookupRepository(repositoryFile);
- RevWalk rw = new RevWalk(repo);
- ObjectId id = repo.resolve(repo.getFullBranch());
- return rw.parseCommit(id);
+ try (RevWalk rw = new RevWalk(repo)) {
+ ObjectId id = repo.resolve(repo.getFullBranch());
+ return rw.parseCommit(id);
+ }
}
private void mergeBranch(String branchToMerge, boolean squash) throws Exception {
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/PushActionTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/PushActionTest.java
index 520fbfc854..e03dde5f2b 100644
--- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/PushActionTest.java
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/PushActionTest.java
@@ -65,50 +65,52 @@ public class PushActionTest extends LocalRepositoryTestCase {
throws Exception, MissingObjectException,
IncorrectObjectTypeException, IOException {
Repository repo = lookupRepository(remoteRepositoryFile);
- RevWalk rw = new RevWalk(repo);
- String previous = rw.parseCommit(repo.resolve("HEAD")).name();
+ try (RevWalk rw = new RevWalk(repo)) {
+ String previous = rw.parseCommit(repo.resolve("HEAD")).name();
+ touchAndSubmit(null);
+ SWTBotShell pushDialog = openPushDialog();
+
+ SWTBotCombo destinationCombo = pushDialog.bot().comboBox();
+ String[] items = destinationCombo.items();
+ for (int i = 0; i < items.length; i++) {
+ if (items[i].startsWith(destination))
+ destinationCombo.setSelection(i);
+ }
- touchAndSubmit(null);
- SWTBotShell pushDialog = openPushDialog();
-
- SWTBotCombo destinationCombo = pushDialog.bot().comboBox();
- String[] items = destinationCombo.items();
- for (int i = 0; i < items.length; i++) {
- if (items[i].startsWith(destination))
- destinationCombo.setSelection(i);
- }
-
- pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click();
- if (withConfirmPage)
pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click();
- pushDialog.bot().button(IDialogConstants.FINISH_LABEL).click();
- SWTBotShell confirm = bot.shell(NLS.bind(UIText.PushResultDialog_title,
- destination));
- String result = confirm.bot().tree().getAllItems()[0].getText();
+ if (withConfirmPage)
+ pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click();
+ pushDialog.bot().button(IDialogConstants.FINISH_LABEL).click();
+ SWTBotShell confirm = bot.shell(
+ NLS.bind(UIText.PushResultDialog_title, destination));
+ String result = confirm.bot().tree().getAllItems()[0].getText();
- assertTrue("Wrong result", result.contains(previous.substring(0, 7)));
+ assertTrue("Wrong result",
+ result.contains(previous.substring(0, 7)));
- confirm.close();
+ confirm.close();
- pushDialog = openPushDialog();
+ pushDialog = openPushDialog();
- destinationCombo = pushDialog.bot().comboBox();
- for (int i = 0; i < items.length; i++) {
- if (items[i].startsWith(destination))
- destinationCombo.setSelection(i);
- }
+ destinationCombo = pushDialog.bot().comboBox();
+ for (int i = 0; i < items.length; i++) {
+ if (items[i].startsWith(destination))
+ destinationCombo.setSelection(i);
+ }
- pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click();
- if (withConfirmPage)
pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click();
- pushDialog.bot().button(IDialogConstants.FINISH_LABEL).click();
- confirm = bot.shell(NLS.bind(UIText.PushResultDialog_title, destination));
- result = confirm.bot().tree().getAllItems()[0].getText();
+ if (withConfirmPage)
+ pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click();
+ pushDialog.bot().button(IDialogConstants.FINISH_LABEL).click();
+ confirm = bot.shell(
+ NLS.bind(UIText.PushResultDialog_title, destination));
+ result = confirm.bot().tree().getAllItems()[0].getText();
- confirm.close();
+ confirm.close();
- assertTrue("Wrong result",
- result.contains(UIText.PushResultTable_statusUpToDate));
+ assertTrue("Wrong result",
+ result.contains(UIText.PushResultTable_statusUpToDate));
+ }
}
private SWTBotShell openPushDialog() throws Exception {

Back to the top