diff options
Diffstat (limited to 'org.eclipse.jgit.test')
64 files changed, 3520 insertions, 1988 deletions
diff --git a/org.eclipse.jgit.test/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.jgit.test/.settings/org.eclipse.jdt.core.prefs index 2ca78ff2d0..3dd5840397 100644 --- a/org.eclipse.jgit.test/.settings/org.eclipse.jdt.core.prefs +++ b/org.eclipse.jgit.test/.settings/org.eclipse.jdt.core.prefs @@ -92,7 +92,7 @@ org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=warning org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=ignore -org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore +org.eclipse.jdt.core.compiler.problem.unnecessaryElse=warning org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=error org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=error diff --git a/org.eclipse.jgit.test/BUILD b/org.eclipse.jgit.test/BUILD index 3f664f93f3..3d487960c4 100644 --- a/org.eclipse.jgit.test/BUILD +++ b/org.eclipse.jgit.test/BUILD @@ -24,6 +24,7 @@ HELPERS = glob( "revwalk/RevQueueTestCase.java", "revwalk/RevWalkTestCase.java", "transport/ObjectIdMatcher.java", + "transport/RequestValidatorTestCase.java", "transport/SpiTransport.java", "treewalk/filter/AlwaysCloneTreeFilter.java", "test/resources/SampleDataRepositoryTestCase.java", diff --git a/org.eclipse.jgit.test/META-INF/MANIFEST.MF b/org.eclipse.jgit.test/META-INF/MANIFEST.MF index 42c9c573a9..8043065389 100644 --- a/org.eclipse.jgit.test/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit.test/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: %Bundle-Name Automatic-Module-Name: org.eclipse.jgit.test Bundle-SymbolicName: org.eclipse.jgit.test -Bundle-Version: 5.5.2.qualifier +Bundle-Version: 5.6.0.qualifier Bundle-Localization: plugin Bundle-Vendor: %Bundle-Vendor Bundle-ActivationPolicy: lazy @@ -18,57 +18,58 @@ Import-Package: com.googlecode.javaewah;version="[1.1.6,2.0.0)", org.apache.commons.compress.compressors.gzip;version="[1.15.0,2.0)", org.apache.commons.compress.compressors.xz;version="[1.15.0,2.0)", org.bouncycastle.util.encoders;version="[1.61.0,2.0.0)", - org.eclipse.jgit.annotations;version="[5.5.2,5.6.0)", - org.eclipse.jgit.api;version="[5.5.2,5.6.0)", - org.eclipse.jgit.api.errors;version="[5.5.2,5.6.0)", - org.eclipse.jgit.archive;version="[5.5.2,5.6.0)", - org.eclipse.jgit.attributes;version="[5.5.2,5.6.0)", - org.eclipse.jgit.awtui;version="[5.5.2,5.6.0)", - org.eclipse.jgit.blame;version="[5.5.2,5.6.0)", - org.eclipse.jgit.diff;version="[5.5.2,5.6.0)", - org.eclipse.jgit.dircache;version="[5.5.2,5.6.0)", - org.eclipse.jgit.errors;version="[5.5.2,5.6.0)", - org.eclipse.jgit.events;version="[5.5.2,5.6.0)", - org.eclipse.jgit.fnmatch;version="[5.5.2,5.6.0)", - org.eclipse.jgit.gitrepo;version="[5.5.2,5.6.0)", - org.eclipse.jgit.hooks;version="[5.5.2,5.6.0)", - org.eclipse.jgit.ignore;version="[5.5.2,5.6.0)", - org.eclipse.jgit.ignore.internal;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal.fsck;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal.storage.dfs;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal.storage.file;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal.storage.io;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal.storage.pack;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal.storage.reftable;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal.storage.reftree;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal.transport.http;version="[5.5.2,5.6.0)", - org.eclipse.jgit.internal.transport.parser;version="[5.5.2,5.6.0)", - org.eclipse.jgit.junit;version="[5.5.2,5.6.0)", - org.eclipse.jgit.junit.ssh;version="[5.5.2,5.6.0)", - org.eclipse.jgit.junit.time;version="[5.5.2,5.6.0)", - org.eclipse.jgit.lfs;version="[5.5.2,5.6.0)", - org.eclipse.jgit.lib;version="[5.5.2,5.6.0)", - org.eclipse.jgit.merge;version="[5.5.2,5.6.0)", - org.eclipse.jgit.nls;version="[5.5.2,5.6.0)", - org.eclipse.jgit.notes;version="[5.5.2,5.6.0)", - org.eclipse.jgit.patch;version="[5.5.2,5.6.0)", - org.eclipse.jgit.pgm;version="[5.5.2,5.6.0)", - org.eclipse.jgit.pgm.internal;version="[5.5.2,5.6.0)", - org.eclipse.jgit.revplot;version="[5.5.2,5.6.0)", - org.eclipse.jgit.revwalk;version="[5.5.2,5.6.0)", - org.eclipse.jgit.revwalk.filter;version="[5.5.2,5.6.0)", - org.eclipse.jgit.storage.file;version="[5.5.2,5.6.0)", - org.eclipse.jgit.storage.pack;version="[5.5.2,5.6.0)", - org.eclipse.jgit.submodule;version="[5.5.2,5.6.0)", - org.eclipse.jgit.transport;version="[5.5.2,5.6.0)", - org.eclipse.jgit.transport.http;version="[5.5.2,5.6.0)", - org.eclipse.jgit.transport.resolver;version="[5.5.2,5.6.0)", - org.eclipse.jgit.treewalk;version="[5.5.2,5.6.0)", - org.eclipse.jgit.treewalk.filter;version="[5.5.2,5.6.0)", - org.eclipse.jgit.util;version="[5.5.2,5.6.0)", - org.eclipse.jgit.util.io;version="[5.5.2,5.6.0)", - org.eclipse.jgit.util.sha1;version="[5.5.2,5.6.0)", + org.eclipse.jgit.annotations;version="[5.6.0,5.7.0)", + org.eclipse.jgit.api;version="[5.6.0,5.7.0)", + org.eclipse.jgit.api.errors;version="[5.6.0,5.7.0)", + org.eclipse.jgit.archive;version="[5.6.0,5.7.0)", + org.eclipse.jgit.attributes;version="[5.6.0,5.7.0)", + org.eclipse.jgit.awtui;version="[5.6.0,5.7.0)", + org.eclipse.jgit.blame;version="[5.6.0,5.7.0)", + org.eclipse.jgit.diff;version="[5.6.0,5.7.0)", + org.eclipse.jgit.dircache;version="[5.6.0,5.7.0)", + org.eclipse.jgit.errors;version="[5.6.0,5.7.0)", + org.eclipse.jgit.events;version="[5.6.0,5.7.0)", + org.eclipse.jgit.fnmatch;version="[5.6.0,5.7.0)", + org.eclipse.jgit.gitrepo;version="[5.6.0,5.7.0)", + org.eclipse.jgit.hooks;version="[5.6.0,5.7.0)", + org.eclipse.jgit.ignore;version="[5.6.0,5.7.0)", + org.eclipse.jgit.ignore.internal;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal.fsck;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal.storage.dfs;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal.storage.file;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal.storage.io;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal.storage.pack;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal.storage.reftable;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal.storage.reftree;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal.transport.http;version="[5.6.0,5.7.0)", + org.eclipse.jgit.internal.transport.parser;version="[5.6.0,5.7.0)", + org.eclipse.jgit.junit;version="[5.6.0,5.7.0)", + org.eclipse.jgit.junit.ssh;version="[5.6.0,5.7.0)", + org.eclipse.jgit.junit.time;version="[5.6.0,5.7.0)", + org.eclipse.jgit.lfs;version="[5.6.0,5.7.0)", + org.eclipse.jgit.lib;version="[5.6.0,5.7.0)", + org.eclipse.jgit.lib.internal;version="[5.6.0,5.7.0)", + org.eclipse.jgit.merge;version="[5.6.0,5.7.0)", + org.eclipse.jgit.nls;version="[5.6.0,5.7.0)", + org.eclipse.jgit.notes;version="[5.6.0,5.7.0)", + org.eclipse.jgit.patch;version="[5.6.0,5.7.0)", + org.eclipse.jgit.pgm;version="[5.6.0,5.7.0)", + org.eclipse.jgit.pgm.internal;version="[5.6.0,5.7.0)", + org.eclipse.jgit.revplot;version="[5.6.0,5.7.0)", + org.eclipse.jgit.revwalk;version="[5.6.0,5.7.0)", + org.eclipse.jgit.revwalk.filter;version="[5.6.0,5.7.0)", + org.eclipse.jgit.storage.file;version="[5.6.0,5.7.0)", + org.eclipse.jgit.storage.pack;version="[5.6.0,5.7.0)", + org.eclipse.jgit.submodule;version="[5.6.0,5.7.0)", + org.eclipse.jgit.transport;version="[5.6.0,5.7.0)", + org.eclipse.jgit.transport.http;version="[5.6.0,5.7.0)", + org.eclipse.jgit.transport.resolver;version="[5.6.0,5.7.0)", + org.eclipse.jgit.treewalk;version="[5.6.0,5.7.0)", + org.eclipse.jgit.treewalk.filter;version="[5.6.0,5.7.0)", + org.eclipse.jgit.util;version="[5.6.0,5.7.0)", + org.eclipse.jgit.util.io;version="[5.6.0,5.7.0)", + org.eclipse.jgit.util.sha1;version="[5.6.0,5.7.0)", org.junit;version="[4.12,5.0.0)", org.junit.experimental.theories;version="[4.12,5.0.0)", org.junit.rules;version="[4.12,5.0.0)", @@ -83,4 +84,4 @@ Import-Package: com.googlecode.javaewah;version="[1.1.6,2.0.0)", org.tukaani.xz;version="[1.6.0,2.0)" Require-Bundle: org.hamcrest.core;bundle-version="[1.1.0,2.0.0)", org.hamcrest.library;bundle-version="[1.1.0,2.0.0)" -Export-Package: org.eclipse.jgit.transport.ssh;version="5.5.2";x-friends:="org.eclipse.jgit.ssh.apache.test" +Export-Package: org.eclipse.jgit.transport.ssh;version="5.6.0";x-friends:="org.eclipse.jgit.ssh.apache.test" diff --git a/org.eclipse.jgit.test/pom.xml b/org.eclipse.jgit.test/pom.xml index 91c3418b1b..d8af7dc524 100644 --- a/org.eclipse.jgit.test/pom.xml +++ b/org.eclipse.jgit.test/pom.xml @@ -52,7 +52,7 @@ <parent> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit-parent</artifactId> - <version>5.5.2-SNAPSHOT</version> + <version>5.6.0-SNAPSHOT</version> </parent> <artifactId>org.eclipse.jgit.test</artifactId> diff --git a/org.eclipse.jgit.test/src/org/eclipse/jgit/events/ChangeRecorder.java b/org.eclipse.jgit.test/src/org/eclipse/jgit/events/ChangeRecorder.java index 228df35c76..e1f6b12258 100644 --- a/org.eclipse.jgit.test/src/org/eclipse/jgit/events/ChangeRecorder.java +++ b/org.eclipse.jgit.test/src/org/eclipse/jgit/events/ChangeRecorder.java @@ -43,7 +43,6 @@ package org.eclipse.jgit.events; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import java.util.Arrays; @@ -99,10 +98,12 @@ public class ChangeRecorder implements WorkingTreeModifiedListener { Arrays.sort(expectedModified); Arrays.sort(actuallyDeleted); Arrays.sort(expectedDeleted); - assertArrayEquals("Unexpected modifications reported", expectedModified, - actuallyModified); - assertArrayEquals("Unexpected deletions reported", expectedDeleted, - actuallyDeleted); + assertEquals("Unexpected modifications reported", + Arrays.toString(expectedModified), + Arrays.toString(actuallyModified)); + assertEquals("Unexpected deletions reported", + Arrays.toString(expectedDeleted), + Arrays.toString(actuallyDeleted)); reset(); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java index b28e26a639..52a9dfa3e2 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java @@ -700,26 +700,26 @@ public class AddCommandTest extends RepositoryTestCase { writer.print("content b"); } - ObjectInserter newObjectInserter = db.newObjectInserter(); DirCache dc = db.lockDirCache(); - DirCacheBuilder builder = dc.builder(); + try (ObjectInserter newObjectInserter = db.newObjectInserter()) { + DirCacheBuilder builder = dc.builder(); - addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0); - addEntryToBuilder("a.txt", file, newObjectInserter, builder, 1); + addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0); + addEntryToBuilder("a.txt", file, newObjectInserter, builder, 1); - try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { - writer.print("other content"); - } - addEntryToBuilder("a.txt", file, newObjectInserter, builder, 3); - - try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { - writer.print("our content"); - } - addEntryToBuilder("a.txt", file, newObjectInserter, builder, 2) - .getObjectId(); + try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { + writer.print("other content"); + } + addEntryToBuilder("a.txt", file, newObjectInserter, builder, 3); - builder.commit(); + try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { + writer.print("our content"); + } + addEntryToBuilder("a.txt", file, newObjectInserter, builder, 2) + .getObjectId(); + builder.commit(); + } assertEquals( "[a.txt, mode:100644, stage:1, content:content]" + "[a.txt, mode:100644, stage:2, content:our content]" + @@ -1132,41 +1132,42 @@ public class AddCommandTest extends RepositoryTestCase { } }; - Git git = Git.open(db.getDirectory(), executableFs); String path = "a.txt"; String path2 = "a.sh"; writeTrashFile(path, "content"); writeTrashFile(path2, "binary: content"); - git.add().addFilepattern(path).addFilepattern(path2).call(); - RevCommit commit1 = git.commit().setMessage("commit").call(); - try (TreeWalk walk = new TreeWalk(db)) { - walk.addTree(commit1.getTree()); - walk.next(); - assertEquals(path2, walk.getPathString()); - assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); - walk.next(); - assertEquals(path, walk.getPathString()); - assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0)); + try (Git git = Git.open(db.getDirectory(), executableFs)) { + git.add().addFilepattern(path).addFilepattern(path2).call(); + RevCommit commit1 = git.commit().setMessage("commit").call(); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(commit1.getTree()); + walk.next(); + assertEquals(path2, walk.getPathString()); + assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + walk.next(); + assertEquals(path, walk.getPathString()); + assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0)); + } } - config = db.getConfig(); config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, false); config.save(); - Git git2 = Git.open(db.getDirectory(), executableFs); writeTrashFile(path2, "content2"); writeTrashFile(path, "binary: content2"); - git2.add().addFilepattern(path).addFilepattern(path2).call(); - RevCommit commit2 = git2.commit().setMessage("commit2").call(); - try (TreeWalk walk = new TreeWalk(db)) { - walk.addTree(commit2.getTree()); - walk.next(); - assertEquals(path2, walk.getPathString()); - assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); - walk.next(); - assertEquals(path, walk.getPathString()); - assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0)); + try (Git git2 = Git.open(db.getDirectory(), executableFs)) { + git2.add().addFilepattern(path).addFilepattern(path2).call(); + RevCommit commit2 = git2.commit().setMessage("commit2").call(); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(commit2.getTree()); + walk.next(); + assertEquals(path2, walk.getPathString()); + assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + walk.next(); + assertEquals(path, walk.getPathString()); + assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0)); + } } } @@ -1291,18 +1292,19 @@ public class AddCommandTest extends RepositoryTestCase { FileRepositoryBuilder nestedBuilder = new FileRepositoryBuilder(); nestedBuilder.setWorkTree(gitLinkDir); - Repository nestedRepo = nestedBuilder.build(); - nestedRepo.create(); + try (Repository nestedRepo = nestedBuilder.build()) { + nestedRepo.create(); - writeTrashFile(path, "README1.md", "content"); - writeTrashFile(path, "README2.md", "content"); + writeTrashFile(path, "README1.md", "content"); + writeTrashFile(path, "README2.md", "content"); - // Commit these changes in the subrepo - try (Git git = new Git(nestedRepo)) { - git.add().addFilepattern(".").call(); - git.commit().setMessage("subrepo commit").call(); - } catch (GitAPIException e) { - throw new RuntimeException(e); + // Commit these changes in the subrepo + try (Git git = new Git(nestedRepo)) { + git.add().addFilepattern(".").call(); + git.commit().setMessage("subrepo commit").call(); + } catch (GitAPIException e) { + throw new RuntimeException(e); + } } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java index fbc2cf67e1..76958f1ddf 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java @@ -58,6 +58,8 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.MultipleParentsNotAllowedException; import org.eclipse.jgit.dircache.DirCache; +import org.eclipse.jgit.events.ChangeRecorder; +import org.eclipse.jgit.events.ListenerHandle; import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; @@ -325,6 +327,59 @@ public class CherryPickCommandTest extends RepositoryTestCase { } @Test + public void testCherryPickConflictFiresModifiedEvent() throws Exception { + ListenerHandle listener = null; + try (Git git = new Git(db)) { + RevCommit sideCommit = prepareCherryPick(git); + ChangeRecorder recorder = new ChangeRecorder(); + listener = db.getListenerList() + .addWorkingTreeModifiedListener(recorder); + CherryPickResult result = git.cherryPick() + .include(sideCommit.getId()).call(); + assertEquals(CherryPickStatus.CONFLICTING, result.getStatus()); + recorder.assertEvent(new String[] { "a" }, ChangeRecorder.EMPTY); + } finally { + if (listener != null) { + listener.remove(); + } + } + } + + @Test + public void testCherryPickNewFileFiresModifiedEvent() throws Exception { + ListenerHandle listener = null; + try (Git git = new Git(db)) { + writeTrashFile("test.txt", "a"); + git.add().addFilepattern("test.txt").call(); + git.commit().setMessage("commit1").call(); + git.checkout().setCreateBranch(true).setName("a").call(); + + writeTrashFile("side.txt", "side"); + git.add().addFilepattern("side.txt").call(); + RevCommit side = git.commit().setMessage("side").call(); + assertNotNull(side); + + assertNotNull(git.checkout().setName(Constants.MASTER).call()); + writeTrashFile("test.txt", "b"); + assertNotNull(git.add().addFilepattern("test.txt").call()); + assertNotNull(git.commit().setMessage("commit2").call()); + + ChangeRecorder recorder = new ChangeRecorder(); + listener = db.getListenerList() + .addWorkingTreeModifiedListener(recorder); + CherryPickResult result = git.cherryPick() + .include(side.getId()).call(); + assertEquals(CherryPickStatus.OK, result.getStatus()); + recorder.assertEvent(new String[] { "side.txt" }, + ChangeRecorder.EMPTY); + } finally { + if (listener != null) { + listener.remove(); + } + } + } + + @Test public void testCherryPickOurCommitName() throws Exception { try (Git git = new Git(db)) { RevCommit sideCommit = prepareCherryPick(git); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java index 2270a6a8be..3224bbb784 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java @@ -67,6 +67,7 @@ import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.revwalk.RevBlob; @@ -96,10 +97,15 @@ public class CloneCommandTest extends RepositoryTestCase { writeTrashFile("Test.txt", "Hello world"); git.add().addFilepattern("Test.txt").call(); git.commit().setMessage("Initial commit").call(); - git.tag().setName("tag-initial").setMessage("Tag initial").call(); + Ref head = git.tag().setName("tag-initial").setMessage("Tag initial") + .call(); // create a test branch and switch to it git.checkout().setCreateBranch(true).setName("test").call(); + // create a non-standard ref + RefUpdate ru = db.updateRef("refs/meta/foo/bar"); + ru.setNewObjectId(head.getObjectId()); + ru.update(); // commit something on the test branch writeTrashFile("Test.txt", "Some change"); @@ -397,7 +403,6 @@ public class CloneCommandTest extends RepositoryTestCase { @Test public void testBareCloneRepositoryOnlyOneBranch() throws Exception { - // Same thing, but now test with bare repo File directory = createTempDirectory( "testCloneRepositoryWithBranch_bare"); CloneCommand command = Git.cloneRepository(); @@ -425,6 +430,32 @@ public class CloneCommandTest extends RepositoryTestCase { } @Test + public void testBareCloneRepositoryMirror() throws Exception { + File directory = createTempDirectory( + "testCloneRepositoryWithBranch_mirror"); + CloneCommand command = Git.cloneRepository(); + command.setBranch("refs/heads/master"); + command.setMirror(true); // implies bare repository + command.setDirectory(directory); + command.setURI(fileUri()); + Git git2 = command.call(); + addRepoToClose(git2.getRepository()); + assertNotNull(git2); + assertNotNull(git2.getRepository().resolve("tag-for-blob")); + assertNotNull(git2.getRepository().resolve("tag-initial")); + assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master"); + assertEquals("refs/heads/master, refs/heads/test", allRefNames( + git2.branchList().setListMode(ListMode.ALL).call())); + assertNotNull(git2.getRepository().exactRef("refs/meta/foo/bar")); + RemoteConfig cfg = new RemoteConfig(git2.getRepository().getConfig(), + Constants.DEFAULT_REMOTE_NAME); + List<RefSpec> specs = cfg.getFetchRefSpecs(); + assertEquals(1, specs.size()); + assertEquals(new RefSpec("+refs/*:refs/*"), + specs.get(0)); + } + + @Test public void testCloneRepositoryOnlyOneTag() throws Exception { File directory = createTempDirectory("testCloneRepositoryWithBranch"); CloneCommand command = Git.cloneRepository(); @@ -646,25 +677,27 @@ public class CloneCommandTest extends RepositoryTestCase { assertEquals(sub1Head, pathStatus.getHeadId()); assertEquals(sub1Head, pathStatus.getIndexId()); - SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository()); - assertTrue(walk.next()); - try (Repository clonedSub1 = walk.getRepository()) { - assertNotNull(clonedSub1); - assertEquals(new File(git2.getRepository().getWorkTree(), - walk.getPath()), clonedSub1.getWorkTree()); - assertEquals( - new File(new File(git2.getRepository().getDirectory(), - "modules"), walk.getPath()), - clonedSub1.getDirectory()); - status = new SubmoduleStatusCommand(clonedSub1); - statuses = status.call(); + try (SubmoduleWalk walk = SubmoduleWalk + .forIndex(git2.getRepository())) { + assertTrue(walk.next()); + try (Repository clonedSub1 = walk.getRepository()) { + assertNotNull(clonedSub1); + assertEquals(new File(git2.getRepository().getWorkTree(), + walk.getPath()), clonedSub1.getWorkTree()); + assertEquals( + new File(new File(git2.getRepository().getDirectory(), + "modules"), walk.getPath()), + clonedSub1.getDirectory()); + status = new SubmoduleStatusCommand(clonedSub1); + statuses = status.call(); + } + assertFalse(walk.next()); } pathStatus = statuses.get(path); assertNotNull(pathStatus); assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType()); assertEquals(sub2Head, pathStatus.getHeadId()); assertEquals(sub2Head, pathStatus.getIndexId()); - assertFalse(walk.next()); } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java index 3bde0eb33f..b5661e8440 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java @@ -148,9 +148,10 @@ public class CommitCommandTest extends RepositoryTestCase { writeTrashFile(path, "content"); git.add().addFilepattern(path).call(); RevCommit commit1 = git.commit().setMessage("commit").call(); - TreeWalk walk = TreeWalk.forPath(db, path, commit1.getTree()); - assertNotNull(walk); - assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + try (TreeWalk walk = TreeWalk.forPath(db, path, commit1.getTree())) { + assertNotNull(walk); + assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + } FS nonExecutableFs = new FS() { @@ -204,9 +205,10 @@ public class CommitCommandTest extends RepositoryTestCase { writeTrashFile(path, "content2"); RevCommit commit2 = git2.commit().setOnly(path).setMessage("commit2") .call(); - walk = TreeWalk.forPath(db, path, commit2.getTree()); - assertNotNull(walk); - assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + try (TreeWalk walk = TreeWalk.forPath(db, path, commit2.getTree())) { + assertNotNull(walk); + assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + } } @Test @@ -225,15 +227,16 @@ public class CommitCommandTest extends RepositoryTestCase { assertNotNull(repo); addRepoToClose(repo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - assertEquals(uri, generator.getConfigUrl()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + assertEquals(uri, generator.getConfigUrl()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + } } assertEquals(commit, repo.resolve(Constants.HEAD)); @@ -275,15 +278,16 @@ public class CommitCommandTest extends RepositoryTestCase { assertNotNull(repo); addRepoToClose(repo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(path, generator.getPath()); - assertEquals(commit2, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - assertEquals(uri, generator.getConfigUrl()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(path, generator.getPath()); + assertEquals(commit2, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + assertEquals(uri, generator.getConfigUrl()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + } } assertEquals(commit2, repo.resolve(Constants.HEAD)); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/EolRepositoryTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/EolRepositoryTest.java index 47806cb99d..d0dfd1ab92 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/EolRepositoryTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/EolRepositoryTest.java @@ -142,8 +142,6 @@ public class EolRepositoryTest extends RepositoryTestCase { protected String CONTENT_MIXED; - private TreeWalk walk; - /** work tree root .gitattributes */ private File dotGitattributes; @@ -689,27 +687,25 @@ public class EolRepositoryTest extends RepositoryTestCase { private void collectRepositoryState() throws Exception { dirCache = db.readDirCache(); - walk = beginWalk(); - if (dotGitattributes != null) - collectEntryContentAndAttributes(F, ".gitattributes", null); - collectEntryContentAndAttributes(F, fileCRLF.getName(), entryCRLF); - collectEntryContentAndAttributes(F, fileLF.getName(), entryLF); - collectEntryContentAndAttributes(F, fileMixed.getName(), entryMixed); - endWalk(); - } - - private TreeWalk beginWalk() throws Exception { - TreeWalk newWalk = new TreeWalk(db); - newWalk.addTree(new FileTreeIterator(db)); - newWalk.addTree(new DirCacheIterator(db.readDirCache())); - return newWalk; - } - - private void endWalk() throws IOException { - assertFalse("Not all files tested", walk.next()); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(new FileTreeIterator(db)); + walk.addTree(new DirCacheIterator(db.readDirCache())); + if (dotGitattributes != null) { + collectEntryContentAndAttributes(walk, F, ".gitattributes", + null); + } + collectEntryContentAndAttributes(walk, F, fileCRLF.getName(), + entryCRLF); + collectEntryContentAndAttributes(walk, F, fileLF.getName(), + entryLF); + collectEntryContentAndAttributes(walk, F, fileMixed.getName(), + entryMixed); + assertFalse("Not all files tested", walk.next()); + } } - private void collectEntryContentAndAttributes(FileMode type, String pathName, + private void collectEntryContentAndAttributes(TreeWalk walk, FileMode type, + String pathName, ActualEntry e) throws IOException { assertTrue("walk has entry", walk.next()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchAndPullCommandsRecurseSubmodulesTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchAndPullCommandsRecurseSubmodulesTest.java index 73a705b252..08ded559f5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchAndPullCommandsRecurseSubmodulesTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchAndPullCommandsRecurseSubmodulesTest.java @@ -343,19 +343,22 @@ public class FetchAndPullCommandsRecurseSubmodulesTest extends RepositoryTestCas private void assertSubmoduleFetchHeads(ObjectId expectedHead1, ObjectId expectedHead2) throws Exception { + Object newHead1 = null; + ObjectId newHead2 = null; try (SubmoduleWalk walk = SubmoduleWalk .forIndex(git2.getRepository())) { assertTrue(walk.next()); - Repository r = walk.getRepository(); - ObjectId newHead1 = r.resolve(Constants.FETCH_HEAD); - ObjectId newHead2; - try (SubmoduleWalk walk2 = SubmoduleWalk.forIndex(r)) { - assertTrue(walk2.next()); - newHead2 = walk2.getRepository().resolve(Constants.FETCH_HEAD); + try (Repository r = walk.getRepository()) { + newHead1 = r.resolve(Constants.FETCH_HEAD); + try (SubmoduleWalk walk2 = SubmoduleWalk.forIndex(r)) { + assertTrue(walk2.next()); + try (Repository r2 = walk2.getRepository()) { + newHead2 = r2.resolve(Constants.FETCH_HEAD); + } + } } - - assertEquals(expectedHead1, newHead1); - assertEquals(expectedHead2, newHead2); } + assertEquals(expectedHead1, newHead1); + assertEquals(expectedHead2, newHead2); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java index 4401bcedb3..7c8ec23848 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java @@ -57,12 +57,9 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; -import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Set; import org.eclipse.jgit.api.MergeResult.MergeStatus; import org.eclipse.jgit.api.RebaseCommand.InteractiveHandler; @@ -78,6 +75,7 @@ import org.eclipse.jgit.errors.AmbiguousObjectException; import org.eclipse.jgit.errors.IllegalTodoFileModification; import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.MissingObjectException; +import org.eclipse.jgit.events.ChangeRecorder; import org.eclipse.jgit.events.ListenerHandle; import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.lib.AbbreviatedObjectId; @@ -2014,10 +2012,9 @@ public class RebaseCommandTest extends RepositoryTestCase { checkoutBranch("refs/heads/topic"); writeTrashFile("sub/file0", "unstaged modified file0"); - Set<String> modifiedFiles = new HashSet<>(); + ChangeRecorder recorder = new ChangeRecorder(); ListenerHandle handle = db.getListenerList() - .addWorkingTreeModifiedListener( - event -> modifiedFiles.addAll(event.getModified())); + .addWorkingTreeModifiedListener(recorder); try { // rebase assertEquals(Status.OK, git.rebase() @@ -2035,9 +2032,8 @@ public class RebaseCommandTest extends RepositoryTestCase { + "[sub/file0, mode:100644, content:file0]", indexState(CONTENT)); assertEquals(RepositoryState.SAFE, db.getRepositoryState()); - List<String> modified = new ArrayList<>(modifiedFiles); - Collections.sort(modified); - assertEquals("[file1, sub/file0]", modified.toString()); + recorder.assertEvent(new String[] { "file1", "file2", "sub/file0" }, + new String[0]); } @Test @@ -2136,10 +2132,12 @@ public class RebaseCommandTest extends RepositoryTestCase { private List<DiffEntry> getStashedDiff() throws AmbiguousObjectException, IncorrectObjectTypeException, IOException, MissingObjectException { ObjectId stashId = db.resolve("stash@{0}"); - RevWalk revWalk = new RevWalk(db); - RevCommit stashCommit = revWalk.parseCommit(stashId); - List<DiffEntry> diffs = diffWorkingAgainstHead(stashCommit, revWalk); - return diffs; + try (RevWalk revWalk = new RevWalk(db)) { + RevCommit stashCommit = revWalk.parseCommit(stashId); + List<DiffEntry> diffs = diffWorkingAgainstHead(stashCommit, + revWalk); + return diffs; + } } private TreeWalk createTreeWalk() { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesHandlerTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesHandlerTest.java index 5868482c88..adc64d227e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesHandlerTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesHandlerTest.java @@ -69,83 +69,90 @@ public class AttributesHandlerTest extends RepositoryTestCase { private static final FileMode F = FileMode.REGULAR_FILE; - private TreeWalk walk; - @Test public void testExpandNonMacro1() throws Exception { setupRepo(null, null, null, "*.txt text"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("text")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("text")); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testExpandNonMacro2() throws Exception { setupRepo(null, null, null, "*.txt -text"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("-text")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("-text")); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testExpandNonMacro3() throws Exception { setupRepo(null, null, null, "*.txt !text"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("")); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testExpandNonMacro4() throws Exception { setupRepo(null, null, null, "*.txt text=auto"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("text=auto")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("text=auto")); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testExpandBuiltInMacro1() throws Exception { setupRepo(null, null, null, "*.txt binary"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("binary -diff -merge -text")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", + attrs("binary -diff -merge -text")); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testExpandBuiltInMacro2() throws Exception { setupRepo(null, null, null, "*.txt -binary"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("-binary diff merge text")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", + attrs("-binary diff merge text")); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testExpandBuiltInMacro3() throws Exception { setupRepo(null, null, null, "*.txt !binary"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -153,44 +160,48 @@ public class AttributesHandlerTest extends RepositoryTestCase { setupRepo( "[attr]foo a -b !c d=e", null, null, "*.txt foo"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("foo a -b d=e")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("foo a -b d=e")); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testCustomGlobalMacro2() throws Exception { setupRepo("[attr]foo a -b !c d=e", null, null, "*.txt -foo"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("-foo -a b d=e")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("-foo -a b d=e")); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testCustomGlobalMacro3() throws Exception { setupRepo("[attr]foo a -b !c d=e", null, null, "*.txt !foo"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("")); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testCustomGlobalMacro4() throws Exception { setupRepo("[attr]foo a -b !c d=e", null, null, "*.txt foo=bar"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("foo=bar a -b d=bar")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("foo=bar a -b d=bar")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -198,11 +209,12 @@ public class AttributesHandlerTest extends RepositoryTestCase { setupRepo("[attr]foo bar1", "[attr]foo bar2", null, "*.txt foo"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("foo bar2")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("foo bar2")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -211,12 +223,13 @@ public class AttributesHandlerTest extends RepositoryTestCase { null, "[attr]foo bar3", "*.txt foo"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("foo bar3")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("foo bar3")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -224,12 +237,13 @@ public class AttributesHandlerTest extends RepositoryTestCase { setupRepo("[attr]foo bar1", "[attr]foo bar2", "[attr]foo bar3", "*.txt foo"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("foo bar2")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("foo bar2")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -238,11 +252,12 @@ public class AttributesHandlerTest extends RepositoryTestCase { "[attr]foo x bar -foo", null, null, "*.txt foo"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("foo x bar")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("foo x bar")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -250,11 +265,12 @@ public class AttributesHandlerTest extends RepositoryTestCase { setupRepo( "[attr]foo x -bar\n[attr]bar y -foo", null, null, "*.txt foo"); - walk = beginWalk(); - assertIteration(D, "sub"); - assertIteration(F, "sub/.gitattributes"); - assertIteration(F, "sub/a.txt", attrs("foo x -bar -y")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/.gitattributes"); + assertIteration(walk, F, "sub/a.txt", attrs("foo x -bar -y")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -266,23 +282,30 @@ public class AttributesHandlerTest extends RepositoryTestCase { // apply to any of the files here. It would match for a // further subdirectory sub/sub. The sub/ rules must match // only for directories. - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "sub", attrs("global")); - assertIteration(F, "sub/.gitattributes", attrs("init top_sub")); - assertIteration(F, "sub/a.txt", attrs("init foo top top_sub")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "sub", attrs("global")); + assertIteration(walk, F, "sub/.gitattributes", + attrs("init top_sub")); + assertIteration(walk, F, "sub/a.txt", + attrs("init foo top top_sub")); + assertFalse("Not all files tested", walk.next()); + } // All right, let's see that they *do* apply in sub/sub: writeTrashFile("sub/sub/b.txt", "b"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "sub", attrs("global")); - assertIteration(F, "sub/.gitattributes", attrs("init top_sub")); - assertIteration(F, "sub/a.txt", attrs("init foo top top_sub")); - assertIteration(D, "sub/sub", attrs("init subsub2 top_sub global")); - assertIteration(F, "sub/sub/b.txt", - attrs("init foo subsub top top_sub")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "sub", attrs("global")); + assertIteration(walk, F, "sub/.gitattributes", + attrs("init top_sub")); + assertIteration(walk, F, "sub/a.txt", + attrs("init foo top top_sub")); + assertIteration(walk, D, "sub/sub", + attrs("init subsub2 top_sub global")); + assertIteration(walk, F, "sub/sub/b.txt", + attrs("init foo subsub top top_sub")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -293,16 +316,17 @@ public class AttributesHandlerTest extends RepositoryTestCase { writeTrashFile("sub/b.jar", "bj"); writeTrashFile("sub/b.xml", "bx"); // On foo.xml/bar.jar we must not have 'xml' - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo.xml", attrs("xml")); - assertIteration(F, "foo.xml/bar.jar", attrs("jar")); - assertIteration(F, "foo.xml/bar.xml", attrs("xml")); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(F, "sub/b.jar", attrs("jar")); - assertIteration(F, "sub/b.xml", attrs("xml")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo.xml", attrs("xml")); + assertIteration(walk, F, "foo.xml/bar.jar", attrs("jar")); + assertIteration(walk, F, "foo.xml/bar.xml", attrs("xml")); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, F, "sub/b.jar", attrs("jar")); + assertIteration(walk, F, "sub/b.xml", attrs("xml")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -314,18 +338,19 @@ public class AttributesHandlerTest extends RepositoryTestCase { writeTrashFile("sub/b.jar", "bj"); writeTrashFile("sub/b.xml", "bx"); writeTrashFile("sub/foo/b.jar", "bf"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo", attrs("xml")); - assertIteration(F, "foo/bar.jar", attrs("jar")); - assertIteration(F, "foo/bar.xml"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(F, "sub/b.jar", attrs("jar")); - assertIteration(F, "sub/b.xml"); - assertIteration(D, "sub/foo", attrs("sub xml")); - assertIteration(F, "sub/foo/b.jar", attrs("jar")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo", attrs("xml")); + assertIteration(walk, F, "foo/bar.jar", attrs("jar")); + assertIteration(walk, F, "foo/bar.xml"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, F, "sub/b.jar", attrs("jar")); + assertIteration(walk, F, "sub/b.xml"); + assertIteration(walk, D, "sub/foo", attrs("sub xml")); + assertIteration(walk, F, "sub/foo/b.jar", attrs("jar")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -337,18 +362,19 @@ public class AttributesHandlerTest extends RepositoryTestCase { writeTrashFile("sub/b.xml", "bx"); writeTrashFile("sub/foo/b.jar", "bf"); // On foo.xml/bar.jar we must not have 'xml' - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo"); - assertIteration(F, "foo/bar.jar", attrs("jar xml")); - assertIteration(F, "foo/bar.xml", attrs("xml")); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(F, "sub/b.jar", attrs("jar")); - assertIteration(F, "sub/b.xml"); - assertIteration(D, "sub/foo"); - assertIteration(F, "sub/foo/b.jar", attrs("jar")); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo"); + assertIteration(walk, F, "foo/bar.jar", attrs("jar xml")); + assertIteration(walk, F, "foo/bar.xml", attrs("xml")); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, F, "sub/b.jar", attrs("jar")); + assertIteration(walk, F, "sub/b.xml"); + assertIteration(walk, D, "sub/foo"); + assertIteration(walk, F, "sub/foo/b.jar", attrs("jar")); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -357,27 +383,29 @@ public class AttributesHandlerTest extends RepositoryTestCase { writeTrashFile("sub/a.txt", "1"); writeTrashFile("foo/sext", "2"); writeTrashFile("foo/s.txt", "3"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo"); - assertIteration(F, "foo/s.txt", attrs("bar")); - assertIteration(F, "foo/sext", attrs("bar")); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo"); + assertIteration(walk, F, "foo/s.txt", attrs("bar")); + assertIteration(walk, F, "foo/sext", attrs("bar")); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertFalse("Not all files tested", walk.next()); + } } @Test public void testPrefixMatchNot() throws Exception { setupRepo(null, null, "sub/new bar", null); writeTrashFile("sub/new/foo.txt", "1"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(D, "sub/new", attrs("bar")); - assertIteration(F, "sub/new/foo.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, D, "sub/new", attrs("bar")); + assertIteration(walk, F, "sub/new/foo.txt"); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -385,14 +413,15 @@ public class AttributesHandlerTest extends RepositoryTestCase { setupRepo(null, null, "s[t-v]b/n[de]w bar", null); writeTrashFile("sub/new/foo.txt", "1"); writeTrashFile("sub/ndw", "2"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(F, "sub/ndw", attrs("bar")); - assertIteration(D, "sub/new", attrs("bar")); - assertIteration(F, "sub/new/foo.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, F, "sub/ndw", attrs("bar")); + assertIteration(walk, D, "sub/new", attrs("bar")); + assertIteration(walk, F, "sub/new/foo.txt"); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -400,15 +429,16 @@ public class AttributesHandlerTest extends RepositoryTestCase { setupRepo(null, null, "sub/new/* bar", null); writeTrashFile("sub/new/foo.txt", "1"); writeTrashFile("sub/new/lower/foo.txt", "2"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(D, "sub/new"); - assertIteration(F, "sub/new/foo.txt", attrs("bar")); - assertIteration(D, "sub/new/lower", attrs("bar")); - assertIteration(F, "sub/new/lower/foo.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, D, "sub/new"); + assertIteration(walk, F, "sub/new/foo.txt", attrs("bar")); + assertIteration(walk, D, "sub/new/lower", attrs("bar")); + assertIteration(walk, F, "sub/new/lower/foo.txt"); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -417,20 +447,21 @@ public class AttributesHandlerTest extends RepositoryTestCase { writeTrashFile("sub/new/foo.txt", "1"); writeTrashFile("foo/sub/new/foo.txt", "2"); writeTrashFile("sub/sub/new/foo.txt", "3"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo"); - assertIteration(D, "foo/sub"); - assertIteration(D, "foo/sub/new"); - assertIteration(F, "foo/sub/new/foo.txt"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(D, "sub/new", attrs("bar")); - assertIteration(F, "sub/new/foo.txt"); - assertIteration(D, "sub/sub"); - assertIteration(D, "sub/sub/new"); - assertIteration(F, "sub/sub/new/foo.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo"); + assertIteration(walk, D, "foo/sub"); + assertIteration(walk, D, "foo/sub/new"); + assertIteration(walk, F, "foo/sub/new/foo.txt"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, D, "sub/new", attrs("bar")); + assertIteration(walk, F, "sub/new/foo.txt"); + assertIteration(walk, D, "sub/sub"); + assertIteration(walk, D, "sub/sub/new"); + assertIteration(walk, F, "sub/sub/new/foo.txt"); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -438,17 +469,18 @@ public class AttributesHandlerTest extends RepositoryTestCase { setupRepo(null, null, "**/sub/new/ bar", null); writeTrashFile("sub/new/foo.txt", "1"); writeTrashFile("foo/sub/new/foo.txt", "2"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo"); - assertIteration(D, "foo/sub"); - assertIteration(D, "foo/sub/new", attrs("bar")); - assertIteration(F, "foo/sub/new/foo.txt"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(D, "sub/new", attrs("bar")); - assertIteration(F, "sub/new/foo.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo"); + assertIteration(walk, D, "foo/sub"); + assertIteration(walk, D, "foo/sub/new", attrs("bar")); + assertIteration(walk, F, "foo/sub/new/foo.txt"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, D, "sub/new", attrs("bar")); + assertIteration(walk, F, "sub/new/foo.txt"); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -457,20 +489,21 @@ public class AttributesHandlerTest extends RepositoryTestCase { writeTrashFile("sub/new/foo.txt", "1"); writeTrashFile("foo/sub/new/foo.txt", "2"); writeTrashFile("sub/sub/new/foo.txt", "3"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo"); - assertIteration(D, "foo/sub"); - assertIteration(D, "foo/sub/new", attrs("bar")); - assertIteration(F, "foo/sub/new/foo.txt"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(D, "sub/new", attrs("bar")); - assertIteration(F, "sub/new/foo.txt"); - assertIteration(D, "sub/sub"); - assertIteration(D, "sub/sub/new", attrs("bar")); - assertIteration(F, "sub/sub/new/foo.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo"); + assertIteration(walk, D, "foo/sub"); + assertIteration(walk, D, "foo/sub/new", attrs("bar")); + assertIteration(walk, F, "foo/sub/new/foo.txt"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, D, "sub/new", attrs("bar")); + assertIteration(walk, F, "sub/new/foo.txt"); + assertIteration(walk, D, "sub/sub"); + assertIteration(walk, D, "sub/sub/new", attrs("bar")); + assertIteration(walk, F, "sub/sub/new/foo.txt"); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -479,20 +512,21 @@ public class AttributesHandlerTest extends RepositoryTestCase { writeTrashFile("sub/new/foo.txt", "1"); writeTrashFile("foo/sub/new/foo.txt", "2"); writeTrashFile("sub/sub/new/foo.txt", "3"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo"); - assertIteration(D, "foo/sub"); - assertIteration(D, "foo/sub/new", attrs("bar")); - assertIteration(F, "foo/sub/new/foo.txt"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(D, "sub/new", attrs("bar")); - assertIteration(F, "sub/new/foo.txt"); - assertIteration(D, "sub/sub"); - assertIteration(D, "sub/sub/new", attrs("bar")); - assertIteration(F, "sub/sub/new/foo.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo"); + assertIteration(walk, D, "foo/sub"); + assertIteration(walk, D, "foo/sub/new", attrs("bar")); + assertIteration(walk, F, "foo/sub/new/foo.txt"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, D, "sub/new", attrs("bar")); + assertIteration(walk, F, "sub/new/foo.txt"); + assertIteration(walk, D, "sub/sub"); + assertIteration(walk, D, "sub/sub/new", attrs("bar")); + assertIteration(walk, F, "sub/sub/new/foo.txt"); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -500,17 +534,18 @@ public class AttributesHandlerTest extends RepositoryTestCase { setupRepo(null, null, "s[uv]b/n*/ bar", null); writeTrashFile("sub/new/foo.txt", "1"); writeTrashFile("foo/sub/new/foo.txt", "2"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo"); - assertIteration(D, "foo/sub"); - assertIteration(D, "foo/sub/new"); - assertIteration(F, "foo/sub/new/foo.txt"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(D, "sub/new", attrs("bar")); - assertIteration(F, "sub/new/foo.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo"); + assertIteration(walk, D, "foo/sub"); + assertIteration(walk, D, "foo/sub/new"); + assertIteration(walk, F, "foo/sub/new/foo.txt"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, D, "sub/new", attrs("bar")); + assertIteration(walk, F, "sub/new/foo.txt"); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -519,30 +554,32 @@ public class AttributesHandlerTest extends RepositoryTestCase { writeTrashFile("sub/new/foo.txt", "1"); writeTrashFile("foo/sub/new/foo.txt", "2"); writeTrashFile("foo/new", "3"); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(D, "foo"); - assertIteration(F, "foo/new"); - assertIteration(D, "foo/sub"); - assertIteration(D, "foo/sub/new", attrs("bar")); - assertIteration(F, "foo/sub/new/foo.txt"); - assertIteration(D, "sub"); - assertIteration(F, "sub/a.txt"); - assertIteration(D, "sub/new", attrs("bar")); - assertIteration(F, "sub/new/foo.txt"); - endWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, D, "foo"); + assertIteration(walk, F, "foo/new"); + assertIteration(walk, D, "foo/sub"); + assertIteration(walk, D, "foo/sub/new", attrs("bar")); + assertIteration(walk, F, "foo/sub/new/foo.txt"); + assertIteration(walk, D, "sub"); + assertIteration(walk, F, "sub/a.txt"); + assertIteration(walk, D, "sub/new", attrs("bar")); + assertIteration(walk, F, "sub/new/foo.txt"); + assertFalse("Not all files tested", walk.next()); + } } private static Collection<Attribute> attrs(String s) { return new AttributesRule("*", s).getAttributes(); } - private void assertIteration(FileMode type, String pathName) + private void assertIteration(TreeWalk walk, FileMode type, String pathName) throws IOException { - assertIteration(type, pathName, Collections.<Attribute> emptyList()); + assertIteration(walk, type, pathName, + Collections.<Attribute> emptyList()); } - private void assertIteration(FileMode type, String pathName, + private void assertIteration(TreeWalk walk, FileMode type, String pathName, Collection<Attribute> expectedAttrs) throws IOException { assertTrue("walk has entry", walk.next()); assertEquals(pathName, walk.getPathString()); @@ -611,8 +648,4 @@ public class AttributesHandlerTest extends RepositoryTestCase { newWalk.addTree(new FileTreeIterator(db)); return newWalk; } - - private void endWalk() throws IOException { - assertFalse("Not all files tested", walk.next()); - } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeDirCacheIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeDirCacheIteratorTest.java index 837de74818..cb37d8978a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeDirCacheIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeDirCacheIteratorTest.java @@ -78,8 +78,6 @@ public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase { private Git git; - private TreeWalk walk; - @Override @Before public void setUp() throws Exception { @@ -105,23 +103,25 @@ public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase { // Adds file to index git.add().addFilepattern(".").call(); - walk = beginWalk(); - - assertIteration(F, ".gitattributes"); - assertIteration(F, "readme.txt", asList(EOL_LF)); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, F, "readme.txt", asList(EOL_LF)); - assertIteration(D, "src"); + assertIteration(walk, D, "src"); - assertIteration(D, "src/config"); - assertIteration(F, "src/config/.gitattributes"); - assertIteration(F, "src/config/readme.txt", asList(DELTA_UNSET)); - assertIteration(F, "src/config/windows.file", null); - assertIteration(F, "src/config/windows.txt", asList(DELTA_UNSET)); + assertIteration(walk, D, "src/config"); + assertIteration(walk, F, "src/config/.gitattributes"); + assertIteration(walk, F, "src/config/readme.txt", + asList(DELTA_UNSET)); + assertIteration(walk, F, "src/config/windows.file", null); + assertIteration(walk, F, "src/config/windows.txt", + asList(DELTA_UNSET)); - assertIteration(F, "windows.file", null); - assertIteration(F, "windows.txt", asList(EOL_LF)); + assertIteration(walk, F, "windows.file", null); + assertIteration(walk, F, "windows.txt", asList(EOL_LF)); - endWalk(); + assertFalse("Not all files tested", walk.next()); + } } /** @@ -138,17 +138,18 @@ public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase { // Adds file to index git.add().addFilepattern(".").call(); - walk = beginWalk(); - assertIteration(F, "l0.txt"); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, "l0.txt"); - assertIteration(D, "level1"); - assertIteration(F, "level1/l1.txt"); + assertIteration(walk, D, "level1"); + assertIteration(walk, F, "level1/l1.txt"); - assertIteration(D, "level1/level2"); - assertIteration(F, "level1/level2/l2.txt"); + assertIteration(walk, D, "level1/level2"); + assertIteration(walk, F, "level1/level2/l2.txt"); - endWalk(); + assertFalse("Not all files tested", walk.next()); + } } /** @@ -166,18 +167,19 @@ public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase { // Adds file to index git.add().addFilepattern(".").call(); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); - assertIteration(F, "l0.txt"); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, F, "l0.txt"); - assertIteration(D, "level1"); - assertIteration(F, "level1/l1.txt"); + assertIteration(walk, D, "level1"); + assertIteration(walk, F, "level1/l1.txt"); - assertIteration(D, "level1/level2"); - assertIteration(F, "level1/level2/l2.txt"); + assertIteration(walk, D, "level1/level2"); + assertIteration(walk, F, "level1/level2/l2.txt"); - endWalk(); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -191,18 +193,19 @@ public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase { // Adds file to index git.add().addFilepattern(".").call(); - walk = beginWalk(); - assertIteration(F, ".gitattributes"); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); - assertIteration(D, "levelA"); - assertIteration(F, "levelA/.gitattributes"); - assertIteration(F, "levelA/lA.txt"); + assertIteration(walk, D, "levelA"); + assertIteration(walk, F, "levelA/.gitattributes"); + assertIteration(walk, F, "levelA/lA.txt"); - assertIteration(D, "levelB"); - assertIteration(F, "levelB/.gitattributes"); + assertIteration(walk, D, "levelB"); + assertIteration(walk, F, "levelB/.gitattributes"); - endWalk(); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -215,25 +218,27 @@ public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase { // Adds file to index git.add().addFilepattern(".").call(); - walk = beginWalk(); - assertIteration(F, "gitattributes"); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, "gitattributes"); - assertIteration(F, "l0.txt"); + assertIteration(walk, F, "l0.txt"); - assertIteration(D, "levelA"); - assertIteration(F, "levelA/file.gitattributes"); - assertIteration(F, "levelA/lA.txt"); + assertIteration(walk, D, "levelA"); + assertIteration(walk, F, "levelA/file.gitattributes"); + assertIteration(walk, F, "levelA/lA.txt"); - endWalk(); + assertFalse("Not all files tested", walk.next()); + } } - private void assertIteration(FileMode type, String pathName) + private void assertIteration(TreeWalk walk, FileMode type, String pathName) throws IOException { - assertIteration(type, pathName, Collections.<Attribute> emptyList()); + assertIteration(walk, type, pathName, + Collections.<Attribute> emptyList()); } - private void assertIteration(FileMode type, String pathName, + private void assertIteration(TreeWalk walk, FileMode type, String pathName, List<Attribute> nodeAttrs) throws IOException { assertTrue("walk has entry", walk.next()); assertEquals(pathName, walk.getPathString()); @@ -243,14 +248,14 @@ public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase { AttributesNode attributesNode = itr.getEntryAttributesNode(db .newObjectReader()); - assertAttributesNode(pathName, attributesNode, nodeAttrs); + assertAttributesNode(walk, pathName, attributesNode, nodeAttrs); if (D.equals(type)) walk.enterSubtree(); } - private void assertAttributesNode(String pathName, + private void assertAttributesNode(TreeWalk walk, String pathName, AttributesNode attributesNode, List<Attribute> nodeAttrs) throws IOException { if (attributesNode == null) @@ -292,8 +297,4 @@ public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase { newWalk.addTree(new DirCacheIterator(db.readDirCache())); return newWalk; } - - private void endWalk() throws IOException { - assertFalse("Not all files tested", walk.next()); - } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeWorkingTreeIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeWorkingTreeIteratorTest.java index b159cca0d5..9991ae59b0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeWorkingTreeIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeWorkingTreeIteratorTest.java @@ -77,8 +77,6 @@ public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase { private static Attribute DELTA_UNSET = new Attribute("delta", State.UNSET); - private TreeWalk walk; - @Test public void testRules() throws Exception { @@ -102,24 +100,26 @@ public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase { writeTrashFile("src/config/windows.file", ""); writeTrashFile("src/config/windows.txt", ""); - walk = beginWalk(); - - assertIteration(F, ".gitattributes"); - assertIteration(F, "global.txt", asList(EOL_LF)); - assertIteration(F, "readme.txt", asList(EOL_LF)); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, F, "global.txt", asList(EOL_LF)); + assertIteration(walk, F, "readme.txt", asList(EOL_LF)); - assertIteration(D, "src"); + assertIteration(walk, D, "src"); - assertIteration(D, "src/config"); - assertIteration(F, "src/config/.gitattributes"); - assertIteration(F, "src/config/readme.txt", asList(DELTA_UNSET)); - assertIteration(F, "src/config/windows.file", null); - assertIteration(F, "src/config/windows.txt", asList(DELTA_UNSET)); + assertIteration(walk, D, "src/config"); + assertIteration(walk, F, "src/config/.gitattributes"); + assertIteration(walk, F, "src/config/readme.txt", + asList(DELTA_UNSET)); + assertIteration(walk, F, "src/config/windows.file", null); + assertIteration(walk, F, "src/config/windows.txt", + asList(DELTA_UNSET)); - assertIteration(F, "windows.file", null); - assertIteration(F, "windows.txt", asList(EOL_LF)); + assertIteration(walk, F, "windows.file", null); + assertIteration(walk, F, "windows.txt", asList(EOL_LF)); - endWalk(); + assertFalse("Not all files tested", walk.next()); + } } /** @@ -134,17 +134,17 @@ public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase { writeTrashFile("level1/l1.txt", ""); writeTrashFile("level1/level2/l2.txt", ""); - walk = beginWalk(); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, "l0.txt"); - assertIteration(F, "l0.txt"); + assertIteration(walk, D, "level1"); + assertIteration(walk, F, "level1/l1.txt"); - assertIteration(D, "level1"); - assertIteration(F, "level1/l1.txt"); + assertIteration(walk, D, "level1/level2"); + assertIteration(walk, F, "level1/level2/l2.txt"); - assertIteration(D, "level1/level2"); - assertIteration(F, "level1/level2/l2.txt"); - - endWalk(); + assertFalse("Not all files tested", walk.next()); + } } /** @@ -160,18 +160,18 @@ public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase { writeTrashFile("level1/l1.txt", ""); writeTrashFile("level1/level2/l2.txt", ""); - walk = beginWalk(); - - assertIteration(F, ".gitattributes"); - assertIteration(F, "l0.txt"); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); + assertIteration(walk, F, "l0.txt"); - assertIteration(D, "level1"); - assertIteration(F, "level1/l1.txt"); + assertIteration(walk, D, "level1"); + assertIteration(walk, F, "level1/l1.txt"); - assertIteration(D, "level1/level2"); - assertIteration(F, "level1/level2/l2.txt"); + assertIteration(walk, D, "level1/level2"); + assertIteration(walk, F, "level1/level2/l2.txt"); - endWalk(); + assertFalse("Not all files tested", walk.next()); + } } @Test @@ -183,26 +183,27 @@ public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase { writeTrashFile("levelA/lA.txt", ""); - walk = beginWalk(); - - assertIteration(F, ".gitattributes"); + try (TreeWalk walk = beginWalk()) { + assertIteration(walk, F, ".gitattributes"); - assertIteration(D, "levelA"); - assertIteration(F, "levelA/.gitattributes"); - assertIteration(F, "levelA/lA.txt"); + assertIteration(walk, D, "levelA"); + assertIteration(walk, F, "levelA/.gitattributes"); + assertIteration(walk, F, "levelA/lA.txt"); - assertIteration(D, "levelB"); - assertIteration(F, "levelB/.gitattributes"); + assertIteration(walk, D, "levelB"); + assertIteration(walk, F, "levelB/.gitattributes"); - endWalk(); + assertFalse("Not all files tested", walk.next()); + } } - private void assertIteration(FileMode type, String pathName) + private void assertIteration(TreeWalk walk, FileMode type, String pathName) throws IOException { - assertIteration(type, pathName, Collections.<Attribute> emptyList()); + assertIteration(walk, type, pathName, + Collections.<Attribute> emptyList()); } - private void assertIteration(FileMode type, String pathName, + private void assertIteration(TreeWalk walk, FileMode type, String pathName, List<Attribute> nodeAttrs) throws IOException { assertTrue("walk has entry", walk.next()); @@ -212,13 +213,13 @@ public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase { assertNotNull("has tree", itr); AttributesNode attributesNode = itr.getEntryAttributesNode(); - assertAttributesNode(pathName, attributesNode, nodeAttrs); + assertAttributesNode(walk, pathName, attributesNode, nodeAttrs); if (D.equals(type)) walk.enterSubtree(); } - private void assertAttributesNode(String pathName, + private void assertAttributesNode(TreeWalk walk, String pathName, AttributesNode attributesNode, List<Attribute> nodeAttrs) throws IOException { if (attributesNode == null) @@ -259,8 +260,4 @@ public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase { newWalk.addTree(new FileTreeIterator(db)); return newWalk; } - - private void endWalk() throws IOException { - assertFalse("Not all files tested", walk.next()); - } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/TreeWalkAttributeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/TreeWalkAttributeTest.java index d6aead4a52..0291fcc1e9 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/TreeWalkAttributeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/TreeWalkAttributeTest.java @@ -131,6 +131,12 @@ public class TreeWalkAttributeTest extends RepositoryTestCase { @Override @After public void tearDown() throws Exception { + if (walk != null) { + walk.close(); + } + if (ci_walk != null) { + ci_walk.close(); + } super.tearDown(); if (customAttributeFile != null) customAttributeFile.delete(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java index cbc0761463..45046a3999 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java @@ -63,6 +63,7 @@ import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.WorkingTreeIterator; import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.SystemReader; +import org.junit.After; import org.junit.Test; /** @@ -79,6 +80,13 @@ public class IgnoreNodeTest extends RepositoryTestCase { private TreeWalk walk; + @After + public void closeWalk() { + if (walk != null) { + walk.close(); + } + } + @Test public void testSimpleRootGitIgnoreGlobalIgnore() throws IOException { writeIgnoreFile(".gitignore", "x"); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollectorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollectorTest.java index 5a5ae1d7a3..cfc275a7de 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollectorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollectorTest.java @@ -723,7 +723,7 @@ public class DfsGarbageCollectorTest { DfsPackDescription t1 = odb.newPack(INSERT); try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) { - new ReftableWriter().begin(out).finish(); + new ReftableWriter(out).begin().finish(); t1.addFileExt(REFTABLE); } odb.commitPack(Collections.singleton(t1), null); @@ -755,7 +755,7 @@ public class DfsGarbageCollectorTest { DfsPackDescription t1 = odb.newPack(INSERT); try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) { - new ReftableWriter().begin(out).finish(); + new ReftableWriter(out).begin().finish(); t1.addFileExt(REFTABLE); } odb.commitPack(Collections.singleton(t1), null); @@ -795,10 +795,10 @@ public class DfsGarbageCollectorTest { Ref next = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, "refs/heads/next", commit0.copy()); try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) { - ReftableWriter w = new ReftableWriter(); + ReftableWriter w = new ReftableWriter(out); w.setMinUpdateIndex(42); w.setMaxUpdateIndex(42); - w.begin(out); + w.begin(); w.sortAndWriteRefs(Collections.singleton(next)); w.finish(); t1.addFileExt(REFTABLE); @@ -877,10 +877,10 @@ public class DfsGarbageCollectorTest { Ref newNext = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, NEXT, commit1); try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) { - ReftableWriter w = new ReftableWriter(); - w.setMinUpdateIndex(1); - w.setMaxUpdateIndex(1); - w.begin(out); + ReftableWriter w = new ReftableWriter(out) + .setMinUpdateIndex(1) + .setMaxUpdateIndex(1) + .begin(); w.writeRef(newNext, 1); w.finish(); t1.addFileExt(REFTABLE); @@ -929,10 +929,10 @@ public class DfsGarbageCollectorTest { Ref newNext = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, NEXT, commit1); try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) { - ReftableWriter w = new ReftableWriter(); - w.setMinUpdateIndex(1); - w.setMaxUpdateIndex(1); - w.begin(out); + ReftableWriter w = new ReftableWriter(out) + .setMinUpdateIndex(1) + .setMaxUpdateIndex(1) + .begin(); w.writeRef(newNext, 1); w.finish(); t1.addFileExt(REFTABLE); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java index e71ee6d25f..4d36144212 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java @@ -92,18 +92,22 @@ public class DfsInserterTest { @Test public void testReadFromInserterSmallObjects() throws IOException { - ObjectInserter ins = db.newObjectInserter(); - ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo")); - ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar")); - assertEquals(0, db.getObjectDatabase().listPacks().size()); + try (ObjectInserter ins = db.newObjectInserter()) { + ObjectId id1 = ins.insert(Constants.OBJ_BLOB, + Constants.encode("foo")); + ObjectId id2 = ins.insert(Constants.OBJ_BLOB, + Constants.encode("bar")); + assertEquals(0, db.getObjectDatabase().listPacks().size()); - ObjectReader reader = ins.newReader(); - assertSame(ins, reader.getCreatedFromInserter()); - assertEquals("foo", readString(reader.open(id1))); - assertEquals("bar", readString(reader.open(id2))); - assertEquals(0, db.getObjectDatabase().listPacks().size()); - ins.flush(); - assertEquals(1, db.getObjectDatabase().listPacks().size()); + try (ObjectReader reader = ins.newReader()) { + assertSame(ins, reader.getCreatedFromInserter()); + assertEquals("foo", readString(reader.open(id1))); + assertEquals("bar", readString(reader.open(id2))); + assertEquals(0, db.getObjectDatabase().listPacks().size()); + ins.flush(); + assertEquals(1, db.getObjectDatabase().listPacks().size()); + } + } } @Test @@ -114,17 +118,19 @@ public class DfsInserterTest { .setBlockLimit(2048)); byte[] data = new TestRng(JGitTestUtil.getName()).nextBytes(8192); - DfsInserter ins = (DfsInserter) db.newObjectInserter(); - ins.setCompressionLevel(Deflater.NO_COMPRESSION); - ObjectId id1 = ins.insert(Constants.OBJ_BLOB, data); - assertEquals(0, db.getObjectDatabase().listPacks().size()); + try (DfsInserter ins = (DfsInserter) db.newObjectInserter()) { + ins.setCompressionLevel(Deflater.NO_COMPRESSION); + ObjectId id1 = ins.insert(Constants.OBJ_BLOB, data); + assertEquals(0, db.getObjectDatabase().listPacks().size()); - ObjectReader reader = ins.newReader(); - assertSame(ins, reader.getCreatedFromInserter()); - assertTrue(Arrays.equals(data, readStream(reader.open(id1)))); - assertEquals(0, db.getObjectDatabase().listPacks().size()); - ins.flush(); + try (ObjectReader reader = ins.newReader()) { + assertSame(ins, reader.getCreatedFromInserter()); + assertTrue(Arrays.equals(data, readStream(reader.open(id1)))); + assertEquals(0, db.getObjectDatabase().listPacks().size()); + } + ins.flush(); + } List<DfsPackDescription> packs = db.getObjectDatabase().listPacks(); assertEquals(1, packs.size()); assertTrue(packs.get(0).getFileSize(PackExt.PACK) > 2048); @@ -132,48 +138,58 @@ public class DfsInserterTest { @Test public void testReadFromFallback() throws IOException { - ObjectInserter ins = db.newObjectInserter(); - ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo")); - ins.flush(); - ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar")); - assertEquals(1, db.getObjectDatabase().listPacks().size()); - - ObjectReader reader = ins.newReader(); - assertSame(ins, reader.getCreatedFromInserter()); - assertEquals("foo", readString(reader.open(id1))); - assertEquals("bar", readString(reader.open(id2))); - assertEquals(1, db.getObjectDatabase().listPacks().size()); - ins.flush(); - assertEquals(2, db.getObjectDatabase().listPacks().size()); + try (ObjectInserter ins = db.newObjectInserter()) { + ObjectId id1 = ins.insert(Constants.OBJ_BLOB, + Constants.encode("foo")); + ins.flush(); + ObjectId id2 = ins.insert(Constants.OBJ_BLOB, + Constants.encode("bar")); + assertEquals(1, db.getObjectDatabase().listPacks().size()); + + try (ObjectReader reader = ins.newReader()) { + assertSame(ins, reader.getCreatedFromInserter()); + assertEquals("foo", readString(reader.open(id1))); + assertEquals("bar", readString(reader.open(id2))); + assertEquals(1, db.getObjectDatabase().listPacks().size()); + } + ins.flush(); + assertEquals(2, db.getObjectDatabase().listPacks().size()); + } } @Test public void testReaderResolve() throws IOException { - ObjectInserter ins = db.newObjectInserter(); - ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo")); - ins.flush(); - ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar")); - String abbr1 = ObjectId.toString(id1).substring(0, 4); - String abbr2 = ObjectId.toString(id2).substring(0, 4); - assertFalse(abbr1.equals(abbr2)); - - ObjectReader reader = ins.newReader(); - assertSame(ins, reader.getCreatedFromInserter()); - Collection<ObjectId> objs; - objs = reader.resolve(AbbreviatedObjectId.fromString(abbr1)); - assertEquals(1, objs.size()); - assertEquals(id1, objs.iterator().next()); - - objs = reader.resolve(AbbreviatedObjectId.fromString(abbr2)); - assertEquals(1, objs.size()); - assertEquals(id2, objs.iterator().next()); + try (ObjectInserter ins = db.newObjectInserter()) { + ObjectId id1 = ins.insert(Constants.OBJ_BLOB, + Constants.encode("foo")); + ins.flush(); + ObjectId id2 = ins.insert(Constants.OBJ_BLOB, + Constants.encode("bar")); + String abbr1 = ObjectId.toString(id1).substring(0, 4); + String abbr2 = ObjectId.toString(id2).substring(0, 4); + assertFalse(abbr1.equals(abbr2)); + + try (ObjectReader reader = ins.newReader()) { + assertSame(ins, reader.getCreatedFromInserter()); + Collection<ObjectId> objs; + objs = reader.resolve(AbbreviatedObjectId.fromString(abbr1)); + assertEquals(1, objs.size()); + assertEquals(id1, objs.iterator().next()); + + objs = reader.resolve(AbbreviatedObjectId.fromString(abbr2)); + assertEquals(1, objs.size()); + assertEquals(id2, objs.iterator().next()); + } + } } @Test public void testGarbageSelectivelyVisible() throws IOException { - ObjectInserter ins = db.newObjectInserter(); - ObjectId fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo")); - ins.flush(); + ObjectId fooId; + try (ObjectInserter ins = db.newObjectInserter()) { + fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo")); + ins.flush(); + } assertEquals(1, db.getObjectDatabase().listPacks().size()); // Make pack 0 garbage. @@ -187,36 +203,40 @@ public class DfsInserterTest { @Test public void testInserterIgnoresUnreachable() throws IOException { - ObjectInserter ins = db.newObjectInserter(); - ObjectId fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo")); - ins.flush(); - assertEquals(1, db.getObjectDatabase().listPacks().size()); + ObjectId fooId; + try (ObjectInserter ins = db.newObjectInserter()) { + fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo")); + ins.flush(); + assertEquals(1, db.getObjectDatabase().listPacks().size()); - // Make pack 0 garbage. - db.getObjectDatabase().listPacks().get(0).setPackSource(PackSource.UNREACHABLE_GARBAGE); + // Make pack 0 garbage. + db.getObjectDatabase().listPacks().get(0) + .setPackSource(PackSource.UNREACHABLE_GARBAGE); - // We shouldn't be able to see foo because it's garbage. - assertFalse(db.getObjectDatabase().has(fooId, true)); + // We shouldn't be able to see foo because it's garbage. + assertFalse(db.getObjectDatabase().has(fooId, true)); - // But if we re-insert foo, it should become visible again. - ins.insert(Constants.OBJ_BLOB, Constants.encode("foo")); - ins.flush(); + // But if we re-insert foo, it should become visible again. + ins.insert(Constants.OBJ_BLOB, Constants.encode("foo")); + ins.flush(); + } assertTrue(db.getObjectDatabase().has(fooId, true)); // Verify that we have a foo in both packs, and 1 of them is garbage. - DfsReader reader = new DfsReader(db.getObjectDatabase()); - DfsPackFile packs[] = db.getObjectDatabase().getPacks(); - Set<PackSource> pack_sources = new HashSet<>(); + try (DfsReader reader = new DfsReader(db.getObjectDatabase())) { + DfsPackFile packs[] = db.getObjectDatabase().getPacks(); + Set<PackSource> pack_sources = new HashSet<>(); - assertEquals(2, packs.length); + assertEquals(2, packs.length); - pack_sources.add(packs[0].getPackDescription().getPackSource()); - pack_sources.add(packs[1].getPackDescription().getPackSource()); + pack_sources.add(packs[0].getPackDescription().getPackSource()); + pack_sources.add(packs[1].getPackDescription().getPackSource()); - assertTrue(packs[0].hasObject(reader, fooId)); - assertTrue(packs[1].hasObject(reader, fooId)); - assertTrue(pack_sources.contains(PackSource.UNREACHABLE_GARBAGE)); - assertTrue(pack_sources.contains(PackSource.INSERT)); + assertTrue(packs[0].hasObject(reader, fooId)); + assertTrue(packs[1].hasObject(reader, fooId)); + assertTrue(pack_sources.contains(PackSource.UNREACHABLE_GARBAGE)); + assertTrue(pack_sources.contains(PackSource.INSERT)); + } } @Test @@ -237,17 +257,20 @@ public class DfsInserterTest { assertEquals(2, db.getObjectDatabase().listPacks().size()); // Verify that we have a foo in both INSERT packs. - DfsReader reader = new DfsReader(db.getObjectDatabase()); - DfsPackFile packs[] = db.getObjectDatabase().getPacks(); - - assertEquals(2, packs.length); - DfsPackFile p1 = packs[0]; - assertEquals(PackSource.INSERT, p1.getPackDescription().getPackSource()); - assertTrue(p1.hasObject(reader, fooId)); - - DfsPackFile p2 = packs[1]; - assertEquals(PackSource.INSERT, p2.getPackDescription().getPackSource()); - assertTrue(p2.hasObject(reader, fooId)); + try (DfsReader reader = new DfsReader(db.getObjectDatabase())) { + DfsPackFile packs[] = db.getObjectDatabase().getPacks(); + + assertEquals(2, packs.length); + DfsPackFile p1 = packs[0]; + assertEquals(PackSource.INSERT, + p1.getPackDescription().getPackSource()); + assertTrue(p1.hasObject(reader, fooId)); + + DfsPackFile p2 = packs[1]; + assertEquals(PackSource.INSERT, + p2.getPackDescription().getPackSource()); + assertTrue(p2.hasObject(reader, fooId)); + } } private static String readString(ObjectLoader loader) throws IOException { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileSnapshotTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileSnapshotTest.java index d5bc61a692..9016a02b0b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileSnapshotTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileSnapshotTest.java @@ -295,9 +295,8 @@ public class PackFileSnapshotTest extends RepositoryTestCase { Files.copy(src, dstOut); return dst; } - } else { - return Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING); } + return Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING); } private Path copyPack(Path base, String srcSuffix, String dstSuffix) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackWriterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackWriterTest.java index 1e2341b6a5..3f281c49d2 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackWriterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackWriterTest.java @@ -468,22 +468,22 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { @Test public void testDeltaStatistics() throws Exception { config.setDeltaCompress(true); + // TestRepository will close repo FileRepository repo = createBareRepository(); ArrayList<RevObject> blobs = new ArrayList<>(); try (TestRepository<FileRepository> testRepo = new TestRepository<>( repo)) { blobs.add(testRepo.blob(genDeltableData(1000))); blobs.add(testRepo.blob(genDeltableData(1005))); - } - - try (PackWriter pw = new PackWriter(repo)) { - NullProgressMonitor m = NullProgressMonitor.INSTANCE; - pw.preparePack(blobs.iterator()); - pw.writePack(m, m, os); - PackStatistics stats = pw.getStatistics(); - assertEquals(1, stats.getTotalDeltas()); - assertTrue("Delta bytes not set.", - stats.byObjectType(OBJ_BLOB).getDeltaBytes() > 0); + try (PackWriter pw = new PackWriter(repo)) { + NullProgressMonitor m = NullProgressMonitor.INSTANCE; + pw.preparePack(blobs.iterator()); + pw.writePack(m, m, os); + PackStatistics stats = pw.getStatistics(); + assertEquals(1, stats.getTotalDeltas()); + assertTrue("Delta bytes not set.", + stats.byObjectType(OBJ_BLOB).getDeltaBytes() > 0); + } } } @@ -535,6 +535,7 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { @Test public void testExclude() throws Exception { + // TestRepository closes repo FileRepository repo = createBareRepository(); try (TestRepository<FileRepository> testRepo = new TestRepository<>( @@ -568,98 +569,102 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { @Test public void testShallowIsMinimalDepth1() throws Exception { - FileRepository repo = setupRepoForShallowFetch(); - - PackIndex idx = writeShallowPack(repo, 1, wants(c2), NONE, NONE); - assertContent(idx, Arrays.asList(c2.getId(), c2.getTree().getId(), - contentA.getId(), contentB.getId())); - - // Client already has blobs A and B, verify those are not packed. - idx = writeShallowPack(repo, 1, wants(c5), haves(c2), shallows(c2)); - assertContent(idx, Arrays.asList(c5.getId(), c5.getTree().getId(), - contentC.getId(), contentD.getId(), contentE.getId())); + try (FileRepository repo = setupRepoForShallowFetch()) { + PackIndex idx = writeShallowPack(repo, 1, wants(c2), NONE, NONE); + assertContent(idx, Arrays.asList(c2.getId(), c2.getTree().getId(), + contentA.getId(), contentB.getId())); + + // Client already has blobs A and B, verify those are not packed. + idx = writeShallowPack(repo, 1, wants(c5), haves(c2), shallows(c2)); + assertContent(idx, Arrays.asList(c5.getId(), c5.getTree().getId(), + contentC.getId(), contentD.getId(), contentE.getId())); + } } @Test public void testShallowIsMinimalDepth2() throws Exception { - FileRepository repo = setupRepoForShallowFetch(); - - PackIndex idx = writeShallowPack(repo, 2, wants(c2), NONE, NONE); - assertContent(idx, - Arrays.asList(c1.getId(), c2.getId(), c1.getTree().getId(), - c2.getTree().getId(), contentA.getId(), - contentB.getId())); - - // Client already has blobs A and B, verify those are not packed. - idx = writeShallowPack(repo, 2, wants(c5), haves(c1, c2), shallows(c1)); - assertContent(idx, - Arrays.asList(c4.getId(), c5.getId(), c4.getTree().getId(), - c5.getTree().getId(), contentC.getId(), - contentD.getId(), contentE.getId())); + try (FileRepository repo = setupRepoForShallowFetch()) { + PackIndex idx = writeShallowPack(repo, 2, wants(c2), NONE, NONE); + assertContent(idx, + Arrays.asList(c1.getId(), c2.getId(), c1.getTree().getId(), + c2.getTree().getId(), contentA.getId(), + contentB.getId())); + + // Client already has blobs A and B, verify those are not packed. + idx = writeShallowPack(repo, 2, wants(c5), haves(c1, c2), + shallows(c1)); + assertContent(idx, + Arrays.asList(c4.getId(), c5.getId(), c4.getTree().getId(), + c5.getTree().getId(), contentC.getId(), + contentD.getId(), contentE.getId())); + } } @Test public void testShallowFetchShallowParentDepth1() throws Exception { - FileRepository repo = setupRepoForShallowFetch(); - - PackIndex idx = writeShallowPack(repo, 1, wants(c5), NONE, NONE); - assertContent(idx, - Arrays.asList(c5.getId(), c5.getTree().getId(), - contentA.getId(), contentB.getId(), contentC.getId(), - contentD.getId(), contentE.getId())); - - idx = writeShallowPack(repo, 1, wants(c4), haves(c5), shallows(c5)); - assertContent(idx, Arrays.asList(c4.getId(), c4.getTree().getId())); + try (FileRepository repo = setupRepoForShallowFetch()) { + PackIndex idx = writeShallowPack(repo, 1, wants(c5), NONE, NONE); + assertContent(idx, Arrays.asList(c5.getId(), c5.getTree().getId(), + contentA.getId(), contentB.getId(), contentC.getId(), + contentD.getId(), contentE.getId())); + + idx = writeShallowPack(repo, 1, wants(c4), haves(c5), shallows(c5)); + assertContent(idx, Arrays.asList(c4.getId(), c4.getTree().getId())); + } } @Test public void testShallowFetchShallowParentDepth2() throws Exception { - FileRepository repo = setupRepoForShallowFetch(); - - PackIndex idx = writeShallowPack(repo, 2, wants(c5), NONE, NONE); - assertContent(idx, - Arrays.asList(c4.getId(), c5.getId(), c4.getTree().getId(), - c5.getTree().getId(), contentA.getId(), - contentB.getId(), contentC.getId(), contentD.getId(), - contentE.getId())); - - idx = writeShallowPack(repo, 2, wants(c3), haves(c4, c5), shallows(c4)); - assertContent(idx, Arrays.asList(c2.getId(), c3.getId(), - c2.getTree().getId(), c3.getTree().getId())); + try (FileRepository repo = setupRepoForShallowFetch()) { + PackIndex idx = writeShallowPack(repo, 2, wants(c5), NONE, NONE); + assertContent(idx, + Arrays.asList(c4.getId(), c5.getId(), c4.getTree().getId(), + c5.getTree().getId(), contentA.getId(), + contentB.getId(), contentC.getId(), + contentD.getId(), contentE.getId())); + + idx = writeShallowPack(repo, 2, wants(c3), haves(c4, c5), + shallows(c4)); + assertContent(idx, Arrays.asList(c2.getId(), c3.getId(), + c2.getTree().getId(), c3.getTree().getId())); + } } @Test public void testShallowFetchShallowAncestorDepth1() throws Exception { - FileRepository repo = setupRepoForShallowFetch(); - - PackIndex idx = writeShallowPack(repo, 1, wants(c5), NONE, NONE); - assertContent(idx, - Arrays.asList(c5.getId(), c5.getTree().getId(), - contentA.getId(), contentB.getId(), contentC.getId(), - contentD.getId(), contentE.getId())); - - idx = writeShallowPack(repo, 1, wants(c3), haves(c5), shallows(c5)); - assertContent(idx, Arrays.asList(c3.getId(), c3.getTree().getId())); + try (FileRepository repo = setupRepoForShallowFetch()) { + PackIndex idx = writeShallowPack(repo, 1, wants(c5), NONE, NONE); + assertContent(idx, Arrays.asList(c5.getId(), c5.getTree().getId(), + contentA.getId(), contentB.getId(), contentC.getId(), + contentD.getId(), contentE.getId())); + + idx = writeShallowPack(repo, 1, wants(c3), haves(c5), shallows(c5)); + assertContent(idx, Arrays.asList(c3.getId(), c3.getTree().getId())); + } } @Test public void testShallowFetchShallowAncestorDepth2() throws Exception { - FileRepository repo = setupRepoForShallowFetch(); - - PackIndex idx = writeShallowPack(repo, 2, wants(c5), NONE, NONE); - assertContent(idx, - Arrays.asList(c4.getId(), c5.getId(), c4.getTree().getId(), - c5.getTree().getId(), contentA.getId(), - contentB.getId(), contentC.getId(), contentD.getId(), - contentE.getId())); - - idx = writeShallowPack(repo, 2, wants(c2), haves(c4, c5), shallows(c4)); - assertContent(idx, Arrays.asList(c1.getId(), c2.getId(), - c1.getTree().getId(), c2.getTree().getId())); + try (FileRepository repo = setupRepoForShallowFetch()) { + PackIndex idx = writeShallowPack(repo, 2, wants(c5), NONE, NONE); + assertContent(idx, + Arrays.asList(c4.getId(), c5.getId(), c4.getTree().getId(), + c5.getTree().getId(), contentA.getId(), + contentB.getId(), contentC.getId(), + contentD.getId(), contentE.getId())); + + idx = writeShallowPack(repo, 2, wants(c2), haves(c4, c5), + shallows(c4)); + assertContent(idx, Arrays.asList(c1.getId(), c2.getId(), + c1.getTree().getId(), c2.getTree().getId())); + } } private FileRepository setupRepoForShallowFetch() throws Exception { FileRepository repo = createBareRepository(); + // TestRepository will close the repo, but we need to return an open + // one! + repo.incrementOpen(); try (TestRepository<Repository> r = new TestRepository<>(repo)) { BranchBuilder bb = r.branch("refs/heads/master"); contentA = r.blob("A"); @@ -680,8 +685,9 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { private static PackIndex writePack(FileRepository repo, Set<? extends ObjectId> want, Set<ObjectIdSet> excludeObjects) throws IOException { - RevWalk walk = new RevWalk(repo); - return writePack(repo, walk, 0, want, NONE, excludeObjects); + try (RevWalk walk = new RevWalk(repo)) { + return writePack(repo, walk, 0, want, NONE, excludeObjects); + } } private static PackIndex writeShallowPack(FileRepository repo, int depth, @@ -689,9 +695,10 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { Set<? extends ObjectId> shallow) throws IOException { // During negotiation, UploadPack would have set up a DepthWalk and // marked the client's "shallow" commits. Emulate that here. - DepthWalk.RevWalk walk = new DepthWalk.RevWalk(repo, depth - 1); - walk.assumeShallow(shallow); - return writePack(repo, walk, depth, want, have, EMPTY_ID_SET); + try (DepthWalk.RevWalk walk = new DepthWalk.RevWalk(repo, depth - 1)) { + walk.assumeShallow(shallow); + return writePack(repo, walk, depth, want, have, EMPTY_ID_SET); + } } private static PackIndex writePack(FileRepository repo, RevWalk walk, @@ -707,6 +714,7 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { if (depth > 0) { pw.setShallowPack(depth, null); } + // ow doesn't need to be closed; caller closes walk. ObjectWalk ow = walk.toObjectWalkWithSameObjects(); pw.preparePack(NullProgressMonitor.INSTANCE, ow, want, have, NONE); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java index 43f30d8ca8..5100c1ce29 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java @@ -145,13 +145,14 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { } File theDir = new File(repo1Parent, Constants.DOT_GIT); - FileRepository r = (FileRepository) new FileRepositoryBuilder() - .setGitDir(theDir).build(); - assertEqualsPath(theDir, r.getDirectory()); - assertEqualsPath(repo1Parent, r.getWorkTree()); - assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); - assertEqualsPath(new File(theDir, Constants.OBJECTS), r.getObjectDatabase() - .getDirectory()); + try (FileRepository r = (FileRepository) new FileRepositoryBuilder() + .setGitDir(theDir).build()) { + assertEqualsPath(theDir, r.getDirectory()); + assertEqualsPath(repo1Parent, r.getWorkTree()); + assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); + assertEqualsPath(new File(theDir, Constants.OBJECTS), + r.getObjectDatabase().getDirectory()); + } } /** @@ -170,14 +171,15 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { } File theDir = new File(repo1Parent, Constants.DOT_GIT); - FileRepository r = (FileRepository) new FileRepositoryBuilder() + try (FileRepository r = (FileRepository) new FileRepositoryBuilder() .setGitDir(theDir).setWorkTree(repo1Parent.getParentFile()) - .build(); - assertEqualsPath(theDir, r.getDirectory()); - assertEqualsPath(repo1Parent.getParentFile(), r.getWorkTree()); - assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); - assertEqualsPath(new File(theDir, Constants.OBJECTS), r.getObjectDatabase() - .getDirectory()); + .build()) { + assertEqualsPath(theDir, r.getDirectory()); + assertEqualsPath(repo1Parent.getParentFile(), r.getWorkTree()); + assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); + assertEqualsPath(new File(theDir, Constants.OBJECTS), + r.getObjectDatabase().getDirectory()); + } } /** @@ -195,13 +197,14 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { } File theDir = new File(repo1Parent, Constants.DOT_GIT); - FileRepository r = (FileRepository) new FileRepositoryBuilder() - .setWorkTree(repo1Parent).build(); - assertEqualsPath(theDir, r.getDirectory()); - assertEqualsPath(repo1Parent, r.getWorkTree()); - assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); - assertEqualsPath(new File(theDir, Constants.OBJECTS), r.getObjectDatabase() - .getDirectory()); + try (FileRepository r = (FileRepository) new FileRepositoryBuilder() + .setWorkTree(repo1Parent).build()) { + assertEqualsPath(theDir, r.getDirectory()); + assertEqualsPath(repo1Parent, r.getWorkTree()); + assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); + assertEqualsPath(new File(theDir, Constants.OBJECTS), + r.getObjectDatabase().getDirectory()); + } } /** @@ -224,13 +227,14 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { } File theDir = new File(repo1Parent, Constants.DOT_GIT); - FileRepository r = (FileRepository) new FileRepositoryBuilder() - .setGitDir(theDir).build(); - assertEqualsPath(theDir, r.getDirectory()); - assertEqualsPath(workdir, r.getWorkTree()); - assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); - assertEqualsPath(new File(theDir, Constants.OBJECTS), r.getObjectDatabase() - .getDirectory()); + try (FileRepository r = (FileRepository) new FileRepositoryBuilder() + .setGitDir(theDir).build()) { + assertEqualsPath(theDir, r.getDirectory()); + assertEqualsPath(workdir, r.getWorkTree()); + assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); + assertEqualsPath(new File(theDir, Constants.OBJECTS), + r.getObjectDatabase().getDirectory()); + } } /** @@ -253,13 +257,14 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { } File theDir = new File(repo1Parent, Constants.DOT_GIT); - FileRepository r = (FileRepository) new FileRepositoryBuilder() - .setGitDir(theDir).build(); - assertEqualsPath(theDir, r.getDirectory()); - assertEqualsPath(workdir, r.getWorkTree()); - assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); - assertEqualsPath(new File(theDir, Constants.OBJECTS), r.getObjectDatabase() - .getDirectory()); + try (FileRepository r = (FileRepository) new FileRepositoryBuilder() + .setGitDir(theDir).build()) { + assertEqualsPath(theDir, r.getDirectory()); + assertEqualsPath(workdir, r.getWorkTree()); + assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); + assertEqualsPath(new File(theDir, Constants.OBJECTS), + r.getObjectDatabase().getDirectory()); + } } /** @@ -306,17 +311,20 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { // open when we create it we won't write the object file out as a loose // object (as it already exists in the pack). // - final Repository newdb = createBareRepository(); - try (ObjectInserter oi = newdb.newObjectInserter()) { - final ObjectId treeId = oi.insert(new TreeFormatter()); - assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", - treeId.name()); + try (Repository newdb = createBareRepository()) { + try (ObjectInserter oi = newdb.newObjectInserter()) { + final ObjectId treeId = oi.insert(new TreeFormatter()); + assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", + treeId.name()); + } + + final File o = new File( + new File(new File(newdb.getDirectory(), Constants.OBJECTS), + "4b"), + "825dc642cb6eb9a060e54bf8d69288fbee4904"); + assertTrue("Exists " + o, o.isFile()); + assertTrue("Read-only " + o, !o.canWrite()); } - - final File o = new File(new File(new File(newdb.getDirectory(), - Constants.OBJECTS), "4b"), "825dc642cb6eb9a060e54bf8d69288fbee4904"); - assertTrue("Exists " + o, o.isFile()); - assertTrue("Read-only " + o, !o.canWrite()); } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java index 11d6439931..53d13f1d60 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java @@ -187,6 +187,19 @@ public class MergedReftableTest { } } + @Test + public void tableByIDDeletion() throws IOException { + List<Ref> delta1 = Arrays.asList( + ref("refs/heads/apple", 1), + ref("refs/heads/master", 2)); + List<Ref> delta2 = Arrays.asList(ref("refs/heads/master", 3)); + + MergedReftable mr = merge(write(delta1), write(delta2)); + try (RefCursor rc = mr.byObjectId(id(2))) { + assertFalse(rc.next()); + } + } + @SuppressWarnings("boxing") @Test public void fourTableScan() throws IOException { @@ -225,29 +238,6 @@ public class MergedReftableTest { } @Test - public void scanDuplicates() throws IOException { - List<Ref> delta1 = Arrays.asList( - ref("refs/heads/apple", 1), - ref("refs/heads/banana", 2)); - List<Ref> delta2 = Arrays.asList( - ref("refs/heads/apple", 3), - ref("refs/heads/apple", 4)); - - MergedReftable mr = merge(write(delta1, 1000), write(delta2, 2000)); - try (RefCursor rc = mr.allRefs()) { - assertTrue(rc.next()); - assertEquals("refs/heads/apple", rc.getRef().getName()); - assertEquals(id(3), rc.getRef().getObjectId()); - assertEquals(2000, rc.getRef().getUpdateIndex()); - assertTrue(rc.next()); - assertEquals("refs/heads/banana", rc.getRef().getName()); - assertEquals(id(2), rc.getRef().getObjectId()); - assertEquals(1000, rc.getRef().getUpdateIndex()); - assertFalse(rc.next()); - } - } - - @Test public void scanIncludeDeletes() throws IOException { List<Ref> delta1 = Arrays.asList(ref("refs/heads/next", 4)); List<Ref> delta2 = Arrays.asList(delete("refs/heads/next")); @@ -297,10 +287,10 @@ public class MergedReftableTest { @Test public void missedUpdate() throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(buf) .setMinUpdateIndex(1) .setMaxUpdateIndex(3) - .begin(buf); + .begin(); writer.writeRef(ref("refs/heads/a", 1), 1); writer.writeRef(ref("refs/heads/c", 3), 3); writer.finish(); @@ -337,13 +327,13 @@ public class MergedReftableTest { List<Ref> delta2 = Arrays.asList(delete("refs/heads/next")); List<Ref> delta3 = Arrays.asList(ref("refs/heads/master", 8)); - ReftableCompactor compactor = new ReftableCompactor(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ReftableCompactor compactor = new ReftableCompactor(out); compactor.addAll(Arrays.asList( read(write(delta1)), read(write(delta2)), read(write(delta3)))); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - compactor.compact(out); + compactor.compact(); byte[] table = out.toByteArray(); ReftableReader reader = read(table); @@ -414,7 +404,7 @@ public class MergedReftableTest { } private static MergedReftable merge(byte[]... table) { - List<Reftable> stack = new ArrayList<>(table.length); + List<ReftableReader> stack = new ArrayList<>(table.length); for (byte[] b : table) { stack.add(read(b)); } @@ -461,10 +451,10 @@ public class MergedReftableTest { private byte[] write(Collection<Ref> refs, long updateIndex) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - new ReftableWriter() + new ReftableWriter(buffer) .setMinUpdateIndex(updateIndex) .setMaxUpdateIndex(updateIndex) - .begin(buffer) + .begin() .sortAndWriteRefs(refs) .finish(); return buffer.toByteArray(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableCompactorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableCompactorTest.java index 1ea73097fe..9a7f240951 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableCompactorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableCompactorTest.java @@ -67,9 +67,10 @@ public class ReftableCompactorTest { @Test public void noTables() throws IOException { - ReftableCompactor compactor = new ReftableCompactor(); + ReftableCompactor compactor; try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { - compactor.compact(out); + compactor = new ReftableCompactor(out); + compactor.compact(); } Stats stats = compactor.getStats(); assertEquals(0, stats.minUpdateIndex()); @@ -81,10 +82,10 @@ public class ReftableCompactorTest { public void oneTable() throws IOException { byte[] inTab; try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(inBuf) .setMinUpdateIndex(0) .setMaxUpdateIndex(0) - .begin(inBuf); + .begin(); writer.writeRef(ref(MASTER, 1)); writer.finish(); @@ -92,10 +93,11 @@ public class ReftableCompactorTest { } byte[] outTab; - ReftableCompactor compactor = new ReftableCompactor(); + ReftableCompactor compactor; try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) { + compactor = new ReftableCompactor(outBuf); compactor.tryAddFirst(read(inTab)); - compactor.compact(outBuf); + compactor.compact(); outTab = outBuf.toByteArray(); } Stats stats = compactor.getStats(); @@ -116,10 +118,10 @@ public class ReftableCompactorTest { public void twoTablesOneRef() throws IOException { byte[] inTab1; try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(inBuf) .setMinUpdateIndex(0) .setMaxUpdateIndex(0) - .begin(inBuf); + .begin(); writer.writeRef(ref(MASTER, 1)); writer.finish(); @@ -128,10 +130,10 @@ public class ReftableCompactorTest { byte[] inTab2; try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(inBuf) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(inBuf); + .begin(); writer.writeRef(ref(MASTER, 2)); writer.finish(); @@ -139,10 +141,11 @@ public class ReftableCompactorTest { } byte[] outTab; - ReftableCompactor compactor = new ReftableCompactor(); + ReftableCompactor compactor; try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) { + compactor = new ReftableCompactor(outBuf); compactor.addAll(Arrays.asList(read(inTab1), read(inTab2))); - compactor.compact(outBuf); + compactor.compact(); outTab = outBuf.toByteArray(); } Stats stats = compactor.getStats(); @@ -163,10 +166,10 @@ public class ReftableCompactorTest { public void twoTablesTwoRefs() throws IOException { byte[] inTab1; try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(inBuf) .setMinUpdateIndex(0) .setMaxUpdateIndex(0) - .begin(inBuf); + .begin(); writer.writeRef(ref(MASTER, 1)); writer.writeRef(ref(NEXT, 2)); @@ -176,10 +179,10 @@ public class ReftableCompactorTest { byte[] inTab2; try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(inBuf) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(inBuf); + .begin(); writer.writeRef(ref(MASTER, 3)); writer.finish(); @@ -187,10 +190,11 @@ public class ReftableCompactorTest { } byte[] outTab; - ReftableCompactor compactor = new ReftableCompactor(); + ReftableCompactor compactor; try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) { + compactor = new ReftableCompactor(outBuf); compactor.addAll(Arrays.asList(read(inTab1), read(inTab2))); - compactor.compact(outBuf); + compactor.compact(); outTab = outBuf.toByteArray(); } Stats stats = compactor.getStats(); @@ -216,10 +220,10 @@ public class ReftableCompactorTest { public void twoTablesIncludeOneDelete() throws IOException { byte[] inTab1; try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(inBuf) .setMinUpdateIndex(0) .setMaxUpdateIndex(0) - .begin(inBuf); + .begin(); writer.writeRef(ref(MASTER, 1)); writer.finish(); @@ -228,10 +232,10 @@ public class ReftableCompactorTest { byte[] inTab2; try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(inBuf) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(inBuf); + .begin(); writer.writeRef(tombstone(MASTER)); writer.finish(); @@ -239,11 +243,12 @@ public class ReftableCompactorTest { } byte[] outTab; - ReftableCompactor compactor = new ReftableCompactor(); + ReftableCompactor compactor; try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) { + compactor = new ReftableCompactor(outBuf); compactor.setIncludeDeletes(true); compactor.addAll(Arrays.asList(read(inTab1), read(inTab2))); - compactor.compact(outBuf); + compactor.compact(); outTab = outBuf.toByteArray(); } Stats stats = compactor.getStats(); @@ -261,10 +266,10 @@ public class ReftableCompactorTest { public void twoTablesNotIncludeOneDelete() throws IOException { byte[] inTab1; try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(inBuf) .setMinUpdateIndex(0) .setMaxUpdateIndex(0) - .begin(inBuf); + .begin(); writer.writeRef(ref(MASTER, 1)); writer.finish(); @@ -273,10 +278,10 @@ public class ReftableCompactorTest { byte[] inTab2; try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(inBuf) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(inBuf); + .begin(); writer.writeRef(tombstone(MASTER)); writer.finish(); @@ -284,11 +289,12 @@ public class ReftableCompactorTest { } byte[] outTab; - ReftableCompactor compactor = new ReftableCompactor(); + ReftableCompactor compactor; try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) { + compactor = new ReftableCompactor(outBuf); compactor.setIncludeDeletes(false); compactor.addAll(Arrays.asList(read(inTab1), read(inTab2))); - compactor.compact(outBuf); + compactor.compact(); outTab = outBuf.toByteArray(); } Stats stats = compactor.getStats(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java index 0e33fa6e76..45e6c7d128 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java @@ -66,6 +66,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.concurrent.locks.ReentrantLock; +import java.util.stream.Collectors; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.storage.io.BlockSource; @@ -76,6 +78,7 @@ import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.ReflogEntry; import org.eclipse.jgit.lib.SymbolicRef; +import org.hamcrest.Matchers; import org.junit.Test; public class ReftableTest { @@ -134,9 +137,9 @@ public class ReftableTest { byte[] table; ReftableConfig cfg = new ReftableConfig(); cfg.setIndexObjects(false); - ReftableWriter writer = new ReftableWriter().setConfig(cfg); try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) { - writer.begin(buf); + ReftableWriter writer = new ReftableWriter(buf).setConfig(cfg); + writer.begin(); assertEquals(92, writer.estimateTotalBytes()); writer.writeRef(exp); assertEquals(expBytes, writer.estimateTotalBytes()); @@ -160,9 +163,9 @@ public class ReftableTest { int expBytes = 147860; byte[] table; - ReftableWriter writer = new ReftableWriter().setConfig(cfg); try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) { - writer.begin(buf); + ReftableWriter writer = new ReftableWriter(buf).setConfig(cfg); + writer.begin(); writer.sortAndWriteRefs(refs); assertEquals(expBytes, writer.estimateTotalBytes()); writer.finish(); @@ -418,27 +421,29 @@ public class ReftableTest { } @Test - public void invalidRefWriteOrder() throws IOException { + public void invalidRefWriteOrderSortAndWrite() { Ref master = ref(MASTER, 1); - Ref next = ref(NEXT, 2); - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(new ByteArrayOutputStream()) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(new ByteArrayOutputStream()); + .begin(); + + List<Ref> refs = new ArrayList<>(); + refs.add(master); + refs.add(master); - writer.writeRef(next); IllegalArgumentException e = assertThrows( IllegalArgumentException.class, - () -> writer.writeRef(master)); + () -> writer.sortAndWriteRefs(refs)); assertThat(e.getMessage(), containsString("records must be increasing")); } @Test public void invalidReflogWriteOrderUpdateIndex() throws IOException { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(new ByteArrayOutputStream()) .setMinUpdateIndex(1) .setMaxUpdateIndex(2) - .begin(new ByteArrayOutputStream()); + .begin(); PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60); String msg = "test"; @@ -451,10 +456,10 @@ public class ReftableTest { @Test public void invalidReflogWriteOrderName() throws IOException { - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(new ByteArrayOutputStream()) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(new ByteArrayOutputStream()); + .begin(); PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60); String msg = "test"; @@ -473,10 +478,10 @@ public class ReftableTest { String msg = "test"; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(buffer) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(buffer); + .begin(); writer.writeRef(master); writer.writeRef(next); @@ -523,16 +528,95 @@ public class ReftableTest { } @Test + public void reflogReader() throws IOException { + Ref master = ref(MASTER, 1); + Ref next = ref(NEXT, 2); + + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + ReftableWriter writer = new ReftableWriter(buffer).setMinUpdateIndex(1) + .setMaxUpdateIndex(1).begin(); + + writer.writeRef(master); + writer.writeRef(next); + + PersonIdent who1 = new PersonIdent("Log", "Ger", 1500079709, -8 * 60); + writer.writeLog(MASTER, 3, who1, ObjectId.zeroId(), id(1), "1"); + PersonIdent who2 = new PersonIdent("Log", "Ger", 1500079710, -8 * 60); + writer.writeLog(MASTER, 2, who2, id(1), id(2), "2"); + PersonIdent who3 = new PersonIdent("Log", "Ger", 1500079711, -8 * 60); + writer.writeLog(MASTER, 1, who3, id(2), id(3), "3"); + + writer.finish(); + byte[] table = buffer.toByteArray(); + + ReentrantLock lock = new ReentrantLock(); + ReftableReader t = read(table); + ReftableReflogReader rlr = new ReftableReflogReader(lock, t, MASTER); + + assertEquals(rlr.getLastEntry().getWho(), who1); + List<PersonIdent> all = rlr.getReverseEntries().stream() + .map(x -> x.getWho()).collect(Collectors.toList()); + Matchers.contains(all, who3, who2, who1); + + assertEquals(rlr.getReverseEntry(1).getWho(), who2); + + List<ReflogEntry> reverse2 = rlr.getReverseEntries(2); + Matchers.contains(reverse2, who3, who2); + + List<PersonIdent> more = rlr.getReverseEntries(4).stream() + .map(x -> x.getWho()).collect(Collectors.toList()); + assertEquals(all, more); + } + + @Test + public void allRefs() throws IOException { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + ReftableConfig cfg = new ReftableConfig(); + cfg.setRefBlockSize(1024); + cfg.setLogBlockSize(1024); + cfg.setAlignBlocks(true); + ReftableWriter writer = new ReftableWriter(buffer) + .setMinUpdateIndex(1) + .setMaxUpdateIndex(1) + .setConfig(cfg) + .begin(); + PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60); + + // Fill out the 1st ref block. + List<String> names = new ArrayList<>(); + for (int i = 0; i < 4; i++) { + String name = new String(new char[220]).replace("\0", String.format("%c", i + 'a')); + names.add(name); + writer.writeRef(ref(name, i)); + } + + // Add some log data. + writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), "msg"); + writer.finish(); + byte[] table = buffer.toByteArray(); + + ReftableReader t = read(table); + RefCursor c = t.allRefs(); + + int j = 0; + while (c.next()) { + assertEquals(names.get(j), c.getRef().getName()); + j++; + } + } + + + @Test public void reflogSeek() throws IOException { PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60); String msg = "test"; String msgNext = "test next"; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(buffer) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(buffer); + .begin(); writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), msg); writer.writeLog(NEXT, 1, who, ObjectId.zeroId(), id(2), msgNext); @@ -572,10 +656,10 @@ public class ReftableTest { PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(buffer) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(buffer); + .begin(); writer.writeLog("branchname", 1, who, ObjectId.zeroId(), id(1), "branchname"); @@ -596,10 +680,10 @@ public class ReftableTest { String msg = "test"; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - ReftableWriter writer = new ReftableWriter() + ReftableWriter writer = new ReftableWriter(buffer) .setMinUpdateIndex(1) .setMaxUpdateIndex(1) - .begin(buffer); + .begin(); writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), msg); writer.writeLog(NEXT, 1, who, ObjectId.zeroId(), id(2), msg); writer.finish(); @@ -648,8 +732,8 @@ public class ReftableTest { cfg.setLogBlockSize(2048); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - ReftableWriter writer = new ReftableWriter(cfg); - writer.setMinUpdateIndex(1).setMaxUpdateIndex(1).begin(buffer); + ReftableWriter writer = new ReftableWriter(cfg, buffer); + writer.setMinUpdateIndex(1).setMaxUpdateIndex(1).begin(); List<Ref> refs = new ArrayList<>(); for (int i = 1; i <= 5670; i++) { @@ -768,7 +852,7 @@ public class ReftableTest { cfg.setRefBlockSize(64); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - ReftableWriter writer = new ReftableWriter(cfg).begin(buffer); + ReftableWriter writer = new ReftableWriter(cfg, buffer).begin(); writer.writeRef(ref("refs/heads/i-am-not-a-teapot", 1)); writer.finish(); fail("expected BlockSizeTooSmallException"); @@ -853,8 +937,8 @@ public class ReftableTest { private byte[] write(Collection<Ref> refs) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - stats = new ReftableWriter() - .begin(buffer) + stats = new ReftableWriter(buffer) + .begin() .sortAndWriteRefs(refs) .finish() .getStats(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftree/RefTreeDatabaseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftree/RefTreeDatabaseTest.java index c3f5baa67b..50e0ed27c0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftree/RefTreeDatabaseTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftree/RefTreeDatabaseTest.java @@ -441,8 +441,9 @@ public class RefTreeDatabaseTest { ReceiveCommand cmd = command(null, B, "refs/txn/tmp"); BatchRefUpdate batch = refdb.newBatchUpdate(); batch.addCommand(cmd); - batch.execute(new RevWalk(repo), NullProgressMonitor.INSTANCE); - + try (RevWalk rw = new RevWalk(repo)) { + batch.execute(rw, NullProgressMonitor.INSTANCE); + } assertEquals(REJECTED_OTHER_REASON, cmd.getResult()); assertEquals(MessageFormat.format(JGitText.get().invalidRefName, "refs/txn/tmp"), cmd.getMessage()); @@ -461,8 +462,9 @@ public class RefTreeDatabaseTest { ReceiveCommand cmd = command(null, B, "refs/heads/pu.lock"); BatchRefUpdate batch = refdb.newBatchUpdate(); batch.addCommand(cmd); - batch.execute(new RevWalk(repo), NullProgressMonitor.INSTANCE); - + try (RevWalk rw = new RevWalk(repo)) { + batch.execute(rw, NullProgressMonitor.INSTANCE); + } assertEquals(REJECTED_OTHER_REASON, cmd.getResult()); assertEquals(JGitText.get().funnyRefname, cmd.getMessage()); assertEquals(txnId, getTxnCommitted()); @@ -481,7 +483,9 @@ public class RefTreeDatabaseTest { ReceiveCommand cmd = command(null, B, ORIG_HEAD); BatchRefUpdate batch = refdb.newBatchUpdate(); batch.addCommand(cmd); - batch.execute(new RevWalk(repo), NullProgressMonitor.INSTANCE); + try (RevWalk rw = new RevWalk(repo)) { + batch.execute(rw, NullProgressMonitor.INSTANCE); + } assertEquals(REJECTED_OTHER_REASON, cmd.getResult()); assertEquals( MessageFormat.format(JGitText.get().invalidRefName, ORIG_HEAD), @@ -500,7 +504,9 @@ public class RefTreeDatabaseTest { command(B, A, "refs/heads/masters")); BatchRefUpdate batchUpdate = refdb.newBatchUpdate(); batchUpdate.addCommand(commands); - batchUpdate.execute(new RevWalk(repo), NullProgressMonitor.INSTANCE); + try (RevWalk rw = new RevWalk(repo)) { + batchUpdate.execute(rw, NullProgressMonitor.INSTANCE); + } assertEquals(txnId, getTxnCommitted()); assertEquals(REJECTED_NONFASTFORWARD, @@ -523,7 +529,9 @@ public class RefTreeDatabaseTest { BatchRefUpdate batchUpdate = refdb.newBatchUpdate(); batchUpdate.setAllowNonFastForwards(true); batchUpdate.addCommand(commands); - batchUpdate.execute(new RevWalk(repo), NullProgressMonitor.INSTANCE); + try (RevWalk rw = new RevWalk(repo)) { + batchUpdate.execute(rw, NullProgressMonitor.INSTANCE); + } assertNotEquals(txnId, getTxnCommitted()); Map<String, Ref> refs = refdb.getRefs(ALL); @@ -547,13 +555,15 @@ public class RefTreeDatabaseTest { BatchRefUpdate batchUpdate = refdb.newBatchUpdate(); batchUpdate.setAllowNonFastForwards(true); batchUpdate.addCommand(commands); - batchUpdate.execute(new RevWalk(repo) { + try (RevWalk rw = new RevWalk(repo) { @Override public boolean isMergedInto(RevCommit base, RevCommit tip) { fail("isMergedInto() should not be called"); return false; } - }, NullProgressMonitor.INSTANCE); + }) { + batchUpdate.execute(rw, NullProgressMonitor.INSTANCE); + } assertNotEquals(txnId, getTxnCommitted()); Map<String, Ref> refs = refdb.getRefs(ALL); @@ -574,7 +584,9 @@ public class RefTreeDatabaseTest { BatchRefUpdate batchUpdate = refdb.newBatchUpdate(); batchUpdate.setAllowNonFastForwards(true); batchUpdate.addCommand(commands); - batchUpdate.execute(new RevWalk(repo), NullProgressMonitor.INSTANCE); + try (RevWalk rw = new RevWalk(repo)) { + batchUpdate.execute(rw, NullProgressMonitor.INSTANCE); + } assertEquals(txnId, getTxnCommitted()); assertEquals(LOCK_FAILURE, commands.get(0).getResult()); @@ -601,7 +613,9 @@ public class RefTreeDatabaseTest { BatchRefUpdate batchUpdate = refdb.newBatchUpdate(); batchUpdate.setAllowNonFastForwards(true); batchUpdate.addCommand(commands); - batchUpdate.execute(new RevWalk(repo), NullProgressMonitor.INSTANCE); + try (RevWalk rw = new RevWalk(repo)) { + batchUpdate.execute(rw, NullProgressMonitor.INSTANCE); + } assertNotEquals(txnId, getTxnCommitted()); assertEquals(OK, commands.get(0).getResult()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java index 057e0c881b..46fd902b6c 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java @@ -341,48 +341,56 @@ public class DirCacheCheckoutMaliciousPathTest extends RepositoryTestCase { String... path) throws GitAPIException, IOException { try (Git git = new Git(db); RevWalk revWalk = new RevWalk(git.getRepository())) { - ObjectInserter newObjectInserter; - newObjectInserter = git.getRepository().newObjectInserter(); - ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB, + ObjectId blobId; + try (ObjectInserter newObjectInserter = git.getRepository() + .newObjectInserter()) { + blobId = newObjectInserter.insert(Constants.OBJ_BLOB, "data".getBytes(UTF_8)); - newObjectInserter = git.getRepository().newObjectInserter(); + } FileMode mode = FileMode.REGULAR_FILE; ObjectId insertId = blobId; - for (int i = path.length - 1; i >= 0; --i) { - TreeFormatter treeFormatter = new TreeFormatter(); - treeFormatter.append("goodpath", mode, insertId); - insertId = newObjectInserter.insert(treeFormatter); - mode = FileMode.TREE; + try (ObjectInserter newObjectInserter = git.getRepository() + .newObjectInserter()) { + for (int i = path.length - 1; i >= 0; --i) { + TreeFormatter treeFormatter = new TreeFormatter(); + treeFormatter.append("goodpath", mode, insertId); + insertId = newObjectInserter.insert(treeFormatter); + mode = FileMode.TREE; + } } - newObjectInserter = git.getRepository().newObjectInserter(); - CommitBuilder commitBuilder = new CommitBuilder(); - commitBuilder.setAuthor(author); - commitBuilder.setCommitter(committer); - commitBuilder.setMessage("foo#1"); - commitBuilder.setTreeId(insertId); - ObjectId firstCommitId = newObjectInserter.insert(commitBuilder); - - newObjectInserter = git.getRepository().newObjectInserter(); - mode = FileMode.REGULAR_FILE; - insertId = blobId; - for (int i = path.length - 1; i >= 0; --i) { - TreeFormatter treeFormatter = new TreeFormatter(); - treeFormatter.append(path[i].getBytes(UTF_8), 0, - path[i].getBytes(UTF_8).length, - mode, insertId, true); - insertId = newObjectInserter.insert(treeFormatter); - mode = FileMode.TREE; + ObjectId firstCommitId; + try (ObjectInserter newObjectInserter = git.getRepository() + .newObjectInserter()) { + CommitBuilder commitBuilder = new CommitBuilder(); + commitBuilder.setAuthor(author); + commitBuilder.setCommitter(committer); + commitBuilder.setMessage("foo#1"); + commitBuilder.setTreeId(insertId); + firstCommitId = newObjectInserter.insert(commitBuilder); } + ObjectId commitId; + try (ObjectInserter newObjectInserter = git.getRepository() + .newObjectInserter()) { + mode = FileMode.REGULAR_FILE; + insertId = blobId; + for (int i = path.length - 1; i >= 0; --i) { + TreeFormatter treeFormatter = new TreeFormatter(); + treeFormatter.append(path[i].getBytes(UTF_8), 0, + path[i].getBytes(UTF_8).length, mode, insertId, + true); + insertId = newObjectInserter.insert(treeFormatter); + mode = FileMode.TREE; + } - // Create another commit - commitBuilder = new CommitBuilder(); - commitBuilder.setAuthor(author); - commitBuilder.setCommitter(committer); - commitBuilder.setMessage("foo#2"); - commitBuilder.setTreeId(insertId); - commitBuilder.setParentId(firstCommitId); - ObjectId commitId = newObjectInserter.insert(commitBuilder); - + // Create another commit + CommitBuilder commitBuilder = new CommitBuilder(); + commitBuilder.setAuthor(author); + commitBuilder.setCommitter(committer); + commitBuilder.setMessage("foo#2"); + commitBuilder.setTreeId(insertId); + commitBuilder.setParentId(firstCommitId); + commitId = newObjectInserter.insert(commitBuilder); + } if (!secondCheckout) git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId)) .setName("refs/heads/master").setCreateBranch(true).call(); 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 8092c3134b..a272c8f2ee 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 @@ -349,9 +349,11 @@ public class DirCacheCheckoutTest extends RepositoryTestCase { DirCacheEditor editor = dirCache.editor(); for (java.util.Map.Entry<String,String> e : indexEntries.entrySet()) { writeTrashFile(e.getKey(), e.getValue()); - ObjectInserter inserter = db.newObjectInserter(); - final ObjectId id = inserter.insert(Constants.OBJ_BLOB, + ObjectId id; + try (ObjectInserter inserter = db.newObjectInserter()) { + id = inserter.insert(Constants.OBJ_BLOB, Constants.encode(e.getValue())); + } editor.add(new DirCacheEditor.DeletePath(e.getKey())); editor.add(new DirCacheEditor.PathEdit(e.getKey()) { @Override diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java index ba5aaf1b18..cf954070e0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java @@ -81,10 +81,12 @@ public class IndexDiffTest extends RepositoryTestCase { static PathEdit add(final Repository db, final File workdir, final String path) throws FileNotFoundException, IOException { - ObjectInserter inserter = db.newObjectInserter(); final File f = new File(workdir, path); - final ObjectId id = inserter.insert(Constants.OBJ_BLOB, + ObjectId id; + try (ObjectInserter inserter = db.newObjectInserter()) { + id = inserter.insert(Constants.OBJ_BLOB, IO.readFully(f)); + } return new PathEdit(path) { @Override public void apply(DirCacheEntry ent) { @@ -122,9 +124,11 @@ public class IndexDiffTest extends RepositoryTestCase { public void testMissing() throws Exception { File file2 = writeTrashFile("file2", "file2"); File file3 = writeTrashFile("dir/file3", "dir/file3"); - Git git = Git.wrap(db); - git.add().addFilepattern("file2").addFilepattern("dir/file3").call(); - git.commit().setMessage("commit").call(); + try (Git git = new Git(db)) { + git.add().addFilepattern("file2").addFilepattern("dir/file3") + .call(); + git.commit().setMessage("commit").call(); + } assertTrue(file2.delete()); assertTrue(file3.delete()); IndexDiff diff = new IndexDiff(db, Constants.HEAD, diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdSerializerTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdSerializerTest.java index d98b792d75..f0733f4782 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdSerializerTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdSerializerTest.java @@ -88,9 +88,8 @@ public class ObjectIdSerializerTest { try (InputStream in = new FileInputStream(file)) { if (objectId == null) { return ObjectIdSerializer.read(in); - } else { - return ObjectIdSerializer.readWithoutMarker(in); } + return ObjectIdSerializer.readWithoutMarker(in); } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefDatabaseConflictingNamesTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefDatabaseConflictingNamesTest.java index cbb47fa829..3f77ca25a3 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefDatabaseConflictingNamesTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefDatabaseConflictingNamesTest.java @@ -67,9 +67,8 @@ public class RefDatabaseConflictingNamesTest { existing.put("refs/heads/a/b", null /* not used */); existing.put("refs/heads/q", null /* not used */); return existing; - } else { - return Collections.emptyMap(); } + return Collections.emptyMap(); } @Override diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java index f2f277c6ea..9be71c3a02 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java @@ -45,7 +45,7 @@ package org.eclipse.jgit.lib; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.IOException; @@ -78,7 +78,10 @@ public class ReflogConfigTest extends RepositoryTestCase { // set the logAllRefUpdates parameter to true and check it cfg.setBoolean("core", null, "logallrefupdates", true); cfg.save(); - assertTrue(cfg.get(CoreConfig.KEY).isLogAllRefUpdates()); + assertEquals(CoreConfig.LogRefUpdates.TRUE, + cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, + CoreConfig.LogRefUpdates.FALSE)); // do one commit and check that reflog size is increased to 1 commit("A Commit\n", commitTime, tz); @@ -90,13 +93,32 @@ public class ReflogConfigTest extends RepositoryTestCase { // set the logAllRefUpdates parameter to false and check it cfg.setBoolean("core", null, "logallrefupdates", false); cfg.save(); - assertFalse(cfg.get(CoreConfig.KEY).isLogAllRefUpdates()); + assertEquals(CoreConfig.LogRefUpdates.FALSE, + cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, + CoreConfig.LogRefUpdates.TRUE)); // do one commit and check that reflog size is 2 commit("A Commit\n", commitTime, tz); + commitTime += 60 * 1000; assertTrue( "Reflog for HEAD should contain two entries", db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 2); + + // set the logAllRefUpdates parameter to false and check it + cfg.setEnum("core", null, "logallrefupdates", + CoreConfig.LogRefUpdates.ALWAYS); + cfg.save(); + assertEquals(CoreConfig.LogRefUpdates.ALWAYS, + cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, + CoreConfig.LogRefUpdates.FALSE)); + + // do one commit and check that reflog size is 3 + commit("A Commit\n", commitTime, tz); + assertTrue("Reflog for HEAD should contain three entries", + db.getReflogReader(Constants.HEAD).getReverseEntries() + .size() == 3); } private void commit(String commitMsg, long commitTime, int tz) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/internal/BouncyCastleGpgKeyLocatorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/internal/BouncyCastleGpgKeyLocatorTest.java new file mode 100644 index 0000000000..220b2becba --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/internal/BouncyCastleGpgKeyLocatorTest.java @@ -0,0 +1,167 @@ +/* + * Copyright (C) 2019, Thomas Wolf <thomas.wolf@paranor.ch> + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.lib.internal; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Locale; + +import org.junit.Test; + +public class BouncyCastleGpgKeyLocatorTest { + + private static final String USER_ID = "Heinrich Heine <heinrichh@uni-duesseldorf.de>"; + + private static boolean match(String userId, String pattern) { + return BouncyCastleGpgKeyLocator.containsSigningKey(userId, pattern); + } + + @Test + public void testFullMatch() throws Exception { + assertTrue(match(USER_ID, + "=Heinrich Heine <heinrichh@uni-duesseldorf.de>")); + assertFalse(match(USER_ID, "=Heinrich Heine")); + assertFalse(match(USER_ID, "= ")); + assertFalse(match(USER_ID, "=heinrichh@uni-duesseldorf.de")); + } + + @Test + public void testEmpty() throws Exception { + assertFalse(match(USER_ID, "")); + assertFalse(match(USER_ID, null)); + assertFalse(match("", "")); + assertFalse(match(null, "")); + assertFalse(match(null, null)); + assertFalse(match("", "something")); + assertFalse(match(null, "something")); + } + + @Test + public void testFullEmail() throws Exception { + assertTrue(match(USER_ID, "<heinrichh@uni-duesseldorf.de>")); + assertTrue(match(USER_ID + " ", "<heinrichh@uni-duesseldorf.de>")); + assertFalse(match(USER_ID, "<>")); + assertFalse(match(USER_ID, "<h>")); + assertFalse(match(USER_ID, "<heinrichh>")); + assertFalse(match(USER_ID, "<uni-duesseldorf>")); + assertFalse(match(USER_ID, "<h@u>")); + assertFalse(match(USER_ID, "<HeinrichH@uni-duesseldorf.de>")); + assertFalse(match(USER_ID.substring(0, USER_ID.length() - 1), + "<heinrichh@uni-duesseldorf.de>")); + assertFalse(match("", "<>")); + assertFalse(match("", "<heinrichh@uni-duesseldorf.de>")); + } + + @Test + public void testPartialEmail() throws Exception { + assertTrue(match(USER_ID, "@heinrichh@uni-duesseldorf.de")); + assertTrue(match(USER_ID, "@heinrichh")); + assertTrue(match(USER_ID, "@duesseldorf")); + assertTrue(match(USER_ID, "@uni-d")); + assertTrue(match(USER_ID, "@h")); + assertTrue(match(USER_ID, "@.")); + assertTrue(match(USER_ID, "@h@u")); + assertFalse(match(USER_ID, "@ ")); + assertFalse(match(USER_ID, "@")); + assertFalse(match(USER_ID, "@Heine")); + assertFalse(match(USER_ID, "@HeinrichH")); + assertFalse(match(USER_ID, "@Heinrich")); + assertFalse(match("", "@")); + assertFalse(match("", "@h")); + } + + private void substringTests(String prefix) throws Exception { + assertTrue(match(USER_ID, prefix + "heinrichh@uni-duesseldorf.de")); + assertTrue(match(USER_ID, prefix + "heinrich")); + assertTrue(match(USER_ID, prefix + "HEIN")); + assertTrue(match(USER_ID, prefix + "Heine <")); + assertTrue(match(USER_ID, prefix + "UNI")); + assertTrue(match(USER_ID, prefix + "uni")); + assertTrue(match(USER_ID, prefix + "rich He")); + assertTrue(match(USER_ID, prefix + "h@u")); + assertTrue(match(USER_ID, prefix + USER_ID)); + assertTrue(match(USER_ID, prefix + USER_ID.toUpperCase(Locale.ROOT))); + assertFalse(match(USER_ID, prefix + "")); + assertFalse(match(USER_ID, prefix + " ")); + assertFalse(match(USER_ID, prefix + "yy")); + assertFalse(match("", prefix + "")); + assertFalse(match("", prefix + "uni")); + } + + @Test + public void testSubstringPlain() throws Exception { + substringTests(""); + } + + @Test + public void testSubstringAsterisk() throws Exception { + substringTests("*"); + } + + @Test + public void testExplicitFingerprint() throws Exception { + assertFalse(match("John Fade <j.fade@example.com>", "0xfade")); + assertFalse(match("John Fade <0xfade@example.com>", "0xfade")); + assertFalse(match("", "0xfade")); + } + + @Test + public void testImplicitFingerprint() throws Exception { + assertTrue(match("John Fade <j.fade@example.com>", "fade")); + assertTrue(match("John Fade <0xfade@example.com>", "fade")); + assertTrue(match("John Fade <j.fade@example.com>", "FADE")); + assertTrue(match("John Fade <0xfade@example.com>", "FADE")); + } + + @Test + public void testZeroX() throws Exception { + assertTrue(match("John Fade <0xfade@example.com>", "0x")); + assertTrue(match("John Fade <0xfade@example.com>", "*0x")); + assertTrue(match("John Fade <0xfade@example.com>", "*0xfade")); + assertTrue(match("John Fade <0xfade@example.com>", "*0xFADE")); + assertTrue(match("John Fade <0xfade@example.com>", "@0xfade")); + assertFalse(match("John Fade <0xfade@example.com>", "@0xFADE")); + assertFalse(match("", "0x")); + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CherryPickTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CherryPickTest.java index 19f6dcbc59..aa4392fcac 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CherryPickTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CherryPickTest.java @@ -106,23 +106,24 @@ public class CherryPickTest extends RepositoryTestCase { boolean merge = twm.merge(new ObjectId[] { O, T }); assertTrue(merge); - final TreeWalk tw = new TreeWalk(db); - tw.setRecursive(true); - tw.reset(twm.getResultTreeId()); + try (TreeWalk tw = new TreeWalk(db)) { + tw.setRecursive(true); + tw.reset(twm.getResultTreeId()); - assertTrue(tw.next()); - assertEquals("a", tw.getPathString()); - assertCorrectId(treeO, tw); + assertTrue(tw.next()); + assertEquals("a", tw.getPathString()); + assertCorrectId(treeO, tw); - assertTrue(tw.next()); - assertEquals("o", tw.getPathString()); - assertCorrectId(treeO, tw); + assertTrue(tw.next()); + assertEquals("o", tw.getPathString()); + assertCorrectId(treeO, tw); - assertTrue(tw.next()); - assertEquals("t", tw.getPathString()); - assertCorrectId(treeT, tw); + assertTrue(tw.next()); + assertEquals("t", tw.getPathString()); + assertCorrectId(treeT, tw); - assertFalse(tw.next()); + assertFalse(tw.next()); + } } @Test @@ -168,19 +169,20 @@ public class CherryPickTest extends RepositoryTestCase { boolean merge = twm.merge(new ObjectId[] { B, T }); assertTrue(merge); - final TreeWalk tw = new TreeWalk(db); - tw.setRecursive(true); - tw.reset(twm.getResultTreeId()); + try (TreeWalk tw = new TreeWalk(db)) { + tw.setRecursive(true); + tw.reset(twm.getResultTreeId()); - assertTrue(tw.next()); - assertEquals("a", tw.getPathString()); - assertCorrectId(treeB, tw); + assertTrue(tw.next()); + assertEquals("a", tw.getPathString()); + assertCorrectId(treeB, tw); - assertTrue(tw.next()); - assertEquals("t", tw.getPathString()); - assertCorrectId(treeT, tw); + assertTrue(tw.next()); + assertEquals("t", tw.getPathString()); + assertCorrectId(treeT, tw); - assertFalse(tw.next()); + assertFalse(tw.next()); + } } private static void assertCorrectId(DirCache treeT, TreeWalk tw) { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CrissCrossMergeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CrissCrossMergeTest.java index a67c750dba..5c6636c89e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CrissCrossMergeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CrissCrossMergeTest.java @@ -881,9 +881,9 @@ public class CrissCrossMergeTest extends RepositoryTestCase { } StringBuilder result = new StringBuilder(); - ObjectReader or = r.newObjectReader(); - try (BufferedReader br = new BufferedReader( - new InputStreamReader(or.open(blobId).openStream(), UTF_8))) { + try (ObjectReader or = r.newObjectReader(); + BufferedReader br = new BufferedReader(new InputStreamReader( + or.open(blobId).openStream(), UTF_8))) { String line; boolean first = true; while ((line = br.readLine()) != null) { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergerTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergerTest.java index 62495fb023..3379a25377 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergerTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergerTest.java @@ -1365,8 +1365,8 @@ public class MergerTest extends RepositoryTestCase { } private String readBlob(ObjectId treeish, String path) throws Exception { - try (TestRepository<?> tr = new TestRepository<>(db)) { - RevWalk rw = tr.getRevWalk(); + try (TestRepository<?> tr = new TestRepository<>(db); + RevWalk rw = tr.getRevWalk()) { RevTree tree = rw.parseTree(treeish); RevObject obj = tr.get(tree, path); if (obj == null) { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SimpleMergeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SimpleMergeTest.java index dd2c2e84b6..6c0b165a4f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SimpleMergeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SimpleMergeTest.java @@ -174,23 +174,24 @@ public class SimpleMergeTest extends SampleDataRepositoryTestCase { boolean merge = ourMerger.merge(new ObjectId[] { o, t }); assertTrue(merge); - final TreeWalk tw = new TreeWalk(db); - tw.setRecursive(true); - tw.reset(ourMerger.getResultTreeId()); + try (TreeWalk tw = new TreeWalk(db)) { + tw.setRecursive(true); + tw.reset(ourMerger.getResultTreeId()); - assertTrue(tw.next()); - assertEquals("Makefile", tw.getPathString()); - assertCorrectId(treeO, tw); + assertTrue(tw.next()); + assertEquals("Makefile", tw.getPathString()); + assertCorrectId(treeO, tw); - assertTrue(tw.next()); - assertEquals("libelf-po/a", tw.getPathString()); - assertCorrectId(treeO, tw); + assertTrue(tw.next()); + assertEquals("libelf-po/a", tw.getPathString()); + assertCorrectId(treeO, tw); - assertTrue(tw.next()); - assertEquals("libelf/c", tw.getPathString()); - assertCorrectId(treeT, tw); + assertTrue(tw.next()); + assertEquals("libelf/c", tw.getPathString()); + assertCorrectId(treeT, tw); - assertFalse(tw.next()); + assertFalse(tw.next()); + } } @Test @@ -226,19 +227,20 @@ public class SimpleMergeTest extends SampleDataRepositoryTestCase { boolean merge = ourMerger.merge(new ObjectId[] { o, t }); assertTrue(merge); - final TreeWalk tw = new TreeWalk(db); - tw.setRecursive(true); - tw.reset(ourMerger.getResultTreeId()); + try (TreeWalk tw = new TreeWalk(db)) { + tw.setRecursive(true); + tw.reset(ourMerger.getResultTreeId()); - assertTrue(tw.next()); - assertEquals("d/o", tw.getPathString()); - assertCorrectId(treeO, tw); + assertTrue(tw.next()); + assertEquals("d/o", tw.getPathString()); + assertCorrectId(treeO, tw); - assertTrue(tw.next()); - assertEquals("d/t", tw.getPathString()); - assertCorrectId(treeT, tw); + assertTrue(tw.next()); + assertEquals("d/t", tw.getPathString()); + assertCorrectId(treeT, tw); - assertFalse(tw.next()); + assertFalse(tw.next()); + } } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revplot/AbstractPlotRendererTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revplot/AbstractPlotRendererTest.java index f265315338..ac157b6b87 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revplot/AbstractPlotRendererTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revplot/AbstractPlotRendererTest.java @@ -44,7 +44,6 @@ package org.eclipse.jgit.revplot; import static org.junit.Assert.assertEquals; -import java.io.IOException; import java.util.LinkedList; import java.util.List; @@ -55,7 +54,6 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; -import org.eclipse.jgit.lib.Repository; import org.junit.Before; import org.junit.Test; @@ -83,23 +81,18 @@ public class AbstractPlotRendererTest extends RepositoryTestCase { git.commit().setMessage("commit on master").call(); MergeResult mergeCall = merge(db.resolve("topic")); ObjectId start = mergeCall.getNewHead(); - PlotCommitList<PlotLane> commitList = createCommitList(start); + try (PlotWalk walk = new PlotWalk(db)) { + walk.markStart(walk.parseCommit(start)); + PlotCommitList<PlotLane> commitList = new PlotCommitList<>(); + commitList.source(walk); + commitList.fillTo(1000); - for (int i = 0; i < commitList.size(); i++) - plotRenderer.paintCommit(commitList.get(i), 30); + for (int i = 0; i < commitList.size(); i++) + plotRenderer.paintCommit(commitList.get(i), 30); - List<Integer> indentations = plotRenderer.indentations; - assertEquals(indentations.get(2), indentations.get(3)); - } - - private PlotCommitList<PlotLane> createCommitList(ObjectId start) - throws IOException { - TestPlotWalk walk = new TestPlotWalk(db); - walk.markStart(walk.parseCommit(start)); - PlotCommitList<PlotLane> commitList = new PlotCommitList<>(); - commitList.source(walk); - commitList.fillTo(1000); - return commitList; + List<Integer> indentations = plotRenderer.indentations; + assertEquals(indentations.get(2), indentations.get(3)); + } } private MergeResult merge(ObjectId includeId) throws GitAPIException { @@ -107,12 +100,6 @@ public class AbstractPlotRendererTest extends RepositoryTestCase { .include(includeId).call(); } - private static class TestPlotWalk extends PlotWalk { - public TestPlotWalk(Repository repo) { - super(repo); - } - } - private static class TestPlotRenderer extends AbstractPlotRenderer<PlotLane, Object> { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revplot/PlotCommitListTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revplot/PlotCommitListTest.java index 7297de3646..0c367f457e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revplot/PlotCommitListTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revplot/PlotCommitListTest.java @@ -135,18 +135,19 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit b = commit(a); final RevCommit c = commit(b); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(c.getId())); - - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - CommitListAssert test = new CommitListAssert(pcl); - test.commit(c).lanePos(0).parents(b); - test.commit(b).lanePos(0).parents(a); - test.commit(a).lanePos(0).parents(); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(c.getId())); + + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + CommitListAssert test = new CommitListAssert(pcl); + test.commit(c).lanePos(0).parents(b); + test.commit(b).lanePos(0).parents(a); + test.commit(a).lanePos(0).parents(); + test.noMoreCommits(); + } } @Test @@ -156,19 +157,20 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit c = commit(a); final RevCommit d = commit(b, c); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(d.getId())); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(d.getId())); - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); - CommitListAssert test = new CommitListAssert(pcl); - test.commit(d).lanePos(0).parents(b, c); - test.commit(c).lanePos(1).parents(a); - test.commit(b).lanePos(0).parents(a); - test.commit(a).lanePos(0).parents(); - test.noMoreCommits(); + CommitListAssert test = new CommitListAssert(pcl); + test.commit(d).lanePos(0).parents(b, c); + test.commit(c).lanePos(1).parents(a); + test.commit(b).lanePos(0).parents(a); + test.commit(a).lanePos(0).parents(); + test.noMoreCommits(); + } } @Test @@ -177,20 +179,21 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit b = commit(a); final RevCommit c = commit(a); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(b.getId())); - pw.markStart(pw.lookupCommit(c.getId())); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(b.getId())); + pw.markStart(pw.lookupCommit(c.getId())); - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); - Set<Integer> childPositions = asSet(0, 1); - CommitListAssert test = new CommitListAssert(pcl); - test.commit(c).lanePos(childPositions).parents(a); - test.commit(b).lanePos(childPositions).parents(a); - test.commit(a).lanePos(0).parents(); - test.noMoreCommits(); + Set<Integer> childPositions = asSet(0, 1); + CommitListAssert test = new CommitListAssert(pcl); + test.commit(c).lanePos(childPositions).parents(a); + test.commit(b).lanePos(childPositions).parents(a); + test.commit(a).lanePos(0).parents(); + test.noMoreCommits(); + } } @Test @@ -200,22 +203,23 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit c = commit(a); final RevCommit d = commit(a); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(b.getId())); - pw.markStart(pw.lookupCommit(c.getId())); - pw.markStart(pw.lookupCommit(d.getId())); - - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - Set<Integer> childPositions = asSet(0, 1, 2); - CommitListAssert test = new CommitListAssert(pcl); - test.commit(d).lanePos(childPositions).parents(a); - test.commit(c).lanePos(childPositions).parents(a); - test.commit(b).lanePos(childPositions).parents(a); - test.commit(a).lanePos(0).parents(); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(b.getId())); + pw.markStart(pw.lookupCommit(c.getId())); + pw.markStart(pw.lookupCommit(d.getId())); + + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + Set<Integer> childPositions = asSet(0, 1, 2); + CommitListAssert test = new CommitListAssert(pcl); + test.commit(d).lanePos(childPositions).parents(a); + test.commit(c).lanePos(childPositions).parents(a); + test.commit(b).lanePos(childPositions).parents(a); + test.commit(a).lanePos(0).parents(); + test.noMoreCommits(); + } } @Test @@ -228,34 +232,35 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit f = commit(a); final RevCommit g = commit(f); - PlotWalk pw = new PlotWalk(db); - // TODO: when we add unnecessary commit's as tips (e.g. a commit which - // is a parent of another tip) the walk will return those commits twice. - // Find out why! - // pw.markStart(pw.lookupCommit(a.getId())); - pw.markStart(pw.lookupCommit(b.getId())); - pw.markStart(pw.lookupCommit(c.getId())); - pw.markStart(pw.lookupCommit(d.getId())); - pw.markStart(pw.lookupCommit(e.getId())); - // pw.markStart(pw.lookupCommit(f.getId())); - pw.markStart(pw.lookupCommit(g.getId())); - - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - Set<Integer> childPositions = asSet(0, 1, 2, 3, 4); - CommitListAssert test = new CommitListAssert(pcl); - int posG = test.commit(g).lanePos(childPositions).parents(f) - .getLanePos(); - test.commit(f).lanePos(posG).parents(a); - - test.commit(e).lanePos(childPositions).parents(a); - test.commit(d).lanePos(childPositions).parents(a); - test.commit(c).lanePos(childPositions).parents(a); - test.commit(b).lanePos(childPositions).parents(a); - test.commit(a).lanePos(0).parents(); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + // TODO: when we add unnecessary commit's as tips (e.g. a commit + // which is a parent of another tip) the walk will return those + // commits twice. Find out why! + // pw.markStart(pw.lookupCommit(a.getId())); + pw.markStart(pw.lookupCommit(b.getId())); + pw.markStart(pw.lookupCommit(c.getId())); + pw.markStart(pw.lookupCommit(d.getId())); + pw.markStart(pw.lookupCommit(e.getId())); + // pw.markStart(pw.lookupCommit(f.getId())); + pw.markStart(pw.lookupCommit(g.getId())); + + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + Set<Integer> childPositions = asSet(0, 1, 2, 3, 4); + CommitListAssert test = new CommitListAssert(pcl); + int posG = test.commit(g).lanePos(childPositions).parents(f) + .getLanePos(); + test.commit(f).lanePos(posG).parents(a); + + test.commit(e).lanePos(childPositions).parents(a); + test.commit(d).lanePos(childPositions).parents(a); + test.commit(c).lanePos(childPositions).parents(a); + test.commit(b).lanePos(childPositions).parents(a); + test.commit(a).lanePos(0).parents(); + test.noMoreCommits(); + } } @Test @@ -270,25 +275,26 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit h = commit(f); final RevCommit i = commit(h); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(i.getId())); - pw.markStart(pw.lookupCommit(g.getId())); - - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - Set<Integer> childPositions = asSet(0, 1); - CommitListAssert test = new CommitListAssert(pcl); - int posI = test.commit(i).lanePos(childPositions).parents(h) - .getLanePos(); - test.commit(h).lanePos(posI).parents(f); - test.commit(g).lanePos(childPositions).parents(a); - test.commit(f).lanePos(posI).parents(e, d); - test.commit(e).lanePos(posI).parents(c); - test.commit(d).lanePos(2).parents(b); - test.commit(c).lanePos(posI).parents(b); - test.commit(b).lanePos(posI).parents(a); - test.commit(a).lanePos(0).parents(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(i.getId())); + pw.markStart(pw.lookupCommit(g.getId())); + + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + Set<Integer> childPositions = asSet(0, 1); + CommitListAssert test = new CommitListAssert(pcl); + int posI = test.commit(i).lanePos(childPositions).parents(h) + .getLanePos(); + test.commit(h).lanePos(posI).parents(f); + test.commit(g).lanePos(childPositions).parents(a); + test.commit(f).lanePos(posI).parents(e, d); + test.commit(e).lanePos(posI).parents(c); + test.commit(d).lanePos(2).parents(b); + test.commit(c).lanePos(posI).parents(b); + test.commit(b).lanePos(posI).parents(a); + test.commit(a).lanePos(0).parents(); + } } // test the history of the egit project between 9fdaf3c1 and e76ad9170f @@ -330,67 +336,71 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit merge_fixed_logged_npe = commit(sort_roots, fix_logged_npe); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(merge_fixed_logged_npe.getId())); - - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - CommitListAssert test = new CommitListAssert(pcl); - - // Note: all positions of side branches are rather arbitrary, but some - // may not overlap. Testing for the positions yielded by the current - // implementation, which was manually checked to not overlap. - final int mainPos = 0; - test.commit(merge_fixed_logged_npe).parents(sort_roots, fix_logged_npe) - .lanePos(mainPos); - test.commit(fix_logged_npe).parents(merge_changeset_implementation) - .lanePos(1); - test.commit(sort_roots).parents(merge_update_eclipse).lanePos(mainPos); - test.commit(merge_update_eclipse).parents(add_a_clear, update_eclipse) - .lanePos(mainPos); - test.commit(add_a_clear).parents(fix_broken).lanePos(mainPos); - test.commit(fix_broken).parents(merge_disable_comment).lanePos(mainPos); - test.commit(merge_disable_comment) - .parents(merge_resolve_handler, disable_comment) - .lanePos(mainPos); - test.commit(disable_comment).parents(clone_operation).lanePos(2); - test.commit(merge_resolve_handler) - .parents(clone_operation, resolve_handler).lanePos(mainPos); - test.commit(update_eclipse).parents(add_Maven).lanePos(3); - test.commit(clone_operation).parents(merge_changeset_implementation) - .lanePos(mainPos); - test.commit(merge_changeset_implementation) - .parents(merge_disable_source, changeset_implementation) - .lanePos(mainPos); - test.commit(merge_disable_source) - .parents(update_eclipse_iplog2, disable_source) - .lanePos(mainPos); - test.commit(update_eclipse_iplog2).parents(merge_use_remote) - .lanePos(mainPos); - test.commit(disable_source).parents(merge_use_remote).lanePos(1); - test.commit(merge_use_remote).parents(update_eclipse_iplog, use_remote) - .lanePos(mainPos); - test.commit(changeset_implementation).parents(clear_repositorycache) - .lanePos(2); - test.commit(update_eclipse_iplog).parents(merge_add_Maven) - .lanePos(mainPos); - test.commit(merge_add_Maven).parents(findToolBar_layout, add_Maven) - .lanePos(mainPos); - test.commit(findToolBar_layout).parents(clear_repositorycache) - .lanePos(mainPos); - test.commit(use_remote).parents(clear_repositorycache).lanePos(1); - test.commit(add_Maven).parents(clear_repositorycache).lanePos(3); - test.commit(clear_repositorycache).parents(merge_remove) - .lanePos(mainPos); - test.commit(resolve_handler).parents(merge_fix).lanePos(4); - test.commit(merge_remove).parents(add_simple, remove_unused) - .lanePos(mainPos); - test.commit(remove_unused).parents(merge_fix).lanePos(1); - test.commit(add_simple).parents(merge_fix).lanePos(mainPos); - test.commit(merge_fix).parents().lanePos(mainPos); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(merge_fixed_logged_npe.getId())); + + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + CommitListAssert test = new CommitListAssert(pcl); + + // Note: all positions of side branches are rather arbitrary, but + // some + // may not overlap. Testing for the positions yielded by the current + // implementation, which was manually checked to not overlap. + final int mainPos = 0; + test.commit(merge_fixed_logged_npe) + .parents(sort_roots, fix_logged_npe).lanePos(mainPos); + test.commit(fix_logged_npe).parents(merge_changeset_implementation) + .lanePos(1); + test.commit(sort_roots).parents(merge_update_eclipse) + .lanePos(mainPos); + test.commit(merge_update_eclipse) + .parents(add_a_clear, update_eclipse).lanePos(mainPos); + test.commit(add_a_clear).parents(fix_broken).lanePos(mainPos); + test.commit(fix_broken).parents(merge_disable_comment) + .lanePos(mainPos); + test.commit(merge_disable_comment) + .parents(merge_resolve_handler, disable_comment) + .lanePos(mainPos); + test.commit(disable_comment).parents(clone_operation).lanePos(2); + test.commit(merge_resolve_handler) + .parents(clone_operation, resolve_handler).lanePos(mainPos); + test.commit(update_eclipse).parents(add_Maven).lanePos(3); + test.commit(clone_operation).parents(merge_changeset_implementation) + .lanePos(mainPos); + test.commit(merge_changeset_implementation) + .parents(merge_disable_source, changeset_implementation) + .lanePos(mainPos); + test.commit(merge_disable_source) + .parents(update_eclipse_iplog2, disable_source) + .lanePos(mainPos); + test.commit(update_eclipse_iplog2).parents(merge_use_remote) + .lanePos(mainPos); + test.commit(disable_source).parents(merge_use_remote).lanePos(1); + test.commit(merge_use_remote) + .parents(update_eclipse_iplog, use_remote).lanePos(mainPos); + test.commit(changeset_implementation).parents(clear_repositorycache) + .lanePos(2); + test.commit(update_eclipse_iplog).parents(merge_add_Maven) + .lanePos(mainPos); + test.commit(merge_add_Maven).parents(findToolBar_layout, add_Maven) + .lanePos(mainPos); + test.commit(findToolBar_layout).parents(clear_repositorycache) + .lanePos(mainPos); + test.commit(use_remote).parents(clear_repositorycache).lanePos(1); + test.commit(add_Maven).parents(clear_repositorycache).lanePos(3); + test.commit(clear_repositorycache).parents(merge_remove) + .lanePos(mainPos); + test.commit(resolve_handler).parents(merge_fix).lanePos(4); + test.commit(merge_remove).parents(add_simple, remove_unused) + .lanePos(mainPos); + test.commit(remove_unused).parents(merge_fix).lanePos(1); + test.commit(add_simple).parents(merge_fix).lanePos(mainPos); + test.commit(merge_fix).parents().lanePos(mainPos); + test.noMoreCommits(); + } } // test a history where a merge commit has two time the same parent @@ -403,20 +413,21 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit s1 = commit(m2); final RevCommit s2 = commit(s1); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(m3)); - pw.markStart(pw.lookupCommit(s2)); - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - CommitListAssert test = new CommitListAssert(pcl); - test.commit(s2).nrOfPassingLanes(0); - test.commit(s1).nrOfPassingLanes(0); - test.commit(m3).nrOfPassingLanes(1); - test.commit(m2).nrOfPassingLanes(0); - test.commit(m1).nrOfPassingLanes(0); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(m3)); + pw.markStart(pw.lookupCommit(s2)); + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + CommitListAssert test = new CommitListAssert(pcl); + test.commit(s2).nrOfPassingLanes(0); + test.commit(s1).nrOfPassingLanes(0); + test.commit(m3).nrOfPassingLanes(1); + test.commit(m2).nrOfPassingLanes(0); + test.commit(m1).nrOfPassingLanes(0); + test.noMoreCommits(); + } } /** @@ -465,30 +476,31 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit a4 = commit(a3); final RevCommit a5 = commit(a3, a4); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(b3.getId())); - pw.markStart(pw.lookupCommit(c.getId())); - pw.markStart(pw.lookupCommit(e.getId())); - pw.markStart(pw.lookupCommit(a5.getId())); - - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - // test that the commits b1, b2 and b3 are on the same position - int bPos = pcl.get(9).lane.position; // b1 - assertEquals("b2 is an a different position", bPos, - pcl.get(7).lane.position); - assertEquals("b3 is on a different position", bPos, - pcl.get(4).lane.position); - - // test that nothing blocks the connections between b1, b2 and b3 - assertNotEquals("b lane is blocked by c", bPos, - pcl.get(8).lane.position); - assertNotEquals("b lane is blocked by a2", bPos, - pcl.get(6).lane.position); - assertNotEquals("b lane is blocked by d", bPos, - pcl.get(5).lane.position); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(b3.getId())); + pw.markStart(pw.lookupCommit(c.getId())); + pw.markStart(pw.lookupCommit(e.getId())); + pw.markStart(pw.lookupCommit(a5.getId())); + + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + // test that the commits b1, b2 and b3 are on the same position + int bPos = pcl.get(9).lane.position; // b1 + assertEquals("b2 is an a different position", bPos, + pcl.get(7).lane.position); + assertEquals("b3 is on a different position", bPos, + pcl.get(4).lane.position); + + // test that nothing blocks the connections between b1, b2 and b3 + assertNotEquals("b lane is blocked by c", bPos, + pcl.get(8).lane.position); + assertNotEquals("b lane is blocked by a2", bPos, + pcl.get(6).lane.position); + assertNotEquals("b lane is blocked by d", bPos, + pcl.get(5).lane.position); + } } /** @@ -517,23 +529,24 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit a4 = commit(a3, b2); final RevCommit b3 = commit(b2); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(a4)); - pw.markStart(pw.lookupCommit(b3)); - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - Set<Integer> positions = asSet(0, 1); - CommitListAssert test = new CommitListAssert(pcl); - int posB = test.commit(b3).lanePos(positions).getLanePos(); - int posA = test.commit(a4).lanePos(positions).getLanePos(); - test.commit(b2).lanePos(posB); - test.commit(a3).lanePos(posA); - test.commit(a2).lanePos(posA); - test.commit(b1).lanePos(posB); - test.commit(a1).lanePos(posA); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(a4)); + pw.markStart(pw.lookupCommit(b3)); + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + Set<Integer> positions = asSet(0, 1); + CommitListAssert test = new CommitListAssert(pcl); + int posB = test.commit(b3).lanePos(positions).getLanePos(); + int posA = test.commit(a4).lanePos(positions).getLanePos(); + test.commit(b2).lanePos(posB); + test.commit(a3).lanePos(posA); + test.commit(a2).lanePos(posA); + test.commit(b1).lanePos(posB); + test.commit(a1).lanePos(posA); + test.noMoreCommits(); + } } /** @@ -562,25 +575,26 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit b3 = commit(b2); final RevCommit a4 = commit(a3); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(a4)); - pw.markStart(pw.lookupCommit(b3)); - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - Set<Integer> positions = asSet(0, 1); - CommitListAssert test = new CommitListAssert(pcl); - int posA = test.commit(a4).lanePos(positions).getLanePos(); - int posB = test.commit(b3).lanePos(positions).getLanePos(); - test.commit(a3).lanePos(posA); - test.commit(b2).lanePos(posB); - test.commit(a2).lanePos(posA); - // b1 is not repositioned, uses "detour lane" - // (drawn as a double arc in the ascii graph above) - test.commit(b1).lanePos(posB); - test.commit(a1).lanePos(posA); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(a4)); + pw.markStart(pw.lookupCommit(b3)); + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + Set<Integer> positions = asSet(0, 1); + CommitListAssert test = new CommitListAssert(pcl); + int posA = test.commit(a4).lanePos(positions).getLanePos(); + int posB = test.commit(b3).lanePos(positions).getLanePos(); + test.commit(a3).lanePos(posA); + test.commit(b2).lanePos(posB); + test.commit(a2).lanePos(posA); + // b1 is not repositioned, uses "detour lane" + // (drawn as a double arc in the ascii graph above) + test.commit(b1).lanePos(posB); + test.commit(a1).lanePos(posA); + test.noMoreCommits(); + } } /** @@ -611,24 +625,25 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit a4 = commit(a3, b1); final RevCommit b2 = commit(b1); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(a4)); - pw.markStart(pw.lookupCommit(b2)); - pw.markStart(pw.lookupCommit(c)); - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - Set<Integer> positions = asSet(0, 1, 2); - CommitListAssert test = new CommitListAssert(pcl); - int posB = test.commit(b2).lanePos(positions).getLanePos(); - int posA = test.commit(a4).lanePos(positions).getLanePos(); - test.commit(a3).lanePos(posA); - test.commit(c).lanePos(positions); - test.commit(a2).lanePos(posA); - test.commit(b1).lanePos(posB); // repositioned to go around c - test.commit(a1).lanePos(posA); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(a4)); + pw.markStart(pw.lookupCommit(b2)); + pw.markStart(pw.lookupCommit(c)); + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + Set<Integer> positions = asSet(0, 1, 2); + CommitListAssert test = new CommitListAssert(pcl); + int posB = test.commit(b2).lanePos(positions).getLanePos(); + int posA = test.commit(a4).lanePos(positions).getLanePos(); + test.commit(a3).lanePos(posA); + test.commit(c).lanePos(positions); + test.commit(a2).lanePos(posA); + test.commit(b1).lanePos(posB); // repositioned to go around c + test.commit(a1).lanePos(posA); + test.noMoreCommits(); + } } /** @@ -651,22 +666,24 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit a3 = commit(a2); final RevCommit b1 = commit(a1); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(a3)); - pw.markStart(pw.lookupCommit(b1)); - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(2); // don't process a1 - - Set<Integer> positions = asSet(0, 1); - CommitListAssert test = new CommitListAssert(pcl); - PlotLane laneB = test.commit(b1).lanePos(positions).current.getLane(); - int posA = test.commit(a3).lanePos(positions).getLanePos(); - test.commit(a2).lanePos(posA); - assertArrayEquals( - "Although the parent of b1, a1, is not processed yet, the b lane should still be drawn", - new PlotLane[] { laneB }, test.current.passingLanes); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(a3)); + pw.markStart(pw.lookupCommit(b1)); + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(2); // don't process a1 + + Set<Integer> positions = asSet(0, 1); + CommitListAssert test = new CommitListAssert(pcl); + PlotLane laneB = test.commit(b1).lanePos(positions).current + .getLane(); + int posA = test.commit(a3).lanePos(positions).getLanePos(); + test.commit(a2).lanePos(posA); + assertArrayEquals( + "Although the parent of b1, a1, is not processed yet, the b lane should still be drawn", + new PlotLane[] { laneB }, test.current.passingLanes); + test.noMoreCommits(); + } } @Test @@ -674,17 +691,18 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit a = commit(); final RevCommit b = commit(); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(a)); - pw.markStart(pw.lookupCommit(b)); - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - CommitListAssert test = new CommitListAssert(pcl); - test.commit(b).lanePos(0); - test.commit(a).lanePos(0); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(a)); + pw.markStart(pw.lookupCommit(b)); + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + CommitListAssert test = new CommitListAssert(pcl); + test.commit(b).lanePos(0); + test.commit(a).lanePos(0); + test.noMoreCommits(); + } } @Test @@ -693,17 +711,18 @@ public class PlotCommitListTest extends RevWalkTestCase { final RevCommit b1 = commit(); final RevCommit b2 = commit(b1); - PlotWalk pw = new PlotWalk(db); - pw.markStart(pw.lookupCommit(a)); - pw.markStart(pw.lookupCommit(b2)); - PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); - pcl.source(pw); - pcl.fillTo(Integer.MAX_VALUE); - - CommitListAssert test = new CommitListAssert(pcl); - test.commit(b2).lanePos(0); - test.commit(b1).lanePos(0); - test.commit(a).lanePos(0); - test.noMoreCommits(); + try (PlotWalk pw = new PlotWalk(db)) { + pw.markStart(pw.lookupCommit(a)); + pw.markStart(pw.lookupCommit(b2)); + PlotCommitList<PlotLane> pcl = new PlotCommitList<>(); + pcl.source(pw); + pcl.fillTo(Integer.MAX_VALUE); + + CommitListAssert test = new CommitListAssert(pcl); + test.commit(b2).lanePos(0); + test.commit(b1).lanePos(0); + test.commit(a).lanePos(0); + test.noMoreCommits(); + } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java index b814984935..d6b63ab236 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java @@ -107,18 +107,18 @@ public class RevCommitParseTest extends RepositoryTestCase { body.append("\n"); - final RevWalk rw = new RevWalk(db); final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); assertNull(c.getTree()); assertNull(c.parents); - c.parseCanonical(rw, body.toString().getBytes(UTF_8)); - assertNotNull(c.getTree()); - assertEquals(treeId, c.getTree().getId()); - assertSame(rw.lookupTree(treeId), c.getTree()); - + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, body.toString().getBytes(UTF_8)); + assertNotNull(c.getTree()); + assertEquals(treeId, c.getTree().getId()); + assertSame(rw.lookupTree(treeId), c.getTree()); + } assertNotNull(c.parents); assertEquals(0, c.parents.length); assertEquals("", c.getFullMessage()); @@ -148,8 +148,10 @@ public class RevCommitParseTest extends RepositoryTestCase { final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toString().getBytes(UTF_8)); - return c; + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toString().getBytes(UTF_8)); + return c; + } } @Test @@ -161,8 +163,9 @@ public class RevCommitParseTest extends RepositoryTestCase { final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toString().getBytes(UTF_8)); - + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toString().getBytes(UTF_8)); + } assertEquals("", c.getFullMessage()); assertEquals("", c.getShortMessage()); } @@ -176,8 +179,9 @@ public class RevCommitParseTest extends RepositoryTestCase { final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toString().getBytes(UTF_8)); - + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toString().getBytes(UTF_8)); + } assertEquals(new PersonIdent("", "a_u_thor@example.com", 1218123387000l, 7), c.getAuthorIdent()); assertEquals(new PersonIdent("", "", 1218123390000l, -5), c.getCommitterIdent()); } @@ -194,8 +198,9 @@ public class RevCommitParseTest extends RepositoryTestCase { b.write("\u304d\u308c\u3044\n".getBytes(UTF_8)); final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id - c.parseCanonical(new RevWalk(db), b.toByteArray()); - + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertSame(UTF_8, c.getEncoding()); assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName()); assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage()); @@ -214,8 +219,9 @@ public class RevCommitParseTest extends RepositoryTestCase { b.write("\u304d\u308c\u3044\n".getBytes(UTF_8)); final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id - c.parseCanonical(new RevWalk(db), b.toByteArray()); - + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertSame(UTF_8, c.getEncoding()); assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName()); assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage()); @@ -240,7 +246,9 @@ public class RevCommitParseTest extends RepositoryTestCase { b.write("Hi\n".getBytes("EUC-JP")); final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("EUC-JP", c.getEncoding().name()); assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName()); @@ -270,7 +278,9 @@ public class RevCommitParseTest extends RepositoryTestCase { b.write("Hi\n".getBytes(UTF_8)); final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("EUC-JP", c.getEncoding().name()); assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName()); @@ -301,7 +311,9 @@ public class RevCommitParseTest extends RepositoryTestCase { b.write("Hi\n".getBytes(UTF_8)); final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("ISO-8859-1", c.getEncoding().name()); assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName()); @@ -323,7 +335,9 @@ public class RevCommitParseTest extends RepositoryTestCase { RevCommit c = new RevCommit( id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("'utf8'", c.getEncodingName()); assertEquals("Sm\u00f6rg\u00e5sbord\n", c.getFullMessage()); @@ -347,7 +361,9 @@ public class RevCommitParseTest extends RepositoryTestCase { RevCommit c = new RevCommit( id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("utf-8logoutputencoding=gbk", c.getEncodingName()); assertEquals("message\n", c.getFullMessage()); assertEquals("message", c.getShortMessage()); @@ -374,7 +390,9 @@ public class RevCommitParseTest extends RepositoryTestCase { RevCommit c = new RevCommit( id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("it_IT.UTF8", c.getEncodingName()); assertEquals("message\n", c.getFullMessage()); assertEquals("message", c.getShortMessage()); @@ -507,7 +525,9 @@ public class RevCommitParseTest extends RepositoryTestCase { final RevCommit c; c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), commit.getBytes(UTF_8)); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, commit.getBytes(UTF_8)); + } String gpgSig = new String(c.getRawGpgSignature(), UTF_8); assertTrue(gpgSig.startsWith("-----BEGIN")); assertTrue(gpgSig.endsWith("END PGP SIGNATURE-----")); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java index 1b45473066..bbc559b2db 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java @@ -91,17 +91,18 @@ public class RevTagParseTest extends RepositoryTestCase { b.append("tagger A U. Thor <a_u_thor@example.com> 1218123387 +0700\n"); b.append("\n"); - final RevWalk rw = new RevWalk(db); final RevTag c; c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); assertNull(c.getObject()); assertNull(c.getTagName()); - c.parseCanonical(rw, b.toString().getBytes(UTF_8)); - assertNotNull(c.getObject()); - assertEquals(id, c.getObject().getId()); - assertSame(rw.lookupAny(id, typeCode), c.getObject()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toString().getBytes(UTF_8)); + assertNotNull(c.getObject()); + assertEquals(id, c.getObject().getId()); + assertSame(rw.lookupAny(id, typeCode), c.getObject()); + } } @Test @@ -134,18 +135,18 @@ public class RevTagParseTest extends RepositoryTestCase { body.append("\n"); - final RevWalk rw = new RevWalk(db); final RevTag c; c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); assertNull(c.getObject()); assertNull(c.getTagName()); - c.parseCanonical(rw, body.toString().getBytes(UTF_8)); - assertNotNull(c.getObject()); - assertEquals(treeId, c.getObject().getId()); - assertSame(rw.lookupTree(treeId), c.getObject()); - + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, body.toString().getBytes(UTF_8)); + assertNotNull(c.getObject()); + assertEquals(treeId, c.getObject().getId()); + assertSame(rw.lookupTree(treeId), c.getObject()); + } assertNotNull(c.getTagName()); assertEquals(name, c.getTagName()); assertEquals("", c.getFullMessage()); @@ -182,17 +183,18 @@ public class RevTagParseTest extends RepositoryTestCase { body.append("\n"); body.append(message); - final RevWalk rw = new RevWalk(db); final RevTag c; c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); assertNull(c.getObject()); assertNull(c.getTagName()); - c.parseCanonical(rw, body.toString().getBytes(UTF_8)); - assertNotNull(c.getObject()); - assertEquals(treeId, c.getObject().getId()); - assertSame(rw.lookupTree(treeId), c.getObject()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, body.toString().getBytes(UTF_8)); + assertNotNull(c.getObject()); + assertEquals(treeId, c.getObject().getId()); + assertSame(rw.lookupTree(treeId), c.getObject()); + } assertNotNull(c.getTagName()); assertEquals(name, c.getTagName()); @@ -213,7 +215,9 @@ public class RevTagParseTest extends RepositoryTestCase { final RevTag c; c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toString().getBytes(UTF_8)); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toString().getBytes(UTF_8)); + } return c; } @@ -234,7 +238,9 @@ public class RevTagParseTest extends RepositoryTestCase { b.write("\u304d\u308c\u3044\n".getBytes(UTF_8)); final RevTag c; c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName()); assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage()); @@ -257,7 +263,9 @@ public class RevTagParseTest extends RepositoryTestCase { b.write("\u304d\u308c\u3044\n".getBytes(UTF_8)); final RevTag c; c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName()); assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage()); @@ -287,7 +295,9 @@ public class RevTagParseTest extends RepositoryTestCase { b.write("Hi\n".getBytes("EUC-JP")); final RevTag c; c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName()); assertEquals("\u304d\u308c\u3044", c.getShortMessage()); @@ -320,7 +330,9 @@ public class RevTagParseTest extends RepositoryTestCase { b.write("Hi\n".getBytes(UTF_8)); final RevTag c; c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName()); assertEquals("\u304d\u308c\u3044", c.getShortMessage()); @@ -355,7 +367,9 @@ public class RevTagParseTest extends RepositoryTestCase { b.write("Hi\n".getBytes(UTF_8)); final RevTag c; c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - c.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + c.parseCanonical(rw, b.toByteArray()); + } assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName()); assertEquals("\u304d\u308c\u3044", c.getShortMessage()); @@ -374,7 +388,9 @@ public class RevTagParseTest extends RepositoryTestCase { b.write("message\n".getBytes(UTF_8)); RevTag t = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - t.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + t.parseCanonical(rw, b.toByteArray()); + } assertEquals("t", t.getTaggerIdent().getName()); assertEquals("message", t.getShortMessage()); @@ -393,7 +409,9 @@ public class RevTagParseTest extends RepositoryTestCase { b.write("message\n".getBytes(UTF_8)); RevTag t = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); - t.parseCanonical(new RevWalk(db), b.toByteArray()); + try (RevWalk rw = new RevWalk(db)) { + t.parseCanonical(rw, b.toByteArray()); + } assertEquals("t", t.getTaggerIdent().getName()); assertEquals("message", t.getShortMessage()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/TreeRevFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/TreeRevFilterTest.java index 9548992a2b..a345459b3b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/TreeRevFilterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/TreeRevFilterTest.java @@ -46,25 +46,15 @@ package org.eclipse.jgit.revwalk; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import java.util.Collections; - import org.eclipse.jgit.revwalk.filter.OrRevFilter; import org.eclipse.jgit.revwalk.filter.RevFilter; import org.eclipse.jgit.revwalk.filter.SkipRevFilter; -import org.eclipse.jgit.treewalk.filter.AndTreeFilter; -import org.eclipse.jgit.treewalk.filter.PathFilterGroup; import org.eclipse.jgit.treewalk.filter.TreeFilter; import org.junit.Test; public class TreeRevFilterTest extends RevWalkTestCase { - private RevFilter treeRevFilter(String path) { - return new TreeRevFilter(rw, treeFilter(path)); - } - - private static TreeFilter treeFilter(String path) { - return AndTreeFilter.create( - PathFilterGroup.createFromStrings(Collections.singleton(path)), - TreeFilter.ANY_DIFF); + private RevFilter treeRevFilter() { + return new TreeRevFilter(rw, TreeFilter.ANY_DIFF); } @Test @@ -73,7 +63,7 @@ public class TreeRevFilterTest extends RevWalkTestCase { RevCommit a = commit(tree(file("d/f", blob("a")))); RevCommit b = commit(tree(file("d/f", blob("a"))), a); RevCommit c = commit(tree(file("d/f", blob("b"))), b); - rw.setRevFilter(treeRevFilter("d/f")); + rw.setRevFilter(treeRevFilter()); markStart(c); assertCommit(c, rw.next()); @@ -91,7 +81,7 @@ public class TreeRevFilterTest extends RevWalkTestCase { RevCommit b = commit(tree(file("d/f", blob("a"))), a); RevCommit c = commit(tree(file("d/f", blob("b"))), b); RevCommit d = commit(tree(file("d/f", blob("b"))), c); - rw.setRevFilter(treeRevFilter("d/f")); + rw.setRevFilter(treeRevFilter()); markStart(d); // d was skipped @@ -111,7 +101,7 @@ public class TreeRevFilterTest extends RevWalkTestCase { RevCommit b = commit(tree(file("d/f", blob("a"))), a); RevCommit c = commit(tree(file("d/f", blob("b"))), b); RevCommit d = commit(tree(file("d/f", blob("b"))), c); - rw.setRevFilter(treeRevFilter("d")); + rw.setRevFilter(treeRevFilter()); markStart(d); // d was skipped @@ -136,7 +126,7 @@ public class TreeRevFilterTest extends RevWalkTestCase { RevCommit g = commit(tree(file("d/f", blob("b"))), f); RevCommit h = commit(tree(file("d/f", blob("b"))), g); RevCommit i = commit(tree(file("d/f", blob("c"))), h); - rw.setRevFilter(treeRevFilter("d/f")); + rw.setRevFilter(treeRevFilter()); markStart(i); assertCommit(i, rw.next()); @@ -156,7 +146,7 @@ public class TreeRevFilterTest extends RevWalkTestCase { @Test public void testPathFilterOrOtherFilter() throws Exception { - RevFilter pathFilter = treeRevFilter("d/f"); + RevFilter pathFilter = treeRevFilter(); RevFilter skipFilter = SkipRevFilter.create(1); RevFilter orFilter = OrRevFilter.create(skipFilter, pathFilter); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java index 1ff64a2e28..80e9e6b14b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java @@ -135,20 +135,20 @@ public class SubmoduleAddTest extends RepositoryTestCase { subCommit = repo.resolve(Constants.HEAD); } - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - generator.loadModulesConfig(); - assertTrue(generator.next()); - assertEquals(path, generator.getModuleName()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - assertEquals(uri, generator.getConfigUrl()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); - assertEquals(subCommit, commit); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + generator.loadModulesConfig(); + assertTrue(generator.next()); + assertEquals(path, generator.getModuleName()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + assertEquals(uri, generator.getConfigUrl()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + assertEquals(subCommit, commit); + } } - Status status = Git.wrap(db).status().call(); assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); assertTrue(status.getAdded().contains(path)); @@ -175,20 +175,20 @@ public class SubmoduleAddTest extends RepositoryTestCase { subCommit = repo.resolve(Constants.HEAD); } - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - generator.loadModulesConfig(); - assertTrue(generator.next()); - assertEquals(name, generator.getModuleName()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - assertEquals(uri, generator.getConfigUrl()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); - assertEquals(subCommit, commit); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + generator.loadModulesConfig(); + assertTrue(generator.next()); + assertEquals(name, generator.getModuleName()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + assertEquals(uri, generator.getConfigUrl()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + assertEquals(subCommit, commit); + } } - Status status = Git.wrap(db).status().call(); assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); assertTrue(status.getAdded().contains(path)); @@ -269,24 +269,25 @@ public class SubmoduleAddTest extends RepositoryTestCase { assertNotNull(repo); addRepoToClose(repo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - String fullUri = db.getDirectory().getAbsolutePath(); - if (File.separatorChar == '\\') { - fullUri = fullUri.replace('\\', '/'); - } - assertEquals(fullUri, generator.getConfigUrl()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); - assertEquals(fullUri, - subModRepo.getConfig().getString( - ConfigConstants.CONFIG_REMOTE_SECTION, - Constants.DEFAULT_REMOTE_NAME, - ConfigConstants.CONFIG_KEY_URL)); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + String fullUri = db.getDirectory().getAbsolutePath(); + if (File.separatorChar == '\\') { + fullUri = fullUri.replace('\\', '/'); + } + assertEquals(fullUri, generator.getConfigUrl()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + assertEquals(fullUri, + subModRepo.getConfig().getString( + ConfigConstants.CONFIG_REMOTE_SECTION, + Constants.DEFAULT_REMOTE_NAME, + ConfigConstants.CONFIG_KEY_URL)); + } } assertEquals(commit, repo.resolve(Constants.HEAD)); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleDeinitTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleDeinitTest.java index 815ce9b350..bb0085106d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleDeinitTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleDeinitTest.java @@ -107,7 +107,6 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(1, updated.size()); File submoduleDir = assertSubmoduleIsInitialized(); - SubmoduleWalk generator; write(new File(submoduleDir, "untracked"), "untracked"); @@ -115,8 +114,9 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(path, result.getPath()); assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.DIRTY, result.getStatus()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + } assertTrue(submoduleDir.isDirectory()); assertNotEquals(0, submoduleDir.list().length); } @@ -132,33 +132,36 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(1, updated.size()); File submoduleDir = assertSubmoduleIsInitialized(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - generator.next(); - - //want to create a commit inside the repo... - try (Repository submoduleLocalRepo = generator.getRepository()) { - JGitTestUtil.writeTrashFile(submoduleLocalRepo, "file.txt", - "new data"); - Git.wrap(submoduleLocalRepo).commit().setAll(true) - .setMessage("local commit").call(); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + generator.next(); + + // want to create a commit inside the repo... + try (Repository submoduleLocalRepo = generator.getRepository()) { + JGitTestUtil.writeTrashFile(submoduleLocalRepo, "file.txt", + "new data"); + Git.wrap(submoduleLocalRepo).commit().setAll(true) + .setMessage("local commit").call(); + } } SubmoduleDeinitResult result = runDeinit(new SubmoduleDeinitCommand(db).addPath("sub")); assertEquals(path, result.getPath()); assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.DIRTY, result.getStatus()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + } assertTrue(submoduleDir.isDirectory()); assertNotEquals(0, submoduleDir.list().length); } private File assertSubmoduleIsInitialized() throws IOException { - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - File submoduleDir = new File(db.getWorkTree(), generator.getPath()); - assertTrue(submoduleDir.isDirectory()); - assertNotEquals(0, submoduleDir.list().length); - return submoduleDir; + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + File submoduleDir = new File(db.getWorkTree(), generator.getPath()); + assertTrue(submoduleDir.isDirectory()); + assertNotEquals(0, submoduleDir.list().length); + return submoduleDir; + } } @Test @@ -180,8 +183,9 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(path, result.getPath()); assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.FORCED, result.getStatus()); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + } assertTrue(submoduleDir.isDirectory()); assertEquals(0, submoduleDir.list().length); } @@ -202,8 +206,9 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(path, result.getPath()); assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.SUCCESS, result.getStatus()); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + } assertTrue(submoduleDir.isDirectory()); assertEquals(0, submoduleDir.list().length); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleInitTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleInitTest.java index c7a009c5bf..9fe2fc6fdc 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleInitTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleInitTest.java @@ -86,11 +86,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { ConfigInvalidException, GitAPIException { final String path = addSubmoduleToIndex(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -109,10 +109,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(url, generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(url, generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -126,11 +127,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { base); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -149,10 +150,12 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals("git://server/repo.git/sub.git", generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals("git://server/repo.git/sub.git", + generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -167,11 +170,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { base); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -190,10 +193,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals("git://server/sub.git", generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals("git://server/sub.git", generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -208,11 +212,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { base); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -231,10 +235,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals("git://server2/sub.git", generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals("git://server2/sub.git", generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -250,11 +255,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -273,10 +278,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(base + "/sub.git", generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(base + "/sub.git", generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -291,11 +297,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { base); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java index 6f3b52f7bb..1053e31bc9 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java @@ -119,11 +119,11 @@ public class SubmoduleSyncTest extends RepositoryTestCase { addRepoToClose(subRepo); assertNotNull(subRepo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertEquals(url, generator.getModulesUrl()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertEquals(url, generator.getModulesUrl()); + } SubmoduleSyncCommand command = new SubmoduleSyncCommand(db); Map<String, String> synced = command.call(); assertNotNull(synced); @@ -132,16 +132,17 @@ public class SubmoduleSyncTest extends RepositoryTestCase { assertEquals(path, module.getKey()); assertEquals(url, module.getValue()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(url, generator.getConfigUrl()); - try (Repository subModRepository = generator.getRepository()) { - StoredConfig submoduleConfig = subModRepository.getConfig(); - assertEquals(url, - submoduleConfig.getString( - ConfigConstants.CONFIG_REMOTE_SECTION, - Constants.DEFAULT_REMOTE_NAME, - ConfigConstants.CONFIG_KEY_URL)); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(url, generator.getConfigUrl()); + try (Repository subModRepository = generator.getRepository()) { + StoredConfig submoduleConfig = subModRepository.getConfig(); + assertEquals(url, + submoduleConfig.getString( + ConfigConstants.CONFIG_REMOTE_SECTION, + Constants.DEFAULT_REMOTE_NAME, + ConfigConstants.CONFIG_KEY_URL)); + } } } @@ -190,11 +191,11 @@ public class SubmoduleSyncTest extends RepositoryTestCase { assertNotNull(subRepo); addRepoToClose(subRepo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertEquals(current, generator.getModulesUrl()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertEquals(current, generator.getModulesUrl()); + } modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_URL, "../sub.git"); modulesConfig.save(); @@ -207,16 +208,17 @@ public class SubmoduleSyncTest extends RepositoryTestCase { assertEquals(path, module.getKey()); assertEquals("git://server/sub.git", module.getValue()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals("git://server/sub.git", generator.getConfigUrl()); - try (Repository subModRepository1 = generator.getRepository()) { - StoredConfig submoduleConfig = subModRepository1.getConfig(); - assertEquals("git://server/sub.git", - submoduleConfig.getString( - ConfigConstants.CONFIG_REMOTE_SECTION, - Constants.DEFAULT_REMOTE_NAME, - ConfigConstants.CONFIG_KEY_URL)); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals("git://server/sub.git", generator.getConfigUrl()); + try (Repository subModRepository1 = generator.getRepository()) { + StoredConfig submoduleConfig = subModRepository1.getConfig(); + assertEquals("git://server/sub.git", + submoduleConfig.getString( + ConfigConstants.CONFIG_REMOTE_SECTION, + Constants.DEFAULT_REMOTE_NAME, + ConfigConstants.CONFIG_KEY_URL)); + } } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java index bbce413ef3..6f358b47fd 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java @@ -119,11 +119,12 @@ public class SubmoduleUpdateTest extends RepositoryTestCase { assertEquals(1, updated.size()); assertEquals(path, updated.iterator().next()); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - try (Repository subRepo = generator.getRepository()) { - assertNotNull(subRepo); - assertEquals(commit, subRepo.resolve(Constants.HEAD)); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + try (Repository subRepo = generator.getRepository()) { + assertNotNull(subRepo); + assertEquals(commit, subRepo.resolve(Constants.HEAD)); + } } } @@ -181,10 +182,11 @@ public class SubmoduleUpdateTest extends RepositoryTestCase { }); editor.commit(); - Repository subRepo = Git.init().setBare(false) + try (Repository subRepo = Git.init().setBare(false) .setDirectory(new File(db.getWorkTree(), path)).call() - .getRepository(); - assertNotNull(subRepo); + .getRepository()) { + assertNotNull(subRepo); + } SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db); Collection<String> updated = command.call(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java index ea1ace364e..0b6047f881 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java @@ -98,10 +98,11 @@ public class SubmoduleWalkTest extends RepositoryTestCase { @Test public void repositoryWithNoSubmodules() throws IOException { - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertFalse(gen.next()); - assertNull(gen.getPath()); - assertEquals(ObjectId.zeroId(), gen.getObjectId()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertFalse(gen.next()); + assertNull(gen.getPath()); + assertEquals(ObjectId.zeroId(), gen.getObjectId()); + } } @Test @@ -129,20 +130,21 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(id, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertNull(gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertNull(gen.getModulesUrl()); - assertNull(gen.getRepository()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(id, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertNull(gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertNull(gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } Status status = Git.wrap(db).status().call(); - assertTrue(!status.isClean()); - assertFalse(gen.next()); + assertFalse(status.isClean()); } @Test @@ -178,24 +180,25 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(id, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertNull(gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertNull(gen.getModulesUrl()); - try (Repository subRepo = gen.getRepository()) { - assertNotNull(subRepo); - assertEquals(modulesGitDir.getAbsolutePath(), - subRepo.getDirectory().getAbsolutePath()); - assertEquals(new File(db.getWorkTree(), path).getAbsolutePath(), - subRepo.getWorkTree().getAbsolutePath()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(id, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertNull(gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertNull(gen.getModulesUrl()); + try (Repository subRepo = gen.getRepository()) { + assertNotNull(subRepo); + assertEquals(modulesGitDir.getAbsolutePath(), + subRepo.getDirectory().getAbsolutePath()); + assertEquals(new File(db.getWorkTree(), path).getAbsolutePath(), + subRepo.getWorkTree().getAbsolutePath()); + } + assertFalse(gen.next()); } - assertFalse(gen.next()); } @Test @@ -232,23 +235,24 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(id, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertNull(gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertNull(gen.getModulesUrl()); - try (Repository subRepo = gen.getRepository()) { - assertNotNull(subRepo); - assertEqualsFile(modulesGitDir, subRepo.getDirectory()); - assertEqualsFile(new File(db.getWorkTree(), path), - subRepo.getWorkTree()); - subRepo.close(); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(id, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertNull(gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertNull(gen.getModulesUrl()); + try (Repository subRepo = gen.getRepository()) { + assertNotNull(subRepo); + assertEqualsFile(modulesGitDir, subRepo.getDirectory()); + assertEqualsFile(new File(db.getWorkTree(), path), + subRepo.getWorkTree()); + subRepo.close(); + assertFalse(gen.next()); + } } } @@ -270,18 +274,19 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(id, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertNull(gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertNull(gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(id, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertNull(gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertNull(gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } @Test @@ -312,12 +317,13 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - gen.setFilter(PathFilter.create(path1)); - assertTrue(gen.next()); - assertEquals(path1, gen.getPath()); - assertEquals(id1, gen.getObjectId()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + gen.setFilter(PathFilter.create(path1)); + assertTrue(gen.next()); + assertEquals(path1, gen.getPath()); + assertEquals(id1, gen.getObjectId()); + assertFalse(gen.next()); + } } @Test @@ -358,18 +364,19 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(subId, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertEquals("sub", gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertEquals("git://example.com/sub", gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(subId, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertEquals("sub", gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertEquals("git://example.com/sub", gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } @Test @@ -397,17 +404,19 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }) .create()); - SubmoduleWalk gen = SubmoduleWalk.forPath(db, commit.getTree(), "sub"); - assertEquals(path, gen.getPath()); - assertEquals(subId, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertEquals("sub", gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertEquals("git://example.com/sub", gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forPath(db, commit.getTree(), + "sub")) { + assertEquals(path, gen.getPath()); + assertEquals(subId, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertEquals("sub", gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertEquals("git://example.com/sub", gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } @Test @@ -437,17 +446,18 @@ public class SubmoduleWalkTest extends RepositoryTestCase { final CanonicalTreeParser p = new CanonicalTreeParser(); p.reset(testDb.getRevWalk().getObjectReader(), commit.getTree()); - SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub"); - assertEquals(path, gen.getPath()); - assertEquals(subId, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertEquals("sub", gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertEquals("git://example.com/sub", gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub")) { + assertEquals(path, gen.getPath()); + assertEquals(subId, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertEquals("sub", gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertEquals("git://example.com/sub", gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } @Test @@ -477,16 +487,17 @@ public class SubmoduleWalkTest extends RepositoryTestCase { final CanonicalTreeParser p = new CanonicalTreeParser(); p.reset(testDb.getRevWalk().getObjectReader(), commit.getTree()); - SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub"); - assertEquals(path, gen.getPath()); - assertEquals(subId, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertEquals("sub", gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertEquals("git://example.com/sub", gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub")) { + assertEquals(path, gen.getPath()); + assertEquals(subId, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertEquals("sub", gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertEquals("git://example.com/sub", gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/AtomicPushTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/AtomicPushTest.java index c1e078d10d..d6c7a6199d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/AtomicPushTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/AtomicPushTest.java @@ -55,10 +55,9 @@ import org.eclipse.jgit.errors.TransportException; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; -import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.lib.NullProgressMonitor; import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.resolver.ReceivePackFactory; import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; @@ -73,8 +72,8 @@ public class AtomicPushTest { private Object ctx = new Object(); private InMemoryRepository server; private InMemoryRepository client; - private ObjectId obj1; - private ObjectId obj2; + private ObjectId commit1; + private ObjectId commit2; @Before public void setUp() throws Exception { @@ -92,10 +91,11 @@ public class AtomicPushTest { }); uri = testProtocol.register(ctx, server); - try (ObjectInserter ins = client.newObjectInserter()) { - obj1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("test")); - obj2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("file")); - ins.flush(); + try (TestRepository<?> clientRepo = new TestRepository<>(client)) { + commit1 = clientRepo.commit().noFiles().message("test commit 1") + .create(); + commit2 = clientRepo.commit().noFiles().message("test commit 2") + .create(); } } @@ -149,13 +149,13 @@ public class AtomicPushTest { List<RemoteRefUpdate> cmds = new ArrayList<>(); cmds.add(new RemoteRefUpdate( null, null, - obj1, "refs/heads/one", + commit1, "refs/heads/one", true /* force update */, null /* no local tracking ref */, ObjectId.zeroId())); cmds.add(new RemoteRefUpdate( null, null, - obj2, "refs/heads/two", + commit2, "refs/heads/two", true /* force update */, null /* no local tracking ref */, ObjectId.zeroId())); @@ -176,16 +176,16 @@ public class AtomicPushTest { List<RemoteRefUpdate> cmds = new ArrayList<>(); cmds.add(new RemoteRefUpdate( null, null, - obj1, "refs/heads/one", + commit1, "refs/heads/one", true /* force update */, null /* no local tracking ref */, ObjectId.zeroId())); cmds.add(new RemoteRefUpdate( null, null, - obj2, "refs/heads/two", + commit2, "refs/heads/two", true /* force update */, null /* no local tracking ref */, - obj1)); + commit1)); return cmds; } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/HttpAuthTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/HttpAuthTest.java index ea15ebe7de..947ca97615 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/HttpAuthTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/HttpAuthTest.java @@ -133,16 +133,15 @@ public class HttpAuthTest { @Override public String getHeaderField(String name) { - if (!headerFields.containsKey(name)) + if (!headerFields.containsKey(name)) { return null; - else { - int n = headerFields.get(name).size(); + } + int n = headerFields.get(name).size(); - if (n > 0) - return headerFields.get(name).get(n - 1); - else - return null; + if (n > 0) { + return headerFields.get(name).get(n - 1); } + return null; } @Override diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushOptionsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushOptionsTest.java index fd1c3bf8b8..18946e0d50 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushOptionsTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushOptionsTest.java @@ -62,10 +62,9 @@ import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; import org.eclipse.jgit.junit.RepositoryTestCase; -import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.lib.NullProgressMonitor; import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.revwalk.RevCommit; @@ -79,8 +78,8 @@ public class PushOptionsTest extends RepositoryTestCase { private Object ctx = new Object(); private InMemoryRepository server; private InMemoryRepository client; - private ObjectId obj1; - private ObjectId obj2; + private ObjectId commit1; + private ObjectId commit2; private ReceivePack receivePack; @Override @@ -101,10 +100,11 @@ public class PushOptionsTest extends RepositoryTestCase { uri = testProtocol.register(ctx, server); - try (ObjectInserter ins = client.newObjectInserter()) { - obj1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("test")); - obj2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("file")); - ins.flush(); + try (TestRepository<?> clientRepo = new TestRepository<>(client)) { + commit1 = clientRepo.commit().noFiles().message("test commit 1") + .create(); + commit2 = clientRepo.commit().noFiles().message("test commit 2") + .create(); } } @@ -121,12 +121,12 @@ public class PushOptionsTest extends RepositoryTestCase { private List<RemoteRefUpdate> commands(boolean atomicSafe) throws IOException { List<RemoteRefUpdate> cmds = new ArrayList<>(); - cmds.add(new RemoteRefUpdate(null, null, obj1, "refs/heads/one", + cmds.add(new RemoteRefUpdate(null, null, commit1, "refs/heads/one", true /* force update */, null /* no local tracking ref */, ObjectId.zeroId())); - cmds.add(new RemoteRefUpdate(null, null, obj2, "refs/heads/two", + cmds.add(new RemoteRefUpdate(null, null, commit2, "refs/heads/two", true /* force update */, null /* no local tracking ref */, - atomicSafe ? ObjectId.zeroId() : obj1)); + atomicSafe ? ObjectId.zeroId() : commit1)); return cmds; } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReachableCommitRequestValidatorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReachableCommitRequestValidatorTest.java new file mode 100644 index 0000000000..20b490b263 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReachableCommitRequestValidatorTest.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2019, Google LLC. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.transport; + +import org.eclipse.jgit.transport.UploadPack.RequestValidator; + +/** + * Client may ask for any commit reachable from a reference advertised by + * the server. + */ +public class ReachableCommitRequestValidatorTest extends RequestValidatorTestCase { + + @Override + protected RequestValidator createValidator() { + return new UploadPack.ReachableCommitRequestValidator(); + } + + @Override + protected boolean isReachableCommitValid() { + return true; + } + + @Override + protected boolean isUnreachableCommitValid() { + return false; + } + + @Override + protected boolean isReachableBlobValid_withBitmaps() { + return true; + } + + @Override + protected boolean isReachableBlobValid_withoutBitmaps() { + return false; + } + + @Override + protected boolean isUnreachableBlobValid() { + return false; + } + + @Override + protected boolean isAdvertisedTipValid() { + return true; + } + + @Override + protected boolean isUnadvertisedTipCommitValid() { + return false; + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReachableCommitTipRequestValidatorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReachableCommitTipRequestValidatorTest.java new file mode 100644 index 0000000000..5e5391dac7 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReachableCommitTipRequestValidatorTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2019, Google LLC. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.transport; + +import org.eclipse.jgit.transport.UploadPack.RequestValidator; + +/** + * Client may ask for any commit reachable from any reference, even if that + * reference wasn't advertised. + */ +public class ReachableCommitTipRequestValidatorTest + extends RequestValidatorTestCase { + + @Override + protected RequestValidator createValidator() { + return new UploadPack.ReachableCommitTipRequestValidator(); + } + + @Override + protected boolean isReachableCommitValid() { + return true; + } + + @Override + protected boolean isUnreachableCommitValid() { + return false; + } + + @Override + protected boolean isAdvertisedTipValid() { + return true; + } + + @Override + protected boolean isReachableBlobValid_withBitmaps() { + return true; + } + + @Override + protected boolean isReachableBlobValid_withoutBitmaps() { + return false; + } + + @Override + protected boolean isUnreachableBlobValid() { + return false; + } + + @Override + protected boolean isUnadvertisedTipCommitValid() { + return true; + } + +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RequestValidatorTestCase.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RequestValidatorTestCase.java new file mode 100644 index 0000000000..c1e7bcfcbe --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RequestValidatorTestCase.java @@ -0,0 +1,319 @@ +/* + * Copyright (C) 2019, Google LLC. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.transport; + +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jgit.errors.PackProtocolException; +import org.eclipse.jgit.errors.TransportException; +import org.eclipse.jgit.internal.storage.dfs.DfsGarbageCollector; +import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; +import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; +import org.eclipse.jgit.junit.TestRepository; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevBlob; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.transport.UploadPack.RequestValidator; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public abstract class RequestValidatorTestCase { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private RevCommit reachableCommit; + + private RevCommit tipAdvertisedCommit; + + private RevCommit tipUnadvertisedCommit; + + private RevCommit unreachableCommit; + + private RevBlob reachableBlob; + + private RevBlob unreachableBlob; + + private InMemoryRepository repo; + + protected abstract RequestValidator createValidator(); + + @Before + public void setUp() throws Exception { + repo = new InMemoryRepository(new DfsRepositoryDescription()); + try (TestRepository<InMemoryRepository> git = new TestRepository<>( + repo)) { + reachableBlob = git.blob("foo"); + reachableCommit = git + .commit(git.tree(git.file("foo", reachableBlob))); + tipAdvertisedCommit = git.commit(reachableCommit); + git.update("advertised", tipAdvertisedCommit); + + tipUnadvertisedCommit = git.commit(reachableCommit); + git.update("unadvertised", tipUnadvertisedCommit); + + unreachableBlob = git.blob("unreachableFoo"); + unreachableCommit = git + .commit(git.tree(git.file("foo", unreachableBlob))); + } + } + + /** + * @return true if a commit reachable from a visible tip (but not directly + * the tip) is valid + */ + protected abstract boolean isReachableCommitValid(); + + /** @return true if a commit not reachable from any tip is valid */ + protected abstract boolean isUnreachableCommitValid(); + + /** + * @return true if the commit directly pointed by an advertised ref is valid + */ + protected abstract boolean isAdvertisedTipValid(); + + /** + * @return true if the object directly pointed by a non-advertised ref is + * valid + */ + protected abstract boolean isUnadvertisedTipCommitValid(); + + // UploadPack doesn't allow to ask for blobs when there is no + // bitmap. Test both cases separately. + /** + * @return true if a reachable blob is valid (and the repo has bitmaps) + */ + protected abstract boolean isReachableBlobValid_withBitmaps(); + + /** + * @return true if a reachable blob is valid (and the repo does NOT have + * bitmaps) + */ + protected abstract boolean isReachableBlobValid_withoutBitmaps(); + + /** + * @return true if a blob unreachable from any tip is valid + */ + protected abstract boolean isUnreachableBlobValid(); + + @Test + public void validateReachableCommitWithBitmaps() + throws PackProtocolException, IOException { + if (!isReachableCommitValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers + .containsString( + "want " + reachableCommit.name() + " not valid")); + + } + createValidator().checkWants(getUploadPack(getRepoWithBitmaps()), + Arrays.asList(reachableCommit)); + } + + @Test + public void validateReachableCommitWithoutBitmaps() + throws PackProtocolException, IOException { + if (!isReachableCommitValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + reachableCommit.name() + " not valid")); + + } + createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()), + Arrays.asList(reachableCommit)); + } + + @Test + public void validateAdvertisedTipWithBitmaps() + throws PackProtocolException, IOException { + if (!isAdvertisedTipValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + tipAdvertisedCommit.name() + " not valid")); + + } + createValidator().checkWants(getUploadPack(getRepoWithBitmaps()), + Arrays.asList(tipAdvertisedCommit)); + } + + @Test + public void validateAdvertisedTipWithoutBitmaps() + throws PackProtocolException, IOException { + if (!isAdvertisedTipValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + tipAdvertisedCommit.name() + " not valid")); + + } + createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()), + Arrays.asList(tipAdvertisedCommit)); + } + + @Test + public void validateUnadvertisedTipWithBitmaps() + throws PackProtocolException, IOException { + if (!isUnadvertisedTipCommitValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + tipUnadvertisedCommit.name() + " not valid")); + + } + createValidator().checkWants(getUploadPack(getRepoWithBitmaps()), + Arrays.asList(tipUnadvertisedCommit)); + } + + @Test + public void validateUnadvertisedTipWithoutBitmaps() + throws PackProtocolException, IOException { + if (!isUnadvertisedTipCommitValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + tipUnadvertisedCommit.name() + " not valid")); + + } + createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()), + Arrays.asList(tipUnadvertisedCommit)); + } + + @Test + public void validateUnreachableCommitWithBitmaps() + throws PackProtocolException, IOException { + if (!isUnreachableCommitValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + unreachableCommit.name() + " not valid")); + + } + createValidator().checkWants(getUploadPack(getRepoWithBitmaps()), + Arrays.asList(unreachableCommit)); + } + + @Test + public void validateUnreachableCommitWithoutBitmaps() + throws PackProtocolException, IOException { + if (!isUnreachableCommitValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + unreachableCommit.name() + " not valid")); + + } + createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()), + Arrays.asList(unreachableCommit)); + } + + @Test + public void validateReachableBlobWithBitmaps() + throws PackProtocolException, IOException { + if (!isReachableBlobValid_withBitmaps()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + reachableBlob.name() + " not valid")); + } + createValidator().checkWants(getUploadPack(getRepoWithBitmaps()), + Arrays.asList(reachableBlob)); + } + + @Test + public void validateReachableBlobWithoutBitmaps() + throws PackProtocolException, IOException { + if (!isReachableBlobValid_withoutBitmaps()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + reachableBlob.name() + " not valid")); + } + createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()), + Arrays.asList(reachableBlob)); + } + + @Test + public void validateUnreachableBlobWithBitmaps() + throws PackProtocolException, IOException { + if (!isUnreachableBlobValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + unreachableBlob.name() + " not valid")); + } + createValidator().checkWants(getUploadPack(getRepoWithBitmaps()), + Arrays.asList(unreachableBlob)); + } + + @Test + public void validateUnreachableBlobWithoutBitmaps() + throws PackProtocolException, IOException { + if (!isUnreachableBlobValid()) { + thrown.expect(TransportException.class); + thrown.expectMessage(Matchers.containsString( + "want " + unreachableBlob.name() + " not valid")); + } + createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()), + Arrays.asList(unreachableBlob)); + } + + private UploadPack getUploadPack(Repository repository) throws IOException { + UploadPack uploadPack = new UploadPack(repository); + + Ref advertisedRef = repo.getRefDatabase().findRef("advertised"); + Map<String, Ref> advertisedRefs = new HashMap<>(); + advertisedRefs.put(advertisedRef.getName(), advertisedRef); + + uploadPack.setAdvertisedRefs(advertisedRefs); + return uploadPack; + } + + private Repository getRepoWithBitmaps() throws IOException { + new DfsGarbageCollector(repo).pack(null); + repo.scanForRepoChanges(); + return repo; + } + + private Repository getRepoWithoutBitmaps() { + return repo; + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/TipRequestValidatorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/TipRequestValidatorTest.java new file mode 100644 index 0000000000..565d0e6e95 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/TipRequestValidatorTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2019, Google LLC. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.transport; + +import org.eclipse.jgit.transport.UploadPack.RequestValidator; + +/** + * Client may ask for objects that are the tip of any reference, even if not + * advertised. + */ +public class TipRequestValidatorTest extends RequestValidatorTestCase { + + @Override + protected RequestValidator createValidator() { + return new UploadPack.TipRequestValidator(); + } + + @Override + protected boolean isReachableCommitValid() { + return false; + } + + @Override + protected boolean isUnreachableCommitValid() { + return false; + } + + @Override + protected boolean isReachableBlobValid_withBitmaps() { + return false; + } + + @Override + protected boolean isReachableBlobValid_withoutBitmaps() { + return false; + } + + @Override + protected boolean isUnreachableBlobValid() { + return false; + } + + @Override + protected boolean isAdvertisedTipValid() { + return true; + } + + @Override + protected boolean isUnadvertisedTipCommitValid() { + return true; + } + +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackReachabilityTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackReachabilityTest.java new file mode 100644 index 0000000000..7122082d81 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackReachabilityTest.java @@ -0,0 +1,316 @@ +/* + * Copyright (C) 2019, Google LLC. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.transport; + +import static org.eclipse.jgit.lib.MoreAsserts.assertThrows; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.Collections; + +import org.eclipse.jgit.errors.TransportException; +import org.eclipse.jgit.internal.storage.dfs.DfsGarbageCollector; +import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; +import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; +import org.eclipse.jgit.junit.TestRepository; +import org.eclipse.jgit.lib.NullProgressMonitor; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevBlob; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.transport.UploadPack.RequestPolicy; +import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; +import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; +import org.eclipse.jgit.transport.resolver.UploadPackFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +/** + * Test combinations of: + * <ul> + * <li>Fetch a blob or a commit</li> + * <li>Fetched object is reachable or not</li> + * <li>With and without bitmaps</li> + * </ul> + */ +public class UploadPackReachabilityTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private URIish uri; + + private TestProtocol<Object> testProtocol; + + private Object ctx = new Object(); + + private InMemoryRepository server; + + private InMemoryRepository client; + + private TestRepository<InMemoryRepository> remote; + + @Before + public void setUp() throws Exception { + server = newRepo("server"); + client = newRepo("client"); + + remote = new TestRepository<>(server); + } + + @After + public void tearDown() { + Transport.unregister(testProtocol); + } + + @Test + public void testFetchUnreachableBlobWithBitmap() throws Exception { + RevBlob blob = remote.blob("foo"); + remote.commit(remote.tree(remote.file("foo", blob))); + generateBitmaps(server); + + testProtocol = generateReachableCommitUploadPackProtocol(); + uri = testProtocol.register(ctx, server); + + assertFalse(client.getObjectDatabase().has(blob.toObjectId())); + + try (Transport tn = testProtocol.open(uri, client, "server")) { + TransportException e = assertThrows(TransportException.class, + () -> tn.fetch(NullProgressMonitor.INSTANCE, Collections + .singletonList(new RefSpec(blob.name())))); + assertThat(e.getMessage(), + containsString("want " + blob.name() + " not valid")); + } + } + + @Test + public void testFetchReachableBlobWithoutBitmap() throws Exception { + RevBlob blob = remote.blob("foo"); + RevCommit commit = remote.commit(remote.tree(remote.file("foo", blob))); + remote.update("master", commit); + + testProtocol = generateReachableCommitUploadPackProtocol(); + uri = testProtocol.register(ctx, server); + + assertFalse(client.getObjectDatabase().has(blob.toObjectId())); + + try (Transport tn = testProtocol.open(uri, client, "server")) { + TransportException e = assertThrows(TransportException.class, + () -> tn.fetch(NullProgressMonitor.INSTANCE, Collections + .singletonList(new RefSpec(blob.name())))); + assertThat(e.getMessage(), + containsString( + "want " + blob.name() + " not valid")); + } + } + + @Test + public void testFetchReachableBlobWithoutBitmapButFilterAllowed() throws Exception { + InMemoryRepository server2 = newRepo("server2"); + try (TestRepository<InMemoryRepository> remote2 = new TestRepository<>( + server2)) { + RevBlob blob = remote2.blob("foo"); + RevCommit commit = remote2.commit(remote2.tree(remote2.file("foo", blob))); + remote2.update("master", commit); + + server2.getConfig().setBoolean("uploadpack", null, "allowfilter", + true); + + testProtocol = new TestProtocol<>((Object req, Repository db) -> { + UploadPack up = new UploadPack(db); + up.setRequestPolicy(RequestPolicy.REACHABLE_COMMIT); + return up; + }, null); + uri = testProtocol.register(ctx, server2); + + assertFalse(client.getObjectDatabase().has(blob.toObjectId())); + + try (Transport tn = testProtocol.open(uri, client, "server2")) { + tn.fetch(NullProgressMonitor.INSTANCE, + Collections.singletonList(new RefSpec(blob.name()))); + assertTrue(client.getObjectDatabase().has(blob.toObjectId())); + } + } + } + + @Test + public void testFetchUnreachableBlobWithoutBitmap() throws Exception { + RevBlob blob = remote.blob("foo"); + remote.commit(remote.tree(remote.file("foo", blob))); + + testProtocol = generateReachableCommitUploadPackProtocol(); + uri = testProtocol.register(ctx, server); + + assertFalse(client.getObjectDatabase().has(blob.toObjectId())); + + try (Transport tn = testProtocol.open(uri, client, "server")) { + TransportException e = assertThrows(TransportException.class, () -> + tn.fetch(NullProgressMonitor.INSTANCE, + Collections.singletonList(new RefSpec(blob.name())))); + assertThat(e.getMessage(), + containsString("want " + blob.name() + " not valid")); + } + } + + @Test + public void testFetchReachableBlobWithBitmap() throws Exception { + RevBlob blob = remote.blob("foo"); + RevCommit commit = remote.commit(remote.tree(remote.file("foo", blob))); + remote.update("master", commit); + generateBitmaps(server); + + testProtocol = generateReachableCommitUploadPackProtocol(); + uri = testProtocol.register(ctx, server); + + assertFalse(client.getObjectDatabase().has(blob.toObjectId())); + + try (Transport tn = testProtocol.open(uri, client, "server")) { + tn.fetch(NullProgressMonitor.INSTANCE, + Collections.singletonList(new RefSpec(blob.name()))); + assertTrue(client.getObjectDatabase().has(blob.toObjectId())); + } + } + + @Test + public void testFetchReachableCommitWithBitmap() throws Exception { + RevCommit commit = remote + .commit(remote.tree(remote.file("foo", remote.blob("foo")))); + remote.update("master", commit); + generateBitmaps(server); + + testProtocol = generateReachableCommitUploadPackProtocol(); + uri = testProtocol.register(ctx, server); + + assertFalse(client.getObjectDatabase().has(commit.toObjectId())); + + try (Transport tn = testProtocol.open(uri, client, "server")) { + tn.fetch(NullProgressMonitor.INSTANCE, + Collections.singletonList(new RefSpec(commit.name()))); + assertTrue(client.getObjectDatabase().has(commit.toObjectId())); + } + } + + @Test + public void testFetchReachableCommitWithoutBitmap() throws Exception { + RevCommit commit = remote + .commit(remote.tree(remote.file("foo", remote.blob("foo")))); + remote.update("master", commit); + generateBitmaps(server); + + testProtocol = generateReachableCommitUploadPackProtocol(); + uri = testProtocol.register(ctx, server); + + assertFalse(client.getObjectDatabase().has(commit.toObjectId())); + + try (Transport tn = testProtocol.open(uri, client, "server")) { + tn.fetch(NullProgressMonitor.INSTANCE, + Collections.singletonList(new RefSpec(commit.name()))); + assertTrue(client.getObjectDatabase().has(commit.toObjectId())); + } + } + + @Test + public void testFetchUnreachableCommitWithBitmap() throws Exception { + RevCommit commit = remote + .commit(remote.tree(remote.file("foo", remote.blob("foo")))); + generateBitmaps(server); + + testProtocol = generateReachableCommitUploadPackProtocol(); + uri = testProtocol.register(ctx, server); + + assertFalse(client.getObjectDatabase().has(commit.toObjectId())); + + try (Transport tn = testProtocol.open(uri, client, "server")) { + TransportException e = assertThrows(TransportException.class, + () -> tn.fetch(NullProgressMonitor.INSTANCE, + Collections.singletonList(new RefSpec(commit.name())))); + assertThat(e.getMessage(), + containsString("want " + commit.name() + " not valid")); + } + } + + @Test + public void testFetchUnreachableCommitWithoutBitmap() throws Exception { + RevCommit commit = remote + .commit(remote.tree(remote.file("foo", remote.blob("foo")))); + + testProtocol = generateReachableCommitUploadPackProtocol(); + uri = testProtocol.register(ctx, server); + + assertFalse(client.getObjectDatabase().has(commit.toObjectId())); + + try (Transport tn = testProtocol.open(uri, client, "server")) { + TransportException e = assertThrows(TransportException.class, + () -> tn.fetch(NullProgressMonitor.INSTANCE, Collections + .singletonList(new RefSpec(commit.name())))); + assertThat(e.getMessage(), + containsString("want " + commit.name() + " not valid")); + } + } + + private static InMemoryRepository newRepo(String name) { + return new InMemoryRepository(new DfsRepositoryDescription(name)); + } + + private void generateBitmaps(InMemoryRepository repo) throws Exception { + new DfsGarbageCollector(repo).pack(null); + repo.scanForRepoChanges(); + } + + private static TestProtocol<Object> generateReachableCommitUploadPackProtocol() { + return new TestProtocol<>(new UploadPackFactory<Object>() { + @Override + public UploadPack create(Object req, Repository db) + throws ServiceNotEnabledException, + ServiceNotAuthorizedException { + UploadPack up = new UploadPack(db); + up.setRequestPolicy(RequestPolicy.REACHABLE_COMMIT); + return up; + } + }, null); + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java index 528a63f9c0..6d53555e9f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java @@ -98,14 +98,6 @@ public class UploadPackTest { repo.scanForRepoChanges(); } - private static TestProtocol<Object> generateReachableCommitUploadPackProtocol() { - return new TestProtocol<>((Object req, Repository db) -> { - UploadPack up = new UploadPack(db); - up.setRequestPolicy(RequestPolicy.REACHABLE_COMMIT); - return up; - }, null); - } - @Test public void testFetchParentOfShallowCommit() throws Exception { RevCommit commit0 = remote.commit().message("0").create(); @@ -134,95 +126,6 @@ public class UploadPackTest { } @Test - public void testFetchUnreachableBlobWithBitmap() throws Exception { - RevBlob blob = remote.blob("foo"); - remote.commit(remote.tree(remote.file("foo", blob))); - generateBitmaps(server); - - testProtocol = generateReachableCommitUploadPackProtocol(); - uri = testProtocol.register(ctx, server); - - assertFalse(client.getObjectDatabase().has(blob.toObjectId())); - - try (Transport tn = testProtocol.open(uri, client, "server")) { - TransportException e = assertThrows(TransportException.class, - () -> tn.fetch(NullProgressMonitor.INSTANCE, Collections - .singletonList(new RefSpec(blob.name())))); - assertThat(e.getMessage(), - containsString("want " + blob.name() + " not valid")); - } - } - - @Test - public void testFetchReachableBlobWithBitmap() throws Exception { - RevBlob blob = remote.blob("foo"); - RevCommit commit = remote.commit(remote.tree(remote.file("foo", blob))); - remote.update("master", commit); - generateBitmaps(server); - - testProtocol = generateReachableCommitUploadPackProtocol(); - uri = testProtocol.register(ctx, server); - - assertFalse(client.getObjectDatabase().has(blob.toObjectId())); - - try (Transport tn = testProtocol.open(uri, client, "server")) { - tn.fetch(NullProgressMonitor.INSTANCE, - Collections.singletonList(new RefSpec(blob.name()))); - assertTrue(client.getObjectDatabase().has(blob.toObjectId())); - } - } - - @Test - public void testFetchReachableBlobWithoutBitmap() throws Exception { - RevBlob blob = remote.blob("foo"); - RevCommit commit = remote.commit(remote.tree(remote.file("foo", blob))); - remote.update("master", commit); - - testProtocol = generateReachableCommitUploadPackProtocol(); - uri = testProtocol.register(ctx, server); - - assertFalse(client.getObjectDatabase().has(blob.toObjectId())); - - try (Transport tn = testProtocol.open(uri, client, "server")) { - TransportException e = assertThrows(TransportException.class, - () -> tn.fetch(NullProgressMonitor.INSTANCE, Collections - .singletonList(new RefSpec(blob.name())))); - assertThat(e.getMessage(), - containsString( - "want " + blob.name() + " not valid")); - } - } - - @Test - public void testFetchReachableBlobWithoutBitmapButFilterAllowed() throws Exception { - InMemoryRepository server2 = newRepo("server2"); - try (TestRepository<InMemoryRepository> remote2 = new TestRepository<>( - server2)) { - RevBlob blob = remote2.blob("foo"); - RevCommit commit = remote2.commit(remote2.tree(remote2.file("foo", blob))); - remote2.update("master", commit); - - server2.getConfig().setBoolean("uploadpack", null, "allowfilter", - true); - - testProtocol = new TestProtocol<>((Object req, Repository db) -> { - UploadPack up = new UploadPack(db); - up.setRequestPolicy(RequestPolicy.REACHABLE_COMMIT); - return up; - }, null); - uri = testProtocol.register(ctx, server2); - - assertFalse(client.getObjectDatabase().has(blob.toObjectId())); - - try (Transport tn = testProtocol.open(uri, client, "server2")) { - tn.fetch(NullProgressMonitor.INSTANCE, - Collections.singletonList(new RefSpec(blob.name()))); - assertTrue(client.getObjectDatabase().has(blob.toObjectId())); - } - } - } - - @Test public void testFetchWithBlobNoneFilter() throws Exception { InMemoryRepository server2 = newRepo("server2"); try (TestRepository<InMemoryRepository> remote2 = new TestRepository<>( @@ -598,8 +501,11 @@ public class UploadPackTest { } @Test - public void testV2CapabilitiesAllowSidebandAll() throws Exception { - checkAdvertisedIfAllowed("uploadpack", "allowsidebandall", "sideband-all"); + public void testV2CapabilitiesAdvertiseSidebandAll() throws Exception { + server.getConfig().setBoolean("uploadpack", null, "allowsidebandall", + true); + checkAdvertisedIfAllowed("uploadpack", "advertisesidebandall", + "sideband-all"); checkUnadvertisedIfUnallowed("sideband-all"); } @@ -2112,12 +2018,12 @@ public class UploadPackTest { ByteArrayInputStream recvStream = uploadPackV2( "command=fetch\n", - PacketLineIn.DELIM, + PacketLineIn.delimiter(), "sideband-all\n", "want " + fooChild.toObjectId().getName() + "\n", "want " + barChild.toObjectId().getName() + "\n", "have " + fooParent.toObjectId().getName() + "\n", - PacketLineIn.END); + PacketLineIn.end()); PacketLineIn pckIn = new PacketLineIn(recvStream); assertThat(pckIn.readString(), is("\001acknowledgments")); @@ -2133,11 +2039,11 @@ public class UploadPackTest { server.getConfig().setBoolean("uploadpack", null, "allowsidebandall", true); ByteArrayInputStream recvStream = uploadPackV2("command=fetch\n", - PacketLineIn.DELIM, + PacketLineIn.delimiter(), "want " + commit.getName() + "\n", "sideband-all\n", "done\n", - PacketLineIn.END); + PacketLineIn.end()); PacketLineIn pckIn = new PacketLineIn(recvStream); String s; @@ -2174,18 +2080,18 @@ public class UploadPackTest { assertThat(protocolsSupported, hasItems("https")); if (!protocolsSupported.contains("https")) return null; - return new PackInfo("myhash", "myuri"); + return new PackInfo("myhash", "myuri", 100); } }); }, "command=fetch\n", - PacketLineIn.DELIM, + PacketLineIn.delimiter(), "want " + commit2.getName() + "\n", "sideband-all\n", "packfile-uris https\n", "done\n", - PacketLineIn.END); + PacketLineIn.end()); PacketLineIn pckIn = new PacketLineIn(recvStream); String s; diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/WalkEncryptionTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/WalkEncryptionTest.java index 4750d15b3d..d1d7a1d81d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/WalkEncryptionTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/WalkEncryptionTest.java @@ -239,9 +239,8 @@ public class WalkEncryptionTest { loadEnvVar(ENV_SECRET_KEY, SECRET_KEY, props); loadEnvVar(ENV_BUCKET_NAME, TEST_BUCKET, props); return props; - } else { - return null; } + return null; } static Properties fromEnvFile() throws Exception { @@ -250,12 +249,10 @@ public class WalkEncryptionTest { props.load(new FileInputStream(ENV_CONFIG_FILE)); if (checkTestProps(props)) { return props; - } else { - throw new Error("Environment config file is incomplete."); } - } else { - return null; + throw new Error("Environment config file is incomplete."); } + return null; } static Properties fromSysProps() { @@ -266,9 +263,8 @@ public class WalkEncryptionTest { loadSysProp(SYS_SECRET_KEY, SECRET_KEY, props); loadSysProp(SYS_BUCKET_NAME, TEST_BUCKET, props); return props; - } else { - return null; } + return null; } static Properties fromSysFile() throws Exception { @@ -277,12 +273,10 @@ public class WalkEncryptionTest { props.load(new FileInputStream(SYS_CONFIG_FILE)); if (checkTestProps(props)) { return props; - } else { - throw new Error("System props config file is incomplete."); } - } else { - return null; + throw new Error("System props config file is incomplete."); } + return null; } static Properties fromConfigFile(String path) throws Exception { @@ -292,12 +286,10 @@ public class WalkEncryptionTest { props.load(new FileInputStream(file)); if (checkTestProps(props)) { return props; - } else { - throw new Error("Props config file is incomplete: " + path); } - } else { - return null; + throw new Error("Props config file is incomplete: " + path); } + return null; } /** diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/EmptyTreeIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/EmptyTreeIteratorTest.java index 58aa608ff5..4deb188784 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/EmptyTreeIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/EmptyTreeIteratorTest.java @@ -65,12 +65,13 @@ public class EmptyTreeIteratorTest extends RepositoryTestCase { @Test public void testCreateSubtreeIterator() throws Exception { final EmptyTreeIterator etp = new EmptyTreeIterator(); - final ObjectReader reader = db.newObjectReader(); - final AbstractTreeIterator sub = etp.createSubtreeIterator(reader); - assertNotNull(sub); - assertTrue(sub.first()); - assertTrue(sub.eof()); - assertTrue(sub instanceof EmptyTreeIterator); + try (ObjectReader reader = db.newObjectReader()) { + final AbstractTreeIterator sub = etp.createSubtreeIterator(reader); + assertNotNull(sub); + assertTrue(sub.first()); + assertTrue(sub.eof()); + assertTrue(sub instanceof EmptyTreeIterator); + } } @Test @@ -121,8 +122,9 @@ public class EmptyTreeIteratorTest extends RepositoryTestCase { called[0] = true; } }; - final ObjectReader reader = db.newObjectReader(); - parent.createSubtreeIterator(reader).stopWalk(); + try (ObjectReader reader = db.newObjectReader()) { + parent.createSubtreeIterator(reader).stopWalk(); + } assertTrue(called[0]); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java index a3ce4ae94f..ffa6e5d252 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java @@ -217,29 +217,29 @@ public class FileTreeIteratorTest extends RepositoryTestCase { assertFalse(top.eof()); assertEquals(FileMode.TREE.getBits(), top.mode); - final ObjectReader reader = db.newObjectReader(); - final AbstractTreeIterator sub = top.createSubtreeIterator(reader); - assertTrue(sub instanceof FileTreeIterator); - final FileTreeIterator subfti = (FileTreeIterator) sub; - assertTrue(sub.first()); - assertFalse(sub.eof()); - assertEquals(paths[2], nameOf(sub)); - assertEquals(paths[2].length(), subfti.getEntryLength()); - assertEquals(mtime[2], subfti.getEntryLastModifiedInstant()); - - sub.next(1); - assertTrue(sub.eof()); - - top.next(1); - assertFalse(top.first()); - assertFalse(top.eof()); - assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode); - assertEquals(paths[3], nameOf(top)); - assertEquals(paths[3].length(), top.getEntryLength()); - assertEquals(mtime[3], top.getEntryLastModifiedInstant()); - - top.next(1); - assertTrue(top.eof()); + try (ObjectReader reader = db.newObjectReader()) { + final AbstractTreeIterator sub = top.createSubtreeIterator(reader); + assertTrue(sub instanceof FileTreeIterator); + final FileTreeIterator subfti = (FileTreeIterator) sub; + assertTrue(sub.first()); + assertFalse(sub.eof()); + assertEquals(paths[2], nameOf(sub)); + assertEquals(paths[2].length(), subfti.getEntryLength()); + assertEquals(mtime[2], subfti.getEntryLastModifiedInstant()); + + sub.next(1); + assertTrue(sub.eof()); + top.next(1); + assertFalse(top.first()); + assertFalse(top.eof()); + assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode); + assertEquals(paths[3], nameOf(top)); + assertEquals(paths[3].length(), top.getEntryLength()); + assertEquals(mtime[3], top.getEntryLastModifiedInstant()); + + top.next(1); + assertTrue(top.eof()); + } } @Test @@ -272,22 +272,23 @@ public class FileTreeIteratorTest extends RepositoryTestCase { git.add().addFilepattern("file").call(); } DirCacheEntry dce = db.readDirCache().getEntry("file"); - TreeWalk tw = new TreeWalk(db); - FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db - .getConfig().get(WorkingTreeOptions.KEY)); - tw.addTree(fti); - DirCacheIterator dci = new DirCacheIterator(db.readDirCache()); - tw.addTree(dci); - fti.setDirCacheIterator(tw, 1); - while (tw.next() && !tw.getPathString().equals("file")) { - // - } - assertEquals(MetadataDiff.EQUAL, fti.compareMetadata(dce)); - ObjectId fromRaw = ObjectId.fromRaw(fti.idBuffer(), fti.idOffset()); - assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea", - fromRaw.getName()); - try (ObjectReader objectReader = db.newObjectReader()) { - assertFalse(fti.isModified(dce, false, objectReader)); + try (TreeWalk tw = new TreeWalk(db)) { + FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), + db.getConfig().get(WorkingTreeOptions.KEY)); + tw.addTree(fti); + DirCacheIterator dci = new DirCacheIterator(db.readDirCache()); + tw.addTree(dci); + fti.setDirCacheIterator(tw, 1); + while (tw.next() && !tw.getPathString().equals("file")) { + // + } + assertEquals(MetadataDiff.EQUAL, fti.compareMetadata(dce)); + ObjectId fromRaw = ObjectId.fromRaw(fti.idBuffer(), fti.idOffset()); + assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea", + fromRaw.getName()); + try (ObjectReader objectReader = db.newObjectReader()) { + assertFalse(fti.isModified(dce, false, objectReader)); + } } } @@ -624,9 +625,8 @@ public class FileTreeIteratorTest extends RepositoryTestCase { @Test public void testCustomFileModeStrategy() throws Exception { - Repository nestedRepo = createNestedRepo(); - - try (Git git = new Git(nestedRepo)) { + try (Repository nestedRepo = createNestedRepo(); + Git git = new Git(nestedRepo)) { // validate that our custom strategy is honored WorkingTreeIterator customIterator = new FileTreeIterator( nestedRepo, NO_GITLINKS_STRATEGY); @@ -641,9 +641,8 @@ public class FileTreeIteratorTest extends RepositoryTestCase { @Test public void testCustomFileModeStrategyFromParentIterator() throws Exception { - Repository nestedRepo = createNestedRepo(); - - try (Git git = new Git(nestedRepo)) { + try (Repository nestedRepo = createNestedRepo(); + Git git = new Git(nestedRepo)) { FileTreeIterator customIterator = new FileTreeIterator(nestedRepo, NO_GITLINKS_STRATEGY); File r = new File(nestedRepo.getWorkTree(), "sub"); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/NameConflictTreeWalkTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/NameConflictTreeWalkTest.java index 54c217276d..e6a5322374 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/NameConflictTreeWalkTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/NameConflictTreeWalkTest.java @@ -84,16 +84,17 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { assertEquals(1, tree1.getEntryCount()); } - final TreeWalk tw = new TreeWalk(db); - tw.addTree(new DirCacheIterator(tree0)); - tw.addTree(new DirCacheIterator(tree1)); - - assertModes("a", REGULAR_FILE, MISSING, tw); - assertModes("a.b", EXECUTABLE_FILE, MISSING, tw); - assertModes("a", MISSING, TREE, tw); - tw.enterSubtree(); - assertModes("a/b", MISSING, REGULAR_FILE, tw); - assertModes("a0b", SYMLINK, MISSING, tw); + try (TreeWalk tw = new TreeWalk(db)) { + tw.addTree(new DirCacheIterator(tree0)); + tw.addTree(new DirCacheIterator(tree1)); + + assertModes("a", REGULAR_FILE, MISSING, tw); + assertModes("a.b", EXECUTABLE_FILE, MISSING, tw); + assertModes("a", MISSING, TREE, tw); + tw.enterSubtree(); + assertModes("a/b", MISSING, REGULAR_FILE, tw); + assertModes("a0b", SYMLINK, MISSING, tw); + } } @Test @@ -115,20 +116,21 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { assertEquals(1, tree1.getEntryCount()); } - final NameConflictTreeWalk tw = new NameConflictTreeWalk(db); - tw.addTree(new DirCacheIterator(tree0)); - tw.addTree(new DirCacheIterator(tree1)); - - assertModes("a", REGULAR_FILE, TREE, tw); - assertTrue(tw.isDirectoryFileConflict()); - assertTrue(tw.isSubtree()); - tw.enterSubtree(); - assertModes("a/b", MISSING, REGULAR_FILE, tw); - assertTrue(tw.isDirectoryFileConflict()); - assertModes("a.b", EXECUTABLE_FILE, MISSING, tw); - assertFalse(tw.isDirectoryFileConflict()); - assertModes("a0b", SYMLINK, MISSING, tw); - assertFalse(tw.isDirectoryFileConflict()); + try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) { + tw.addTree(new DirCacheIterator(tree0)); + tw.addTree(new DirCacheIterator(tree1)); + + assertModes("a", REGULAR_FILE, TREE, tw); + assertTrue(tw.isDirectoryFileConflict()); + assertTrue(tw.isSubtree()); + tw.enterSubtree(); + assertModes("a/b", MISSING, REGULAR_FILE, tw); + assertTrue(tw.isDirectoryFileConflict()); + assertModes("a.b", EXECUTABLE_FILE, MISSING, tw); + assertFalse(tw.isDirectoryFileConflict()); + assertModes("a0b", SYMLINK, MISSING, tw); + assertFalse(tw.isDirectoryFileConflict()); + } } @Test @@ -151,20 +153,21 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { assertEquals(2, tree1.getEntryCount()); } - final NameConflictTreeWalk tw = new NameConflictTreeWalk(db); - tw.addTree(new DirCacheIterator(tree0)); - tw.addTree(new DirCacheIterator(tree1)); - - assertModes("a", REGULAR_FILE, TREE, tw); - assertTrue(tw.isSubtree()); - assertTrue(tw.isDirectoryFileConflict()); - tw.enterSubtree(); - assertModes("a/b", MISSING, REGULAR_FILE, tw); - assertTrue(tw.isDirectoryFileConflict()); - assertModes("a.b", EXECUTABLE_FILE, EXECUTABLE_FILE, tw); - assertFalse(tw.isDirectoryFileConflict()); - assertModes("a0b", SYMLINK, MISSING, tw); - assertFalse(tw.isDirectoryFileConflict()); + try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) { + tw.addTree(new DirCacheIterator(tree0)); + tw.addTree(new DirCacheIterator(tree1)); + + assertModes("a", REGULAR_FILE, TREE, tw); + assertTrue(tw.isSubtree()); + assertTrue(tw.isDirectoryFileConflict()); + tw.enterSubtree(); + assertModes("a/b", MISSING, REGULAR_FILE, tw); + assertTrue(tw.isDirectoryFileConflict()); + assertModes("a.b", EXECUTABLE_FILE, EXECUTABLE_FILE, tw); + assertFalse(tw.isDirectoryFileConflict()); + assertModes("a0b", SYMLINK, MISSING, tw); + assertFalse(tw.isDirectoryFileConflict()); + } } @Test @@ -187,20 +190,21 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { assertEquals(3, tree1.getEntryCount()); } - final NameConflictTreeWalk tw = new NameConflictTreeWalk(db); - tw.addTree(new DirCacheIterator(tree0)); - tw.addTree(new DirCacheIterator(tree1)); - - assertModes("a", REGULAR_FILE, TREE, tw); - assertTrue(tw.isSubtree()); - assertTrue(tw.isDirectoryFileConflict()); - tw.enterSubtree(); - assertModes("a/b", MISSING, REGULAR_FILE, tw); - assertTrue(tw.isDirectoryFileConflict()); - assertModes("a.b", MISSING, EXECUTABLE_FILE, tw); - assertFalse(tw.isDirectoryFileConflict()); - assertModes("a0b", SYMLINK, SYMLINK, tw); - assertFalse(tw.isDirectoryFileConflict()); + try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) { + tw.addTree(new DirCacheIterator(tree0)); + tw.addTree(new DirCacheIterator(tree1)); + + assertModes("a", REGULAR_FILE, TREE, tw); + assertTrue(tw.isSubtree()); + assertTrue(tw.isDirectoryFileConflict()); + tw.enterSubtree(); + assertModes("a/b", MISSING, REGULAR_FILE, tw); + assertTrue(tw.isDirectoryFileConflict()); + assertModes("a.b", MISSING, EXECUTABLE_FILE, tw); + assertFalse(tw.isDirectoryFileConflict()); + assertModes("a0b", SYMLINK, SYMLINK, tw); + assertFalse(tw.isDirectoryFileConflict()); + } } @Test @@ -224,26 +228,27 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { assertEquals(4, tree1.getEntryCount()); } - final NameConflictTreeWalk tw = new NameConflictTreeWalk(db); - tw.addTree(new DirCacheIterator(tree0)); - tw.addTree(new DirCacheIterator(tree1)); - - assertModes("0", REGULAR_FILE, REGULAR_FILE, tw); - assertFalse(tw.isDirectoryFileConflict()); - assertModes("a", REGULAR_FILE, TREE, tw); - assertTrue(tw.isSubtree()); - assertTrue(tw.isDirectoryFileConflict()); - tw.enterSubtree(); - assertModes("a/b", MISSING, REGULAR_FILE, tw); - assertTrue(tw.isDirectoryFileConflict()); - assertModes("a/c", MISSING, TREE, tw); - assertTrue(tw.isDirectoryFileConflict()); - tw.enterSubtree(); - assertModes("a/c/e", MISSING, REGULAR_FILE, tw); - assertTrue(tw.isDirectoryFileConflict()); - - assertModes("a.b", MISSING, REGULAR_FILE, tw); - assertFalse(tw.isDirectoryFileConflict()); + try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) { + tw.addTree(new DirCacheIterator(tree0)); + tw.addTree(new DirCacheIterator(tree1)); + + assertModes("0", REGULAR_FILE, REGULAR_FILE, tw); + assertFalse(tw.isDirectoryFileConflict()); + assertModes("a", REGULAR_FILE, TREE, tw); + assertTrue(tw.isSubtree()); + assertTrue(tw.isDirectoryFileConflict()); + tw.enterSubtree(); + assertModes("a/b", MISSING, REGULAR_FILE, tw); + assertTrue(tw.isDirectoryFileConflict()); + assertModes("a/c", MISSING, TREE, tw); + assertTrue(tw.isDirectoryFileConflict()); + tw.enterSubtree(); + assertModes("a/c/e", MISSING, REGULAR_FILE, tw); + assertTrue(tw.isDirectoryFileConflict()); + + assertModes("a.b", MISSING, REGULAR_FILE, tw); + assertFalse(tw.isDirectoryFileConflict()); + } } private static void assertModes(final String path, final FileMode mode0, diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/IndexDiffFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/IndexDiffFilterTest.java index 2f797e361a..964ffcaa83 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/IndexDiffFilterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/IndexDiffFilterTest.java @@ -101,13 +101,14 @@ public class IndexDiffFilterTest extends RepositoryTestCase { RevCommit commit = writeFileInFolderAndCommit(); deleteAll(); writeFileWithFolderName(); - TreeWalk treeWalk = createTreeWalk(commit); - assertTrue(treeWalk.next()); - assertEquals("folder", treeWalk.getPathString()); - assertTrue(treeWalk.next()); - assertEquals("folder/file", treeWalk.getPathString()); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertTrue(treeWalk.next()); + assertEquals("folder", treeWalk.getPathString()); + assertTrue(treeWalk.next()); + assertEquals("folder/file", treeWalk.getPathString()); + assertFalse(treeWalk.next()); + } } @Test @@ -115,24 +116,26 @@ public class IndexDiffFilterTest extends RepositoryTestCase { RevCommit commit = writeFileInFolderAndCommit(); deleteAll(); writeFileWithFolderName(); - TreeWalk treeWalk = createNonRecursiveTreeWalk(commit); - - assertTrue(treeWalk.next()); - assertEquals("folder", treeWalk.getPathString()); - assertTrue(treeWalk.next()); - assertEquals("folder", treeWalk.getPathString()); - assertTrue(treeWalk.isSubtree()); - treeWalk.enterSubtree(); - assertTrue(treeWalk.next()); - assertEquals("folder/file", treeWalk.getPathString()); - assertFalse(treeWalk.next()); + + try (TreeWalk treeWalk = createNonRecursiveTreeWalk(commit)) { + assertTrue(treeWalk.next()); + assertEquals("folder", treeWalk.getPathString()); + assertTrue(treeWalk.next()); + assertEquals("folder", treeWalk.getPathString()); + assertTrue(treeWalk.isSubtree()); + treeWalk.enterSubtree(); + assertTrue(treeWalk.next()); + assertEquals("folder/file", treeWalk.getPathString()); + assertFalse(treeWalk.next()); + } } @Test public void testFileCommitted() throws Exception { RevCommit commit = writeFileAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test @@ -153,89 +156,100 @@ public class IndexDiffFilterTest extends RepositoryTestCase { "<<<<<<< HEAD\nside\n=======\nmaster\n>>>>>>> master\n"); writeTrashFile(FILE, "master"); - TreeWalk treeWalk = createTreeWalk(side); - int count = 0; - while (treeWalk.next()) - count++; - assertEquals(2, count); + try (TreeWalk treeWalk = createTreeWalk(side)) { + int count = 0; + while (treeWalk.next()) + count++; + assertEquals(2, count); + } } @Test public void testFileInFolderCommitted() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testEmptyFolderCommitted() throws Exception { RevCommit commit = createEmptyFolderAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testFileCommittedChangedNotModified() throws Exception { RevCommit commit = writeFileAndCommit(); writeFile(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testFileInFolderCommittedChangedNotModified() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); writeFileInFolder(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testFileCommittedModified() throws Exception { RevCommit commit = writeFileAndCommit(); writeFileModified(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE); + } } @Test public void testFileInFolderCommittedModified() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); writeFileInFolderModified(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test public void testFileCommittedDeleted() throws Exception { RevCommit commit = writeFileAndCommit(); deleteFile(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE); + } } @Test public void testFileInFolderCommittedDeleted() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); deleteFileInFolder(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test public void testFileInFolderCommittedAllDeleted() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); deleteAll(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test public void testEmptyFolderCommittedDeleted() throws Exception { RevCommit commit = createEmptyFolderAndCommit(); deleteFolder(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test @@ -243,8 +257,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { throws Exception { RevCommit commit = writeFileAndCommit(); writeFileModifiedAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE); + } } @Test @@ -252,8 +267,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { throws Exception { RevCommit commit = writeFileInFolderAndCommit(); writeFileInFolderModifiedAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test @@ -261,8 +277,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { throws Exception { RevCommit commit = writeFileAndCommit(); deleteFileAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE); + } } @Test @@ -270,8 +287,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { throws Exception { RevCommit commit = writeFileInFolderAndCommit(); deleteFileInFolderAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test @@ -279,8 +297,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { throws Exception { RevCommit commit = writeFileInFolderAndCommit(); deleteAllAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test @@ -288,96 +307,108 @@ public class IndexDiffFilterTest extends RepositoryTestCase { throws Exception { RevCommit commit = createEmptyFolderAndCommit(); deleteFolderAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testFileUntracked() throws Exception { RevCommit commit = writeFileAndCommit(); writeFileUntracked(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, UNTRACKED_FILE); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, UNTRACKED_FILE); + } } @Test public void testFileInFolderUntracked() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); writeFileInFolderUntracked(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, UNTRACKED_FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, UNTRACKED_FILE_IN_FOLDER); + } } @Test public void testEmptyFolderUntracked() throws Exception { RevCommit commit = createEmptyFolderAndCommit(); createEmptyFolderUntracked(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testFileIgnored() throws Exception { RevCommit commit = writeFileAndCommit(); writeFileIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testFileInFolderIgnored() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); writeFileInFolderIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testFileInFolderAllIgnored() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); writeFileInFolderAllIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testEmptyFolderIgnored() throws Exception { RevCommit commit = createEmptyFolderAndCommit(); createEmptyFolderIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testFileIgnoredNotHonored() throws Exception { RevCommit commit = writeFileAndCommit(); writeFileIgnored(); - TreeWalk treeWalk = createTreeWalkDishonorIgnores(commit); - assertPaths(treeWalk, IGNORED_FILE, GITIGNORE); + try (TreeWalk treeWalk = createTreeWalkDishonorIgnores(commit)) { + assertPaths(treeWalk, IGNORED_FILE, GITIGNORE); + } } @Test public void testFileCommittedModifiedIgnored() throws Exception { RevCommit commit = writeFileAndCommit(); writeFileModifiedIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE); + } } @Test public void testFileInFolderCommittedModifiedIgnored() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); writeFileInFolderModifiedIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test public void testFileInFolderCommittedModifiedAllIgnored() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); writeFileInFolderModifiedAllIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test @@ -386,8 +417,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { RevCommit commit = writeFileAndCommit(); deleteFileAndCommit(); rewriteFileIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE); + } } @Test @@ -396,8 +428,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { RevCommit commit = writeFileInFolderAndCommit(); deleteFileInFolderAndCommit(); rewriteFileInFolderIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test @@ -406,8 +439,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { RevCommit commit = writeFileInFolderAndCommit(); deleteAllAndCommit(); rewriteFileInFolderAllIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FILE_IN_FOLDER); + } } @Test @@ -416,15 +450,17 @@ public class IndexDiffFilterTest extends RepositoryTestCase { RevCommit commit = createEmptyFolderAndCommit(); deleteFolderAndCommit(); recreateEmptyFolderIgnored(); - TreeWalk treeWalk = createTreeWalk(commit); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertFalse(treeWalk.next()); + } } @Test public void testFileInFolderCommittedNonRecursive() throws Exception { RevCommit commit = writeFileInFolderAndCommit(); - TreeWalk treeWalk = createNonRecursiveTreeWalk(commit); - assertPaths(treeWalk, FOLDER); + try (TreeWalk treeWalk = createNonRecursiveTreeWalk(commit)) { + assertPaths(treeWalk, FOLDER); + } } @Test @@ -432,8 +468,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { RevCommit commit = writeFileInFolderAndCommit(); deleteAll(); writeFileWithFolderName(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FOLDER, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FOLDER, FILE_IN_FOLDER); + } } @Test @@ -442,8 +479,9 @@ public class IndexDiffFilterTest extends RepositoryTestCase { RevCommit commit = writeFileInFolderAndCommit(); deleteAll(); writeFileWithFolderNameAndCommit(); - TreeWalk treeWalk = createTreeWalk(commit); - assertPaths(treeWalk, FOLDER, FILE_IN_FOLDER); + try (TreeWalk treeWalk = createTreeWalk(commit)) { + assertPaths(treeWalk, FOLDER, FILE_IN_FOLDER); + } } private void writeFile() throws Exception { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HookTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HookTest.java index e5fcbf9d7c..70a2dbbfe8 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HookTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HookTest.java @@ -58,6 +58,8 @@ import org.eclipse.jgit.hooks.PostCommitHook; import org.eclipse.jgit.hooks.PreCommitHook; import org.eclipse.jgit.junit.JGitTestUtil; import org.eclipse.jgit.junit.RepositoryTestCase; +import org.eclipse.jgit.lib.ConfigConstants; +import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.revwalk.RevCommit; import org.junit.Assume; import org.junit.Test; @@ -221,6 +223,75 @@ public class HookTest extends RepositoryTestCase { } @Test + public void testRunHookHooksPathRelative() throws Exception { + assumeSupportedPlatform(); + + writeHookFile(PreCommitHook.NAME, + "#!/bin/sh\necho \"Wrong hook $1 $2\"\nread INPUT\necho $INPUT\n" + + "echo $GIT_DIR\necho $GIT_WORK_TREE\necho 1>&2 \"stderr\""); + writeHookFile("../../" + PreCommitHook.NAME, + "#!/bin/sh\necho \"test $1 $2\"\nread INPUT\necho $INPUT\n" + + "echo $GIT_DIR\necho $GIT_WORK_TREE\necho 1>&2 \"stderr\""); + StoredConfig cfg = db.getConfig(); + cfg.load(); + cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_HOOKS_PATH, "."); + cfg.save(); + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { + ProcessResult res = FS.DETECTED.runHookIfPresent(db, + PreCommitHook.NAME, new String[] { "arg1", "arg2" }, + new PrintStream(out), new PrintStream(err), "stdin"); + + assertEquals("unexpected hook output", + "test arg1 arg2\nstdin\n" + + db.getDirectory().getAbsolutePath() + '\n' + + db.getWorkTree().getAbsolutePath() + '\n', + out.toString("UTF-8")); + assertEquals("unexpected output on stderr stream", "stderr\n", + err.toString("UTF-8")); + assertEquals("unexpected exit code", 0, res.getExitCode()); + assertEquals("unexpected process status", ProcessResult.Status.OK, + res.getStatus()); + } + } + + @Test + public void testRunHookHooksPathAbsolute() throws Exception { + assumeSupportedPlatform(); + + writeHookFile(PreCommitHook.NAME, + "#!/bin/sh\necho \"Wrong hook $1 $2\"\nread INPUT\necho $INPUT\n" + + "echo $GIT_DIR\necho $GIT_WORK_TREE\necho 1>&2 \"stderr\""); + writeHookFile("../../" + PreCommitHook.NAME, + "#!/bin/sh\necho \"test $1 $2\"\nread INPUT\necho $INPUT\n" + + "echo $GIT_DIR\necho $GIT_WORK_TREE\necho 1>&2 \"stderr\""); + StoredConfig cfg = db.getConfig(); + cfg.load(); + cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_HOOKS_PATH, + db.getWorkTree().getAbsolutePath()); + cfg.save(); + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { + ProcessResult res = FS.DETECTED.runHookIfPresent(db, + PreCommitHook.NAME, new String[] { "arg1", "arg2" }, + new PrintStream(out), new PrintStream(err), "stdin"); + + assertEquals("unexpected hook output", + "test arg1 arg2\nstdin\n" + + db.getDirectory().getAbsolutePath() + '\n' + + db.getWorkTree().getAbsolutePath() + '\n', + out.toString("UTF-8")); + assertEquals("unexpected output on stderr stream", "stderr\n", + err.toString("UTF-8")); + assertEquals("unexpected exit code", 0, res.getExitCode()); + assertEquals("unexpected process status", ProcessResult.Status.OK, + res.getStatus()); + } + } + + @Test public void testFailedPreCommitHookBlockCommit() throws Exception { assumeSupportedPlatform(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java new file mode 100644 index 0000000000..cbe4eb2eb0 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2019, Thomas Wolf <thomas.wolf@paranor.ch> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.ProxySelector; +import java.net.SocketAddress; +import java.net.URI; +import java.net.URL; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; + +public class HttpSupportTest { + + private static class TestProxySelector extends ProxySelector { + + private static final Proxy DUMMY = new Proxy(Proxy.Type.HTTP, + InetSocketAddress.createUnresolved("localhost", 0)); + + @Override + public List<Proxy> select(URI uri) { + if ("http".equals(uri.getScheme()) + && "somehost".equals(uri.getHost())) { + return Collections.singletonList(DUMMY); + } + return Collections.singletonList(Proxy.NO_PROXY); + } + + @Override + public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { + // Empty + } + } + + @Test + public void testMalformedUri() throws Exception { + // Valid URL, but backslash is not allowed in a URI in the userinfo part + // per RFC 3986: https://tools.ietf.org/html/rfc3986#section-3.2.1 . + // Test that conversion to URI to call the ProxySelector does not throw + // an exception. + Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL( + "http://infor\\c.jones@somehost/somewhere/someproject.git")); + assertNotNull(proxy); + assertEquals(Proxy.Type.HTTP, proxy.type()); + } + + @Test + public void testCorrectUri() throws Exception { + // Backslash escaped as %5C is correct. + Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL( + "http://infor%5Cc.jones@somehost/somewhere/someproject.git")); + assertNotNull(proxy); + assertEquals(Proxy.Type.HTTP, proxy.type()); + } +} |