fitnesse.testrunner.ClassPathBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fitnesse Show documentation
Show all versions of fitnesse Show documentation
The fully integrated standalone wiki, and acceptance testing framework.
// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.testrunner;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import fitnesse.components.TraversalListener;
import fitnesse.wiki.WikiPage;
import fitnesse.wiki.WikitextPage;
public class ClassPathBuilder {
public List getClassPath(WikiPage page) {
List paths = getInheritedPathElements(page);
return createClassPath(paths);
}
private List getInheritedPathElements(WikiPage page) {
final List items = new ArrayList<>();
page.getPageCrawler().traversePageAndAncestors(new TraversalListener() {
@Override
public void process(WikiPage p) {
items.addAll(getItemsFromPage(p));
}
});
return items;
}
public List createClassPath(List paths) {
paths = expandWildcards(paths);
Set addedPaths = new HashSet<>();
List classPath = new ArrayList<>();
for (String path : paths) {
if (!addedPaths.contains(path)) {
addedPaths.add(path);
classPath.add(path);
}
}
return classPath;
}
private List expandWildcards(List paths) {
List allPaths = new ArrayList<>();
for (String path : paths)
allPaths.addAll(expandWildcard(path));
return allPaths;
}
private List expandWildcard(String path) {
List allPaths = new ArrayList<>();
File file = new File(path);
File dir = new File(file.getAbsolutePath()).getParentFile();
if (isExpandableDoubleWildcard(path, dir))
allPaths.addAll(recursivelyAddMatchingFiles(path, dir));
else if (isExpandableSingleWildcard(path, dir))
allPaths.addAll(getMatchingFiles(path, dir));
else
allPaths.add(path);
return allPaths;
}
private List recursivelyAddMatchingFiles(String path, File dir) {
String singleWildcardPath = convertDoubleToSingleWildcard(path);
return getMatchingSubfiles(singleWildcardPath, dir);
}
private boolean isExpandableSingleWildcard(String path, File dir) {
return pathHasSingleWildcard(path) && dir.exists();
}
private boolean isExpandableDoubleWildcard(String path, File dir) {
return pathHasDoubleWildCard(path) && dir.exists();
}
private boolean pathHasSingleWildcard(String path) {
return path.indexOf('*') != -1;
}
private String convertDoubleToSingleWildcard(String path) {
path = path.replaceFirst("\\*\\*", "*");
return path;
}
private boolean pathHasDoubleWildCard(String path) {
return path.contains("**");
}
private List getMatchingFiles(String path, File dir) {
String fileName = new File(path).getName();
File[] files = dir.listFiles(new Wildcard(fileName));
List allPaths = new ArrayList<>();
for (File file : files) {
allPaths.add(file.getPath());
}
return allPaths;
}
private List getMatchingSubfiles(String path, File dir) {
List allPaths = new ArrayList<>();
allPaths.addAll(getMatchingFiles(path, dir));
for (File file : dir.listFiles()) {
if (file.isDirectory())
allPaths.addAll(getMatchingSubfiles(path, file));
}
return allPaths;
}
protected List getItemsFromPage(WikiPage page) {
if (page instanceof WikitextPage) {
List result = new ArrayList<>();
((WikitextPage) page).getSyntaxTree().findPaths(result::add);
return result;
}
return Collections.emptyList();
}
public static class Wildcard implements FilenameFilter {
private String pattern;
private String prefix;
private String suffix;
private int length;
public Wildcard(String pattern) {
int starIndex = pattern.indexOf("*");
if (starIndex > -1) {
prefix = pattern.substring(0, starIndex);
suffix = pattern.substring(starIndex + 1);
length = prefix.length() + suffix.length();
} else {
this.pattern = pattern;
}
}
@Override
public boolean accept(File dir, String name) {
if (pattern != null)
return pattern.equals(name);
boolean goodLength = name.length() >= length;
boolean goodPrefix = name.startsWith(prefix);
boolean goodSufix = name.endsWith(suffix);
return goodLength && goodPrefix && goodSufix;
}
}
}