Browse Source

DirCacheCheckoutTest: Open Git and TreeWalk in try-with-resource

Change-Id: Icc8eda841e6a437e1acf3438525014e7c46d5eb2
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.2
David Pursehouse 9 years ago
parent
commit
06bf69d8ca
  1. 215
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java

215
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java

@ -140,52 +140,53 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
@Test @Test
public void testResetHard() throws IOException, NoFilepatternException, public void testResetHard() throws IOException, NoFilepatternException,
GitAPIException { GitAPIException {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("f", "f()"); writeTrashFile("f", "f()");
writeTrashFile("D/g", "g()"); writeTrashFile("D/g", "g()");
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("inital").call(); git.commit().setMessage("inital").call();
assertIndex(mkmap("f", "f()", "D/g", "g()")); assertIndex(mkmap("f", "f()", "D/g", "g()"));
git.branchCreate().setName("topic").call(); git.branchCreate().setName("topic").call();
writeTrashFile("f", "f()\nmaster"); writeTrashFile("f", "f()\nmaster");
writeTrashFile("D/g", "g()\ng2()"); writeTrashFile("D/g", "g()\ng2()");
writeTrashFile("E/h", "h()"); writeTrashFile("E/h", "h()");
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
RevCommit master = git.commit().setMessage("master-1").call(); RevCommit master = git.commit().setMessage("master-1").call();
assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()")); assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
checkoutBranch("refs/heads/topic"); checkoutBranch("refs/heads/topic");
assertIndex(mkmap("f", "f()", "D/g", "g()")); assertIndex(mkmap("f", "f()", "D/g", "g()"));
writeTrashFile("f", "f()\nside"); writeTrashFile("f", "f()\nside");
assertTrue(new File(db.getWorkTree(), "D/g").delete()); assertTrue(new File(db.getWorkTree(), "D/g").delete());
writeTrashFile("G/i", "i()"); writeTrashFile("G/i", "i()");
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.add().addFilepattern(".").setUpdate(true).call(); git.add().addFilepattern(".").setUpdate(true).call();
RevCommit topic = git.commit().setMessage("topic-1").call(); RevCommit topic = git.commit().setMessage("topic-1").call();
assertIndex(mkmap("f", "f()\nside", "G/i", "i()")); assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
writeTrashFile("untracked", "untracked"); writeTrashFile("untracked", "untracked");
resetHard(master); resetHard(master);
assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()")); assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
resetHard(topic); resetHard(topic);
assertIndex(mkmap("f", "f()\nside", "G/i", "i()")); assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked", assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked",
"untracked")); "untracked"));
assertEquals(MergeStatus.CONFLICTING, git.merge().include(master) assertEquals(MergeStatus.CONFLICTING, git.merge().include(master)
.call().getMergeStatus()); .call().getMergeStatus());
assertEquals( 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]", "[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)); indexState(0));
resetHard(master); resetHard(master);
assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()")); assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
"h()", "untracked", "untracked")); "h()", "untracked", "untracked"));
}
} }
/** /**
@ -200,21 +201,22 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
@Test @Test
public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile() public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
throws Exception { throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("x", "x"); writeTrashFile("x", "x");
git.add().addFilepattern("x").call(); git.add().addFilepattern("x").call();
RevCommit id1 = git.commit().setMessage("c1").call(); RevCommit id1 = git.commit().setMessage("c1").call();
writeTrashFile("f/g", "f/g"); writeTrashFile("f/g", "f/g");
git.rm().addFilepattern("x").call(); git.rm().addFilepattern("x").call();
git.add().addFilepattern("f/g").call(); git.add().addFilepattern("f/g").call();
git.commit().setMessage("c2").call(); git.commit().setMessage("c2").call();
deleteTrashFile("f/g"); deleteTrashFile("f/g");
deleteTrashFile("f"); deleteTrashFile("f");
// The actual test // The actual test
git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call(); git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call();
assertIndex(mkmap("x", "x")); assertIndex(mkmap("x", "x"));
}
} }
/** /**
@ -224,14 +226,14 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
*/ */
@Test @Test
public void testInitialCheckout() throws Exception { public void testInitialCheckout() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
TestRepository<Repository> db_t = new TestRepository<Repository>(db);
TestRepository<Repository> db_t = new TestRepository<Repository>(db); BranchBuilder master = db_t.branch("master");
BranchBuilder master = db_t.branch("master"); master.commit().add("f", "1").message("m0").create();
master.commit().add("f", "1").message("m0").create(); assertFalse(new File(db.getWorkTree(), "f").exists());
assertFalse(new File(db.getWorkTree(), "f").exists()); git.checkout().setName("master").call();
git.checkout().setName("master").call(); assertTrue(new File(db.getWorkTree(), "f").exists());
assertTrue(new File(db.getWorkTree(), "f").exists()); }
} }
private DirCacheCheckout resetHard(RevCommit commit) private DirCacheCheckout resetHard(RevCommit commit)
@ -1612,46 +1614,47 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
public void assertWorkDir(Map<String, String> i) public void assertWorkDir(Map<String, String> i)
throws CorruptObjectException, throws CorruptObjectException,
IOException { IOException {
TreeWalk walk = new TreeWalk(db); try (TreeWalk walk = new TreeWalk(db)) {
walk.setRecursive(false); walk.setRecursive(false);
walk.addTree(new FileTreeIterator(db)); walk.addTree(new FileTreeIterator(db));
String expectedValue; String expectedValue;
String path; String path;
int nrFiles = 0; int nrFiles = 0;
FileTreeIterator ft; FileTreeIterator ft;
while (walk.next()) { while (walk.next()) {
ft = walk.getTree(0, FileTreeIterator.class); ft = walk.getTree(0, FileTreeIterator.class);
path = ft.getEntryPathString(); path = ft.getEntryPathString();
expectedValue = i.get(path); expectedValue = i.get(path);
File file = new File(db.getWorkTree(), path); File file = new File(db.getWorkTree(), path);
assertTrue(file.exists()); assertTrue(file.exists());
if (file.isFile()) { if (file.isFile()) {
assertNotNull("found unexpected file for path " + path assertNotNull("found unexpected file for path " + path
+ " in workdir", expectedValue); + " in workdir", expectedValue);
FileInputStream is = new FileInputStream(file); FileInputStream is = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()]; byte[] buffer = new byte[(int) file.length()];
int offset = 0; int offset = 0;
int numRead = 0; int numRead = 0;
while (offset < buffer.length while (offset < buffer.length
&& (numRead = is.read(buffer, offset, buffer.length && (numRead = is.read(buffer, offset, buffer.length
- offset)) >= 0) { - offset)) >= 0) {
offset += numRead; offset += numRead;
} }
is.close(); is.close();
assertArrayEquals("unexpected content for path " + path assertArrayEquals("unexpected content for path " + path
+ " in workDir. ", buffer, i.get(path).getBytes()); + " 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++; 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()) { assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
walk.enterSubtree();
}
} }
assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
} }
} }

Loading…
Cancel
Save