From f587706860d8dbeea5521a18041a7b7acdf4c1f7 Mon Sep 17 00:00:00 2001
From: Matthew Jason Benson
Date: Wed, 16 Jul 2008 17:32:44 +0000
Subject: [PATCH] supports an "output" Resource attribute as an
alternative to "file".
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@677355 13f79535-47bb-0310-9956-ffa450edef68
---
WHATSNEW | 2 +
docs/manual/CoreTasks/echo.html | 8 +-
.../org/apache/tools/ant/taskdefs/Echo.java | 37 +++--
src/tests/antunit/taskdefs/echo-test.xml | 148 ++++++++++++++++++
.../apache/tools/ant/taskdefs/EchoTest.java | 76 +++------
5 files changed, 204 insertions(+), 67 deletions(-)
create mode 100644 src/tests/antunit/taskdefs/echo-test.xml
diff --git a/WHATSNEW b/WHATSNEW
index 028d5e17d..71e4173d2 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -188,6 +188,8 @@ Other changes:
are not used by Ant's family of zip tasks.
Bugzilla report 45396.
+ * supports an "output" Resource attribute as an alternative to "file".
+
Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================
diff --git a/docs/manual/CoreTasks/echo.html b/docs/manual/CoreTasks/echo.html
index dedee6257..3fb396920 100644
--- a/docs/manual/CoreTasks/echo.html
+++ b/docs/manual/CoreTasks/echo.html
@@ -51,7 +51,13 @@ ignored
file |
the file to write the message to. |
- No |
+ Optionally one of these may be specified |
+
+
+ output |
+ the Resource
+ to write the message to (see note).
+ Since Ant 1.8 |
append |
diff --git a/src/main/org/apache/tools/ant/taskdefs/Echo.java b/src/main/org/apache/tools/ant/taskdefs/Echo.java
index f724293bc..29b6428f5 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Echo.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Echo.java
@@ -19,8 +19,8 @@
package org.apache.tools.ant.taskdefs;
import java.io.File;
-import java.io.FileWriter;
import java.io.IOException;
+import java.io.OutputStream;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
@@ -31,6 +31,9 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.types.LogLevel;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.resources.FileProvider;
+import org.apache.tools.ant.types.resources.FileResource;
/**
* Writes a message to the Ant logging facilities.
@@ -51,25 +54,25 @@ public class Echo extends Task {
protected int logLevel = Project.MSG_WARN;
// CheckStyle:VisibilityModifier ON
+ private Resource output;
+
/**
* Does the work.
*
* @exception BuildException if something goes wrong with the build
*/
public void execute() throws BuildException {
- if (file == null) {
+ if (output == null) {
log(message, logLevel);
} else {
Writer out = null;
try {
- String filename = file.getAbsolutePath();
- if (encoding == null || encoding.length() == 0) {
- out = new FileWriter(filename, append);
- } else {
- out = new BufferedWriter(
- new OutputStreamWriter(
- new FileOutputStream(filename, append), encoding));
- }
+ OutputStream os = output instanceof FileProvider ? os = new FileOutputStream(
+ ((FileProvider) output).getFile(), append) : output.getOutputStream();
+ OutputStreamWriter osw = (encoding == null || "".equals(encoding)) ? new OutputStreamWriter(
+ os)
+ : new OutputStreamWriter(os, encoding);
+ out = new BufferedWriter(osw);
out.write(message, 0, message.length());
} catch (IOException ioe) {
throw new BuildException(ioe, getLocation());
@@ -95,6 +98,20 @@ public class Echo extends Task {
*/
public void setFile(File file) {
this.file = file;
+ setOutput(new FileResource(getProject(), file));
+ }
+
+ /**
+ * Resource to write to.
+ * @param output the Resource to write to.
+ * @since Ant 1.8
+ */
+ public void setOutput(Resource output) {
+ if (this.output != null) {
+ throw new BuildException("Cannot set > 1 output target");
+ }
+ this.output = output;
+ this.file = output instanceof FileProvider ? ((FileProvider) output).getFile() : null;
}
/**
diff --git a/src/tests/antunit/taskdefs/echo-test.xml b/src/tests/antunit/taskdefs/echo-test.xml
new file mode 100644
index 000000000..e8c6411e2
--- /dev/null
+++ b/src/tests/antunit/taskdefs/echo-test.xml
@@ -0,0 +1,148 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ This
+ is
+ a
+ multiline
+ message
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Simple text
+
+
+
+
+
+
+
+
+
+
+
+ Simple text
+
+
+
+
+
+
+
+
+
+
+
+ Simple text
+ Appended
+
+
+
+
+
+
+
+
+
+
+
+
+ Simple text
+
+
+
+
+
+
+
+
+
+
+
+
+ ${char}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ${char}
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java
index 1cd92630b..05c519875 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java
@@ -21,78 +21,41 @@ package org.apache.tools.ant.taskdefs;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
-import org.apache.tools.ant.BuildFileTest;
+import junit.framework.TestCase;
+
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
/**
+ * Test Java-dependent parts of the Echo task.
*/
-public class EchoTest extends BuildFileTest {
+public class EchoTest extends TestCase {
+ /**
+ * Create a new EchoTest.
+ * @param name
+ */
public EchoTest(String name) {
super(name);
}
- public void setUp() {
- configureProject("src/etc/testcases/taskdefs/echo.xml");
- }
-
- public void tearDown() {
- executeTarget("clean");
- }
-
- // Output an empty String
- public void test1() {
- expectLog("test1", "");
- }
-
public void testLogBlankEcho() {
+ Project p = new Project();
+ p.init();
EchoTestLogger logger = new EchoTestLogger();
- getProject().addBuildListener(logger);
- getProject().executeTarget("test1");
- assertEquals(" [echo] ", logger.lastLoggedMessage );
+ p.addBuildListener(logger);
+ Echo echo = new Echo();
+ echo.setProject(p);
+ echo.setTaskName("testLogBlankEcho");
+ echo.execute();
+ assertEquals("[testLogBlankEcho] ", logger.lastLoggedMessage );
}
- // Output 'OUTPUT OF ECHO'
- public void test2() {
- expectLog("test2", "OUTPUT OF ECHO");
- }
-
- public void test3() {
- expectLog("test3", "\n"+
- " This \n"+
- " is\n"+
- " a \n"+
- " multiline\n"+
- " message\n"+
- " ");
- }
-
- public void testFile() throws Exception {
- executeTarget("testFile");
- }
-
- public void testAppend() throws Exception {
- executeTarget("testAppend");
- }
-
- public void testEmptyEncoding() throws Exception {
- executeTarget("testEmptyEncoding");
- }
-
- public void testUTF16Encoding() throws Exception {
- executeTarget("testUTF16Encoding");
- }
- public void testUTF8Encoding() throws Exception {
- executeTarget("testUTF8Encoding");
- }
-
private class EchoTestLogger extends DefaultLogger {
String lastLoggedMessage;
-
/**
- *
+ * Create a new EchoTestLogger.
*/
public EchoTestLogger() {
super();
@@ -100,8 +63,9 @@ public class EchoTest extends BuildFileTest {
this.setOutputPrintStream(new PrintStream(new ByteArrayOutputStream(256)));
this.setErrorPrintStream(new PrintStream(new ByteArrayOutputStream(256)));
}
- /*
- * @param message
+
+ /**
+ * {@inheritDoc}
*/
protected void log(String message) {
this.lastLoggedMessage = message;