testlisttask.java
来自「MoMEUnit是一个单元测试的J2ME的应用程序xUnit架构实例。这是来自J」· Java 代码 · 共 757 行 · 第 1/2 页
JAVA
757 行
private void initAppDir() { File dir = null; for (Random random = new Random(); dir == null || dir.exists(); dir = new File( getTempDir(), MoMEUnitTask.MOMEUNIT_TMPDIR_PREFIX + random.nextInt(MoMEUnitTask.MOMEUNIT_TMPDIR_RND_MAX))); if (!dir.mkdir()) throw new BuildException("Error creating directory " + this.appDir.getAbsolutePath()); this.filesToDelete.add(dir); this.appDir = dir; log("appDir set to " + this.appDir.getAbsolutePath(), Project.MSG_VERBOSE); } /** * Marks directory and its content to be delete on exit. * * @param dir * directory to be deleted. */ private void removeDirTree(File dir) { this.filesToDelete.add(dir); File[] children = dir.listFiles(); if (children != null) for (int i = children.length - 1; i >= 0; i--) if (children[i].isDirectory()) removeDirTree(children[i]); else this.filesToDelete.add(children[i]); } /** * Adds wtk midp, cldc libs and appjar (if specified) to classpath. */ private void addDefaultClasspath() { File wtkLibDir = new File(getWtkHome(), "lib"); getClasspath().setLocation( new File(wtkLibDir, "cldcapi" + this.config.replaceAll("\\.", "") + ".jar")); getClasspath().setLocation( new File(wtkLibDir, "midpapi" + this.profile.replaceAll("\\.", "") + ".jar")); getClasspath().setLocation(getMULib()); if (this.appJar != null) getClasspath().setLocation(this.appJar); log("default classpath added: " + getClasspath(), Project.MSG_VERBOSE); } /** * Adds path to classpath. * * @param classpath * path to add to classpath. * @since 1.1 */ public void setClassPath(Path classpath) { this.cpDelegate.setClasspath(classpath); } /** * Returns created classpsth, * * @return the created classpsth as {@link Path}. * @since 1.1 */ public Path createClassPath() { return this.cpDelegate.createClasspath(); } /** * Supplies testListHolder with given tests. */ private void calculateTestList() { this.testListHolder = new TestListHolder(getProject(), this.loader); if (this.appJar != null) this.testListHolder.addTestNames( createFileSet(this.appJar), TestListHolder.CLASS_SUFFIX, getPattern(this.pattern), this.onlyTests); else for (Iterator i = this.tests.iterator(); i.hasNext();) { TestElement test = (TestElement) i.next(); if (test.shouldUse(this)) this.testListHolder.addTestNames(test .getFileSet(), test.containsClasses() ? TestListHolder.CLASS_SUFFIX : TestListHolder.SOURCE_SUFFIX, getPattern(test.getPattern()), test .isOnlyTests()); } log("Test list calculated: " + this.testListHolder.getTestsList(','), Project.MSG_VERBOSE); } private void setCPClassLoader() { if (this.loader == null) { this.loader = (AntClassLoader) this.cpDelegate.getClassLoader(); ClassLoader myLoader = getClass().getClassLoader(); if (myLoader != Project.class.getClassLoader()) this.loader .setParent(myLoader); this.loader.addLoaderPackageRoot("mome."); this.loader.addLoaderPackageRoot("momeunit."); this.loader.setParent(getClass().getClassLoader());; this.loader.setParentFirst(false); this.loader.addJavaLibraries(); this.loader.addSystemPackageRoot("org.momeunit"); log("Using CLASSPATH " + this.loader.getClasspath(), Project.MSG_VERBOSE); } } /** * Creates compiler. * * @return the created compiler. * @since 1.1 */ public Compiler createCompiler() { if (this.compiler != null) throw new BuildException( "only one <compiler> nested element of <" + getTaskType() + "> can be specified."); this.compiler = new Compiler(getProject(), getTaskName()); return this.compiler; } /** * Returns a compiler. * * @return a compiler. */ private Compiler getCompiler() { if (this.compiler == null) this.compiler = createCompiler(); return this.compiler; } /** * Sets default pattern to use. * * @param pattern * the default pattern to use. * @since 1.1 */ public void setDefPattern(String pattern) { Utility.assertNotEmpty(pattern, "defpattern", getTaskType()); this.defaultPattern = pattern; } /** * Sets pattern to refine list of test cases of implicit test. The format of * this pattern resembles Ant path pattern. Instead of slash ('/'), dot ('.') * is used. It looks like ant package pattern. e.g. ( "**.*Test" matches any * file that ends with Test at any package, "test.**.*" matches any file below * test package, "**.*" matches all files. The default value is specified by * defpattern attribute. * * @param pattern * the pattern to set. * @since 1.1 */ public void setPattern(String pattern) { Utility.assertNotEmpty(pattern, "pattern", getTaskType()); this.pattern = pattern; } /** * Sets root directory of files to be scanned for test cases. All files below * this directory will be added to J2ME application. * * @param testDir * directory of files to be scanned for test cases. * @since 1.1 */ public void setTestDir(File testDir) { Utility.assertDirectory(testDir, "testdir"); if (this.implicitTest != null && this.implicitTest instanceof ZipFileSet) throw new BuildException( "only one of testdir or testjar attributes of <" + getTaskType() + "> can be set."); if (this.implicitTest == null) this.implicitTest = createTestDir(); ((FileSet) this.implicitTest).setDir(testDir); } /** * Sets jar file to be scanned for test cases. All files in this file will be * included in J2ME application. * * @param testJar * jar file to be scanned for test cases * @since 1.1 */ public void setTestJar(File testJar) { Utility.assertFile(testJar, "testjar"); if (this.implicitTest != null && !(this.implicitTest instanceof ZipFileSet)) throw new BuildException( "only one of testdir or testjar of attributes <" + getTaskType() + "> can be set."); if (this.implicitTest == null) this.implicitTest = createTestJar(); ((ZipFileSet) this.implicitTest).setSrc(testJar); } /** * Sets type of files specified by testdir attribute. * * @param testType * type of files specified by testdir attribute * @since 1.1 */ public void setTestType(String testType) { if (this.implicitTest != null && this.implicitTest instanceof ZipFileSet) throw new BuildException( "testtype can only be set with testdir."); if (this.implicitTest == null) this.implicitTest = createTestDir(); ((TestDir) this.implicitTest).setType(testType); } /** * Sets jar file of J2ME application to scan for test cases * * @param appJar * the appJar file to set. * @since 1.1 */ public void setAppJar(File appJar) { Utility.assertFile(appJar, "appjar"); this.appJar = appJar; } /** * Returns given pattern or * <code>defaultpattern<code> if given is <code>null</code>. * @param pattern pattern to test for nullability. * @return the pattern to use, either given or default. * @since 1.1 */ public String getPattern(String pattern) { return pattern != null ? pattern : this.defaultPattern; } /** * Returns fileset that designates momeunit library. * * @return fileset that designates momeunit library. */ private File getMULib() { if (this.MULib == null) { this.MULib = LoaderUtils.getClassSource(Test.class); log("momeunit library jar found : " + this.MULib.getAbsolutePath(), Project.MSG_VERBOSE); } if (this.MULib == null) throw new BuildException( "Sorry! Can't find momeunit-ant.jar library."); return this.MULib; } /** * Creates <code>testdir</code> tag. * * @return created <code>testdir</code> tag. * @since 1.1 */ public TestDir createTestDir() { TestDir res = new TestDir(); res.setProject(getProject()); this.tests.add(res); return res; } /** * Creates <code>testjar</code> tag. * * @return created <code>testjar</code> tag. * @since 1.1 */ public TestJar createTestJar() { TestJar res = new TestJar(); res.setProject(getProject()); this.tests.add(res); return res; } /** * Sets flag that indicates whether to include only classes from implicit test * that implement {@link Test} interface. * * @param onlyTests * the onlyTests to set. * @since 1.1 */ public void setOnlyTests(boolean onlyTests) { this.onlyTests = onlyTests; } /** * Sets name of property to set to list of tests. * * @param property * name of property to set to list of tests. * @since 1.1 */ public void setProperty(String property) { Utility.assertNotEmpty(property, "property", getTaskType()); this.property = property; } /** * Sets separator used to separate tests in the list. * * @param separator * character used to separate tests in the list. * @since 1.1 */ public void setSeparator(char separator) { this.separator = separator; } /** * Sets if unspecified and returns wtk home directory. * * @return wtk home directory. */ private File getWtkHome() { if (this.wtkHome == null) { String wtkHome = getProject().getProperty(MoMEUnitTask.WTK_HOME_PROPERTY); if (wtkHome != null) this.setWtkHome(new File(wtkHome)); } if (this.wtkHome == null) { String wtkHome = System.getenv(MoMEUnitTask.WTK_HOME_ENV); if (wtkHome != null) this.setWtkHome(new File(wtkHome)); } if (this.wtkHome == null) throw new BuildException( "Please, specify \"wtk.home\" dir either as wtkhome attribute of <" + getTaskType() + "> or " + MoMEUnitTask.WTK_HOME_PROPERTY + " property or " + MoMEUnitTask.WTK_HOME_ENV + " environment variable."); return this.wtkHome; } /** * Sets CLDC version to use. * * @param cldc * CLDC version to use. * @since 1.1 */ public void setConfig(String cldc) { Utility.assertNotEmpty(cldc, "config", getTaskType()); this.config = cldc; } /** * Sets MIDP version to use. * * @param midp * MIDP version to use. * @since 1.1 */ public void setProfile(String midp) { Utility.assertNotEmpty(midp, "profile", getTaskType()); this.profile = midp; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?