Browse Source

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.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271557 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
fdcbd434d8
11 changed files with 251 additions and 22 deletions
  1. +8
    -2
      proposal/sandbox/input/README
  2. +34
    -0
      proposal/sandbox/input/build.xml
  3. +4
    -0
      proposal/sandbox/input/src/etc/testcases/taskdefs/input.properties
  4. +31
    -0
      proposal/sandbox/input/src/etc/testcases/taskdefs/input.xml
  5. +21
    -1
      proposal/sandbox/input/src/main/org/apache/tools/ant/input/DefaultInputHandler.java
  6. +10
    -0
      proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputHandler.java
  7. +7
    -0
      proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputRequest.java
  8. +8
    -0
      proposal/sandbox/input/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java
  9. +24
    -18
      proposal/sandbox/input/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java
  10. +1
    -1
      proposal/sandbox/input/src/testcases/input.xml
  11. +103
    -0
      proposal/sandbox/input/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java

+ 8
- 2
proposal/sandbox/input/README View File

@@ -46,11 +46,17 @@ ant -f proposal/sandbox/input/src/testcases/input.xml -inputhandler org.apache.t


You'll get the ugliest dialog you've ever seen, but it works ;-) 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 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 to see the non-interactive build process in action. fails.properties
provides a sample of possible input failures. 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.

+ 34
- 0
proposal/sandbox/input/build.xml View File

@@ -23,4 +23,38 @@
</fileset> </fileset>
</jar> </jar>
</target> </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> </project>

+ 4
- 0
proposal/sandbox/input/src/etc/testcases/taskdefs/input.properties View File

@@ -0,0 +1,4 @@
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

+ 31
- 0
proposal/sandbox/input/src/etc/testcases/taskdefs/input.xml View File

@@ -0,0 +1,31 @@
<?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>

+ 21
- 1
proposal/sandbox/input/src/main/org/apache/tools/ant/input/DefaultInputHandler.java View File

@@ -57,6 +57,7 @@ package org.apache.tools.ant.input;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Enumeration;


import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;


@@ -75,8 +76,27 @@ public class DefaultInputHandler implements InputHandler {
} }


public void handleInput(InputRequest request) throws BuildException { 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 { do {
System.out.println(request.getPrompt());
System.out.println(prompt);
try { try {
BufferedReader in = BufferedReader in =
new BufferedReader(new InputStreamReader(System.in)); new BufferedReader(new InputStreamReader(System.in));


+ 10
- 0
proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputHandler.java View File

@@ -61,6 +61,16 @@ package org.apache.tools.ant.input;
* @version $Revision$ * @version $Revision$
*/ */
public interface InputHandler { 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) void handleInput(InputRequest request)
throws org.apache.tools.ant.BuildException; throws org.apache.tools.ant.BuildException;
} }

+ 7
- 0
proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputRequest.java View File

@@ -64,7 +64,14 @@ public class InputRequest {
private String prompt; private String prompt;
private String input; private String input;


/**
* @param prompt The prompt to show to the user. Must not be null.
*/
public InputRequest(String prompt) { public InputRequest(String prompt) {
if (prompt == null) {
throw new IllegalArgumentException("prompt must not be null");
}
this.prompt = prompt; this.prompt = prompt;
} }




+ 8
- 0
proposal/sandbox/input/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java View File

@@ -65,8 +65,16 @@ import java.util.Vector;
public class MultipleChoiceInputRequest extends InputRequest { public class MultipleChoiceInputRequest extends InputRequest {
private Vector choices = new Vector(); 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) { public MultipleChoiceInputRequest(String prompt, Vector choices) {
super(prompt); super(prompt);
if (choices == null) {
throw new IllegalArgumentException("choices must not be null");
}
this.choices = choices; this.choices = choices;
} }




+ 24
- 18
proposal/sandbox/input/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java View File

@@ -62,7 +62,7 @@ import java.util.Properties;


/** /**
* Reads input from a property file, the file name is read from the * 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> * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
* @version $Revision$ * @version $Revision$
@@ -70,17 +70,19 @@ import java.util.Properties;
public class PropertyFileInputHandler implements InputHandler { public class PropertyFileInputHandler implements InputHandler {
private Properties props = null; 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. * Empty no-arg constructor.
*/ */
public PropertyFileInputHandler() { 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()); Object o = props.get(request.getPrompt());
if (o == null) { if (o == null) {
throw new BuildException("Unable to find input for " throw new BuildException("Unable to find input for "
@@ -93,18 +95,22 @@ public class PropertyFileInputHandler implements InputHandler {
} }
} }


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
- 1
proposal/sandbox/input/src/testcases/input.xml View File

@@ -6,7 +6,7 @@


<target name="multi"> <target name="multi">
<input addproperty="multi" <input addproperty="multi"
message="This is a prompt (1 and 2 are valid)"
message="This is a prompt"
validargs="1,2" /> validargs="1,2" />
<echo>$${multi} is ${multi}</echo> <echo>$${multi} is ${multi}</echo>
</target> </target>


+ 103
- 0
proposal/sandbox/input/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java View File

@@ -0,0 +1,103 @@
/*
* 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"));
}

}

Loading…
Cancel
Save