fine tune resource collection support

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@425273 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stefan Bodewig 2006-07-25 04:36:58 +00:00
parent 5b3d47a2df
commit 059a6cfc99
4 changed files with 103 additions and 32 deletions

View File

@ -1,3 +1,21 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
<meta http-equiv="Content-Language" content="en-us"></meta>
@ -44,19 +62,22 @@
<pre>
/*
* Copyright 2005 The Apache Software Foundation
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
@ -97,10 +118,10 @@ public class LogCapturer {
<h3>Parameters specified as nested elements</h3>
<h4>fileset - or a child type</h4>
<h4>any file system resource collection</h4>
<p>Specifies the build files to run as tests. At least one
fileset is required.</p>
resource is required.</p>
<h4>any implementation of AntUnitListener</h4>
@ -149,7 +170,7 @@ public class LogCapturer {
<pre>
&lt;au:antunit>
&lt;fileset dir="antunit" includes="base.xml"/>
&lt;file file="antunit/base.xml"/>
&lt;au:plainlistener/>
&lt;/au:antunit>
</pre>
@ -168,8 +189,5 @@ public class LogCapturer {
[au:antunit] Target: test2 took 0 sec
[au:antunit] Target: test1 took 0 sec
</pre>
<hr/>
<p align="center">Copyright &copy; 2005 The Apache Software Foundation. All rights Reserved.</p>
</body>
</html>

View File

@ -27,11 +27,30 @@ under the License.
<target name="antunit-basetest">
<au:antunit>
<fileset dir="antunit" includes="base.xml"/>
<file file="antunit/base.xml"/>
<au:plainlistener/>
</au:antunit>
</target>
<target name="noTests">
<au:antunit/>
</target>
<target name="nonFile">
<au:antunit>
<url url="http://ant.apache.org/"/>
</au:antunit>
</target>
<target name="nonExistingFile">
<au:antunit failOnError="false">
<filelist dir="antunit">
<file name="base.xml"/>
<file name="I don't exist.xml"/>
</filelist>
</au:antunit>
</target>
<target name="antunit-copy">
<au:antunit>
<fileset dir="antunit" includes="copy.xml"/>

View File

@ -36,6 +36,7 @@ import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.LogOutputStream;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.Union;
import org.apache.tools.ant.types.resources.FileResource;
/**
@ -65,7 +66,7 @@ public class AntUnit extends Task {
/**
* The build files to process.
*/
private ArrayList filesets = new ArrayList();
private Union buildFiles = new Union();
/**
* project instance for the build file currently under test.
@ -98,14 +99,20 @@ public class AntUnit extends Task {
/**
* Message if no tests have been specified.
*/
public static final String ERROR_NO_FILESET =
"You must specify at least one nested fileset.";
public static final String ERROR_NO_TESTS =
"You must specify build files to test.";
/**
* Message if non-File resources have been specified.
*/
public static final String ERROR_NON_FILES =
"Only file system resources are supported.";
/**
* adds build files to run as tests.
*/
public void add(ResourceCollection rc) {
filesets.add(rc);
buildFiles.add(rc);
}
/**
@ -123,13 +130,10 @@ public class AntUnit extends Task {
}
public void execute() {
if (filesets.size() == 0) {
throw new BuildException(ERROR_NO_FILESET);
}
Iterator iter = filesets.iterator();
while (iter.hasNext()) {
doFileSet((ResourceCollection) iter.next());
if (buildFiles.size() == 0) {
throw new BuildException(ERROR_NO_TESTS);
}
doResourceCollection(buildFiles);
if (failOnError && (failures > 0 || errors > 0)) {
throw new BuildException(ERROR_TESTS_FAILED
+ failures + " failure" + (failures != 1 ? "s" : "")
@ -139,12 +143,22 @@ public class AntUnit extends Task {
}
/**
* Processes a fileset.
* Processes a ResourceCollection.
*/
private void doFileSet(ResourceCollection rc) {
private void doResourceCollection(ResourceCollection rc) {
if (!rc.isFilesystemOnly()) {
throw new BuildException(ERROR_NON_FILES);
}
Iterator i = rc.iterator();
while(i.hasNext()) {
doFile(((FileResource)i.next()).getFile());
FileResource r = (FileResource) i.next();
if (r.isExists()) {
doFile(r.getFile());
} else {
log("Skipping " + r + " since it doesn't exist",
Project.MSG_VERBOSE);
}
}
}
@ -152,6 +166,8 @@ public class AntUnit extends Task {
* Processes a single build file.
*/
private void doFile(File f) {
log("Running tests in build file " + f, Project.MSG_DEBUG);
// setup project instance
newProject = new Project();
newProject.setDefaultInputStream(getProject().getDefaultInputStream());

View File

@ -62,4 +62,22 @@ public class AntUnitTest extends BuildFileTest {
&& log.indexOf("test4 fails", index2) > -1);
assertTrue("Only one failure", log.indexOf("FAILED", index2 + 1) == -1);
}
public void testNoTests() {
expectSpecificBuildException("noTests", "No tests have been specified",
AntUnit.ERROR_NO_TESTS);
}
public void testNonFile() {
expectSpecificBuildException("nonFile",
"URL has been specified",
AntUnit.ERROR_NON_FILES);
}
public void testNonExistingFile() {
executeTarget("nonExistingFile");
assertDebuglogContaining(java.io.File.separator
+ "I don't exist.xml since it doesn't exist");
}
}