diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java index 6c3b62a62..3ec159198 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009, Google Inc. + * Copyright (C) 2009, 2013 Google Inc. * and other copyright owners as documented in the project's IP log. * * This program and the accompanying materials are made available @@ -43,11 +43,11 @@ package org.eclipse.jgit.treewalk.filter; -import static org.eclipse.jgit.lib.Constants.OBJ_BLOB; import static org.junit.Assert.assertEquals; import java.io.IOException; -import java.util.LinkedList; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.eclipse.jgit.dircache.DirCache; @@ -64,84 +64,54 @@ public class PathSuffixFilterTest extends RepositoryTestCase { @Test public void testNonRecursiveFiltering() throws IOException { - final ObjectInserter odi = db.newObjectInserter(); - final ObjectId aSth = odi.insert(OBJ_BLOB, "a.sth".getBytes()); - final ObjectId aTxt = odi.insert(OBJ_BLOB, "a.txt".getBytes()); - final DirCache dc = db.readDirCache(); - final DirCacheBuilder builder = dc.builder(); - final DirCacheEntry aSthEntry = new DirCacheEntry("a.sth"); - aSthEntry.setFileMode(FileMode.REGULAR_FILE); - aSthEntry.setObjectId(aSth); - final DirCacheEntry aTxtEntry = new DirCacheEntry("a.txt"); - aTxtEntry.setFileMode(FileMode.REGULAR_FILE); - aTxtEntry.setObjectId(aTxt); - builder.add(aSthEntry); - builder.add(aTxtEntry); - builder.finish(); - final ObjectId treeId = dc.writeTree(odi); - odi.flush(); + ObjectId treeId = createTree("a.sth", "a.txt"); - - final TreeWalk tw = new TreeWalk(db); - tw.setFilter(PathSuffixFilter.create(".txt")); - tw.addTree(treeId); - - List paths = new LinkedList(); - while (tw.next()) { - paths.add(tw.getPathString()); - } - - List expected = new LinkedList(); - expected.add("a.txt"); + List paths = getMatchingPaths(".txt", treeId); + List expected = Arrays.asList("a.txt"); assertEquals(expected, paths); } @Test public void testRecursiveFiltering() throws IOException { - final ObjectInserter odi = db.newObjectInserter(); - final ObjectId aSth = odi.insert(OBJ_BLOB, "a.sth".getBytes()); - final ObjectId aTxt = odi.insert(OBJ_BLOB, "a.txt".getBytes()); - final ObjectId bSth = odi.insert(OBJ_BLOB, "b.sth".getBytes()); - final ObjectId bTxt = odi.insert(OBJ_BLOB, "b.txt".getBytes()); - final DirCache dc = db.readDirCache(); - final DirCacheBuilder builder = dc.builder(); - final DirCacheEntry aSthEntry = new DirCacheEntry("a.sth"); - aSthEntry.setFileMode(FileMode.REGULAR_FILE); - aSthEntry.setObjectId(aSth); - final DirCacheEntry aTxtEntry = new DirCacheEntry("a.txt"); - aTxtEntry.setFileMode(FileMode.REGULAR_FILE); - aTxtEntry.setObjectId(aTxt); - builder.add(aSthEntry); - builder.add(aTxtEntry); - final DirCacheEntry bSthEntry = new DirCacheEntry("sub/b.sth"); - bSthEntry.setFileMode(FileMode.REGULAR_FILE); - bSthEntry.setObjectId(bSth); - final DirCacheEntry bTxtEntry = new DirCacheEntry("sub/b.txt"); - bTxtEntry.setFileMode(FileMode.REGULAR_FILE); - bTxtEntry.setObjectId(bTxt); - builder.add(bSthEntry); - builder.add(bTxtEntry); - builder.finish(); - final ObjectId treeId = dc.writeTree(odi); - odi.flush(); + ObjectId treeId = createTree("a.sth", "a.txt", "sub/b.sth", "sub/b.txt"); - - final TreeWalk tw = new TreeWalk(db); - tw.setRecursive(true); - tw.setFilter(PathSuffixFilter.create(".txt")); - tw.addTree(treeId); - - List paths = new LinkedList(); - while (tw.next()) { - paths.add(tw.getPathString()); - } - - List expected = new LinkedList(); - expected.add("a.txt"); - expected.add("sub/b.txt"); + List paths = getMatchingPaths(".txt", treeId, true); + List expected = Arrays.asList("a.txt", "sub/b.txt"); assertEquals(expected, paths); } + private ObjectId createTree(String... paths) throws IOException { + final ObjectInserter odi = db.newObjectInserter(); + final DirCache dc = db.readDirCache(); + final DirCacheBuilder builder = dc.builder(); + for (String path : paths) { + DirCacheEntry entry = createEntry(path, FileMode.REGULAR_FILE); + builder.add(entry); + } + builder.finish(); + final ObjectId treeId = dc.writeTree(odi); + odi.flush(); + return treeId; + } + + private List getMatchingPaths(String suffixFilter, + final ObjectId treeId) throws IOException { + return getMatchingPaths(suffixFilter, treeId, false); + } + + private List getMatchingPaths(String suffixFilter, + final ObjectId treeId, boolean recursiveWalk) throws IOException { + final TreeWalk tw = new TreeWalk(db); + tw.setFilter(PathSuffixFilter.create(suffixFilter)); + tw.setRecursive(recursiveWalk); + tw.addTree(treeId); + + List paths = new ArrayList(); + while (tw.next()) + paths.add(tw.getPathString()); + return paths; + } + }