Browse Source

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>
stable-3.1
Robin Stocker 11 years ago
parent
commit
12326ddead
  1. 92
      org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java

92
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. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
@ -43,11 +43,11 @@
package org.eclipse.jgit.treewalk.filter; package org.eclipse.jgit.treewalk.filter;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCache;
@ -64,84 +64,54 @@ public class PathSuffixFilterTest extends RepositoryTestCase {
@Test @Test
public void testNonRecursiveFiltering() throws IOException { public void testNonRecursiveFiltering() throws IOException {
final ObjectInserter odi = db.newObjectInserter(); ObjectId treeId = createTree("a.sth", "a.txt");
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();
List<String> paths = getMatchingPaths(".txt", treeId);
List<String> expected = Arrays.asList("a.txt");
final TreeWalk tw = new TreeWalk(db); assertEquals(expected, paths);
tw.setFilter(PathSuffixFilter.create(".txt"));
tw.addTree(treeId);
List<String> paths = new LinkedList<String>();
while (tw.next()) {
paths.add(tw.getPathString());
} }
List<String> expected = new LinkedList<String>(); @Test
expected.add("a.txt"); public void testRecursiveFiltering() throws IOException {
ObjectId treeId = createTree("a.sth", "a.txt", "sub/b.sth", "sub/b.txt");
List<String> paths = getMatchingPaths(".txt", treeId, true);
List<String> expected = Arrays.asList("a.txt", "sub/b.txt");
assertEquals(expected, paths); assertEquals(expected, paths);
} }
@Test private ObjectId createTree(String... paths) throws IOException {
public void testRecursiveFiltering() throws IOException {
final ObjectInserter odi = db.newObjectInserter(); 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 DirCache dc = db.readDirCache();
final DirCacheBuilder builder = dc.builder(); final DirCacheBuilder builder = dc.builder();
final DirCacheEntry aSthEntry = new DirCacheEntry("a.sth"); for (String path : paths) {
aSthEntry.setFileMode(FileMode.REGULAR_FILE); DirCacheEntry entry = createEntry(path, FileMode.REGULAR_FILE);
aSthEntry.setObjectId(aSth); builder.add(entry);
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(); builder.finish();
final ObjectId treeId = dc.writeTree(odi); final ObjectId treeId = dc.writeTree(odi);
odi.flush(); 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); final TreeWalk tw = new TreeWalk(db);
tw.setRecursive(true); tw.setFilter(PathSuffixFilter.create(suffixFilter));
tw.setFilter(PathSuffixFilter.create(".txt")); tw.setRecursive(recursiveWalk);
tw.addTree(treeId); tw.addTree(treeId);
List<String> paths = new LinkedList<String>(); List<String> paths = new ArrayList<String>();
while (tw.next()) { while (tw.next())
paths.add(tw.getPathString()); paths.add(tw.getPathString());
} return paths;
List<String> expected = new LinkedList<String>();
expected.add("a.txt");
expected.add("sub/b.txt");
assertEquals(expected, paths);
} }
} }

Loading…
Cancel
Save