testlisttask.java
来自「MoMEUnit是一个单元测试的J2ME的应用程序xUnit架构实例。这是来自J」· Java 代码 · 共 757 行 · 第 1/2 页
JAVA
757 行
package org.momeunit.ant.taskdefs;import java.io.File;import java.util.HashSet;import java.util.Iterator;import java.util.Random;import java.util.Set;import java.util.Vector;import momeunit.framework.Test;import org.apache.tools.ant.AntClassLoader;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;import org.apache.tools.ant.types.FileSet;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.ZipFileSet;import org.apache.tools.ant.util.ClasspathUtils;import org.apache.tools.ant.util.LoaderUtils;import org.momeunit.ant.core.TestListHolder;import org.momeunit.ant.core.Utility;/** * TestList task intended to set the specified property to the list of test * cases to run. List of test cases is created based on given J2ME application * or classes and/or java source files specified via test descriptions. Test * cases included are classes and/or java source files that implement * {@link Test} interface and/or match specified pattern. * </p> * <p> * Tests can be described by using <code><testdir></code>, * <code><tesjar></code> nested tags. <code><testlist></code> * task is an implicit test and can be described using <code>testdir</code>, * <code>testtype</code> and <code>testjar</code> attributes. * </p> * * @author Sergio Morozov * @version 1.1.2 */public class TestListTask extends Task{ /** * Default tests names separator. * * @since 1.1 */ public static final char DEFAULT_TESTS_SEPARATOR = ','; /** * Default cldc version. * * @since 1.1 */ public static final String DEFAULT_CONFIGURATION = "1.1"; /** * Default midp version. * * @since 1.1 */ public static final String DEFAULT_PROFILE = "2.0"; /** * Name of property to set. * * @since 1.1 */ private String property = null; /** * Test list separator. */ private char separator = DEFAULT_TESTS_SEPARATOR; /** * cldc version. */ private String config = DEFAULT_CONFIGURATION; /** * midp version. */ private String profile = DEFAULT_PROFILE; /** * Temp dir. */ private File tmpDir = null; /** * Wtk home dir. */ private File wtkHome = null; /** * List of files to delelte. Made because of invalid implementation of * {@link File#deleteOnExit()} of some jvms (kaffe). */ private Vector filesToDelete = new Vector(); // <File> /** * Pattern of implicit test and appjar. */ private String pattern = null; /** * Default pattern for implicit ,given tests and appjar. */ private String defaultPattern = TestElement.DEFAULT_PATTERN; /** * classpath delegate. */ private ClasspathUtils.Delegate cpDelegate = null; private AntClassLoader loader = null; /** * Test list holder. */ private TestListHolder testListHolder = null; /** * Flag that indicates whether tests must implement {@link Test} interfave. */ private boolean onlyTests = true; /** * Set of specified tests. */ private Set tests = null; /** * Implicit test holder. */ private TestElement implicitTest = null; /** * Compiler. */ private Compiler compiler = null; /** * App jad. */ // private JAD jad = null; /** * file of app to test. */ private File appJar = null; /** * FileSet of momeuint-ant lib. */ private File MULib = null; /** * Directory under what to compile java source files if necessary. */ private File appDir = null; /** * Creates testlist task. * * @since 1.1 */ public TestListTask() { super(); this.tests = new HashSet(); this.cpDelegate = ClasspathUtils.getDelegate(this); } /** * Returns configured classpath. Classpath is configured by classpath, libdir, * libjar, testdir, testjar nested tags and/or classpath, classpathref, * testdir, testjar, appjar attributes. <strong>Note:</strong> Wtk midp and * cldc libraries are added by default. * * @return configured classpath */ private Path getClasspath() { Path res = this.cpDelegate.getClasspath(); return res != null ? res : this.cpDelegate.createClasspath(); } /** * Executes task. * * @see org.apache.tools.ant.Task#execute() * @since 1.1 */ public void execute() throws BuildException { try { Utility.assertRequiredAttribute(this.property, "property", getTaskType()); log(getTaskType() + " task started", Project.MSG_VERBOSE); addDefaultClasspath(); initAppDir(); if (this.appJar == null) { addImplicitTest(); addTestsToClasspath(); compile(); } else if (this.implicitTest != null || this.tests.size() > 0) log( "As appjar attribute is set <testdir>,<testjar> nested tags and " + "testdir, testjar, testtype attributes of <" + getTaskType() + "> are ignored.", Project.MSG_WARN); setCPClassLoader(); calculateTestList(); getProject().setProperty(this.property, this.testListHolder.getTestsList(this.separator)); } finally { for (int i = this.filesToDelete.size() - 1; i >= 0; i--) ((File) this.filesToDelete.elementAt(i)).delete(); } } /** * Adds collected tests to libs and classpath. */ private void addTestsToClasspath() { for (Iterator i = this.tests.iterator(); i.hasNext();) { TestElement test = (TestElement) i.next(); if (test.shouldUse(this) && (test.containsClasses())) { FileSet fs = test.getFileSet(); File pathElement = null; if (fs instanceof ZipFileSet) pathElement = ((ZipFileSet) fs) .getSrc(getProject()); else pathElement = fs.getDir(getProject()); getClasspath().setLocation(pathElement); log("tests under " + pathElement.getAbsolutePath() + " added to classpath", Project.MSG_VERBOSE); } } } /** * Adds implicit test if specified to set of tests. */ private void addImplicitTest() { if (this.implicitTest != null) { if (this.pattern != null) this.implicitTest.setPattern(this.pattern); this.implicitTest.setOnlyTests(this.onlyTests); this.tests.add(this.implicitTest); log("Implicit test added.", Project.MSG_VERBOSE); } } /** * Compiles source java classes if there are some specified. */ private void compile() { Set srcs = new HashSet(); for (Iterator i = this.tests.iterator(); i.hasNext();) { TestElement test = (TestElement) i.next(); if (test.shouldUse(this) && !test.containsClasses()) srcs.add(test .getFileSet()); } if (srcs.size() > 0) { Compiler compiler = getCompiler(); if (compiler.getDestdir() == null) { log("compiler destination directory not specified.", Project.MSG_VERBOSE); File binDir = new File(this.appDir, "bin"); if (!binDir.exists() && !binDir.mkdir()) throw new BuildException( "Error creating directory " + binDir.getAbsolutePath()); compiler.setDestdir(binDir); log( "compiler destination directory set to " + binDir.getAbsolutePath(), Project.MSG_VERBOSE); if (!compiler.wasDelOnExitSet()) compiler.setDelOnExit(true); } else if (!compiler.wasDelOnExitSet()) compiler.setDelOnExit(false); compiler.init(srcs, getClasspath()); compiler.execute(); getClasspath().setLocation(compiler.getDestdir()); log("Compiled classes directory " + compiler.getDestdir().getAbsolutePath() + " added to classpath and libs.", Project.MSG_VERBOSE); if (compiler.isDelOnExit()) removeDirTree(compiler.getDestdir()); } } /** * Create FilSet or ZipFileSet (if file is not a directory) with file as root. * * @param file * root of created fileset. * @return fileset created. */ private FileSet createFileSet(File file) { Utility.assertExists(file, ""); FileSet res = null; if (file.isDirectory()) { res = new FileSet(); res.setDir(file); } else { ZipFileSet zfs = new ZipFileSet(); zfs.setSrc(file); res = zfs; } res.setProject(getProject()); return res; } /** * Sets temporary directory. * * @param dir * temporary directory. * @since 1.1 */ public void setTmpDir(File dir) { Utility.assertDirectory(dir, "tmpdir"); this.tmpDir = dir; } /** * Sets WTK home directory. * * @param dir * WTK home directory. * @since 1.1 */ public void setWtkHome(File dir) { Utility.assertDirectory(dir, "wtkhome"); this.wtkHome = dir; } /** * Sets if unspecified and returns temp directory. * * @return temp directory. */ private File getTempDir() { if (this.tmpDir == null) { String tmpdir = getProject().getProperty(MoMEUnitTask.TEMPDIR_PROPERTY); if (tmpdir != null) this.setTmpDir(new File(tmpdir)); } if (this.tmpDir == null) { String tmpdir = System.getProperty(MoMEUnitTask.TEMPDIR_JAVA_SYSPROPERTY); if (tmpdir != null) this.setTmpDir(new File(tmpdir)); } if (this.tmpDir == null) throw new BuildException( "Can't find " + MoMEUnitTask.TEMPDIR_JAVA_SYSPROPERTY + " java system property.\nPlease, specify temporary dir either as \"tempdir\" attribute of <" + getTaskType() + "> or " + MoMEUnitTask.TEMPDIR_PROPERTY + " ant property."); return this.tmpDir; } /** * Sets appDir to random directory under temp directory, if appDir was not * specified. Initializes delonexit to <code>false</code> if appDir was * specified. */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?