From a20d0ddab1140ab84f5916ba8bdc69abd1679480 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Thu, 22 Jan 2004 18:02:57 +0000 Subject: [PATCH] new condition: typefound git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275950 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 6 + docs/manual/CoreTasks/conditions.html | 21 ++++ .../taskdefs/conditions/typefound.xml | 52 +++++++++ .../ant/taskdefs/condition/ConditionBase.java | 7 ++ .../ant/taskdefs/condition/TypeFound.java | 107 ++++++++++++++++++ .../ant/taskdefs/condition/TypeFoundTest.java | 95 ++++++++++++++++ 6 files changed, 288 insertions(+) create mode 100644 src/etc/testcases/taskdefs/conditions/typefound.xml create mode 100644 src/main/org/apache/tools/ant/taskdefs/condition/TypeFound.java create mode 100644 src/testcases/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java diff --git a/WHATSNEW b/WHATSNEW index 8bc442b51..89a68c818 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -55,6 +55,12 @@ Other changes: - Added three new ClearCase commands: ccmkattr, ccmkdir, ccmkelem Bugzilla Report 26253. +* New condition 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 =================================== diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index 40f25b9ca..65f58102a 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -326,6 +326,27 @@ that is "true","yes", or "on"

+

typefound

+ +

Test whether a given type is defined, and that +its implementation class can be loaded. Types include +tasks, datatypes, scriptdefs, macrodefs and presetdefs.

+ +

This condition has been added in Apache Ant 1.7.

+ + + + + + + + + + + + +
AttributeDescriptionRequired
namename of the typeYes
+

Copyright © 2001-2003 Apache Software Foundation. All rights Reserved.

diff --git a/src/etc/testcases/taskdefs/conditions/typefound.xml b/src/etc/testcases/taskdefs/conditions/typefound.xml new file mode 100644 index 000000000..ea90f325c --- /dev/null +++ b/src/etc/testcases/taskdefs/conditions/typefound.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @{text} + + + + + + + + \ No newline at end of file diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/ConditionBase.java b/src/main/org/apache/tools/ant/taskdefs/condition/ConditionBase.java index 2dc9c3d02..c1a873eaa 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/ConditionBase.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/ConditionBase.java @@ -252,6 +252,13 @@ public abstract class ConditionBase extends ProjectComponent { conditions.addElement(i); } + /** + * Add an <typefound> condition. + * @param test + */ + public void addTypeFound(TypeFound test) { + conditions.addElement(test); + } /** * Add an arbitrary condition * @param c a condition diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/TypeFound.java b/src/main/org/apache/tools/ant/taskdefs/condition/TypeFound.java new file mode 100644 index 000000000..9405063ec --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/condition/TypeFound.java @@ -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 + * . + */ + +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); + } +} diff --git a/src/testcases/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java b/src/testcases/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java new file mode 100644 index 000000000..89fbeb311 --- /dev/null +++ b/src/testcases/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java @@ -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 + * . + */ +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"); + } + + +}