* Split <is-set> into <is-set> and <is-true> conditions. <is-true> uses the converter to determine whether something is 'true', so is a little fussy. * Moved <uptodate> and <equals> into antlib, and made <uptodate> a condition. * Added <type-available> condition, which checks whether a particular type is defined. * Fixed <not> to actually work. * Added test cases for some of the conditions. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272066 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -0,0 +1,130 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.antlib.build; | |||||
| import java.io.File; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Iterator; | |||||
| import org.apache.myrmidon.api.TaskContext; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.myrmidon.framework.FileNameMapper; | |||||
| import org.apache.myrmidon.framework.conditions.Condition; | |||||
| import org.apache.tools.todo.types.DirectoryScanner; | |||||
| import org.apache.tools.todo.types.FileSet; | |||||
| import org.apache.tools.todo.types.ScannerUtil; | |||||
| import org.apache.tools.todo.types.SourceFileScanner; | |||||
| import org.apache.tools.todo.util.mappers.MergingMapper; | |||||
| /** | |||||
| * A condition which evaluates to true when the specified target has a | |||||
| * timestamp greater than all of the source files. | |||||
| * | |||||
| * @author William Ferguson <a href="mailto:williamf@mincom.com"> | |||||
| * williamf@mincom.com</a> | |||||
| * @author Hiroaki Nakamura <a href="mailto:hnakamur@mc.neweb.ne.jp"> | |||||
| * hnakamur@mc.neweb.ne.jp</a> | |||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
| * | |||||
| * @ant.type type="condition" name="uptodate" | |||||
| */ | |||||
| public class UpToDateCondition | |||||
| implements Condition | |||||
| { | |||||
| private final ArrayList m_fileSets = new ArrayList(); | |||||
| private FileNameMapper m_mapper; | |||||
| private File m_targetFile; | |||||
| /** | |||||
| * The file which must be more up to date than each of the source files if | |||||
| * the property is to be set. | |||||
| * | |||||
| * @param file the file which we are checking against. | |||||
| */ | |||||
| public void setTargetFile( final File file ) | |||||
| { | |||||
| m_targetFile = file; | |||||
| } | |||||
| /** | |||||
| * Nested <srcfiles> element. | |||||
| * | |||||
| * @param fs The feature to be added to the Srcfiles attribute | |||||
| */ | |||||
| public void addSrcfiles( final FileSet fs ) | |||||
| { | |||||
| m_fileSets.add( fs ); | |||||
| } | |||||
| /** | |||||
| * Defines the FileNameMapper to use (nested mapper element). | |||||
| */ | |||||
| public void add( final FileNameMapper mapper ) | |||||
| throws TaskException | |||||
| { | |||||
| if( m_mapper != null ) | |||||
| { | |||||
| throw new TaskException( "Cannot define more than one mapper" ); | |||||
| } | |||||
| m_mapper = mapper; | |||||
| } | |||||
| /** | |||||
| * Evaluates this condition. | |||||
| * | |||||
| * @param context | |||||
| * The context to evaluate the condition in. | |||||
| */ | |||||
| public boolean evaluate( TaskContext context ) | |||||
| throws TaskException | |||||
| { | |||||
| if( m_targetFile == null && m_mapper == null ) | |||||
| { | |||||
| throw new TaskException( "The targetfile attribute or a nested mapper element must be set" ); | |||||
| } | |||||
| // if not there then it can't be up to date | |||||
| if( m_targetFile != null && !m_targetFile.exists() ) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| final Iterator enum = m_fileSets.iterator(); | |||||
| while( enum.hasNext() ) | |||||
| { | |||||
| final FileSet fs = (FileSet)enum.next(); | |||||
| final DirectoryScanner ds = ScannerUtil.getDirectoryScanner( fs ); | |||||
| if ( !scanDir( fs.getDir(), ds.getIncludedFiles(), context ) ) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| } | |||||
| return true; | |||||
| } | |||||
| private boolean scanDir( final File srcDir, | |||||
| final String files[], | |||||
| final TaskContext context ) | |||||
| throws TaskException | |||||
| { | |||||
| final SourceFileScanner scanner = new SourceFileScanner(); | |||||
| FileNameMapper mapper = null; | |||||
| File dir = srcDir; | |||||
| if( m_mapper == null ) | |||||
| { | |||||
| final MergingMapper mm = new MergingMapper(); | |||||
| mm.setTo( m_targetFile.getAbsolutePath() ); | |||||
| mapper = mm; | |||||
| dir = null; | |||||
| } | |||||
| else | |||||
| { | |||||
| mapper = m_mapper; | |||||
| } | |||||
| return scanner.restrict( files, srcDir, dir, mapper, context ).length == 0; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,53 @@ | |||||
| package org.apache.antlib.core; | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.myrmidon.api.TaskContext; | |||||
| import org.apache.myrmidon.framework.conditions.Condition; | |||||
| /** | |||||
| * Simple String comparison condition. | |||||
| * | |||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
| * @version $Revision$ | |||||
| * | |||||
| * @ant.type type="condition" name="equals" | |||||
| */ | |||||
| public class Equals implements Condition | |||||
| { | |||||
| private String arg1, arg2; | |||||
| public void setArg1( String a1 ) | |||||
| { | |||||
| arg1 = a1; | |||||
| } | |||||
| public void setArg2( String a2 ) | |||||
| { | |||||
| arg2 = a2; | |||||
| } | |||||
| /** | |||||
| * Evaluates this condition. | |||||
| * | |||||
| * @param context | |||||
| * The context to evaluate the condition in. | |||||
| */ | |||||
| public boolean evaluate( final TaskContext context ) | |||||
| throws TaskException | |||||
| { | |||||
| if( arg1 == null || arg2 == null ) | |||||
| { | |||||
| throw new TaskException( "both arg1 and arg2 are required in equals" ); | |||||
| } | |||||
| return arg1.equals( arg2 ); | |||||
| } | |||||
| } | |||||
| @@ -14,7 +14,7 @@ import org.apache.avalon.framework.configuration.Configuration; | |||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| import org.apache.myrmidon.framework.AbstractContainerTask; | import org.apache.myrmidon.framework.AbstractContainerTask; | ||||
| import org.apache.myrmidon.framework.conditions.Condition; | import org.apache.myrmidon.framework.conditions.Condition; | ||||
| import org.apache.myrmidon.framework.conditions.IsSetCondition; | |||||
| import org.apache.myrmidon.framework.conditions.IsTrueCondition; | |||||
| import org.apache.myrmidon.framework.conditions.NotCondition; | import org.apache.myrmidon.framework.conditions.NotCondition; | ||||
| /** | /** | ||||
| @@ -44,7 +44,7 @@ public class IfTask | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| verifyConditionNull(); | verifyConditionNull(); | ||||
| m_condition = new IsSetCondition( condition ); | |||||
| m_condition = new IsTrueCondition( condition ); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -57,7 +57,7 @@ public class IfTask | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| verifyConditionNull(); | verifyConditionNull(); | ||||
| m_condition = new NotCondition( new IsSetCondition( condition ) ); | |||||
| m_condition = new NotCondition( new IsTrueCondition( condition ) ); | |||||
| } | } | ||||
| public void add( final Configuration task ) | public void add( final Configuration task ) | ||||
| @@ -4,3 +4,7 @@ facility.no-namespace.error=Must specify namespace parameter. | |||||
| import.no-lib.error=Must specify lib parameter. | import.no-lib.error=Must specify lib parameter. | ||||
| import.no-deploy.error=Error importing tasklib. | import.no-deploy.error=Error importing tasklib. | ||||
| typeavailable.no-type-name.error=No type name was specified. | |||||
| typeavailable.unknown-role.error=Unknown role "{0}". | |||||
| typeavailable.evaluate.error=Could not determine if type "{0}" is available. | |||||
| @@ -0,0 +1,102 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.antlib.runtime; | |||||
| import org.apache.myrmidon.framework.conditions.Condition; | |||||
| import org.apache.myrmidon.framework.DataType; | |||||
| import org.apache.myrmidon.api.TaskContext; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.myrmidon.interfaces.role.RoleManager; | |||||
| import org.apache.myrmidon.interfaces.role.RoleInfo; | |||||
| import org.apache.myrmidon.interfaces.type.TypeManager; | |||||
| import org.apache.myrmidon.interfaces.type.TypeFactory; | |||||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||||
| import org.apache.avalon.excalibur.i18n.Resources; | |||||
| /** | |||||
| * A condition that evaluates to true if a particular type is available. | |||||
| * | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| * | |||||
| * @ant.type type="condition" name="type-available" | |||||
| */ | |||||
| public class TypeAvailableCondition | |||||
| implements Condition | |||||
| { | |||||
| private final static Resources REZ = | |||||
| ResourceManager.getPackageResources( TypeAvailableCondition.class ); | |||||
| private String m_roleShorthand; | |||||
| private String m_name; | |||||
| /** | |||||
| * Sets the role to search for. | |||||
| */ | |||||
| public void setType( final String type ) | |||||
| { | |||||
| m_roleShorthand = type; | |||||
| } | |||||
| /** | |||||
| * Sets the type to search for. | |||||
| */ | |||||
| public void setName( final String name ) | |||||
| { | |||||
| m_name = name; | |||||
| } | |||||
| /** | |||||
| * Evaluates this condition. | |||||
| * | |||||
| * @param context | |||||
| * The context to evaluate the condition in. | |||||
| */ | |||||
| public boolean evaluate( final TaskContext context ) | |||||
| throws TaskException | |||||
| { | |||||
| if( m_name == null ) | |||||
| { | |||||
| final String message = REZ.getString( "typeavailable.no-type-name.error" ); | |||||
| throw new TaskException( message ); | |||||
| } | |||||
| try | |||||
| { | |||||
| // Map the shorthand name to a role | |||||
| final String roleName; | |||||
| if( m_roleShorthand != null ) | |||||
| { | |||||
| final RoleManager roleManager = (RoleManager)context.getService( RoleManager.class ); | |||||
| final RoleInfo roleInfo = roleManager.getRoleByShorthandName( m_roleShorthand ); | |||||
| if( roleInfo == null ) | |||||
| { | |||||
| final String message = REZ.getString( "typeavailable.unknown-role.error", m_roleShorthand ); | |||||
| throw new TaskException( message ); | |||||
| } | |||||
| roleName = roleInfo.getName(); | |||||
| } | |||||
| else | |||||
| { | |||||
| roleName = DataType.ROLE; | |||||
| } | |||||
| // Lookup the type | |||||
| final TypeManager typeManager = (TypeManager)context.getService( TypeManager.class ); | |||||
| final TypeFactory typeFactory = typeManager.getFactory( roleName ); | |||||
| // Check if the type is available | |||||
| return typeFactory.canCreate( m_name ); | |||||
| } | |||||
| catch( final Exception e ) | |||||
| { | |||||
| final String message = REZ.getString( "typeavailable.evaluate.error", m_name ); | |||||
| throw new TaskException( message, e ); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -12,7 +12,7 @@ import org.apache.avalon.excalibur.i18n.Resources; | |||||
| import org.apache.myrmidon.api.TaskContext; | import org.apache.myrmidon.api.TaskContext; | ||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| import org.apache.myrmidon.framework.conditions.Condition; | import org.apache.myrmidon.framework.conditions.Condition; | ||||
| import org.apache.myrmidon.framework.conditions.IsSetCondition; | |||||
| import org.apache.myrmidon.framework.conditions.IsTrueCondition; | |||||
| import org.apache.myrmidon.framework.conditions.NotCondition; | import org.apache.myrmidon.framework.conditions.NotCondition; | ||||
| /** | /** | ||||
| @@ -80,7 +80,7 @@ public class Pattern | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| verifyConditionNull(); | verifyConditionNull(); | ||||
| m_condition = new IsSetCondition( condition ); | |||||
| m_condition = new IsTrueCondition( condition ); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -93,7 +93,7 @@ public class Pattern | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| verifyConditionNull(); | verifyConditionNull(); | ||||
| m_condition = new NotCondition( new IsSetCondition( condition ) ); | |||||
| m_condition = new NotCondition( new IsTrueCondition( condition ) ); | |||||
| } | } | ||||
| public String evaluateName( final TaskContext context ) | public String evaluateName( final TaskContext context ) | ||||
| @@ -13,8 +13,7 @@ import org.apache.myrmidon.api.TaskContext; | |||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| /** | /** | ||||
| * A {@link Condition} that is true when a property is set, but not set to | |||||
| * 'false'. | |||||
| * A {@link Condition} that is true when a property is set. | |||||
| * | * | ||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | ||||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | * @author <a href="mailto:peter@apache.org">Peter Donald</a> | ||||
| @@ -62,6 +61,6 @@ public class IsSetCondition | |||||
| // Resolve the condition | // Resolve the condition | ||||
| final Object object = context.getProperty( m_property ); | final Object object = context.getProperty( m_property ); | ||||
| return ( object != null && !object.toString().equals( "false" ) ); | |||||
| return ( object != null ); | |||||
| } | } | ||||
| } | } | ||||
| @@ -0,0 +1,84 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.myrmidon.framework.conditions; | |||||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||||
| import org.apache.avalon.excalibur.i18n.Resources; | |||||
| import org.apache.myrmidon.api.TaskContext; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.aut.converter.Converter; | |||||
| import org.apache.aut.converter.ConverterException; | |||||
| /** | |||||
| * A {@link Condition} that is true when a property is set, but not set to | |||||
| * 'false'. | |||||
| * | |||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| * | |||||
| * @ant.type type="condition" name="is-true" | |||||
| */ | |||||
| public class IsTrueCondition | |||||
| implements Condition | |||||
| { | |||||
| private final static Resources REZ = | |||||
| ResourceManager.getPackageResources( IsTrueCondition.class ); | |||||
| private String m_property; | |||||
| public IsTrueCondition( final String propName ) | |||||
| { | |||||
| m_property = propName; | |||||
| } | |||||
| public IsTrueCondition() | |||||
| { | |||||
| } | |||||
| /** | |||||
| * Set the property name to test. | |||||
| */ | |||||
| public void setProperty( final String propName ) | |||||
| { | |||||
| m_property = propName; | |||||
| } | |||||
| /** | |||||
| * Evaluates the condition. | |||||
| */ | |||||
| public boolean evaluate( final TaskContext context ) | |||||
| throws TaskException | |||||
| { | |||||
| if( m_property == null ) | |||||
| { | |||||
| final String message = REZ.getString( "isset.no-property.error" ); | |||||
| throw new TaskException( message ); | |||||
| } | |||||
| // Resolve the property name | |||||
| final Object object = context.getProperty( m_property ); | |||||
| if( object == null ) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| // Convert value to boolean | |||||
| try | |||||
| { | |||||
| final Converter converter = (Converter)context.getService( Converter.class ); | |||||
| final Boolean value = (Boolean)converter.convert( Boolean.class, object, context ); | |||||
| return value.booleanValue(); | |||||
| } | |||||
| catch( final ConverterException e ) | |||||
| { | |||||
| throw new TaskException( e.getMessage(), e ); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -9,6 +9,8 @@ package org.apache.myrmidon.framework.conditions; | |||||
| import org.apache.myrmidon.api.TaskContext; | import org.apache.myrmidon.api.TaskContext; | ||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||||
| import org.apache.avalon.excalibur.i18n.Resources; | |||||
| /** | /** | ||||
| * <not> condition. Evaluates to true if the single condition nested into | * <not> condition. Evaluates to true if the single condition nested into | ||||
| @@ -22,6 +24,9 @@ import org.apache.myrmidon.api.TaskException; | |||||
| public class NotCondition | public class NotCondition | ||||
| implements Condition | implements Condition | ||||
| { | { | ||||
| private final static Resources REZ = | |||||
| ResourceManager.getPackageResources( NotCondition.class ); | |||||
| private Condition m_condition; | private Condition m_condition; | ||||
| public NotCondition() | public NotCondition() | ||||
| @@ -34,10 +39,16 @@ public class NotCondition | |||||
| } | } | ||||
| /** | /** | ||||
| * Sets the nested condition. | |||||
| * Adds a nested condition. | |||||
| */ | */ | ||||
| public void set( final Condition condition ) | |||||
| public void add( final Condition condition ) | |||||
| throws TaskException | |||||
| { | { | ||||
| if( m_condition != null ) | |||||
| { | |||||
| final String message = REZ.getString( "not.too-many-conditions.error" ); | |||||
| throw new TaskException( message ); | |||||
| } | |||||
| m_condition = condition; | m_condition = condition; | ||||
| } | } | ||||
| @@ -49,7 +60,8 @@ public class NotCondition | |||||
| { | { | ||||
| if( m_condition == null ) | if( m_condition == null ) | ||||
| { | { | ||||
| throw new TaskException( "no condition set" ); | |||||
| final String message = REZ.getString( "not.no-condition.error" ); | |||||
| throw new TaskException( message ); | |||||
| } | } | ||||
| return ! m_condition.evaluate( context ); | return ! m_condition.evaluate( context ); | ||||
| @@ -1 +1,4 @@ | |||||
| isset.no-property.error=No property specified to test. | isset.no-property.error=No property specified to test. | ||||
| istrue.no-property.error=No property specified to test. | |||||
| not.no-condition.error=No condition specified. | |||||
| not.too-many-conditions.error=Too many conditions specified. | |||||
| @@ -42,9 +42,7 @@ public class IfTestCase | |||||
| executeTarget( projectFile, "true-prop", listener ); | executeTarget( projectFile, "true-prop", listener ); | ||||
| // Test when property is set to a value other than 'true' or 'false' | // Test when property is set to a value other than 'true' or 'false' | ||||
| listener = new LogMessageTracker(); | |||||
| listener.addExpectedMessage( "set-prop", "test-prop is set" ); | |||||
| executeTarget( projectFile, "set-prop", listener ); | |||||
| executeTargetExpectError( projectFile, "set-prop", new String[0] ); | |||||
| // Test when property is set to 'false' | // Test when property is set to 'false' | ||||
| listener = new LogMessageTracker(); | listener = new LogMessageTracker(); | ||||
| @@ -127,7 +127,7 @@ public abstract class AbstractMyrmidonTest | |||||
| /** | /** | ||||
| * Returns the directory containing a Myrmidon install. | * Returns the directory containing a Myrmidon install. | ||||
| */ | */ | ||||
| protected File getHomeDirectory() | |||||
| protected File getInstallDirectory() | |||||
| { | { | ||||
| final File file = new File( m_baseDir, "dist" ); | final File file = new File( m_baseDir, "dist" ); | ||||
| return getCanonicalFile( file ); | return getCanonicalFile( file ); | ||||
| @@ -59,7 +59,7 @@ public class AbstractProjectTest | |||||
| m_embeddor.enableLogging( logger ); | m_embeddor.enableLogging( logger ); | ||||
| final Parameters params = new Parameters(); | final Parameters params = new Parameters(); | ||||
| final File instDir = getHomeDirectory(); | |||||
| final File instDir = getInstallDirectory(); | |||||
| params.setParameter( "myrmidon.home", instDir.getAbsolutePath() ); | params.setParameter( "myrmidon.home", instDir.getAbsolutePath() ); | ||||
| m_embeddor.parameterize( params ); | m_embeddor.parameterize( params ); | ||||
| m_embeddor.initialize(); | m_embeddor.initialize(); | ||||
| @@ -0,0 +1,37 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.myrmidon.framework.conditions.test; | |||||
| import java.io.File; | |||||
| import org.apache.myrmidon.AbstractProjectTest; | |||||
| /** | |||||
| * Test cases for the <and> condition. | |||||
| * | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| */ | |||||
| public class AndConditionTestCase | |||||
| extends AbstractProjectTest | |||||
| { | |||||
| public AndConditionTestCase( final String name ) | |||||
| { | |||||
| super( name ); | |||||
| } | |||||
| /** | |||||
| * Tests evaluation of the <and> condition. | |||||
| */ | |||||
| public void testEvaluation() throws Exception | |||||
| { | |||||
| final File projectFile = getTestResource( "and.ant" ); | |||||
| executeTarget( projectFile, "empty" ); | |||||
| executeTarget( projectFile, "truth-table" ); | |||||
| executeTarget( projectFile, "lazy" ); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,50 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.myrmidon.framework.conditions.test; | |||||
| import org.apache.myrmidon.api.AbstractTask; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.myrmidon.framework.conditions.Condition; | |||||
| /** | |||||
| * A simple assert task, used for testing conditions. | |||||
| * | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| * | |||||
| * @ant.task name="assert" | |||||
| */ | |||||
| public class ConditionTestTask | |||||
| extends AbstractTask | |||||
| { | |||||
| private boolean m_expected = true; | |||||
| private Condition m_condition; | |||||
| public void setExpected( final boolean expected ) | |||||
| { | |||||
| m_expected = expected; | |||||
| } | |||||
| public void add( final Condition condition ) | |||||
| { | |||||
| m_condition = condition; | |||||
| } | |||||
| /** | |||||
| * Execute task. | |||||
| */ | |||||
| public void execute() | |||||
| throws TaskException | |||||
| { | |||||
| final boolean result = m_condition.evaluate( getContext() ); | |||||
| if( result != m_expected ) | |||||
| { | |||||
| throw new TaskException( "Expected " + m_expected + ", got " + result ); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,48 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.myrmidon.framework.conditions.test; | |||||
| import java.io.File; | |||||
| import org.apache.avalon.excalibur.i18n.Resources; | |||||
| import org.apache.myrmidon.AbstractProjectTest; | |||||
| import org.apache.myrmidon.framework.conditions.IsSetCondition; | |||||
| /** | |||||
| * Test cases for the <is-set> condition. | |||||
| * | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| */ | |||||
| public class IsSetConditionTestCase | |||||
| extends AbstractProjectTest | |||||
| { | |||||
| public IsSetConditionTestCase( final String name ) | |||||
| { | |||||
| super( name ); | |||||
| } | |||||
| /** | |||||
| * Test cases for <is-set> evaluation. | |||||
| */ | |||||
| public void testEvaluation() throws Exception | |||||
| { | |||||
| final File projectFile = getTestResource( "isset.ant" ); | |||||
| executeTarget( projectFile, "set" ); | |||||
| executeTarget( projectFile, "set2true" ); | |||||
| executeTarget( projectFile, "set2false" ); | |||||
| executeTarget( projectFile, "not-set" ); | |||||
| Resources res = getResourcesForTested( IsSetCondition.class ); | |||||
| final String[] messages = { | |||||
| null, | |||||
| res.getString( "isset.no-property.error" ) | |||||
| }; | |||||
| executeTargetExpectError( projectFile, "no-prop-name", messages ); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,51 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.myrmidon.framework.conditions.test; | |||||
| import java.io.File; | |||||
| import org.apache.avalon.excalibur.i18n.Resources; | |||||
| import org.apache.myrmidon.AbstractProjectTest; | |||||
| import org.apache.myrmidon.framework.conditions.IsTrueCondition; | |||||
| /** | |||||
| * Test cases for the <is-true> condition. | |||||
| * | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| */ | |||||
| public class IsTrueConditionTestCase | |||||
| extends AbstractProjectTest | |||||
| { | |||||
| public IsTrueConditionTestCase( final String name ) | |||||
| { | |||||
| super( name ); | |||||
| } | |||||
| /** | |||||
| * Test cases for <is-true> evaluation. | |||||
| */ | |||||
| public void testEvaluation() throws Exception | |||||
| { | |||||
| final File projectFile = getTestResource( "istrue.ant" ); | |||||
| executeTarget( projectFile, "set2true" ); | |||||
| executeTarget( projectFile, "set2false" ); | |||||
| executeTarget( projectFile, "not-set" ); | |||||
| // TODO - check error message | |||||
| String[] messages = {}; | |||||
| executeTargetExpectError( projectFile, "set", messages ); | |||||
| final Resources res = getResourcesForTested( IsTrueCondition.class ); | |||||
| messages = new String[] { | |||||
| null, | |||||
| res.getString( "istrue.no-property.error" ) | |||||
| }; | |||||
| executeTargetExpectError( projectFile, "no-prop-name", messages ); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,39 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.myrmidon.framework.conditions.test; | |||||
| import org.apache.myrmidon.AbstractProjectTest; | |||||
| import java.io.File; | |||||
| /** | |||||
| * Test cases for the <not> condition. | |||||
| * | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| */ | |||||
| public class NotConditionTestCase | |||||
| extends AbstractProjectTest | |||||
| { | |||||
| public NotConditionTestCase( final String name ) | |||||
| { | |||||
| super( name ); | |||||
| } | |||||
| /** | |||||
| * Tests evaluation of <not>. | |||||
| */ | |||||
| public void testEvaluation() throws Exception | |||||
| { | |||||
| final File projectFile = getTestResource( "not.ant" ); | |||||
| executeTarget( projectFile, "truth-table" ); | |||||
| // TODO - check error messages | |||||
| executeTargetExpectError( projectFile, "empty", new String[0] ); | |||||
| executeTargetExpectError( projectFile, "too-many-nested", new String[0] ); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,37 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.myrmidon.framework.conditions.test; | |||||
| import java.io.File; | |||||
| import org.apache.myrmidon.AbstractProjectTest; | |||||
| /** | |||||
| * Test cases for the <or> condition. | |||||
| * | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| */ | |||||
| public class OrConditionTestCase | |||||
| extends AbstractProjectTest | |||||
| { | |||||
| public OrConditionTestCase( final String name ) | |||||
| { | |||||
| super( name ); | |||||
| } | |||||
| /** | |||||
| * Tests evaluation of the <or> condition. | |||||
| */ | |||||
| public void testEvaluation() throws Exception | |||||
| { | |||||
| final File projectFile = getTestResource( "or.ant" ); | |||||
| executeTarget( projectFile, "empty" ); | |||||
| executeTarget( projectFile, "truth-table" ); | |||||
| executeTarget( projectFile, "lazy" ); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,57 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.myrmidon.framework.conditions.test; | |||||
| import org.apache.myrmidon.framework.conditions.Condition; | |||||
| import org.apache.myrmidon.api.TaskContext; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.avalon.framework.configuration.Configurable; | |||||
| import org.apache.avalon.framework.configuration.Configuration; | |||||
| import org.apache.avalon.framework.configuration.ConfigurationException; | |||||
| /** | |||||
| * A condition used for testing. | |||||
| * | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| * | |||||
| * @ant.type type="condition" name="true" | |||||
| * @ant.type type="condition" name="false" | |||||
| * @ant.type type="condition" name="fail" | |||||
| */ | |||||
| public class TestCondition | |||||
| implements Condition, Configurable | |||||
| { | |||||
| private String m_action; | |||||
| public void configure( final Configuration configuration ) | |||||
| throws ConfigurationException | |||||
| { | |||||
| m_action = configuration.getName(); | |||||
| } | |||||
| /** | |||||
| * Evaluates this condition. | |||||
| */ | |||||
| public boolean evaluate( final TaskContext context ) | |||||
| throws TaskException | |||||
| { | |||||
| if( m_action.equalsIgnoreCase( "true" ) ) | |||||
| { | |||||
| return true; | |||||
| } | |||||
| else if( m_action.equalsIgnoreCase( "false" ) ) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| else | |||||
| { | |||||
| throw new TaskException( "Fail." ); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,40 @@ | |||||
| <!-- Tests for the <and> condition. --> | |||||
| <project version="2.0"> | |||||
| <!-- An empty <and> condition --> | |||||
| <target name="empty"> | |||||
| <assert> | |||||
| <and/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- The truth-table --> | |||||
| <target name="truth-table"> | |||||
| <assert expected="false"> | |||||
| <and><false/></and> | |||||
| </assert> | |||||
| <assert> | |||||
| <and><true/></and> | |||||
| </assert> | |||||
| <assert expected="false"> | |||||
| <and><false/><false/></and> | |||||
| </assert> | |||||
| <assert expected="false"> | |||||
| <and><true/><false/></and> | |||||
| </assert> | |||||
| <assert expected="false"> | |||||
| <and><false/><true/></and> | |||||
| </assert> | |||||
| <assert> | |||||
| <and><true/><true/></and> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- Check lazy evaluation --> | |||||
| <target name="lazy"> | |||||
| <assert expected="false"> | |||||
| <and><false/><fail/></and> | |||||
| </assert> | |||||
| </target> | |||||
| </project> | |||||
| @@ -0,0 +1,44 @@ | |||||
| <!-- Tests for the <is-set> condition. --> | |||||
| <project version="2.0"> | |||||
| <!-- Set to some arbirary value --> | |||||
| <target name="set"> | |||||
| <property name="test-prop"> | |||||
| <path location="some-location"/> | |||||
| </property> | |||||
| <assert> | |||||
| <is-set property="test-prop"/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- Set to true --> | |||||
| <target name="set2true"> | |||||
| <property name="test-prop" value="true"/> | |||||
| <assert> | |||||
| <is-set property="test-prop"/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- Set to false --> | |||||
| <target name="set2false"> | |||||
| <property name="test-prop" value="false"/> | |||||
| <assert> | |||||
| <is-set property="test-prop"/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- Not set --> | |||||
| <target name="not-set"> | |||||
| <assert expected="false"> | |||||
| <is-set property="test-prop"/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- No property name --> | |||||
| <target name="no-prop-name"> | |||||
| <assert> | |||||
| <is-set/> | |||||
| </assert> | |||||
| </target> | |||||
| </project> | |||||
| @@ -0,0 +1,44 @@ | |||||
| <!-- Tests for the <is-true> condition. --> | |||||
| <project version="2.0"> | |||||
| <!-- Set to some arbirary value --> | |||||
| <target name="set"> | |||||
| <property name="test-prop"> | |||||
| <path location="some-location"/> | |||||
| </property> | |||||
| <assert> | |||||
| <is-true property="test-prop"/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- Set to true --> | |||||
| <target name="set2true"> | |||||
| <property name="test-prop" value="true"/> | |||||
| <assert> | |||||
| <is-true property="test-prop"/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- Set to false --> | |||||
| <target name="set2false"> | |||||
| <property name="test-prop" value="false"/> | |||||
| <assert expected="false"> | |||||
| <is-true property="test-prop"/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- Not set --> | |||||
| <target name="not-set"> | |||||
| <assert expected="false"> | |||||
| <is-true property="test-prop"/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- No property name --> | |||||
| <target name="no-prop-name"> | |||||
| <assert> | |||||
| <is-true/> | |||||
| </assert> | |||||
| </target> | |||||
| </project> | |||||
| @@ -0,0 +1,28 @@ | |||||
| <!-- Tests for the <not> condition. --> | |||||
| <project version="2.0"> | |||||
| <!-- An empty <or> condition --> | |||||
| <target name="empty"> | |||||
| <assert> | |||||
| <not/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- The truth-table --> | |||||
| <target name="truth-table"> | |||||
| <assert expected="false"> | |||||
| <not><true/></not> | |||||
| </assert> | |||||
| <assert> | |||||
| <not><false/></not> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- Too many nested conditions --> | |||||
| <target name="too-many-nested"> | |||||
| <assert> | |||||
| <not><true/><false/></not> | |||||
| </assert> | |||||
| </target> | |||||
| </project> | |||||
| @@ -0,0 +1,40 @@ | |||||
| <!-- Tests for the <or> condition. --> | |||||
| <project version="2.0"> | |||||
| <!-- An empty <or> condition --> | |||||
| <target name="empty"> | |||||
| <assert> | |||||
| <or/> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- The truth-table --> | |||||
| <target name="truth-table"> | |||||
| <assert expected="false"> | |||||
| <or><false/></or> | |||||
| </assert> | |||||
| <assert> | |||||
| <or><true/></or> | |||||
| </assert> | |||||
| <assert expected="false"> | |||||
| <or><false/><false/></or> | |||||
| </assert> | |||||
| <assert> | |||||
| <or><true/><false/></or> | |||||
| </assert> | |||||
| <assert> | |||||
| <or><false/><true/></or> | |||||
| </assert> | |||||
| <assert> | |||||
| <or><true/><true/></or> | |||||
| </assert> | |||||
| </target> | |||||
| <!-- Check lazy evaluation --> | |||||
| <target name="lazy"> | |||||
| <assert> | |||||
| <or><true/><fail/></or> | |||||
| </assert> | |||||
| </target> | |||||
| </project> | |||||