diff --git a/WHATSNEW b/WHATSNEW index 04bfd98d3..16bfdf1d2 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -335,6 +335,8 @@ Other changes: * add textfile attribute to the filesmatch condition. +* new resourcesmatch condition. + Changes from Ant 1.6.4 to Ant 1.6.5 =================================== diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index 86c682bee..ed01b5f8b 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -221,7 +221,8 @@ TCP/IP listener at the specified host and port.

filesmatch

-

Test two files for matching. Nonexistence of either file results in "false". +

Test two files for matching. Nonexistence of one file results in "false", +although if neither exists they are considered equal in terms of content. This test does a byte for byte comparision, so test time scales with byte size. NB: if the files are different sizes, one of them is missing or the filenames match the answer is so obvious the detailed test is omitted. @@ -245,8 +246,9 @@ or the filenames match the answer is so obvious the detailed test is omitted. textfile - Whether to ignore line endings when comparing files; - default false + Whether to ignore line endings + when comparing files; defaults to false, while + true bypasses the size comparison. No @@ -676,6 +678,34 @@ Probe for the maven repository being reachable using the hostname, ten second ti <resourcecount refid="myresourcecollection" when="greater" length="0" />

Verify that a resource collection is not empty.

+ +

resourcesmatch

+

Test resources for matching. Nonexistence of one or more resources results in +"false", although if none exists they are considered equal in terms of content. +By default this test does a byte for byte comparision, so test time scales with +byte size. NB: if the files are different sizes, one of them is missing +or the filenames match the answer is so obvious the detailed test is omitted. +The resources to check are specified as nested +resource collections, +meaning that more than two resources can be checked; in this case all resources +must match. Since Ant 1.7 +

+ + + + + + + + + + + +
AttributeDescriptionRequired
astextWhether to ignore line endings + when comparing resource content; defaults to false, + while true bypasses the size comparison. + No
+

Copyright © 2001-2005 Apache Software Foundation. All rights Reserved.

