From c222b1880861e80898befff17ee3cf0d5414521e Mon Sep 17 00:00:00 2001 From: Robin Stocker Date: Mon, 8 Jul 2013 10:48:45 +0200 Subject: [PATCH] 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 --- .../treewalk/filter/PathSuffixFilterTest.java | 92 +++++++------------ 1 file 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 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"); + List paths = getMatchingPaths(".txt", treeId); + List expected = Arrays.asList("a.txt"); - final TreeWalk tw = new TreeWalk(db); - tw.setFilter(PathSuffixFilter.create(".txt")); - tw.addTree(treeId); + assertEquals(expected, paths); + } - List paths = new LinkedList(); - 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 expected = new LinkedList(); - expected.add("a.txt"); + List paths = getMatchingPaths(".txt", treeId, true); + List 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 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.setRecursive(true); - tw.setFilter(PathSuffixFilter.create(".txt")); + tw.setFilter(PathSuffixFilter.create(suffixFilter)); + tw.setRecursive(recursiveWalk); tw.addTree(treeId); - List paths = new LinkedList(); - while (tw.next()) { + List paths = new ArrayList(); + while (tw.next()) paths.add(tw.getPathString()); - } - - List expected = new LinkedList(); - expected.add("a.txt"); - expected.add("sub/b.txt"); - - assertEquals(expected, paths); + return paths; } }