Browse Source

make select selector and include/exclude use the same logic as target for if/unless

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@821847 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 15 years ago
parent
commit
73c0030ac6
4 changed files with 259 additions and 40 deletions
  1. +53
    -20
      src/main/org/apache/tools/ant/types/PatternSet.java
  2. +40
    -20
      src/main/org/apache/tools/ant/types/selectors/SelectSelector.java
  3. +78
    -0
      src/tests/antunit/types/patternset-test.xml
  4. +88
    -0
      src/tests/antunit/types/selectors/select-test.xml

+ 53
- 20
src/main/org/apache/tools/ant/types/PatternSet.java View File

@@ -26,6 +26,7 @@ import java.util.StringTokenizer;
import java.util.Vector; import java.util.Vector;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.PropertyHelper;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;


/** /**
@@ -44,12 +45,13 @@ public class PatternSet extends DataType implements Cloneable {
/** /**
* inner class to hold a name on list. "If" and "Unless" attributes * inner class to hold a name on list. "If" and "Unless" attributes
* may be used to invalidate the entry based on the existence of a * may be used to invalidate the entry based on the existence of a
* property (typically set thru the use of the Available task).
* property (typically set thru the use of the Available task)
* or value of an expression.
*/ */
public class NameEntry { public class NameEntry {
private String name; private String name;
private String ifCond;
private String unlessCond;
private Object ifCond;
private Object unlessCond;


/** /**
* Sets the name pattern. * Sets the name pattern.
@@ -62,28 +64,63 @@ public class PatternSet extends DataType implements Cloneable {


/** /**
* Sets the if attribute. This attribute and the "unless" * Sets the if attribute. This attribute and the "unless"
* attribute are used to validate the name, based in the
* existence of the property.
* attribute are used to validate the name, based on the
* existence of the property or the value of the evaluated
* property expression.
* *
* @param cond A property name. If this property is not
* present, the name is invalid.
* @param cond A property name or expression. If the
* expression evaluates to false or no property of
* its value is present, the name is invalid.
* @since Ant 1.8.0
*/ */
public void setIf(String cond) {
public void setIf(Object cond) {
ifCond = cond; ifCond = cond;
} }


/**
* Sets the if attribute. This attribute and the "unless"
* attribute are used to validate the name, based on the
* existence of the property or the value of the evaluated
* property expression.
*
* @param cond A property name or expression. If the
* expression evaluates to false or no property of
* its value is present, the name is invalid.
*/
public void setIf(String cond) {
setIf((Object) cond);
}

/** /**
* Sets the unless attribute. This attribute and the "if" * Sets the unless attribute. This attribute and the "if"
* attribute are used to validate the name, based in the
* existence of the property.
* attribute are used to validate the name, based on the
* existence of the property or the value of the evaluated
* property expression.
* *
* @param cond A property name. If this property is
* present, the name is invalid.
* @param cond A property name or expression. If the
* expression evaluates to true or a property of
* its value is present, the name is invalid.
* @param cond A property name or expression.
* @since Ant 1.8.0
*/ */
public void setUnless(String cond) {
public void setUnless(Object cond) {
unlessCond = cond; unlessCond = cond;
} }


/**
* Sets the unless attribute. This attribute and the "if"
* attribute are used to validate the name, based on the
* existence of the property or the value of the evaluated
* property expression.
*
* @param cond A property name or expression. If the
* expression evaluates to true or a property of
* its value is present, the name is invalid.
*/
public void setUnless(String cond) {
setUnless((Object) cond);
}

/** /**
* @return the name attribute. * @return the name attribute.
*/ */
@@ -105,13 +142,9 @@ public class PatternSet extends DataType implements Cloneable {
} }


private boolean valid(Project p) { private boolean valid(Project p) {
if (ifCond != null && p.getProperty(ifCond) == null) {
return false;
}
if (unlessCond != null && p.getProperty(unlessCond) != null) {
return false;
}
return true;
PropertyHelper ph = PropertyHelper.getPropertyHelper(p);
return ph.testIfCondition(ifCond)
&& ph.testUnlessCondition(unlessCond);
} }


/** /**


+ 40
- 20
src/main/org/apache/tools/ant/types/selectors/SelectSelector.java View File

@@ -22,6 +22,7 @@ import java.util.Enumeration;
import java.io.File; import java.io.File;


import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.PropertyHelper;


/** /**
* This selector just holds one other selector and forwards all * This selector just holds one other selector and forwards all
@@ -35,8 +36,8 @@ import org.apache.tools.ant.Project;
*/ */
public class SelectSelector extends BaseSelectorContainer { public class SelectSelector extends BaseSelectorContainer {


private String ifProperty;
private String unlessProperty;
private Object ifCondition;
private Object unlessCondition;


/** /**
* Default constructor. * Default constructor.
@@ -51,13 +52,13 @@ public class SelectSelector extends BaseSelectorContainer {
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
if (hasSelectors()) { if (hasSelectors()) {
buf.append("{select"); buf.append("{select");
if (ifProperty != null) {
if (ifCondition != null) {
buf.append(" if: "); buf.append(" if: ");
buf.append(ifProperty);
buf.append(ifCondition);
} }
if (unlessProperty != null) {
if (unlessCondition != null) {
buf.append(" unless: "); buf.append(" unless: ");
buf.append(unlessProperty);
buf.append(unlessCondition);
} }
buf.append(" "); buf.append(" ");
buf.append(super.toString()); buf.append(super.toString());
@@ -151,32 +152,51 @@ public class SelectSelector extends BaseSelectorContainer {
* @return true if conditions are passed * @return true if conditions are passed
*/ */
public boolean passesConditions() { public boolean passesConditions() {
if (ifProperty != null
&& getProject().getProperty(ifProperty) == null) {
return false;
} else if (unlessProperty != null
&& getProject().getProperty(unlessProperty) != null) {
return false;
}
return true;
PropertyHelper ph = PropertyHelper.getPropertyHelper(getProject());
return ph.testIfCondition(ifCondition)
&& ph.testUnlessCondition(unlessCondition);
} }


/** /**
* Sets the if attribute to a property which must exist for the
* Sets the if attribute to an expression which must evaluate to
* true or the name of an existing property for the
* selector to select any files. * selector to select any files.
* @param ifProperty the property to check
* @param ifProperty the expression to check
* @since Ant 1.8.0
*/
public void setIf(Object ifProperty) {
this.ifCondition = ifProperty;
}

/**
* Sets the if attribute to an expression which must evaluate to
* true or the name of an existing property for the
* selector to select any files.
* @param ifProperty the expression to check
*/ */
public void setIf(String ifProperty) { public void setIf(String ifProperty) {
this.ifProperty = ifProperty;
setIf((Object) ifProperty);
}

/**
* Sets the unless attribute to an expression which must evaluate to
* false or the name of a property which cannot exist for the
* selector to select any files.
* @param unlessProperty the expression to check
* @since Ant 1.8.0
*/
public void setUnless(Object unlessProperty) {
this.unlessCondition = unlessProperty;
} }


/** /**
* Sets the unless attribute to a property which cannot exist for the
* Sets the unless attribute to an expression which must evaluate to
* false or the name of a property which cannot exist for the
* selector to select any files. * selector to select any files.
* @param unlessProperty the property to check
* @param unlessProperty the expression to check
*/ */
public void setUnless(String unlessProperty) { public void setUnless(String unlessProperty) {
this.unlessProperty = unlessProperty;
setUnless((Object) unlessProperty);
} }


/** /**


+ 78
- 0
src/tests/antunit/types/patternset-test.xml View File

@@ -0,0 +1,78 @@
<?xml version="1.0"?>
<!--
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.
-->
<project xmlns:au="antlib:org.apache.ant.antunit" default="antunit">

<import file="../antunit-base.xml" />

<target name="setUp">
<mkdir dir="${input}"/>
<touch file="${input}/A"/>
<touch file="${input}/B"/>
<touch file="${input}/C"/>
<touch file="${input}/D"/>
<mkdir dir="${output}"/>
<macrodef name="cp">
<sequential>
<copy todir="${output}">
<fileset dir="${input}">
<include name="A" if="${if}"/>
<include name="B" unless="${if}"/>
<include name="C" if="if"/>
<include name="D" unless="if"/>
</fileset>
</copy>
</sequential>
</macrodef>
</target>

<target name="testIfNotSet" depends="setUp">
<cp/>
<au:assertFileDoesntExist file="${output}/A"/>
<au:assertFileExists file="${output}/B"/>
<au:assertFileDoesntExist file="${output}/C"/>
<au:assertFileExists file="${output}/D"/>
</target>

<target name="testIfSet" depends="setUp">
<property name="if" value="whatever"/>
<cp/>
<au:assertFileDoesntExist file="${output}/A"/>
<au:assertFileExists file="${output}/B"/>
<au:assertFileExists file="${output}/C"/>
<au:assertFileDoesntExist file="${output}/D"/>
</target>

<target name="testIfTrue" depends="setUp">
<property name="if" value="true"/>
<cp/>
<au:assertFileExists file="${output}/A"/>
<au:assertFileDoesntExist file="${output}/B"/>
<au:assertFileExists file="${output}/C"/>
<au:assertFileDoesntExist file="${output}/D"/>
</target>

<target name="testIfFalse" depends="setUp">
<property name="if" value="false"/>
<cp/>
<au:assertFileDoesntExist file="${output}/A"/>
<au:assertFileExists file="${output}/B"/>
<au:assertFileExists file="${output}/C"/>
<au:assertFileDoesntExist file="${output}/D"/>
</target>

</project>

+ 88
- 0
src/tests/antunit/types/selectors/select-test.xml View File

@@ -0,0 +1,88 @@
<?xml version="1.0"?>
<!--
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.
-->
<project xmlns:au="antlib:org.apache.ant.antunit" default="antunit">

<import file="../../antunit-base.xml" />

<target name="setUp">
<mkdir dir="${input}"/>
<touch file="${input}/A"/>
<touch file="${input}/B"/>
<touch file="${input}/C"/>
<touch file="${input}/D"/>
<mkdir dir="${output}"/>
<macrodef name="cp">
<sequential>
<copy todir="${output}">
<fileset dir="${input}">
<or>
<selector if="${if}">
<filename name="A"/>
</selector>
<selector unless="${if}">
<filename name="B"/>
</selector>
<selector if="if">
<filename name="C"/>
</selector>
<selector unless="if">
<filename name="D"/>
</selector>
</or>
</fileset>
</copy>
</sequential>
</macrodef>
</target>

<target name="testIfNotSet" depends="setUp">
<cp/>
<au:assertFileDoesntExist file="${output}/C"/>
<au:assertFileExists file="${output}/D"/>
<au:assertFileDoesntExist file="${output}/A"/>
<au:assertFileExists file="${output}/B"/>
</target>

<target name="testIfSet" depends="setUp">
<property name="if" value="whatever"/>
<cp/>
<au:assertFileDoesntExist file="${output}/A"/>
<au:assertFileExists file="${output}/B"/>
<au:assertFileExists file="${output}/C"/>
<au:assertFileDoesntExist file="${output}/D"/>
</target>

<target name="testIfTrue" depends="setUp">
<property name="if" value="true"/>
<cp/>
<au:assertFileExists file="${output}/A"/>
<au:assertFileDoesntExist file="${output}/B"/>
<au:assertFileExists file="${output}/C"/>
<au:assertFileDoesntExist file="${output}/D"/>
</target>

<target name="testIfFalse" depends="setUp">
<property name="if" value="false"/>
<cp/>
<au:assertFileDoesntExist file="${output}/A"/>
<au:assertFileExists file="${output}/B"/>
<au:assertFileExists file="${output}/C"/>
<au:assertFileDoesntExist file="${output}/D"/>
</target>

</project>

Loading…
Cancel
Save