You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by pe...@apache.org on 2004/12/01 12:25:47 UTC

cvs commit: ant/src/testcases/org/apache/tools/ant/taskdefs/condition IsFileSelectedTest.java

peterreilly    2004/12/01 03:25:47

  Modified:    .        WHATSNEW
               docs/manual/CoreTasks conditions.html
  Added:       src/main/org/apache/tools/ant/taskdefs/condition
                        IsFileSelected.java
               src/main/org/apache/tools/ant/types/selectors
                        AbstractSelectorContainer.java
               src/etc/testcases/taskdefs/conditions isfileselected.xml
               src/testcases/org/apache/tools/ant/taskdefs/condition
                        IsFileSelectedTest.java
  Log:
  added isfileselected test
  
  Revision  Changes    Path
  1.690     +2 -0      ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/ant/WHATSNEW,v
  retrieving revision 1.689
  retrieving revision 1.690
  diff -u -r1.689 -r1.690
  --- WHATSNEW	1 Dec 2004 11:16:41 -0000	1.689
  +++ WHATSNEW	1 Dec 2004 11:25:46 -0000	1.690
  @@ -95,6 +95,8 @@
   * Added preserveLastModified attribute to signjar task.
     Bugzilla report 30987.
   
  +* Added isfileselected condition.
  +
   Changes from Ant 1.6.2 to current Ant 1.6 CVS version
   =====================================================
   
  
  
  
  1.1                  ant/src/main/org/apache/tools/ant/taskdefs/condition/IsFileSelected.java
  
  Index: IsFileSelected.java
  ===================================================================
  /*
   * Copyright  2004 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 org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.util.FileUtils;
  import java.io.File;
  import org.apache.tools.ant.types.selectors.FileSelector;
  import org.apache.tools.ant.types.selectors.AbstractSelectorContainer;
  
  /**
   * This is a condition that checks to see if a file passes an embedded selector.
   */
  public class IsFileSelected extends AbstractSelectorContainer implements Condition {
      private static final FileUtils FILE_UTILS = FileUtils.newFileUtils();
      private File file;
      private File baseDir;
  
      /**
       * The file to check.
       * @param file the file to check if if passes the embedded selector.
       */
      public void setFile(File file) {
          this.file = file;
      }
  
      /**
       * The base directory to use.
       * @param baseDir the base directory to use, if null use the project's
       *                basedir.
       */
      public void setBaseDir(File baseDir) {
          this.baseDir = baseDir;
      }
  
      /**
       * validate the parameters.
       */
      public void validate() {
          if (selectorCount() != 1) {
              throw new BuildException("Only one selector allowed");
          }
          super.validate();
      }
  
      /**
       * Evaluate the selector with the file.
       * @return true if the file is selected by the embedded selector.
       */
      public boolean eval() {
          if (file == null) {
              throw new BuildException("file attribute not set");
          }
          validate();
          File myBaseDir = baseDir;
          if (myBaseDir == null) {
              myBaseDir = getProject().getBaseDir();
          }
  
          FileSelector f = getSelectors(getProject())[0];
          return f.isSelected(
              myBaseDir, FILE_UTILS.removeLeadingPath(myBaseDir, file), file);
      }
  }
  
  
  
  1.1                  ant/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java
  
  Index: AbstractSelectorContainer.java
  ===================================================================
  /*
   * Copyright  2002-2004 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.types.selectors;
  
  import java.io.File;
  import java.util.Enumeration;
  import java.util.Vector;
  
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.types.DataType;
  import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;
  
  /**
   * This is the a base class a container of selectors - it does
   * not need do be a selector itself.
   *
   * @since 1.7
   */
  public abstract class AbstractSelectorContainer extends DataType
          implements SelectorContainer {
  
      private Vector selectorsList = new Vector();
  
      /**
       * Indicates whether there are any selectors here.
       * @return true if there are selectors
       */
      public boolean hasSelectors() {
          return !(selectorsList.isEmpty());
      }
  
      /**
       * Gives the count of the number of selectors in this container
       * @return the number of selectors
       */
      public int selectorCount() {
          return selectorsList.size();
      }
  
      /**
       * Returns the set of selectors as an array.
       * @param p the current project
       * @return an array of selectors
       */
      public FileSelector[] getSelectors(Project p) {
          FileSelector[] result = new FileSelector[selectorsList.size()];
          selectorsList.copyInto(result);
          return result;
      }
  
      /**
       * Returns an enumerator for accessing the set of selectors.
       * @return an enumerator for the selectors
       */
      public Enumeration selectorElements() {
          return selectorsList.elements();
      }
  
      /**
       * Convert the Selectors within this container to a string. This will
       * just be a helper class for the subclasses that put their own name
       * around the contents listed here.
       *
       * @return comma separated list of Selectors contained in this one
       */
      public String toString() {
          StringBuffer buf = new StringBuffer();
          Enumeration e = selectorElements();
          if (e.hasMoreElements()) {
              while (e.hasMoreElements()) {
                  buf.append(e.nextElement().toString());
                  if (e.hasMoreElements()) {
                      buf.append(", ");
                  }
              }
          }
  
          return buf.toString();
      }
  
      /**
       * Add a new selector into this container.
       *
       * @param selector the new selector to add
       */
      public void appendSelector(FileSelector selector) {
          selectorsList.addElement(selector);
      }
  
      /**
       * <p>
       * This validates each contained selector
       * provided that the selector implements the validate interface.
       * </p>
       * <p>Ordinarily, this will validate all the elements of a selector
       * container even if the isSelected() method of some elements is
       * never called. This has two effects:</p>
       * <ul>
       * <li>Validation will often occur twice.
       * <li>Since it is not required that selectors derive from
       * BaseSelector, there could be selectors in the container whose
       * error conditions are not detected if their isSelected() call
       * is never made.
       * </ul>
       */
      public void validate() {
          Enumeration e = selectorElements();
          while (e.hasMoreElements()) {
              Object o = e.nextElement();
              if (o instanceof BaseSelector) {
                  ((BaseSelector) o).validate();
              }
          }
      }
  
  
      /* Methods below all add specific selectors */
  
      /**
       * add a "Select" selector entry on the selector list
       * @param selector the selector to add
       */
      public void addSelector(SelectSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add an "And" selector entry on the selector list
       * @param selector the selector to add
       */
      public void addAnd(AndSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add an "Or" selector entry on the selector list
       * @param selector the selector to add
       */
      public void addOr(OrSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a "Not" selector entry on the selector list
       * @param selector the selector to add
       */
      public void addNot(NotSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a "None" selector entry on the selector list
       * @param selector the selector to add
       */
      public void addNone(NoneSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a majority selector entry on the selector list
       * @param selector the selector to add
       */
      public void addMajority(MajoritySelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a selector date entry on the selector list
       * @param selector the selector to add
       */
      public void addDate(DateSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a selector size entry on the selector list
       * @param selector the selector to add
       */
      public void addSize(SizeSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a selector filename entry on the selector list
       * @param selector the selector to add
       */
      public void addFilename(FilenameSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add an extended selector entry on the selector list
       * @param selector the selector to add
       */
      public void addCustom(ExtendSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a contains selector entry on the selector list
       * @param selector the selector to add
       */
      public void addContains(ContainsSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a present selector entry on the selector list
       * @param selector the selector to add
       */
      public void addPresent(PresentSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a depth selector entry on the selector list
       * @param selector the selector to add
       */
      public void addDepth(DepthSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a depends selector entry on the selector list
       * @param selector the selector to add
       */
      public void addDepend(DependSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * adds a different selector to the selector list
       * @param selector the selector to add
       */
      public void addDifferent(DifferentSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * adds a type selector to the selector list
       * @param selector the selector to add
       */
      public void addType(TypeSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add a regular expression selector entry on the selector list
       * @param selector the selector to add
       */
      public void addContainsRegexp(ContainsRegexpSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add the modified selector
       * @param selector the selector to add
       * @since ant 1.6
       */
      public void addModified(ModifiedSelector selector) {
          appendSelector(selector);
      }
  
      /**
       * add an arbitary selector
       * @param selector the selector to add
       * @since Ant 1.6
       */
      public void add(FileSelector selector) {
          appendSelector(selector);
      }
  
  }
  
  
  
  1.24      +36 -0     ant/docs/manual/CoreTasks/conditions.html
  
  Index: conditions.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/CoreTasks/conditions.html,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- conditions.html	26 Nov 2004 13:42:43 -0000	1.23
  +++ conditions.html	1 Dec 2004 11:25:46 -0000	1.24
  @@ -356,6 +356,42 @@
       <td valign="top" align="center">No</td>
     </tr>
     </table>
  +  <h4>isfileselected</h4>
  +  <p>
  +    Test whether a file passes an embedded selector.
  +  </p>
  +  <p>
  +    This condition has been added in Apache Ant 1.7.
  +  </p>
  +  <table border="1" cellpadding="2" cellspacing="0">
  +  <tr>
  +    <td valign="top"><b>Attribute</b></td>
  +    <td valign="top"><b>Description</b></td>
  +    <td align="center" valign="top"><b>Required</b></td>
  +  </tr>
  +  <tr>
  +    <td valign="top">file</td>
  +    <td valign="top">
  +      The file to check if is passes the embedded selector.
  +    </td>
  +    <td valign="top" align="center">Yes</td>
  +  </tr>
  +  <tr>
  +    <td valign="top">basedir</td>
  +    <td valign="top">The base directory to use for name based selectors. It this is not set,
  +      the project's basedirectory will be used.</td>
  +    <td valign="top" align="center">No</td>
  +  </tr>
  +  </table>
  +  <p>
  +    Example usage:
  +  </p>
  +  <blockquote>
  +  <pre>
  +    &lt;isfileselected file="a.xml"&gt;
  +      &lt;date datetime="06/28/2000 2:02 pm" when="equal"/&gt;
  +    &lt;/isfileselected&gt;
  +  </pre></blockquote>
   <h4>typefound</h4>
   
   <p>Test whether a given type is defined, and that
  
  
  
  1.1                  ant/src/etc/testcases/taskdefs/conditions/isfileselected.xml
  
  Index: isfileselected.xml
  ===================================================================
  <project>
    <macrodef name="pass">
      <element name="conditions" implicit="yes"/>
      <attribute name="failmessage"/>
      <sequential>
        <fail message="@{failmessage}">
          <condition>
            <not>
              <conditions/>
            </not>
          </condition>
        </fail>
      </sequential>
    </macrodef>
  
    <target name="simple">
      <pass failmessage="a simple test">
        <isfileselected file="jars/pass.jar">
          <signedselector/>
        </isfileselected>
      </pass>
    </target>
  
    <target name="name">
      <pass failmessage="name did not match">
        <isfileselected file="jars/nosign.jar">
          <filename name="jars/nosign.jar"/>
        </isfileselected>
      </pass>
    </target>
  
    <target name="basedir">
      <pass failmessage="name did not match with a basedir change">
        <isfileselected file="jars/nosign.jar" basedir="jars">
          <filename name="nosign.jar"/>
        </isfileselected>
      </pass>
    </target>
  
    <target name="type">
      <pass failmessage="type selector did not work">
        <isfileselected file="isfileselected.xml">
          <type type="file"/>
        </isfileselected>
      </pass>
    </target>
  
    <target name="not.selector">
      <fileset dir=".">
        <isfileselected file="nosigned.jar"/>
      </fileset>
    </target>
  </project>
  
  
  
  1.1                  ant/src/testcases/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java
  
  Index: IsFileSelectedTest.java
  ===================================================================
  /*
   * Copyright  2004 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 org.apache.tools.ant.BuildFileTest;
  
  /**
   * Testcase for the &lt;isfileselected&gt; condition.
   *
   */
  public class IsFileSelectedTest extends BuildFileTest {
  
      public IsFileSelectedTest(String name) {
          super(name);
      }
      public void setUp() {
          configureProject("src/etc/testcases/taskdefs/conditions/isfileselected.xml");
      }
  
      public void testSimple() {
          executeTarget("simple");
      }
      public void testName() {
          executeTarget("name");
      }
      public void testBaseDir() {
          executeTarget("basedir");
      }
      public void testType() {
          executeTarget("type");
      }
      public void testNotSelector() {
          expectBuildExceptionContaining(
              "not.selector", "checking for use as a selector (not allowed)",
              "fileset doesn't support the nested \"isfile");
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org