diff --git a/proposal/sandbox/input/README b/proposal/sandbox/input/README index 9f3870d56..43c3c2d36 100644 --- a/proposal/sandbox/input/README +++ b/proposal/sandbox/input/README @@ -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 ;-) -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 has been ported to the new framework +as well, run it via + +ant run-test + +in this directory. \ No newline at end of file diff --git a/proposal/sandbox/input/build.xml b/proposal/sandbox/input/build.xml index f7364ad38..89968af12 100644 --- a/proposal/sandbox/input/build.xml +++ b/proposal/sandbox/input/build.xml @@ -23,4 +23,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/proposal/sandbox/input/src/etc/testcases/taskdefs/input.properties b/proposal/sandbox/input/src/etc/testcases/taskdefs/input.properties new file mode 100644 index 000000000..c6981c720 --- /dev/null +++ b/proposal/sandbox/input/src/etc/testcases/taskdefs/input.properties @@ -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 diff --git a/proposal/sandbox/input/src/etc/testcases/taskdefs/input.xml b/proposal/sandbox/input/src/etc/testcases/taskdefs/input.xml new file mode 100644 index 000000000..a2ec247fd --- /dev/null +++ b/proposal/sandbox/input/src/etc/testcases/taskdefs/input.xml @@ -0,0 +1,31 @@ + + + + + + Press Return key to continue... + + + + + + + + + + + + + + + + + + + diff --git a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/DefaultInputHandler.java b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/DefaultInputHandler.java index e1603cf79..b95085767 100644 --- a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/DefaultInputHandler.java +++ b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/DefaultInputHandler.java @@ -57,6 +57,7 @@ package org.apache.tools.ant.input; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.util.Enumeration; import org.apache.tools.ant.BuildException; @@ -75,8 +76,27 @@ public class DefaultInputHandler implements InputHandler { } 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)); diff --git a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputHandler.java b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputHandler.java index 77fcc93b0..5fb55e564 100644 --- a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputHandler.java +++ b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputHandler.java @@ -61,6 +61,16 @@ package org.apache.tools.ant.input; * @version $Revision$ */ public interface InputHandler { + + /** + * Handle the request encapsulated in the argument. + * + *

Precondition: the request.getPrompt will return a non-null + * value.

+ * + *

Postcondition: request.getInput will return a non-null + * value, request.isInputValid will return true.

+ */ void handleInput(InputRequest request) throws org.apache.tools.ant.BuildException; } diff --git a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputRequest.java b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputRequest.java index 8a0226bf2..f1fbfad43 100644 --- a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputRequest.java +++ b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/InputRequest.java @@ -64,7 +64,14 @@ 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; } diff --git a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java index 5d419fb0f..5163239a1 100644 --- a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java +++ b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java @@ -65,8 +65,16 @@ import java.util.Vector; 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; } diff --git a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java index 17a82961d..516a9ffcd 100644 --- a/proposal/sandbox/input/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java +++ b/proposal/sandbox/input/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java @@ -62,7 +62,7 @@ import java.util.Properties; /** * 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 Stefan Bodewig * @version $Revision$ @@ -70,17 +70,19 @@ import java.util.Properties; 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 @@ 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); + } } } diff --git a/proposal/sandbox/input/src/testcases/input.xml b/proposal/sandbox/input/src/testcases/input.xml index 72eda374b..1ae58cd4d 100644 --- a/proposal/sandbox/input/src/testcases/input.xml +++ b/proposal/sandbox/input/src/testcases/input.xml @@ -6,7 +6,7 @@ $${multi} is ${multi} diff --git a/proposal/sandbox/input/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java b/proposal/sandbox/input/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java new file mode 100644 index 000000000..51f6e4e4a --- /dev/null +++ b/proposal/sandbox/input/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java @@ -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 + * . + */ + +package org.apache.tools.ant.taskdefs; + +import org.apache.tools.ant.BuildFileTest; +import org.apache.tools.ant.input.PropertyFileInputHandler; + +/** + * @author Ulrich Schmidt + */ +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")); + } + +}