git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275950 13f79535-47bb-0310-9956-ffa450edef68master
@@ -55,6 +55,12 @@ Other changes: | |||||
- Added three new ClearCase commands: ccmkattr, ccmkdir, ccmkelem | - Added three new ClearCase commands: ccmkattr, ccmkdir, ccmkelem | ||||
Bugzilla Report 26253. | Bugzilla Report 26253. | ||||
* New condition <typefound> that can be used to probe for the declaration | |||||
and implementation of a task, type, preset, macro, scriptdef, whatever. | |||||
As it tests for the implementation, it can be used to check for optional | |||||
tasks being available. | |||||
Changes from Ant 1.5.4 to Ant 1.6.0 | Changes from Ant 1.5.4 to Ant 1.6.0 | ||||
=================================== | =================================== | ||||
@@ -326,6 +326,27 @@ that is "true","yes", or "on"</p> | |||||
</tr> | </tr> | ||||
</table> | </table> | ||||
<h4>typefound</h4> | |||||
<p>Test whether a given type is defined, and that | |||||
its implementation class can be loaded. Types include | |||||
tasks, datatypes, scriptdefs, macrodefs and presetdefs.</p> | |||||
<p>This condition has been added in Apache Ant 1.7.</p> | |||||
<table border="1" cellpadding="2" cellspacing="0"> | |||||
<tr> | |||||
<td valign="top"><b>Attribute</b></td> | |||||
<td valign="top"><b>Description</b></td> | |||||
<td align="center" valign="top"><b>Required</b></td> | |||||
</tr> | |||||
<tr> | |||||
<td valign="top">name</td> | |||||
<td valign="top">name of the type</td> | |||||
<td valign="top" align="center">Yes</td> | |||||
</tr> | |||||
</table> | |||||
<hr> | <hr> | ||||
<p align="center">Copyright © 2001-2003 Apache Software | <p align="center">Copyright © 2001-2003 Apache Software | ||||
Foundation. All rights Reserved.</p> | Foundation. All rights Reserved.</p> | ||||
@@ -0,0 +1,52 @@ | |||||
<project name="typefound"> | |||||
<target name="testTask"> | |||||
<condition property="testTask"> | |||||
<typefound name="echo"/> | |||||
</condition> | |||||
</target> | |||||
<target name="testUndefined"> | |||||
<condition property="testUndefined"> | |||||
<typefound /> | |||||
</condition> | |||||
</target> | |||||
<target name="testTaskThatDoesntReallyExist"> | |||||
<!-- <taskdef name="invalid-task-name" | |||||
classname="org.example.invalid.task.name.hopefully"/> --> | |||||
<condition property="testTaskThatDoesntReallyExist"> | |||||
<typefound name="invalid-task-name"/> | |||||
</condition> | |||||
</target> | |||||
<target name="testType"> | |||||
<condition property="testType"> | |||||
<typefound name="path"/> | |||||
</condition> | |||||
</target> | |||||
<target name="testPreset"> | |||||
<presetdef name="important-echo"> | |||||
<echo level="error"/> | |||||
</presetdef> | |||||
<condition property="testPreset"> | |||||
<typefound name="important-echo"/> | |||||
</condition> | |||||
</target> | |||||
<target name="testMacro"> | |||||
<macrodef name="error-message"> | |||||
<element name="text" optional="false"/> | |||||
<sequential> | |||||
<echo level="error">@{text}</echo> | |||||
</sequential> | |||||
</macrodef> | |||||
<condition property="testMacro"> | |||||
<typefound name="error-message"/> | |||||
</condition> | |||||
</target> | |||||
</project> |
@@ -252,6 +252,13 @@ public abstract class ConditionBase extends ProjectComponent { | |||||
conditions.addElement(i); | conditions.addElement(i); | ||||
} | } | ||||
/** | |||||
* Add an <typefound> condition. | |||||
* @param test | |||||
*/ | |||||
public void addTypeFound(TypeFound test) { | |||||
conditions.addElement(test); | |||||
} | |||||
/** | /** | ||||
* Add an arbitrary condition | * Add an arbitrary condition | ||||
* @param c a condition | * @param c a condition | ||||
@@ -0,0 +1,107 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2004 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "Ant" and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs.condition; | |||||
import org.apache.tools.ant.BuildException; | |||||
import org.apache.tools.ant.ComponentHelper; | |||||
import org.apache.tools.ant.ProjectComponent; | |||||
import org.apache.tools.ant.AntTypeDefinition; | |||||
/** | |||||
* looks for a task or other Ant type that exists. Existence is defined as | |||||
* the type is defined, and its implementation class is present. This | |||||
* will work for datatypes and preset, script and macro definitions. | |||||
*/ | |||||
public class TypeFound extends ProjectComponent implements Condition { | |||||
private String name; | |||||
/** | |||||
* the task or other type to look for | |||||
* @param name | |||||
*/ | |||||
public void setName(String name) { | |||||
this.name = name; | |||||
} | |||||
/** | |||||
* test for a task or other ant type existing in the current project | |||||
* @param typename | |||||
* @return true if the typename exists | |||||
*/ | |||||
protected boolean doesTypeExist(String typename) { | |||||
ComponentHelper helper=ComponentHelper.getComponentHelper(getProject()); | |||||
AntTypeDefinition def=helper.getDefinition(typename); | |||||
if(def==null) { | |||||
return false; | |||||
} | |||||
//now verify that the class has an implementation | |||||
return def.getExposedClass(getProject())!=null; | |||||
} | |||||
/** | |||||
* Is this condition true? | |||||
* @return true if the condition is true | |||||
* @exception BuildException if an error occurs | |||||
*/ | |||||
public boolean eval() throws BuildException { | |||||
if(name==null) { | |||||
throw new BuildException("No type specified"); | |||||
} | |||||
return doesTypeExist(name); | |||||
} | |||||
} |
@@ -0,0 +1,95 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2004 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "Ant" and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs.condition; | |||||
import org.apache.tools.ant.BuildFileTest; | |||||
/** | |||||
* test the typeexists condition | |||||
*/ | |||||
public class TypeFoundTest extends BuildFileTest { | |||||
public TypeFoundTest(String name) { | |||||
super(name); | |||||
} | |||||
/** | |||||
* The JUnit setup method | |||||
*/ | |||||
public void setUp() { | |||||
configureProject("src/etc/testcases/taskdefs/conditions/typefound.xml"); | |||||
} | |||||
public void testTask() { | |||||
expectPropertySet("testTask", "testTask"); | |||||
} | |||||
public void testUndefined() { | |||||
expectBuildExceptionContaining("testUndefined","left out the name attribute", "No type specified"); | |||||
} | |||||
public void testTaskThatDoesntReallyExist() { | |||||
expectPropertyUnset("testTaskThatDoesntReallyExist", "testTaskThatDoesntReallyExist"); | |||||
} | |||||
public void testType() { | |||||
expectPropertySet("testType", "testType"); | |||||
} | |||||
public void testPreset() { | |||||
expectPropertySet("testPreset", "testPreset"); | |||||
} | |||||
public void testMacro() { | |||||
expectPropertySet("testMacro", "testMacro"); | |||||
} | |||||
} |