From adbcbc791b854071f0d2ca60a85832a025982ef8 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Thu, 21 May 2015 16:15:06 -0700 Subject: [PATCH 1/2] Expose Sets helper to tests outside org.eclipse.jgit.api A later patch will make use of this class in a org.eclipse.jgit.lib test. Change-Id: I2b268e6a5dbf12174201f45259f9f007686708d2 Signed-off-by: Jonathan Nieder --- org.eclipse.jgit.test/pom.xml | 1 + .../eclipse/jgit/api => src/org/eclipse/jgit/lib}/Sets.java | 6 +++--- .../tst/org/eclipse/jgit/api/MergeCommandTest.java | 1 + .../tst/org/eclipse/jgit/api/StatusCommandTest.java | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) rename org.eclipse.jgit.test/{tst/org/eclipse/jgit/api => src/org/eclipse/jgit/lib}/Sets.java (95%) diff --git a/org.eclipse.jgit.test/pom.xml b/org.eclipse.jgit.test/pom.xml index 0e1419a16..da700939c 100644 --- a/org.eclipse.jgit.test/pom.xml +++ b/org.eclipse.jgit.test/pom.xml @@ -102,6 +102,7 @@ + src/ tst/ diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/Sets.java b/org.eclipse.jgit.test/src/org/eclipse/jgit/lib/Sets.java similarity index 95% rename from org.eclipse.jgit.test/tst/org/eclipse/jgit/api/Sets.java rename to org.eclipse.jgit.test/src/org/eclipse/jgit/lib/Sets.java index edfab551a..731685731 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/Sets.java +++ b/org.eclipse.jgit.test/src/org/eclipse/jgit/lib/Sets.java @@ -41,13 +41,13 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.eclipse.jgit.api; +package org.eclipse.jgit.lib; import java.util.HashSet; import java.util.Set; -class Sets { - static Set of(T... elements) { +public class Sets { + public static Set of(T... elements) { Set ret = new HashSet(); for (T element : elements) ret.add(element); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java index 761aafa3f..0c0c6e5b5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java @@ -62,6 +62,7 @@ import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryState; +import org.eclipse.jgit.lib.Sets; import org.eclipse.jgit.merge.MergeStrategy; import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason; import org.eclipse.jgit.revwalk.RevCommit; diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java index 47650232b..c70604ecb 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java @@ -53,6 +53,7 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.NoFilepatternException; import org.eclipse.jgit.errors.NoWorkTreeException; import org.eclipse.jgit.junit.RepositoryTestCase; +import org.eclipse.jgit.lib.Sets; import org.junit.Test; public class StatusCommandTest extends RepositoryTestCase { From 02418ede592c9c6fc22a957d237c2f217fdb582d Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Mon, 18 May 2015 18:28:31 -0700 Subject: [PATCH 2/2] Add tests for ObjectFilter Test that - the default ObjectFilter is ALL - ObjectFilter affects nextObject() and not next() - omitting a tree implies omitting its subtrees - a blob or tree reached by another path is still returned - ObjectFilter can be mixed with RevFilter Change-Id: I144a53fe677070fff8c3ddf8cba07a848773bc1b Signed-off-by: Jonathan Nieder --- .../jgit/revwalk/ObjectWalkFilterTest.java | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkFilterTest.java diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkFilterTest.java new file mode 100644 index 000000000..55117b78e --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkFilterTest.java @@ -0,0 +1,186 @@ +/* + * Copyright (C) 2015, Google Inc. + * 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.revwalk; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +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.AnyObjectId; +import org.eclipse.jgit.lib.Sets; +import org.eclipse.jgit.revwalk.ObjectWalk; +import org.eclipse.jgit.revwalk.filter.MessageRevFilter; +import org.eclipse.jgit.revwalk.filter.NotRevFilter; +import org.eclipse.jgit.revwalk.filter.ObjectFilter; + +import java.io.IOException; +import java.util.Set; + +public class ObjectWalkFilterTest { + private TestRepository tr; + private ObjectWalk rw; + + // 3 commits, 2 top-level trees, 4 subtrees, 3 blobs + private static final int OBJECT_COUNT = 12; + + @Before + public void setUp() throws Exception { + tr = new TestRepository<>(new InMemoryRepository( + new DfsRepositoryDescription("test"))); + rw = new ObjectWalk(tr.getRepository()); + + rw.markStart(tr.branch("master").commit() + .add("a/a", "1") + .add("b/b", "2") + .add("c/c", "3") + .message("initial commit") + + .child() + .rm("a/a") + .add("a/A", "1") + .message("capitalize a/a") + + .child() + .rm("a/A") + .add("a/a", "1") + .message("make a/A lowercase again") + .create()); + } + + @After + public void tearDown() { + rw.close(); + tr.getRepository().close(); + } + + private static class BlacklistObjectFilter extends ObjectFilter { + final Set badObjects; + + BlacklistObjectFilter(Set badObjects) { + this.badObjects = badObjects; + } + + @Override + public boolean include(ObjectWalk walker, AnyObjectId o) { + return !badObjects.contains(o); + } + } + + private AnyObjectId resolve(String revstr) throws Exception { + return tr.getRepository().resolve(revstr); + } + + private int countObjects() throws IOException { + int n = 0; + while (rw.next() != null) { + n++; + } + while (rw.nextObject() != null) { + n++; + } + return n; + } + + @Test + public void testDefaultFilter() throws Exception { + assertTrue("filter is ALL", + rw.getObjectFilter() == ObjectFilter.ALL); + assertEquals(OBJECT_COUNT, countObjects()); + } + + @Test + public void testObjectFilterCanFilterOutBlob() throws Exception { + AnyObjectId one = rw.parseAny(resolve("master:a/a")); + AnyObjectId two = rw.parseAny(resolve("master:b/b")); + rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two))); + + // 2 blobs filtered out + assertEquals(OBJECT_COUNT - 2, countObjects()); + } + + @Test + public void testFilteringCommitsHasNoEffect() throws Exception { + AnyObjectId initial = rw.parseCommit(resolve("master^^")); + rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(initial))); + assertEquals(OBJECT_COUNT, countObjects()); + } + + @Test + public void testRevFilterAndObjectFilterCanCombine() throws Exception { + AnyObjectId one = rw.parseAny(resolve("master:a/a")); + AnyObjectId two = rw.parseAny(resolve("master:b/b")); + rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two))); + rw.setRevFilter(NotRevFilter.create( + MessageRevFilter.create("capitalize"))); + + // 2 blobs, one commit, two trees filtered out + assertEquals(OBJECT_COUNT - 5, countObjects()); + } + + @Test + public void testFilteringTreeFiltersSubtrees() throws Exception { + AnyObjectId capitalizeTree = rw.parseAny(resolve("master^:")); + rw.setObjectFilter(new BlacklistObjectFilter( + Sets.of(capitalizeTree))); + + // trees "master^:" and "master^:a" filtered out + assertEquals(OBJECT_COUNT - 2, countObjects()); + } + + @Test + public void testFilteringTreeFiltersReferencedBlobs() throws Exception { + AnyObjectId a1 = rw.parseAny(resolve("master:a")); + AnyObjectId a2 = rw.parseAny(resolve("master^:a")); + rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(a1, a2))); + + // 2 trees, one blob filtered out + assertEquals(OBJECT_COUNT - 3, countObjects()); + } +}