git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271669 13f79535-47bb-0310-9956-ffa450edef68master
@@ -0,0 +1,63 @@ | |||
<project version="2.0"> | |||
<target name="true-prop"> | |||
<property name="test-prop" value="true"/> | |||
<if test="test-prop"> | |||
<log>test-prop is set</log> | |||
</if> | |||
<if not-test="test-prop"> | |||
<log>test-prop is not set</log> | |||
</if> | |||
</target> | |||
<target name="set-prop"> | |||
<property name="test-prop" value="some value"/> | |||
<if test="test-prop"> | |||
<log>test-prop is set</log> | |||
</if> | |||
<if not-test="test-prop"> | |||
<log>test-prop is not set</log> | |||
</if> | |||
</target> | |||
<target name="not-set-prop"> | |||
<if test="test-prop"> | |||
<log>test-prop is set</log> | |||
</if> | |||
<if not-test="test-prop"> | |||
<log>test-prop is not set</log> | |||
</if> | |||
</target> | |||
<target name="false-prop"> | |||
<property name="test-prop" value="false"/> | |||
<if test="test-prop"> | |||
<log>test-prop is set</log> | |||
</if> | |||
<if not-test="test-prop"> | |||
<log>test-prop is not set</log> | |||
</if> | |||
</target> | |||
<target name="multiple-nested-tasks"> | |||
<property name="test-prop" value="true"/> | |||
<if test="test-prop"> | |||
<log>task 1</log> | |||
<log>task 2</log> | |||
<log>task 3</log> | |||
<log>task 4</log> | |||
</if> | |||
</target> | |||
<target name="no-condition"> | |||
<if> | |||
<log>no go</log> | |||
</if> | |||
</target> | |||
<target name="too-many-conditions"> | |||
<if test="test-prop" not-test="test-prop"> | |||
<log>no go</log> | |||
</if> | |||
</target> | |||
</project> |
@@ -74,9 +74,16 @@ public class IfTask | |||
throw new TaskException( message ); | |||
} | |||
// Evaluate the condition | |||
if( ! m_condition.evaluate( getContext() ) ) | |||
{ | |||
return; | |||
} | |||
final Configuration[] tasks = | |||
(Configuration[])m_tasks.toArray( new Configuration[ m_tasks.size() ] ); | |||
// TODO - don't use getService() | |||
final ExecutionFrame frame = (ExecutionFrame)getService( ExecutionFrame.class ); | |||
final Executor executor = (Executor)getService( Executor.class ); | |||
@@ -0,0 +1,99 @@ | |||
/* | |||
* 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.core; | |||
import java.io.File; | |||
import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
import org.apache.avalon.excalibur.i18n.Resources; | |||
import org.apache.myrmidon.AbstractProjectTest; | |||
import org.apache.myrmidon.LogMessageTracker; | |||
/** | |||
* Test cases for the <if> task. | |||
* | |||
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
* @version $Revision$ $Date$ | |||
*/ | |||
public class IfTest | |||
extends AbstractProjectTest | |||
{ | |||
private final static Resources REZ | |||
= ResourceManager.getPackageResources( IfTest.class ); | |||
public IfTest( String name ) | |||
{ | |||
super( name ); | |||
} | |||
/** | |||
* Test checking whether a property is set and not 'false'. | |||
*/ | |||
public void testConditions() | |||
throws Exception | |||
{ | |||
final File projectFile = getTestResource( "if.ant" ); | |||
// Test when property is set to 'true' | |||
LogMessageTracker listener = new LogMessageTracker(); | |||
listener.addExpectedMessage( "true-prop", "test-prop is set" ); | |||
executeTarget( projectFile, "true-prop", listener ); | |||
// 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 ); | |||
// Test when property is set to 'false' | |||
listener = new LogMessageTracker(); | |||
listener.addExpectedMessage( "false-prop", "test-prop is not set" ); | |||
executeTarget( projectFile, "false-prop", listener ); | |||
// Test when property is not set | |||
listener = new LogMessageTracker(); | |||
listener.addExpectedMessage( "not-set-prop", "test-prop is not set" ); | |||
executeTarget( projectFile, "not-set-prop", listener ); | |||
} | |||
/** | |||
* Tests that the <if> task can handle multiple nested tasks. | |||
*/ | |||
public void testMultipleTasks() throws Exception | |||
{ | |||
final File projectFile = getTestResource( "if.ant" ); | |||
// Test when property is not set | |||
LogMessageTracker listener = new LogMessageTracker(); | |||
listener.addExpectedMessage( "multiple-nested-tasks", "task 1" ); | |||
listener.addExpectedMessage( "multiple-nested-tasks", "task 2" ); | |||
listener.addExpectedMessage( "multiple-nested-tasks", "task 3" ); | |||
listener.addExpectedMessage( "multiple-nested-tasks", "task 4" ); | |||
executeTarget( projectFile, "multiple-nested-tasks", listener ); | |||
} | |||
/** | |||
* Tests validation. | |||
*/ | |||
public void testValidation() throws Exception | |||
{ | |||
final File projectFile = getTestResource( "if.ant" ); | |||
// Check for missing condition | |||
String message = REZ.getString( "if.no-condition.error" ); | |||
executeTargetExpectError( projectFile, "no-condition", message ); | |||
// Check for too many conditions | |||
String[] messages = | |||
{ | |||
null, | |||
null, | |||
REZ.getString( "if.ifelse-duplicate.error" ) | |||
}; | |||
executeTargetExpectError( projectFile, "too-many-conditions", messages ); | |||
} | |||
} |
@@ -0,0 +1,99 @@ | |||
/* | |||
* 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.core; | |||
import java.io.File; | |||
import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
import org.apache.avalon.excalibur.i18n.Resources; | |||
import org.apache.myrmidon.AbstractProjectTest; | |||
import org.apache.myrmidon.LogMessageTracker; | |||
/** | |||
* Test cases for the <if> task. | |||
* | |||
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
* @version $Revision$ $Date$ | |||
*/ | |||
public class IfTest | |||
extends AbstractProjectTest | |||
{ | |||
private final static Resources REZ | |||
= ResourceManager.getPackageResources( IfTest.class ); | |||
public IfTest( String name ) | |||
{ | |||
super( name ); | |||
} | |||
/** | |||
* Test checking whether a property is set and not 'false'. | |||
*/ | |||
public void testConditions() | |||
throws Exception | |||
{ | |||
final File projectFile = getTestResource( "if.ant" ); | |||
// Test when property is set to 'true' | |||
LogMessageTracker listener = new LogMessageTracker(); | |||
listener.addExpectedMessage( "true-prop", "test-prop is set" ); | |||
executeTarget( projectFile, "true-prop", listener ); | |||
// 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 ); | |||
// Test when property is set to 'false' | |||
listener = new LogMessageTracker(); | |||
listener.addExpectedMessage( "false-prop", "test-prop is not set" ); | |||
executeTarget( projectFile, "false-prop", listener ); | |||
// Test when property is not set | |||
listener = new LogMessageTracker(); | |||
listener.addExpectedMessage( "not-set-prop", "test-prop is not set" ); | |||
executeTarget( projectFile, "not-set-prop", listener ); | |||
} | |||
/** | |||
* Tests that the <if> task can handle multiple nested tasks. | |||
*/ | |||
public void testMultipleTasks() throws Exception | |||
{ | |||
final File projectFile = getTestResource( "if.ant" ); | |||
// Test when property is not set | |||
LogMessageTracker listener = new LogMessageTracker(); | |||
listener.addExpectedMessage( "multiple-nested-tasks", "task 1" ); | |||
listener.addExpectedMessage( "multiple-nested-tasks", "task 2" ); | |||
listener.addExpectedMessage( "multiple-nested-tasks", "task 3" ); | |||
listener.addExpectedMessage( "multiple-nested-tasks", "task 4" ); | |||
executeTarget( projectFile, "multiple-nested-tasks", listener ); | |||
} | |||
/** | |||
* Tests validation. | |||
*/ | |||
public void testValidation() throws Exception | |||
{ | |||
final File projectFile = getTestResource( "if.ant" ); | |||
// Check for missing condition | |||
String message = REZ.getString( "if.no-condition.error" ); | |||
executeTargetExpectError( projectFile, "no-condition", message ); | |||
// Check for too many conditions | |||
String[] messages = | |||
{ | |||
null, | |||
null, | |||
REZ.getString( "if.ifelse-duplicate.error" ) | |||
}; | |||
executeTargetExpectError( projectFile, "too-many-conditions", messages ); | |||
} | |||
} |