📄 testlistholder.java
字号:
/** * */package org.momeunit.ant.core;import java.util.Collections;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.DirectoryScanner;import org.apache.tools.ant.Project;import org.apache.tools.ant.types.FileSet;import org.apache.tools.ant.types.selectors.SelectorUtils;/** * Class that holds the list of test cases that satisfy class and pattern * requirements. Developers should use * {@link #addTestNames(FileSet, String, String, boolean)} to add test cases to * TestListHolder. List of test cases can be accessed by * {@link #getTestsList(char)} method. * * @author Sergio Morozov * @version 1.1.2 */public class TestListHolder{ /** * Suffix of java source files. * * @since 1.1 */ public static final String SOURCE_SUFFIX = ".java"; /** * Suffix of java class files. * * @since 1.1 */ public static final String CLASS_SUFFIX = ".class"; private Set testNames = null; private Project project = null; private ClassLoader loader = null; /** * Instantiates TestListHolder with given project and class loader. * * @param project * owning project. * @param loader * {@link ClassLoader} intended to load classes. * @since 1.1 */ public TestListHolder(Project project, ClassLoader loader) { super(); this.testNames = new HashSet(); setProject(project); setClassLoader(loader); } /** * Instantiates TestListHolder with given project. * * @param project * owning project. * @since 1.1 */ public TestListHolder(Project project) { this(project, null); } /** * Instantiates TestListHolder. * * @since 1.1 */ public TestListHolder() { this(null, null); } /** * Returns owning project. * * @return owning project. * @since 1.1 */ public Project getProject() { return this.project; } /** * Sets the owning project. * * @param project * owning project to set. * @since 1.1 */ public void setProject(Project project) { this.project = project; } /** * Adds given test case if it satisfies class and pattern requirements. * * @param name * Name of test to add. * @param suffix * suffix of file to add. * @param pattern * pattern that every test must satisfy. * @param onlyTests * whether to execute test case class check. * @since 1.1 */ private void addTestName(String name, String suffix, String pattern, boolean onlyTests) { int suffixLength = suffix != null ? suffix.length() : 0; if (suffixLength > 0 && name.endsWith(suffix) && !name.startsWith("momeunit/")) { name = name.substring(0, name.length() - suffixLength); if (SelectorUtils.matchPath(pattern.replace('.', '/'), name)) { name = name.replace('/', '.'); if (!onlyTests || loadClass("momeunit.framework.Test").isAssignableFrom( loadClass(name))) testNames.add(name); } } } /** * Adds test cases designated by given fileset that satisfy pattern and class * requirements. * * @param src * fileset that designates test cases to add. * @param suffix * suffix of files designated by <code>src</code>. * @param pattern * pattern that every test must satisfy. * @param onlyTests * whether to perform class check. * @since 1.1 */ public void addTestNames(FileSet src, String suffix, String pattern, boolean onlyTests) { DirectoryScanner scanner = src.getDirectoryScanner(this.project); scanner.scan(); String[] names = scanner.getIncludedFiles(); for (int i = names.length - 1; i >= 0; i--) addTestName(names[i], suffix, pattern, onlyTests); } /** * Returns set of test case names contained in this TestListHolder. * * @return set of test case names contained in this TestListHolder. * @since 1.1 */ public Set getTestNames() { return Collections.unmodifiableSet(this.testNames); } /** * Returns list of test case names contained in this TestListHolder separated * by given separator. * * @param separator * character to separate test case names. * @return list of test case names contained in this TestListHolder as string. * @since 1.1 */ public String getTestsList(char separator) { StringBuffer sb = new StringBuffer(); for (Iterator i = this.testNames.iterator(); i.hasNext();) sb.append((String) i.next()).append(separator); int length = sb.length(); if (length > 0) sb.deleteCharAt(length - 1); return sb.toString(); } /** * Returns number of test cases contained in this TestListHolder. * * @return number of test cases contained in this TestListHolder. * @since 1.1 */ public int size() { return this.testNames.size(); } private Class loadClass(String name) { Class res = null; try { if (this.loader != null) res = this.loader.loadClass(name); else res = Class.forName(name); } catch (ClassNotFoundException e1) {} if (res == null) throw new BuildException("Error loading class " + name); return res; } /** * Sets {@link ClassLoader} intended to load classes. * * @param loader * {@link ClassLoader} intended to load classes. * @since 1.1 */ public void setClassLoader(ClassLoader loader) { this.loader = loader; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -