git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@677355 13f79535-47bb-0310-9956-ffa450edef68master
@@ -188,6 +188,8 @@ Other changes: | |||||
are not used by Ant's family of zip tasks. | are not used by Ant's family of zip tasks. | ||||
Bugzilla report 45396. | Bugzilla report 45396. | ||||
* <echo> supports an "output" Resource attribute as an alternative to "file". | |||||
Changes from Ant 1.7.0 TO Ant 1.7.1 | Changes from Ant 1.7.0 TO Ant 1.7.1 | ||||
============================================= | ============================================= | ||||
@@ -51,7 +51,13 @@ ignored</p> | |||||
<tr> | <tr> | ||||
<td valign="top">file</td> | <td valign="top">file</td> | ||||
<td valign="top">the file to write the message to.</td> | <td valign="top">the file to write the message to.</td> | ||||
<td valign="top" align="center">No</td> | |||||
<td valign="top" align="center" rowspan="2">Optionally one of these may be specified</td> | |||||
</tr> | |||||
<tr> | |||||
<td valign="top">output</td> | |||||
<td valign="top">the <a href="../CoreTypes/resources.html">Resource</a> | |||||
to write the message to (see <a href="../develop.html#set-magic">note</a>). | |||||
<b>Since Ant 1.8</b></td> | |||||
</tr> | </tr> | ||||
<tr> | <tr> | ||||
<td valign="top">append</td> | <td valign="top">append</td> | ||||
@@ -19,8 +19,8 @@ | |||||
package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
import java.io.File; | import java.io.File; | ||||
import java.io.FileWriter; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.OutputStream; | |||||
import java.io.Writer; | import java.io.Writer; | ||||
import java.io.BufferedWriter; | import java.io.BufferedWriter; | ||||
import java.io.OutputStreamWriter; | 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.Task; | ||||
import org.apache.tools.ant.util.FileUtils; | import org.apache.tools.ant.util.FileUtils; | ||||
import org.apache.tools.ant.types.LogLevel; | 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. | * Writes a message to the Ant logging facilities. | ||||
@@ -51,25 +54,25 @@ public class Echo extends Task { | |||||
protected int logLevel = Project.MSG_WARN; | protected int logLevel = Project.MSG_WARN; | ||||
// CheckStyle:VisibilityModifier ON | // CheckStyle:VisibilityModifier ON | ||||
private Resource output; | |||||
/** | /** | ||||
* Does the work. | * Does the work. | ||||
* | * | ||||
* @exception BuildException if something goes wrong with the build | * @exception BuildException if something goes wrong with the build | ||||
*/ | */ | ||||
public void execute() throws BuildException { | public void execute() throws BuildException { | ||||
if (file == null) { | |||||
if (output == null) { | |||||
log(message, logLevel); | log(message, logLevel); | ||||
} else { | } else { | ||||
Writer out = null; | Writer out = null; | ||||
try { | 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()); | out.write(message, 0, message.length()); | ||||
} catch (IOException ioe) { | } catch (IOException ioe) { | ||||
throw new BuildException(ioe, getLocation()); | throw new BuildException(ioe, getLocation()); | ||||
@@ -95,6 +98,20 @@ public class Echo extends Task { | |||||
*/ | */ | ||||
public void setFile(File file) { | public void setFile(File file) { | ||||
this.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; | |||||
} | } | ||||
/** | /** | ||||
@@ -0,0 +1,148 @@ | |||||
<?xml version="1.0"?> | |||||
<!-- | |||||
Licensed to the Apache Software Foundation (ASF) under one or more | |||||
contributor license agreements. See the NOTICE file distributed with | |||||
this work for additional information regarding copyright ownership. | |||||
The ASF licenses this file to You 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. | |||||
--> | |||||
<project name="echo-test" default="antunit" xmlns:au="antlib:org.apache.ant.antunit"> | |||||
<import file="../antunit-base.xml" /> | |||||
<property name="dest.dir" location="echo.dest"/> | |||||
<target name="setUp"> | |||||
<mkdir dir="${dest.dir}" /> | |||||
</target> | |||||
<target name="tearDown"> | |||||
<delete dir="${dest.dir}"/> | |||||
</target> | |||||
<target name="test1"> | |||||
<echo/> | |||||
<au:assertTrue> | |||||
<length length="0"> | |||||
<au:logcontent /> | |||||
</length> | |||||
</au:assertTrue> | |||||
</target> | |||||
<target name="test2"> | |||||
<echo message="OUTPUT OF ECHO"/> | |||||
<au:assertLogContains text="OUTPUT OF ECHO" /> | |||||
</target> | |||||
<target name="test3"> | |||||
<echo> | |||||
This | |||||
is | |||||
a | |||||
multiline | |||||
message | |||||
</echo> | |||||
<au:assertTrue> | |||||
<resourcecount count="1"> | |||||
<restrict> | |||||
<au:logcontent /> | |||||
<contains text="This" /> | |||||
<contains text="is" /> | |||||
<contains text="a" /> | |||||
<contains text="multiline" /> | |||||
<contains text="message" /> | |||||
</restrict> | |||||
</resourcecount> | |||||
</au:assertTrue> | |||||
</target> | |||||
<target name="testFile"> | |||||
<echo file="${dest.dir}/echo.txt">Simple text</echo> | |||||
<au:assertTrue> | |||||
<resourcecount count="1"> | |||||
<restrict> | |||||
<file file="${dest.dir}/echo.txt" /> | |||||
<contains text="Simple text" /> | |||||
</restrict> | |||||
</resourcecount> | |||||
</au:assertTrue> | |||||
</target> | |||||
<target name="testOutputFile"> | |||||
<echo output="${dest.dir}/echo.txt">Simple text</echo> | |||||
<au:assertTrue> | |||||
<resourcecount count="1"> | |||||
<restrict> | |||||
<file file="${dest.dir}/echo.txt" /> | |||||
<contains text="Simple text" /> | |||||
</restrict> | |||||
</resourcecount> | |||||
</au:assertTrue> | |||||
</target> | |||||
<target name="testAppend"> | |||||
<echo file="${dest.dir}/echo.txt">Simple text</echo> | |||||
<echo file="${dest.dir}/echo.txt" append="true">Appended</echo> | |||||
<au:assertTrue> | |||||
<resourcecount count="1"> | |||||
<restrict> | |||||
<file file="${dest.dir}/echo.txt" /> | |||||
<contains text="Simple text" /> | |||||
<contains text="Appended" /> | |||||
</restrict> | |||||
</resourcecount> | |||||
</au:assertTrue> | |||||
</target> | |||||
<target name="testEmptyEncoding"> | |||||
<echo file="${dest.dir}/echo.txt" encoding="">Simple text</echo> | |||||
<au:assertTrue> | |||||
<resourcecount count="1"> | |||||
<restrict> | |||||
<file file="${dest.dir}/echo.txt" /> | |||||
<contains text="Simple text" /> | |||||
</restrict> | |||||
</resourcecount> | |||||
</au:assertTrue> | |||||
</target> | |||||
<target name="testUTF16Encoding"> | |||||
<property name="char" value="©" /> | |||||
<echo file="${dest.dir}/echo16.txt" encoding="UTF-16">${char}</echo> | |||||
<au:assertTrue> | |||||
<resourcecount count="1"> | |||||
<restrict> | |||||
<concat encoding="UTF-16"> | |||||
<file file="${dest.dir}/echo16.txt" /> | |||||
</concat> | |||||
<contains text="${char}" /> | |||||
</restrict> | |||||
</resourcecount> | |||||
</au:assertTrue> | |||||
</target> | |||||
<target name="testUTF8Encoding"> | |||||
<property name="char" value="©" /> | |||||
<echo file="${dest.dir}/echo8.txt" encoding="UTF8">${char}</echo> | |||||
<au:assertTrue> | |||||
<resourcecount count="1"> | |||||
<restrict> | |||||
<concat encoding="UTF-8"> | |||||
<file file="${dest.dir}/echo8.txt" /> | |||||
</concat> | |||||
<contains text="${char}" /> | |||||
</restrict> | |||||
</resourcecount> | |||||
</au:assertTrue> | |||||
</target> | |||||
</project> |
@@ -21,78 +21,41 @@ package org.apache.tools.ant.taskdefs; | |||||
import java.io.ByteArrayOutputStream; | import java.io.ByteArrayOutputStream; | ||||
import java.io.PrintStream; | 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.DefaultLogger; | ||||
import org.apache.tools.ant.Project; | 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) { | public EchoTest(String name) { | ||||
super(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() { | public void testLogBlankEcho() { | ||||
Project p = new Project(); | |||||
p.init(); | |||||
EchoTestLogger logger = new EchoTestLogger(); | 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 { | private class EchoTestLogger extends DefaultLogger { | ||||
String lastLoggedMessage; | String lastLoggedMessage; | ||||
/** | /** | ||||
* | |||||
* Create a new EchoTestLogger. | |||||
*/ | */ | ||||
public EchoTestLogger() { | public EchoTestLogger() { | ||||
super(); | super(); | ||||
@@ -100,8 +63,9 @@ public class EchoTest extends BuildFileTest { | |||||
this.setOutputPrintStream(new PrintStream(new ByteArrayOutputStream(256))); | this.setOutputPrintStream(new PrintStream(new ByteArrayOutputStream(256))); | ||||
this.setErrorPrintStream(new PrintStream(new ByteArrayOutputStream(256))); | this.setErrorPrintStream(new PrintStream(new ByteArrayOutputStream(256))); | ||||
} | } | ||||
/* | |||||
* @param message | |||||
/** | |||||
* {@inheritDoc} | |||||
*/ | */ | ||||
protected void log(String message) { | protected void log(String message) { | ||||
this.lastLoggedMessage = message; | this.lastLoggedMessage = message; | ||||