Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Pursehouse2016-02-02 09:30:30 +0000
committerDavid Pursehouse2016-02-02 09:30:30 +0000
commit06bf69d8ca36ca81d6f1d86026c72302fdf72189 (patch)
tree3af9ef30487efe7cda13f6c55da3b8a467919c6a
parent521f513d6d5d43bbf27b976434a4b75c4798d718 (diff)
downloadjgit-06bf69d8ca36ca81d6f1d86026c72302fdf72189.tar.gz
jgit-06bf69d8ca36ca81d6f1d86026c72302fdf72189.tar.xz
jgit-06bf69d8ca36ca81d6f1d86026c72302fdf72189.zip
DirCacheCheckoutTest: Open Git and TreeWalk in try-with-resource
Change-Id: Icc8eda841e6a437e1acf3438525014e7c46d5eb2 Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java215
1 files changed, 109 insertions, 106 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java
index 92901f826b..c1b882a656 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java
@@ -140,52 +140,53 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
@Test
public void testResetHard() throws IOException, NoFilepatternException,
GitAPIException {
- Git git = new Git(db);
- writeTrashFile("f", "f()");
- writeTrashFile("D/g", "g()");
- git.add().addFilepattern(".").call();
- git.commit().setMessage("inital").call();
- assertIndex(mkmap("f", "f()", "D/g", "g()"));
-
- git.branchCreate().setName("topic").call();
-
- writeTrashFile("f", "f()\nmaster");
- writeTrashFile("D/g", "g()\ng2()");
- writeTrashFile("E/h", "h()");
- git.add().addFilepattern(".").call();
- RevCommit master = git.commit().setMessage("master-1").call();
- assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
-
- checkoutBranch("refs/heads/topic");
- assertIndex(mkmap("f", "f()", "D/g", "g()"));
-
- writeTrashFile("f", "f()\nside");
- assertTrue(new File(db.getWorkTree(), "D/g").delete());
- writeTrashFile("G/i", "i()");
- git.add().addFilepattern(".").call();
- git.add().addFilepattern(".").setUpdate(true).call();
- RevCommit topic = git.commit().setMessage("topic-1").call();
- assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
-
- writeTrashFile("untracked", "untracked");
-
- resetHard(master);
- assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
- resetHard(topic);
- assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
- assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked",
- "untracked"));
-
- assertEquals(MergeStatus.CONFLICTING, git.merge().include(master)
- .call().getMergeStatus());
- assertEquals(
- "[D/g, mode:100644, stage:1][D/g, mode:100644, stage:3][E/h, mode:100644][G/i, mode:100644][f, mode:100644, stage:1][f, mode:100644, stage:2][f, mode:100644, stage:3]",
- indexState(0));
-
- resetHard(master);
- assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
- assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
- "h()", "untracked", "untracked"));
+ try (Git git = new Git(db)) {
+ writeTrashFile("f", "f()");
+ writeTrashFile("D/g", "g()");
+ git.add().addFilepattern(".").call();
+ git.commit().setMessage("inital").call();
+ assertIndex(mkmap("f", "f()", "D/g", "g()"));
+
+ git.branchCreate().setName("topic").call();
+
+ writeTrashFile("f", "f()\nmaster");
+ writeTrashFile("D/g", "g()\ng2()");
+ writeTrashFile("E/h", "h()");
+ git.add().addFilepattern(".").call();
+ RevCommit master = git.commit().setMessage("master-1").call();
+ assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
+
+ checkoutBranch("refs/heads/topic");
+ assertIndex(mkmap("f", "f()", "D/g", "g()"));
+
+ writeTrashFile("f", "f()\nside");
+ assertTrue(new File(db.getWorkTree(), "D/g").delete());
+ writeTrashFile("G/i", "i()");
+ git.add().addFilepattern(".").call();
+ git.add().addFilepattern(".").setUpdate(true).call();
+ RevCommit topic = git.commit().setMessage("topic-1").call();
+ assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
+
+ writeTrashFile("untracked", "untracked");
+
+ resetHard(master);
+ assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
+ resetHard(topic);
+ assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
+ assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked",
+ "untracked"));
+
+ assertEquals(MergeStatus.CONFLICTING, git.merge().include(master)
+ .call().getMergeStatus());
+ assertEquals(
+ "[D/g, mode:100644, stage:1][D/g, mode:100644, stage:3][E/h, mode:100644][G/i, mode:100644][f, mode:100644, stage:1][f, mode:100644, stage:2][f, mode:100644, stage:3]",
+ indexState(0));
+
+ resetHard(master);
+ assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
+ assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
+ "h()", "untracked", "untracked"));
+ }
}
/**
@@ -200,21 +201,22 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
@Test
public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
throws Exception {
- Git git = new Git(db);
- writeTrashFile("x", "x");
- git.add().addFilepattern("x").call();
- RevCommit id1 = git.commit().setMessage("c1").call();
-
- writeTrashFile("f/g", "f/g");
- git.rm().addFilepattern("x").call();
- git.add().addFilepattern("f/g").call();
- git.commit().setMessage("c2").call();
- deleteTrashFile("f/g");
- deleteTrashFile("f");
-
- // The actual test
- git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call();
- assertIndex(mkmap("x", "x"));
+ try (Git git = new Git(db)) {
+ writeTrashFile("x", "x");
+ git.add().addFilepattern("x").call();
+ RevCommit id1 = git.commit().setMessage("c1").call();
+
+ writeTrashFile("f/g", "f/g");
+ git.rm().addFilepattern("x").call();
+ git.add().addFilepattern("f/g").call();
+ git.commit().setMessage("c2").call();
+ deleteTrashFile("f/g");
+ deleteTrashFile("f");
+
+ // The actual test
+ git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call();
+ assertIndex(mkmap("x", "x"));
+ }
}
/**
@@ -224,14 +226,14 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
*/
@Test
public void testInitialCheckout() throws Exception {
- Git git = new Git(db);
-
- TestRepository<Repository> db_t = new TestRepository<Repository>(db);
- BranchBuilder master = db_t.branch("master");
- master.commit().add("f", "1").message("m0").create();
- assertFalse(new File(db.getWorkTree(), "f").exists());
- git.checkout().setName("master").call();
- assertTrue(new File(db.getWorkTree(), "f").exists());
+ try (Git git = new Git(db)) {
+ TestRepository<Repository> db_t = new TestRepository<Repository>(db);
+ BranchBuilder master = db_t.branch("master");
+ master.commit().add("f", "1").message("m0").create();
+ assertFalse(new File(db.getWorkTree(), "f").exists());
+ git.checkout().setName("master").call();
+ assertTrue(new File(db.getWorkTree(), "f").exists());
+ }
}
private DirCacheCheckout resetHard(RevCommit commit)
@@ -1612,46 +1614,47 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
public void assertWorkDir(Map<String, String> i)
throws CorruptObjectException,
IOException {
- TreeWalk walk = new TreeWalk(db);
- walk.setRecursive(false);
- walk.addTree(new FileTreeIterator(db));
- String expectedValue;
- String path;
- int nrFiles = 0;
- FileTreeIterator ft;
- while (walk.next()) {
- ft = walk.getTree(0, FileTreeIterator.class);
- path = ft.getEntryPathString();
- expectedValue = i.get(path);
- File file = new File(db.getWorkTree(), path);
- assertTrue(file.exists());
- if (file.isFile()) {
- assertNotNull("found unexpected file for path " + path
- + " in workdir", expectedValue);
- FileInputStream is = new FileInputStream(file);
- byte[] buffer = new byte[(int) file.length()];
- int offset = 0;
- int numRead = 0;
- while (offset < buffer.length
- && (numRead = is.read(buffer, offset, buffer.length
- - offset)) >= 0) {
- offset += numRead;
- }
- is.close();
- assertArrayEquals("unexpected content for path " + path
- + " in workDir. ", buffer, i.get(path).getBytes());
- nrFiles++;
- } else if (file.isDirectory()) {
- if (file.list().length == 0) {
- assertEquals("found unexpected empty folder for path "
- + path + " in workDir. ", "/", i.get(path));
+ try (TreeWalk walk = new TreeWalk(db)) {
+ walk.setRecursive(false);
+ walk.addTree(new FileTreeIterator(db));
+ String expectedValue;
+ String path;
+ int nrFiles = 0;
+ FileTreeIterator ft;
+ while (walk.next()) {
+ ft = walk.getTree(0, FileTreeIterator.class);
+ path = ft.getEntryPathString();
+ expectedValue = i.get(path);
+ File file = new File(db.getWorkTree(), path);
+ assertTrue(file.exists());
+ if (file.isFile()) {
+ assertNotNull("found unexpected file for path " + path
+ + " in workdir", expectedValue);
+ FileInputStream is = new FileInputStream(file);
+ byte[] buffer = new byte[(int) file.length()];
+ int offset = 0;
+ int numRead = 0;
+ while (offset < buffer.length
+ && (numRead = is.read(buffer, offset, buffer.length
+ - offset)) >= 0) {
+ offset += numRead;
+ }
+ is.close();
+ assertArrayEquals("unexpected content for path " + path
+ + " in workDir. ", buffer, i.get(path).getBytes());
nrFiles++;
+ } else if (file.isDirectory()) {
+ if (file.list().length == 0) {
+ assertEquals("found unexpected empty folder for path "
+ + path + " in workDir. ", "/", i.get(path));
+ nrFiles++;
+ }
+ }
+ if (walk.isSubtree()) {
+ walk.enterSubtree();
}
}
- if (walk.isSubtree()) {
- walk.enterSubtree();
- }
+ assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
}
- assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
}
}

Back to the top