diff --git a/src/etc/testcases/taskdefs/condition.xml b/src/etc/testcases/taskdefs/condition.xml index 2cb092b3d..60cc94b6a 100644 --- a/src/etc/testcases/taskdefs/condition.xml +++ b/src/etc/testcases/taskdefs/condition.xml @@ -185,6 +185,16 @@ ${filesmatch-existence} + + + + + + + + + + @@ -395,6 +405,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/ResourcesMatch.java b/src/main/org/apache/tools/ant/taskdefs/condition/ResourcesMatch.java new file mode 100644 index 000000000..b4a2ffa35 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/condition/ResourcesMatch.java @@ -0,0 +1,92 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed 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. + * + */ + package org.apache.tools.ant.taskdefs.condition; + +import java.io.IOException; +import java.util.Iterator; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.ResourceCollection; +import org.apache.tools.ant.types.resources.Union; +import org.apache.tools.ant.util.ResourceUtils; + +/** + * Compares resources for equality based on size and content. + * All resources specified must match; no resource collections + * specified is an error condition; if resource collections are + * specified, but yield fewer than two elements, the condition + * evaluates to true. + * @since Ant 1.7 + */ +public class ResourcesMatch implements Condition { + + private Union resources = null; + private boolean asText = false; + + /** + * Set whether to treat resources as if they were text files, + * ignoring line endings. + * @param astext whether to ignore line endings. + */ + public void setAsText(boolean asText) { + this.asText = asText; + } + + /** + * Add a resource collection. + * @param rc the resource collection to add. + */ + public void add(ResourceCollection rc) { + if (rc == null) { + return; + } + resources = resources == null ? new Union() : resources; + resources.add(rc); + } + + /** + * Verify that all resources match. + * @return true if all resources are equal. + * @exception BuildException if there is an error. + */ + public boolean eval() throws BuildException { + if (resources == null) { + throw new BuildException( + "You must specify one or more nested resource collections"); + } + if (resources.size() > 1) { + Iterator i = resources.iterator(); + Resource r1 = (Resource) i.next(); + Resource r2 = null; + + while (i.hasNext()) { + r2 = (Resource) i.next(); + try { + if (!ResourceUtils.contentEquals(r1, r2, asText)) { + return false; + } + } catch (IOException ioe) { + throw new BuildException("when comparing resources " + + r1.toString() + " and " + r2.toString(), ioe); + } + r1 = r2; + } + } + return true; + } +} diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index e1170eaab..a876a143f 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -4,6 +4,21 @@ filterreader=org.apache.tools.ant.types.AntFilterReader filterset=org.apache.tools.ant.types.FilterSet mapper=org.apache.tools.ant.types.Mapper redirector=org.apache.tools.ant.types.RedirectorElement +patternset=org.apache.tools.ant.types.PatternSet +regexp=org.apache.tools.ant.types.RegularExpression +substitution=org.apache.tools.ant.types.Substitution +xmlcatalog=org.apache.tools.ant.types.XMLCatalog +extensionSet=org.apache.tools.ant.taskdefs.optional.extension.ExtensionSet +extension=org.apache.tools.ant.taskdefs.optional.extension.ExtensionAdapter +selector=org.apache.tools.ant.types.selectors.SelectSelector +signedselector=org.apache.tools.ant.types.selectors.SignedSelector +scriptfilter=org.apache.tools.ant.types.optional.ScriptFilter +assertions=org.apache.tools.ant.types.Assertions +concatfilter=org.apache.tools.ant.filters.ConcatFilter +mavenrepository=org.apache.tools.ant.taskdefs.repository.MavenRepository +scriptselector=org.apache.tools.ant.types.optional.ScriptSelector +scriptmapper=org.apache.tools.ant.types.optional.ScriptMapper + # different filename mappers identitymapper=org.apache.tools.ant.util.IdentityMapper flattenmapper=org.apache.tools.ant.util.FlatFileNameMapper @@ -16,27 +31,15 @@ compositemapper=org.apache.tools.ant.util.CompositeMapper chainedmapper=org.apache.tools.ant.util.ChainedMapper filtermapper=org.apache.tools.ant.types.mappers.FilterMapper -patternset=org.apache.tools.ant.types.PatternSet -regexp=org.apache.tools.ant.types.RegularExpression -substitution=org.apache.tools.ant.types.Substitution -xmlcatalog=org.apache.tools.ant.types.XMLCatalog -extensionSet=org.apache.tools.ant.taskdefs.optional.extension.ExtensionSet -extension=org.apache.tools.ant.taskdefs.optional.extension.ExtensionAdapter -selector=org.apache.tools.ant.types.selectors.SelectSelector -signedselector=org.apache.tools.ant.types.selectors.SignedSelector -scriptfilter=org.apache.tools.ant.types.optional.ScriptFilter -assertions=org.apache.tools.ant.types.Assertions -concatfilter=org.apache.tools.ant.filters.ConcatFilter +#conditions: issigned=org.apache.tools.ant.taskdefs.condition.IsSigned isfileselected=org.apache.tools.ant.taskdefs.condition.IsFileSelected isreachable=org.apache.tools.ant.taskdefs.condition.IsReachable -mavenrepository=org.apache.tools.ant.taskdefs.repository.MavenRepository -scriptselector=org.apache.tools.ant.types.optional.ScriptSelector scriptcondition=org.apache.tools.ant.types.optional.ScriptCondition xor=org.apache.tools.ant.taskdefs.condition.Xor parsersupports=org.apache.tools.ant.taskdefs.condition.ParserSupports -scriptmapper=org.apache.tools.ant.types.optional.ScriptMapper isfailure=org.apache.tools.ant.taskdefs.condition.IsFailure +resourcesmatch=org.apache.tools.ant.taskdefs.condition.ResourcesMatch #ResourceCollections: dirset=org.apache.tools.ant.types.DirSet diff --git a/src/testcases/org/apache/tools/ant/taskdefs/ConditionTest.java b/src/testcases/org/apache/tools/ant/taskdefs/ConditionTest.java index 744f029ea..ba436555d 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/ConditionTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/ConditionTest.java @@ -170,6 +170,10 @@ public class ConditionTest extends BuildFileTest { executeTarget("filesmatch-same-eol"); } + public void testFilesmatchNeitherExist() { + executeTarget("filesmatch-neitherexist"); + } + public void testContains() { expectPropertySet("contains","contains"); } @@ -237,5 +241,37 @@ public class ConditionTest extends BuildFileTest { public void testElse() { executeTarget("testElse"); } -} + public void testResourcesmatchError() { + expectBuildException("resourcesmatch-error", + "should fail because no resources specified"); + } + + public void testResourcesmatchEmpty() { + executeTarget("resourcesmatch-match-empty"); + } + + public void testResourcesmatchOne() { + executeTarget("resourcesmatch-match-one"); + } + + public void testResourcesmatchBinary() { + executeTarget("resourcesmatch-match-binary"); + } + + public void testResourcesmatchMultipleBinary() { + executeTarget("resourcesmatch-match-multiple-binary"); + } + + public void testResourcesmatchDiffer() { + executeTarget("resourcesmatch-differ"); + } + + public void testResourcesmatchText() { + executeTarget("resourcesmatch-match-text"); + } + + public void testResourcesmatchNoneExist() { + executeTarget("resourcesmatch-noneexist"); + } +}