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

Loading…
Cancel
Save