diff options
author | Robin Stocker <robin@nibor.org> | 2013-07-08 10:48:45 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2013-08-21 14:44:47 +0200 |
commit | c222b1880861e80898befff17ee3cf0d5414521e (patch) | |
tree | 6751c345872389ecdc09fe344d44e47b2e81349b | |
parent | 781b0b5735f8be2db2a2d4072a17ed7fdeeab36e (diff) | |
download | jgit-c222b1880861e80898befff17ee3cf0d5414521e.tar.gz jgit-c222b1880861e80898befff17ee3cf0d5414521e.zip |
Refactor PathSuffixFilterTest to remove duplication
Makes it possible to add new test cases without copying lots of lines.
Change-Id: I66db3bc0cbd18fb5a07748905c60384b86b1c162
Signed-off-by: Robin Stocker <robin@nibor.org>
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java | 92 |
1 files changed, 31 insertions, 61 deletions
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 6c3b62a62b..3ec159198a 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"); + List<String> paths = getMatchingPaths(".txt", treeId); + List<String> expected = Arrays.asList("a.txt"); - final TreeWalk tw = new TreeWalk(db); - tw.setFilter(PathSuffixFilter.create(".txt")); - tw.addTree(treeId); + assertEquals(expected, paths); + } - List<String> paths = new LinkedList<String>(); - while (tw.next()) { - paths.add(tw.getPathString()); - } + @Test + public void testRecursiveFiltering() throws IOException { + ObjectId treeId = createTree("a.sth", "a.txt", "sub/b.sth", "sub/b.txt"); - List<String> expected = new LinkedList<String>(); - expected.add("a.txt"); + List<String> paths = getMatchingPaths(".txt", treeId, true); + List<String> expected = Arrays.asList("a.txt", "sub/b.txt"); assertEquals(expected, paths); } - @Test - public void testRecursiveFiltering() throws IOException { + private ObjectId createTree(String... paths) 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); + 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<String> getMatchingPaths(String suffixFilter, + final ObjectId treeId) throws IOException { + return getMatchingPaths(suffixFilter, treeId, false); + } + private List<String> getMatchingPaths(String suffixFilter, + final ObjectId treeId, boolean recursiveWalk) throws IOException { final TreeWalk tw = new TreeWalk(db); - tw.setRecursive(true); - tw.setFilter(PathSuffixFilter.create(".txt")); + tw.setFilter(PathSuffixFilter.create(suffixFilter)); + tw.setRecursive(recursiveWalk); tw.addTree(treeId); - List<String> paths = new LinkedList<String>(); - while (tw.next()) { + List<String> paths = new ArrayList<String>(); + while (tw.next()) paths.add(tw.getPathString()); - } - - List<String> expected = new LinkedList<String>(); - expected.add("a.txt"); - expected.add("sub/b.txt"); - - assertEquals(expected, paths); + return paths; } } |