Browse Source

Added forcestring attribute to equals condition

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@916460 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 15 years ago
parent
commit
083e298097
4 changed files with 114 additions and 5 deletions
  1. +5
    -0
      WHATSNEW
  2. +10
    -3
      docs/manual/CoreTasks/conditions.html
  3. +19
    -2
      src/main/org/apache/tools/ant/taskdefs/condition/Equals.java
  4. +80
    -0
      src/tests/antunit/taskdefs/condition/equals-test.xml

+ 5
- 0
WHATSNEW View File

@@ -65,6 +65,11 @@ Other changes:
* PropertyResource will effectively proxy another Resource if ${name}
evaluates to a Resource object.

* Added forcestring attribute to equals condition to force evaluation
of Object args as strings; previously only API-level usage of the
equals condition allowed Object args, but Ant 1.8.x+ property
evaluation may yield values of any type.

Changes from Ant 1.8.0RC1 TO Ant 1.8.0
======================================



+ 10
- 3
docs/manual/CoreTasks/conditions.html View File

@@ -138,7 +138,7 @@ the tests succeed.
</ul>

<h4>equals</h4>
<p>Tests whether the two given Strings are identical</p>
<p>Tests whether the two given values are equal.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
@@ -147,12 +147,12 @@ the tests succeed.
</tr>
<tr>
<td valign="top">arg1</td>
<td valign="top">First string to test.</td>
<td valign="top">First value to test.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">arg2</td>
<td valign="top">Second string to test.</td>
<td valign="top">Second value to test.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
@@ -167,6 +167,13 @@ the tests succeed.
them. Default is false.</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">forcestring</td>
<td valign="top">Force string comparison of <code>arg1/arg2</code>.
Default is false. <em>Since Ant 1.8.1</em>
</td>
<td valign="top" align="center">No</td>
</tr>
</table>

<h4>isset</h4>


+ 19
- 2
src/main/org/apache/tools/ant/taskdefs/condition/Equals.java View File

@@ -32,6 +32,7 @@ public class Equals implements Condition {
private boolean trim = false;
private boolean caseSensitive = true;
private int args;
private boolean forcestring = false;

/**
* Set the first argument
@@ -106,6 +107,16 @@ public class Equals implements Condition {
caseSensitive = b;
}

/**
* Set whether to force string comparisons for non-equal, non-string objects.
* This allows object properties (legal in Ant 1.8.x+) to be compared as strings.
* @param forcestring value to set
* @since Ant 1.8.1
*/
public void setForcestring(boolean forcestring) {
this.forcestring = forcestring;
}

/**
* @return true if the two strings are equal
* @exception BuildException if the attributes are not set correctly
@@ -114,7 +125,13 @@ public class Equals implements Condition {
if ((args & REQUIRED) != REQUIRED) {
throw new BuildException("both arg1 and arg2 are required in equals");
}

if (arg1 == arg2 || arg1 != null && arg1.equals(arg2)) {
return true;
}
if (forcestring) {
arg1 = arg1 == null || arg1 instanceof String ? arg1 : arg1.toString();
arg2 = arg2 == null || arg2 instanceof String ? arg2 : arg2.toString();
}
if (arg1 instanceof String && trim) {
arg1 = ((String) arg1).trim();
}
@@ -126,6 +143,6 @@ public class Equals implements Condition {
String s2 = (String) arg2;
return caseSensitive ? s1.equals(s2) : s1.equalsIgnoreCase(s2);
}
return arg1 == arg2 || arg1 != null && arg1.equals(arg2);
return false;
}
}

+ 80
- 0
src/tests/antunit/taskdefs/condition/equals-test.xml View File

@@ -0,0 +1,80 @@
<?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 name="equals-test" xmlns:au="antlib:org.apache.ant.antunit"
default="antunit">

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

<target name="test-noforcestring">
<string id="s" value="foo" />
<au:assertFalse>
<equals arg1="foo" arg2="${ant.refid:s}" />
</au:assertFalse>
</target>

<target name="test-forcestring">
<string id="s" value="foo" />
<au:assertTrue>
<equals arg1="foo" arg2="${ant.refid:s}" forcestring="true" />
</au:assertTrue>
</target>

<target name="test-forcestring-notrim">
<string id="s" value=" foo " />
<au:assertFalse>
<equals arg1="foo" arg2="${ant.refid:s}" forcestring="true" />
</au:assertFalse>
</target>

<target name="test-forcestring-trim">
<string id="s" value=" foo " />
<au:assertTrue>
<equals arg1="foo" arg2="${ant.refid:s}" forcestring="true" trim="true" />
</au:assertTrue>
</target>

<target name="test-forcestring-cs">
<string id="s" value="Foo" />
<au:assertFalse>
<equals arg1="foo" arg2="${ant.refid:s}" forcestring="true" />
</au:assertFalse>
</target>

<target name="test-forcestring-nocs">
<string id="s" value="Foo" />
<au:assertTrue>
<equals arg1="foo" arg2="${ant.refid:s}" forcestring="true" casesensitive="false" />
</au:assertTrue>
</target>

<target name="test-forcestring-trim-cs">
<string id="s" value=" Foo " />
<au:assertFalse>
<equals arg1="foo" arg2="${ant.refid:s}" forcestring="true" trim="true" />
</au:assertFalse>
</target>

<target name="test-forcestring-trim-nocs">
<string id="s" value=" Foo " />
<au:assertTrue>
<equals arg1="foo" arg2="${ant.refid:s}" forcestring="true"
trim="true" casesensitive="false" />
</au:assertTrue>
</target>

</project>

Loading…
Cancel
Save