Use try-with-resource to avoid leaks with RevWalk and TreeWalk
[egit/eclipse.git] / org.eclipse.egit.core.test / src / org / eclipse / egit / core / test / op / StashCreateOperationTest.java
blob026119079a662e2ef87046ba34a4f098653e735b
1 /*******************************************************************************
2 * Copyright (C) 2012, 2013 Maik Schreiber <blizzy@blizzy.de> and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org.eclipse.egit.core.test.op;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
15 import java.io.File;
16 import java.util.Arrays;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.egit.core.op.AddToIndexOperation;
20 import org.eclipse.egit.core.op.StashCreateOperation;
21 import org.eclipse.egit.core.test.GitTestCase;
22 import org.eclipse.egit.core.test.TestRepository;
23 import org.eclipse.jgit.lib.Constants;
24 import org.eclipse.jgit.lib.Repository;
25 import org.eclipse.jgit.revwalk.RevCommit;
26 import org.eclipse.jgit.revwalk.RevWalk;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
31 public class StashCreateOperationTest extends GitTestCase {
33 TestRepository testRepository;
35 Repository repository;
37 @Before
38 public void setUp() throws Exception {
39 super.setUp();
40 gitDir = new File(project.getProject()
41 .getLocationURI().getPath(), Constants.DOT_GIT);
42 testRepository = new TestRepository(gitDir);
43 repository = testRepository.getRepository();
44 testRepository.connect(project.getProject());
45 testRepository.commit("initial commit");
48 @After
49 public void tearDown() throws Exception {
50 testRepository.dispose();
51 repository = null;
52 super.tearDown();
55 @Test
56 public void testDefaultMessage() throws Exception {
57 IFile file = testUtils.addFileToProject(project.getProject(),
58 "foo/a.txt", "some text");
59 new AddToIndexOperation(Arrays.asList(file)).execute(null);
60 StashCreateOperation stashCreateOperation = new StashCreateOperation(repository);
61 stashCreateOperation.execute(null);
63 try (RevWalk revWalk = new RevWalk(repository)) {
64 RevCommit commit = revWalk
65 .parseCommit(repository.resolve("stash@{0}"));
66 assertTrue(commit.getFullMessage().length() > 0);
70 @Test
71 public void testCustomMessage() throws Exception {
72 IFile file = testUtils.addFileToProject(project.getProject(),
73 "foo/a.txt", "some text");
74 new AddToIndexOperation(Arrays.asList(file)).execute(null);
75 String message = "stash message";
76 StashCreateOperation stashCreateOperation = new StashCreateOperation(repository, message);
77 stashCreateOperation.execute(null);
79 try (RevWalk revWalk = new RevWalk(repository)) {
80 RevCommit commit = revWalk
81 .parseCommit(repository.resolve("stash@{0}"));
82 assertEquals(message, commit.getFullMessage());
86 @Test
87 public void testUntrackedFlag() throws Exception {
88 testUtils.addFileToProject(project.getProject(), "foo/untracked.txt",
89 "some text");
90 String message = "stash with untracked files";
91 StashCreateOperation stashCreateOperation = new StashCreateOperation(
92 repository, message, true);
93 stashCreateOperation.execute(null);
95 try (RevWalk revWalk = new RevWalk(repository)) {
96 RevCommit commit = revWalk
97 .parseCommit(repository.resolve("stash@{0}"));
98 // untracked commit is the third parent
99 assertEquals(commit.getParentCount(), 3);