Browse Source

DiffCommandTest: Open Git and RevWalk in try-with-resource

Change-Id: I966f7e5053651661abc7de63c968e1f5bf2de464
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.2
David Pursehouse 9 years ago
parent
commit
6b66240311
  1. 29
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java

29
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java

@ -70,7 +70,7 @@ public class DiffCommandTest extends RepositoryTestCase {
File folder = new File(db.getWorkTree(), "folder");
folder.mkdir();
write(new File(folder, "folder.txt"), "folder");
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change");
@ -97,13 +97,14 @@ public class DiffCommandTest extends RepositoryTestCase {
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
}
}
@Test
public void testDiffCached() throws Exception {
write(new File(db.getWorkTree(), "test.txt"), "test");
File folder = new File(db.getWorkTree(), "folder");
folder.mkdir();
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder");
@ -131,6 +132,7 @@ public class DiffCommandTest extends RepositoryTestCase {
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
}
}
@Test
public void testDiffTwoCommits() throws Exception {
@ -138,7 +140,7 @@ public class DiffCommandTest extends RepositoryTestCase {
File folder = new File(db.getWorkTree(), "folder");
folder.mkdir();
write(new File(folder, "folder.txt"), "folder");
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change");
@ -179,19 +181,19 @@ public class DiffCommandTest extends RepositoryTestCase {
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
}
}
@Test
public void testDiffWithPrefixes() throws Exception {
write(new File(db.getWorkTree(), "test.txt"), "test");
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"), "test change");
OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setSourcePrefix("old/")
.setDestinationPrefix("new/")
.call();
.setDestinationPrefix("new/").call();
String actual = out.toString();
String expected = "diff --git old/test.txt new/test.txt\n"
@ -201,20 +203,20 @@ public class DiffCommandTest extends RepositoryTestCase {
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
}
}
@Test
public void testDiffWithNegativeLineCount() throws Exception {
write(new File(db.getWorkTree(), "test.txt"),
"0\n1\n2\n3\n4\n5\n6\n7\n8\n9");
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"),
"0\n1\n2\n3\n4a\n5\n6\n7\n8\n9");
OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setContextLines(1)
.call();
git.diff().setOutputStream(out).setContextLines(1).call();
String actual = out.toString();
String expected = "diff --git a/test.txt b/test.txt\n"
@ -223,12 +225,13 @@ public class DiffCommandTest extends RepositoryTestCase {
+ "+4a\n" + " 5\n";
assertEquals(expected.toString(), actual);
}
}
@Test
public void testNoOutputStreamSet() throws Exception {
File file = writeTrashFile("test.txt", "a");
assertTrue(file.setLastModified(file.lastModified() - 5000));
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
write(file, "b");
@ -240,6 +243,7 @@ public class DiffCommandTest extends RepositoryTestCase {
assertEquals("test.txt", diff.getOldPath());
assertEquals("test.txt", diff.getNewPath());
}
}
private AbstractTreeIterator getTreeIterator(String name)
throws IOException {
@ -247,8 +251,9 @@ public class DiffCommandTest extends RepositoryTestCase {
if (id == null)
throw new IllegalArgumentException(name);
final CanonicalTreeParser p = new CanonicalTreeParser();
try (ObjectReader or = db.newObjectReader()) {
p.reset(or, new RevWalk(db).parseTree(id));
try (ObjectReader or = db.newObjectReader();
RevWalk rw = new RevWalk(db)) {
p.reset(or, rw.parseTree(id));
return p;
}
}

Loading…
Cancel
Save