ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/proposal/sandbox/input/src/testcases/org/apache/tools/ant/taskdefs InputTest.java
Date Tue, 26 Feb 2002 10:29:29 GMT
bodewig     02/02/26 02:29:29

  Modified:    proposal/sandbox/input README build.xml
               proposal/sandbox/input/src/main/org/apache/tools/ant/input
                        DefaultInputHandler.java InputHandler.java
                        InputRequest.java MultipleChoiceInputRequest.java
                        PropertyFileInputHandler.java
               proposal/sandbox/input/src/testcases input.xml
  Added:       proposal/sandbox/input/src/etc/testcases/taskdefs
                        input.properties input.xml
               proposal/sandbox/input/src/testcases/org/apache/tools/ant/taskdefs
                        InputTest.java
  Log:
  Add explicit requirements to the handleInput method.
  
  Minor optimization of PropertyFileInputHandler.
  
  Use magic property name ant.input.properties instead of
  input.properties to conform with our unofficial magic property naming
  convention.
  
  Use valid choices in prompt (DefaultInputHandler only) if we have a
  MultipleChoiceInputRequest.
  
  Port old <input> testcase over to this proposal.
  
  Revision  Changes    Path
  1.2       +8 -2      jakarta-ant/proposal/sandbox/input/README
  
  Index: README
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/input/README,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- README	25 Feb 2002 16:10:15 -0000	1.1
  +++ README	26 Feb 2002 10:29:28 -0000	1.2
  @@ -46,11 +46,17 @@
   
   You'll get the ugliest dialog you've ever seen, but it works ;-)
   
  -Finally, use 
  +Use 
   
  -ANT_OPTS=-Dinput.properties=proposal/sandbox/input/src/testcases/works.properties
  +ANT_OPTS=-Dant.input.properties=proposal/sandbox/input/src/testcases/works.properties
   ant -f proposal/sandbox/input/src/testcases/input.xml -inputhandler org.apache.tools.ant.input.PropertyFileInputHandler
   
   to see the non-interactive build process in action.  fails.properties
   provides a sample of possible input failures.
   
  +The original testcase for <input> has been ported to the new framework
  +as well, run it via
  +
  +ant run-test
  +
  +in this directory.
  \ No newline at end of file
  
  
  
  1.2       +34 -0     jakarta-ant/proposal/sandbox/input/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/input/build.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- build.xml	25 Feb 2002 16:10:15 -0000	1.1
  +++ build.xml	26 Feb 2002 10:29:28 -0000	1.2
  @@ -23,4 +23,38 @@
         </fileset>
       </jar>
     </target>
  +
  +  <target name="setup-tests" depends="setup">
  +    <property name="testcases.dir" value="build/testcases" />
  +    <mkdir dir="${testcases.dir}" />
  +    <ant dir="${main.ant}" inheritall="false" target="compile-tests" />
  +    <copy toDir="${testcases.dir}" preservelastmodified="true" >
  +      <fileset dir="${main.ant}/${testcases.dir}">
  +	<include name='org/apache/tools/ant/BuildFileTest*.class' />
  +      </fileset>
  +    </copy>
  +  </target>
  +
  +  <target name="compile-tests" depends="setup-tests,main">
  +    <javac srcdir="src/testcases" destdir="${testcases.dir}"
  +           includeantruntime="false">
  +      <classpath>
  +        <pathelement location="build/ant.jar" />
  +        <pathelement location="${main.ant}/lib/optional/junit.jar" />
  +      </classpath>
  +    </javac>
  +  </target>
  +
  +  <target name="run-test" depends="compile-tests">
  +    <junit fork="true" filtertrace="false" includeantruntime="false">
  +      <classpath>
  +        <pathelement location="${testcases.dir}" />
  +        <pathelement location="build/ant.jar" />
  +        <pathelement location="${main.ant}/lib/crimson.jar" />
  +        <pathelement location="${main.ant}/lib/optional/junit.jar" />
  +      </classpath>
  +      <formatter type="plain" usefile="false" />
  +      <test name="org.apache.tools.ant.taskdefs.InputTest" />
  +    </junit>
  +  </target>
   </project>
  
  
  
  1.1                  jakarta-ant/proposal/sandbox/input/src/etc/testcases/taskdefs/input.properties
  
  Index: input.properties
  ===================================================================
  Press\ Return\ key\ to\ continue...=test
  All\ data\ is\ going\ to\ be\ deleted\ from\ DB\ continue?=test
  All\ data\ is\ going\ to\ be\ deleted\ from\ db\ continue\ (y/n)?=y
  Please\ enter\ db-username\:=scott
  
  
  
  1.1                  jakarta-ant/proposal/sandbox/input/src/etc/testcases/taskdefs/input.xml
  
  Index: input.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <project name="input-test" basedir="." default="test1">
  
    <target name="test1">
      <input>Press Return key to continue...</input>
    </target>
  
    <target name="test2">
      <input message="Press Return key to continue..." />
    </target>
  
    <target name="test3">
      <input message="All data is going to be deleted from DB continue?"
             validargs="y,n"
             />
    </target>
  
    <target name="test5">
      <input message="All data is going to be deleted from db continue (y/n)?"
             validargs="y,n"
             />
    </target>
  
    <target name="test6">
      <input message="Please enter db-username:"
             addproperty="db.user"
             />
    </target>
  
  </project>
  
  
  
  1.2       +22 -2     jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/DefaultInputHandler.java
  
  Index: DefaultInputHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/DefaultInputHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultInputHandler.java	25 Feb 2002 16:10:15 -0000	1.1
  +++ DefaultInputHandler.java	26 Feb 2002 10:29:29 -0000	1.2
  @@ -57,6 +57,7 @@
   import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStreamReader;
  +import java.util.Enumeration;
   
   import org.apache.tools.ant.BuildException;
   
  @@ -64,7 +65,7 @@
    * Prompts on System.out, reads input from System.in
    *
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public class DefaultInputHandler implements InputHandler {
       
  @@ -75,8 +76,27 @@
       }
   
       public void handleInput(InputRequest request) throws BuildException {
  +        String prompt = request.getPrompt();
  +        if (request instanceof MultipleChoiceInputRequest) {
  +            StringBuffer sb = new StringBuffer(prompt);
  +            sb.append("(");
  +            Enumeration enum = 
  +                ((MultipleChoiceInputRequest) request).getChoices().elements();
  +            boolean first = true;
  +            while (enum.hasMoreElements()) {
  +                if (first) {
  +                    first = false;
  +                } else {
  +                    sb.append(",");
  +                }
  +                sb.append(enum.nextElement());
  +            }
  +            sb.append(")");
  +            prompt = sb.toString();
  +        }
  +            
           do {
  -            System.out.println(request.getPrompt());
  +            System.out.println(prompt);
               try {
                   BufferedReader in = 
                       new BufferedReader(new InputStreamReader(System.in));
  
  
  
  1.2       +11 -1     jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputHandler.java
  
  Index: InputHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InputHandler.java	25 Feb 2002 16:10:15 -0000	1.1
  +++ InputHandler.java	26 Feb 2002 10:29:29 -0000	1.2
  @@ -58,9 +58,19 @@
    * Plugin to Ant to handle requests for user input.
    *
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public interface InputHandler {
  +
  +    /**
  +     * Handle the request encapsulated in the argument.
  +     *
  +     * <p>Precondition: the request.getPrompt will return a non-null
  +     * value.</p>
  +     *
  +     * <p>Postcondition: request.getInput will return a non-null
  +     * value, request.isInputValid will return true.</p>
  +     */
       void handleInput(InputRequest request) 
           throws org.apache.tools.ant.BuildException;
   }
  
  
  
  1.2       +8 -1      jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputRequest.java
  
  Index: InputRequest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputRequest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InputRequest.java	25 Feb 2002 16:10:15 -0000	1.1
  +++ InputRequest.java	26 Feb 2002 10:29:29 -0000	1.2
  @@ -58,13 +58,20 @@
    * Encapsulates an input request.
    *
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public class InputRequest {
       private String prompt;
       private String input;
   
  +    /**
  +     * @param prompt The prompt to show to the user.  Must not be null.
  +     */
       public InputRequest(String prompt) {
  +        if (prompt == null) {
  +            throw new IllegalArgumentException("prompt must not be null");
  +        }
  +        
           this.prompt = prompt;
       }
   
  
  
  
  1.2       +9 -1      jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java
  
  Index: MultipleChoiceInputRequest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MultipleChoiceInputRequest.java	25 Feb 2002 16:10:15 -0000	1.1
  +++ MultipleChoiceInputRequest.java	26 Feb 2002 10:29:29 -0000	1.2
  @@ -60,13 +60,21 @@
    * Encapsulates an input request.
    *
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public class MultipleChoiceInputRequest extends InputRequest {
       private Vector choices = new Vector();
   
  +    /**
  +     * @param prompt The prompt to show to the user.  Must not be null.
  +     * @param choices holds all input values that are allowed.
  +     *                Must not be null.
  +     */
       public MultipleChoiceInputRequest(String prompt, Vector choices) {
           super(prompt);
  +        if (choices == null) {
  +            throw new IllegalArgumentException("choices must not be null");
  +        }
           this.choices = choices;
       }
   
  
  
  
  1.2       +25 -19    jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java
  
  Index: PropertyFileInputHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/input/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PropertyFileInputHandler.java	25 Feb 2002 16:10:15 -0000	1.1
  +++ PropertyFileInputHandler.java	26 Feb 2002 10:29:29 -0000	1.2
  @@ -62,25 +62,27 @@
   
   /**
    * Reads input from a property file, the file name is read from the
  - * system property input.properties, the prompt is the key for input.
  + * system property ant.input.properties, the prompt is the key for input.
    *
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public class PropertyFileInputHandler implements InputHandler {
       private Properties props = null;
   
       /**
  +     * Name of the system property we expect to hold the file name.
  +     */
  +    public static final String FILE_NAME_KEY = "ant.input.properties";
  +
  +    /**
        * Empty no-arg constructor.
        */
       public PropertyFileInputHandler() {
       }
   
  -    public synchronized void handleInput(InputRequest request) 
  -        throws BuildException {
  -        if (props == null) {
  -            readProps();
  -        }
  +    public void handleInput(InputRequest request) throws BuildException {
  +        readProps();
           Object o = props.get(request.getPrompt());
           if (o == null) {
               throw new BuildException("Unable to find input for "
  @@ -93,18 +95,22 @@
           }
       }
   
  -    private void readProps() throws BuildException {
  -        String propsFile = System.getProperty("input.properties");
  -        if (propsFile == null) {
  -            throw new BuildException("System property input.properties for PropertyFileInputHandler
not set");
  -        }
  -        
  -        props = new Properties();
  -        
  -        try {
  -            props.load(new FileInputStream(propsFile));
  -        } catch (IOException e) {
  -            throw new BuildException("Couldn't load "+propsFile, e);
  +    private synchronized void readProps() throws BuildException {
  +        if (props == null) {
  +            String propsFile = System.getProperty(FILE_NAME_KEY);
  +            if (propsFile == null) {
  +                throw new BuildException("System property "
  +                                         + FILE_NAME_KEY
  +                                         + " for PropertyFileInputHandler not set");
  +            }
  +            
  +            props = new Properties();
  +            
  +            try {
  +                props.load(new FileInputStream(propsFile));
  +            } catch (IOException e) {
  +                throw new BuildException("Couldn't load "+propsFile, e);
  +            }
           }
       }
   
  
  
  
  1.2       +1 -1      jakarta-ant/proposal/sandbox/input/src/testcases/input.xml
  
  Index: input.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/input/src/testcases/input.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- input.xml	25 Feb 2002 16:10:16 -0000	1.1
  +++ input.xml	26 Feb 2002 10:29:29 -0000	1.2
  @@ -6,7 +6,7 @@
   
     <target name="multi">
       <input addproperty="multi" 
  -           message="This is a prompt (1 and 2 are valid)" 
  +           message="This is a prompt" 
              validargs="1,2" />
       <echo>$${multi} is ${multi}</echo>
     </target>
  
  
  
  1.1                  jakarta-ant/proposal/sandbox/input/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java
  
  Index: InputTest.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Ant", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.tools.ant.taskdefs;
  
  import org.apache.tools.ant.BuildFileTest;
  import org.apache.tools.ant.input.PropertyFileInputHandler;
  
  /**
   * @author Ulrich Schmidt <usch@usch.net>
   */
  public class InputTest extends BuildFileTest {
  
      public InputTest(String name) {
          super(name);
      }
  
      public void setUp() {
          System.getProperties().put(PropertyFileInputHandler.FILE_NAME_KEY,
                                     "src/etc/testcases/taskdefs/input.properties");
          configureProject("src/etc/testcases/taskdefs/input.xml");
          getProject().setInputHandler(new PropertyFileInputHandler());
      }
  
      public void test1() {
          executeTarget("test1");
      }
  
      public void test2() {
          executeTarget("test1");
      }
  
      public void test3() {
          try {
              executeTarget("test3");
              fail("Input for \"All data is going to be deleted from DB continue?\" should
be invalid");
          } catch (org.apache.tools.ant.BuildException e) {
              assertEquals("Found invalid input test for All data is going to be deleted from
DB continue?", 
                           e.getMessage());
          }
      }
  
      public void test5() {
          executeTarget("test5");
      }
  
      public void test6() {
          executeTarget("test6");
          assertEquals("scott", project.getProperty("db.user"));
      }
  
  }
  
  
  

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


Mime
View raw message