Author: mbenson
Date: Thu Feb 25 21:13:27 2010
New Revision: 916460

URL: http://svn.apache.org/viewvc?rev=916460&view=rev
Log:
Added forcestring attribute to equals condition

Added:
    ant/core/trunk/src/tests/antunit/taskdefs/condition/equals-test.xml   (with 
props)
Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTasks/conditions.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/Equals.java

Modified: ant/core/trunk/WHATSNEW
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=916460&r1=916459&r2=916460&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Thu Feb 25 21:13:27 2010
@@ -65,6 +65,11 @@
  * 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
 ======================================
 

Modified: ant/core/trunk/docs/manual/CoreTasks/conditions.html
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/conditions.html?rev=916460&r1=916459&r2=916460&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/conditions.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/conditions.html Thu Feb 25 21:13:27 
2010
@@ -138,7 +138,7 @@
 </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 @@
   </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 @@
         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>

Modified: 
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/Equals.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/Equals.java?rev=916460&r1=916459&r2=916460&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/Equals.java 
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/Equals.java 
Thu Feb 25 21:13:27 2010
@@ -32,6 +32,7 @@
     private boolean trim = false;
     private boolean caseSensitive = true;
     private int args;
+    private boolean forcestring = false;
 
     /**
      * Set the first argument
@@ -107,6 +108,16 @@
     }
 
     /**
+     * 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 @@
         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 @@
             String s2 = (String) arg2;
             return caseSensitive ? s1.equals(s2) : s1.equalsIgnoreCase(s2);
         }
-        return arg1 == arg2 || arg1 != null && arg1.equals(arg2);
+        return false;
     }
 }

Added: ant/core/trunk/src/tests/antunit/taskdefs/condition/equals-test.xml
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/condition/equals-test.xml?rev=916460&view=auto
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/condition/equals-test.xml (added)
+++ ant/core/trunk/src/tests/antunit/taskdefs/condition/equals-test.xml Thu Feb 
25 21:13:27 2010
@@ -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>

Propchange: ant/core/trunk/src/tests/antunit/taskdefs/condition/equals-test.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to