From 4ac04f30c2cdc4c0d31eaf13f797afe2aa72fea0 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Thu, 17 Jul 2003 10:20:16 +0000 Subject: [PATCH] Ensure all generated output gets through the output system to the generating task PR: 21636 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274840 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/DemuxOutputStream.java | 31 ++++++++++-------- src/main/org/apache/tools/ant/Project.java | 30 ++++++++++------- src/main/org/apache/tools/ant/Task.java | 30 ++++++++--------- .../org/apache/tools/ant/UnknownElement.java | 32 +++++++++---------- .../org/apache/tools/ant/taskdefs/Ant.java | 24 +++++++------- .../apache/tools/ant/taskdefs/CallTarget.java | 24 +++++++------- .../org/apache/tools/ant/taskdefs/Java.java | 24 +++++++------- .../apache/tools/ant/taskdefs/Redirector.java | 24 +++++++------- .../taskdefs/optional/junit/JUnitTask.java | 32 +++++++++---------- .../optional/junit/JUnitTestRunner.java | 16 +++++----- .../ant/taskdefs/DefaultExcludesTest.java | 30 ++++++++--------- .../tools/ant/taskdefs/DemuxOutputTask.java | 18 +++++------ 12 files changed, 164 insertions(+), 151 deletions(-) diff --git a/src/main/org/apache/tools/ant/DemuxOutputStream.java b/src/main/org/apache/tools/ant/DemuxOutputStream.java index ae4cd486b..6108828dd 100644 --- a/src/main/org/apache/tools/ant/DemuxOutputStream.java +++ b/src/main/org/apache/tools/ant/DemuxOutputStream.java @@ -80,11 +80,10 @@ public class DemuxOutputStream extends OutputStream { private ByteArrayOutputStream buffer; /** - * Whether or not the next line-terminator should be skipped in terms - * of processing the buffer. Used to avoid \r\n invoking - * processBuffer twice. + * Indicates we have just seen a carriage return. It may be part of + * a crlf pair or a single cr invoking processBuffer twice. */ - private boolean skip = false; + private boolean crSeen = false; } /** Maximum buffer size. */ @@ -129,7 +128,7 @@ public class DemuxOutputStream extends OutputStream { if (bufferInfo == null) { bufferInfo = new BufferInfo(); bufferInfo.buffer = new ByteArrayOutputStream(); - bufferInfo.skip = false; + bufferInfo.crSeen = false; buffers.put(current, bufferInfo); } return bufferInfo; @@ -147,7 +146,7 @@ public class DemuxOutputStream extends OutputStream { // Shouldn't happen } bufferInfo.buffer = new ByteArrayOutputStream(); - bufferInfo.skip = false; + bufferInfo.crSeen = false; } /** @@ -169,17 +168,23 @@ public class DemuxOutputStream extends OutputStream { final byte c = (byte) cc; BufferInfo bufferInfo = getBufferInfo(); - if ((c == '\n') || (c == '\r')) { - if (!bufferInfo.skip) { - processBuffer(bufferInfo.buffer); - } - } else { + + if (c == '\n') { + // LF is always end of line (i.e. CRLF or single LF) bufferInfo.buffer.write(cc); - if (bufferInfo.buffer.size() > MAX_SIZE) { + processBuffer(bufferInfo.buffer); + } else { + if (bufferInfo.crSeen) { + // CR without LF - send buffer then add char processBuffer(bufferInfo.buffer); } + // add into buffer + bufferInfo.buffer.write(cc); + } + bufferInfo.crSeen = (c == '\r'); + if (!bufferInfo.crSeen && bufferInfo.buffer.size() > MAX_SIZE) { + processBuffer(bufferInfo.buffer); } - bufferInfo.skip = (c == '\r'); } /** diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index f80a4b1e9..4ae48dec9 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -75,6 +75,8 @@ import org.apache.tools.ant.types.Description; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.JavaEnvUtils; +import org.apache.tools.ant.util.StringUtils; + /** * Central representation of an Ant project. This class defines an @@ -1089,19 +1091,19 @@ public class Project { * messages. If the current thread is not currently executing a task, * the message is logged directly. * - * @param line Message to handle. Should not be null. + * @param output Message to handle. Should not be null. * @param isError Whether the text represents an error (true) * or information (false). */ - public void demuxOutput(String line, boolean isError) { + public void demuxOutput(String output, boolean isError) { Task task = getThreadTask(Thread.currentThread()); if (task == null) { - fireMessageLogged(this, line, isError ? MSG_ERR : MSG_INFO); + log(output, isError ? MSG_ERR : MSG_INFO); } else { if (isError) { - task.handleErrorOutput(line); + task.handleErrorOutput(output); } else { - task.handleOutput(line); + task.handleOutput(output); } } } @@ -1158,19 +1160,19 @@ public class Project { * * @since Ant 1.5.2 * - * @param line Message to handle. Should not be null. + * @param output Message to handle. Should not be null. * @param isError Whether the text represents an error (true) * or information (false). */ - public void demuxFlush(String line, boolean isError) { + public void demuxFlush(String output, boolean isError) { Task task = getThreadTask(Thread.currentThread()); if (task == null) { - fireMessageLogged(this, line, isError ? MSG_ERR : MSG_INFO); + fireMessageLogged(this, output, isError ? MSG_ERR : MSG_INFO); } else { if (isError) { - task.handleErrorFlush(line); + task.handleErrorFlush(output); } else { - task.handleFlush(line); + task.handleFlush(output); } } } @@ -1915,7 +1917,13 @@ public class Project { */ private void fireMessageLoggedEvent(BuildEvent event, String message, int priority) { - event.setMessage(message, priority); + + if (message.endsWith(StringUtils.LINE_SEP)) { + int endIndex = message.length() - StringUtils.LINE_SEP.length(); + event.setMessage(message.substring(0, endIndex), priority); + } else { + event.setMessage(message, priority); + } Vector listeners = getBuildListeners(); synchronized (this) { if (loggingMessage) { diff --git a/src/main/org/apache/tools/ant/Task.java b/src/main/org/apache/tools/ant/Task.java index 357088244..5cd75ab31 100644 --- a/src/main/org/apache/tools/ant/Task.java +++ b/src/main/org/apache/tools/ant/Task.java @@ -305,23 +305,23 @@ public abstract class Task extends ProjectComponent { } /** - * Handles a line of output by logging it with the INFO priority. + * Handles output by logging it with the INFO priority. * - * @param line The line of output to log. Should not be null. + * @param output The output to log. Should not be null. */ - protected void handleOutput(String line) { - log(line, Project.MSG_INFO); + protected void handleOutput(String output) { + log(output, Project.MSG_INFO); } /** - * Handles a line of output by logging it with the INFO priority. + * Handles output by logging it with the INFO priority. * - * @param line The line of output to log. Should not be null. + * @param output The output to log. Should not be null. * * @since Ant 1.5.2 */ - protected void handleFlush(String line) { - handleOutput(line); + protected void handleFlush(String output) { + handleOutput(output); } /** @@ -342,23 +342,23 @@ public abstract class Task extends ProjectComponent { } /** - * Handles an error line by logging it with the INFO priority. + * Handles an error output by logging it with the INFO priority. * - * @param line The error line to log. Should not be null. + * @param output The error output to log. Should not be null. */ - protected void handleErrorOutput(String line) { - log(line, Project.MSG_ERR); + protected void handleErrorOutput(String output) { + log(output, Project.MSG_ERR); } /** * Handles an error line by logging it with the INFO priority. * - * @param line The error line to log. Should not be null. + * @param output The error output to log. Should not be null. * * @since Ant 1.5.2 */ - protected void handleErrorFlush(String line) { - handleErrorOutput(line); + protected void handleErrorFlush(String output) { + handleErrorOutput(output); } /** diff --git a/src/main/org/apache/tools/ant/UnknownElement.java b/src/main/org/apache/tools/ant/UnknownElement.java index bd77b8ca8..5fd96ae27 100644 --- a/src/main/org/apache/tools/ant/UnknownElement.java +++ b/src/main/org/apache/tools/ant/UnknownElement.java @@ -192,13 +192,13 @@ public class UnknownElement extends Task { /** * Handles output sent to System.out by this task or its real task. * - * @param line The line of output to log. Should not be null. + * @param output The output to log. Should not be null. */ - protected void handleOutput(String line) { + protected void handleOutput(String output) { if (realThing instanceof Task) { - ((Task) realThing).handleOutput(line); + ((Task) realThing).handleOutput(output); } else { - super.handleOutput(line); + super.handleOutput(output); } } @@ -219,26 +219,26 @@ public class UnknownElement extends Task { /** * Handles output sent to System.out by this task or its real task. * - * @param line The line of output to log. Should not be null. + * @param output The output to log. Should not be null. */ - protected void handleFlush(String line) { + protected void handleFlush(String output) { if (realThing instanceof Task) { - ((Task) realThing).handleFlush(line); + ((Task) realThing).handleFlush(output); } else { - super.handleFlush(line); + super.handleFlush(output); } } /** * Handles error output sent to System.err by this task or its real task. * - * @param line The error line to log. Should not be null. + * @param output The error output to log. Should not be null. */ - protected void handleErrorOutput(String line) { + protected void handleErrorOutput(String output) { if (realThing instanceof Task) { - ((Task) realThing).handleErrorOutput(line); + ((Task) realThing).handleErrorOutput(output); } else { - super.handleErrorOutput(line); + super.handleErrorOutput(output); } } @@ -246,13 +246,13 @@ public class UnknownElement extends Task { /** * Handles error output sent to System.err by this task or its real task. * - * @param line The error line to log. Should not be null. + * @param output The error output to log. Should not be null. */ - protected void handleErrorFlush(String line) { + protected void handleErrorFlush(String output) { if (realThing instanceof Task) { - ((Task) realThing).handleErrorOutput(line); + ((Task) realThing).handleErrorOutput(output); } else { - super.handleErrorOutput(line); + super.handleErrorOutput(output); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Ant.java b/src/main/org/apache/tools/ant/taskdefs/Ant.java index 96220c924..303bf5964 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Ant.java +++ b/src/main/org/apache/tools/ant/taskdefs/Ant.java @@ -263,11 +263,11 @@ public class Ant extends Task { * * @since Ant 1.5 */ - public void handleOutput(String line) { + public void handleOutput(String output) { if (newProject != null) { - newProject.demuxOutput(line, false); + newProject.demuxOutput(output, false); } else { - super.handleOutput(line); + super.handleOutput(output); } } @@ -290,11 +290,11 @@ public class Ant extends Task { * * @since Ant 1.5.2 */ - public void handleFlush(String line) { + public void handleFlush(String output) { if (newProject != null) { - newProject.demuxFlush(line, false); + newProject.demuxFlush(output, false); } else { - super.handleFlush(line); + super.handleFlush(output); } } @@ -303,11 +303,11 @@ public class Ant extends Task { * * @since Ant 1.5 */ - public void handleErrorOutput(String line) { + public void handleErrorOutput(String output) { if (newProject != null) { - newProject.demuxOutput(line, true); + newProject.demuxOutput(output, true); } else { - super.handleErrorOutput(line); + super.handleErrorOutput(output); } } @@ -316,11 +316,11 @@ public class Ant extends Task { * * @since Ant 1.5.2 */ - public void handleErrorFlush(String line) { + public void handleErrorFlush(String output) { if (newProject != null) { - newProject.demuxFlush(line, true); + newProject.demuxFlush(output, true); } else { - super.handleErrorFlush(line); + super.handleErrorFlush(output); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/CallTarget.java b/src/main/org/apache/tools/ant/taskdefs/CallTarget.java index d4becee7a..5888df366 100644 --- a/src/main/org/apache/tools/ant/taskdefs/CallTarget.java +++ b/src/main/org/apache/tools/ant/taskdefs/CallTarget.java @@ -190,11 +190,11 @@ public class CallTarget extends Task { * * @since Ant 1.5 */ - public void handleOutput(String line) { + public void handleOutput(String output) { if (callee != null) { - callee.handleOutput(line); + callee.handleOutput(output); } else { - super.handleOutput(line); + super.handleOutput(output); } } @@ -217,11 +217,11 @@ public class CallTarget extends Task { * * @since Ant 1.5.2 */ - public void handleFlush(String line) { + public void handleFlush(String output) { if (callee != null) { - callee.handleFlush(line); + callee.handleFlush(output); } else { - super.handleFlush(line); + super.handleFlush(output); } } @@ -230,11 +230,11 @@ public class CallTarget extends Task { * * @since Ant 1.5 */ - public void handleErrorOutput(String line) { + public void handleErrorOutput(String output) { if (callee != null) { - callee.handleErrorOutput(line); + callee.handleErrorOutput(output); } else { - super.handleErrorOutput(line); + super.handleErrorOutput(output); } } @@ -243,11 +243,11 @@ public class CallTarget extends Task { * * @since Ant 1.5.2 */ - public void handleErrorFlush(String line) { + public void handleErrorFlush(String output) { if (callee != null) { - callee.handleErrorFlush(line); + callee.handleErrorFlush(output); } else { - super.handleErrorFlush(line); + super.handleErrorFlush(output); } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Java.java b/src/main/org/apache/tools/ant/taskdefs/Java.java index 3a0219690..0bd2e4d75 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Java.java +++ b/src/main/org/apache/tools/ant/taskdefs/Java.java @@ -460,11 +460,11 @@ public class Java extends Task { * * @since Ant 1.5 */ - protected void handleOutput(String line) { + protected void handleOutput(String output) { if (redirector.getOutputStream() != null) { - redirector.handleOutput(line); + redirector.handleOutput(output); } else { - super.handleOutput(line); + super.handleOutput(output); } } @@ -482,11 +482,11 @@ public class Java extends Task { * * @since Ant 1.5.2 */ - protected void handleFlush(String line) { + protected void handleFlush(String output) { if (redirector.getOutputStream() != null) { - redirector.handleFlush(line); + redirector.handleFlush(output); } else { - super.handleFlush(line); + super.handleFlush(output); } } @@ -495,11 +495,11 @@ public class Java extends Task { * * @since Ant 1.5 */ - protected void handleErrorOutput(String line) { + protected void handleErrorOutput(String output) { if (redirector.getErrorStream() != null) { - redirector.handleErrorOutput(line); + redirector.handleErrorOutput(output); } else { - super.handleErrorOutput(line); + super.handleErrorOutput(output); } } @@ -508,11 +508,11 @@ public class Java extends Task { * * @since Ant 1.5.2 */ - protected void handleErrorFlush(String line) { + protected void handleErrorFlush(String output) { if (redirector.getErrorStream() != null) { - redirector.handleErrorFlush(line); + redirector.handleErrorFlush(output); } else { - super.handleErrorOutput(line); + super.handleErrorOutput(output); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Redirector.java b/src/main/org/apache/tools/ant/taskdefs/Redirector.java index 2a6928385..2b8353c1c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Redirector.java +++ b/src/main/org/apache/tools/ant/taskdefs/Redirector.java @@ -359,13 +359,13 @@ public class Redirector { /** * Pass output sent to System.out to specified output. * - * @param line the data to be output + * @param output the data to be output */ - protected void handleOutput(String line) { + protected void handleOutput(String output) { if (outPrintStream == null) { outPrintStream = new PrintStream(outputStream); } - outPrintStream.println(line); + outPrintStream.print(output); } /** @@ -392,38 +392,38 @@ public class Redirector { /** * Process data due to a flush operation. * - * @param line the data being flushed. + * @param output the data being flushed. */ - protected void handleFlush(String line) { + protected void handleFlush(String output) { if (outPrintStream == null) { outPrintStream = new PrintStream(outputStream); } - outPrintStream.print(line); + outPrintStream.print(output); outPrintStream.flush(); } /** * Process error output * - * @param line the error output data. + * @param output the error output data. */ - protected void handleErrorOutput(String line) { + protected void handleErrorOutput(String output) { if (errorPrintStream == null) { errorPrintStream = new PrintStream(errorStream); } - errorPrintStream.println(line); + errorPrintStream.print(output); } /** * Handle a flush operation on the error stream * - * @param line the error information being flushed. + * @param output the error information being flushed. */ - protected void handleErrorFlush(String line) { + protected void handleErrorFlush(String output) { if (errorPrintStream == null) { errorPrintStream = new PrintStream(errorStream); } - errorPrintStream.print(line); + errorPrintStream.print(output); } /** diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java index b3c538b9e..4aa8aaffe 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java @@ -748,14 +748,14 @@ public class JUnitTask extends Task { * * @since Ant 1.5 */ - protected void handleOutput(String line) { + protected void handleOutput(String output) { if (runner != null) { - runner.handleOutput(line); + runner.handleOutput(output); if (showOutput) { - super.handleOutput(line); + super.handleOutput(output); } } else { - super.handleOutput(line); + super.handleOutput(output); } } @@ -780,14 +780,14 @@ public class JUnitTask extends Task { * * @since Ant 1.5.2 */ - protected void handleFlush(String line) { + protected void handleFlush(String output) { if (runner != null) { - runner.handleFlush(line); + runner.handleFlush(output); if (showOutput) { - super.handleFlush(line); + super.handleFlush(output); } } else { - super.handleFlush(line); + super.handleFlush(output); } } @@ -797,14 +797,14 @@ public class JUnitTask extends Task { * * @since Ant 1.5 */ - public void handleErrorOutput(String line) { + public void handleErrorOutput(String output) { if (runner != null) { - runner.handleErrorOutput(line); + runner.handleErrorOutput(output); if (showOutput) { - super.handleErrorOutput(line); + super.handleErrorOutput(output); } } else { - super.handleErrorOutput(line); + super.handleErrorOutput(output); } } @@ -815,14 +815,14 @@ public class JUnitTask extends Task { * * @since Ant 1.5.2 */ - public void handleErrorFlush(String line) { + public void handleErrorFlush(String output) { if (runner != null) { - runner.handleErrorFlush(line); + runner.handleErrorFlush(output); if (showOutput) { - super.handleErrorFlush(line); + super.handleErrorFlush(output); } } else { - super.handleErrorFlush(line); + super.handleErrorFlush(output); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java index 37894ec76..8131049d8 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java @@ -398,9 +398,9 @@ public class JUnitTestRunner implements TestListener { } } - protected void handleOutput(String line) { + protected void handleOutput(String output) { if (systemOut != null) { - systemOut.println(line); + systemOut.print(output); } } @@ -414,21 +414,21 @@ public class JUnitTestRunner implements TestListener { return -1; } - protected void handleErrorOutput(String line) { + protected void handleErrorOutput(String output) { if (systemError != null) { - systemError.println(line); + systemError.print(output); } } - protected void handleFlush(String line) { + protected void handleFlush(String output) { if (systemOut != null) { - systemOut.print(line); + systemOut.print(output); } } - protected void handleErrorFlush(String line) { + protected void handleErrorFlush(String output) { if (systemError != null) { - systemError.print(line); + systemError.print(output); } } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/DefaultExcludesTest.java b/src/testcases/org/apache/tools/ant/taskdefs/DefaultExcludesTest.java index 1dcce86e7..9c66475bd 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/DefaultExcludesTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/DefaultExcludesTest.java @@ -57,20 +57,20 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.BuildFileTest; /** - * @author Gus Heck + * @author Gus Heck */ -public class DefaultExcludesTest extends BuildFileTest { - - public DefaultExcludesTest(String name) { +public class DefaultExcludesTest extends BuildFileTest { + + public DefaultExcludesTest(String name) { super(name); - } - - public void setUp() { + } + + public void setUp() { configureProject("src/etc/testcases/taskdefs/defaultexcludes.xml"); } - + // Output the default excludes - public void test1() { + public void test1() { expectLog("test1", "Current Default Excludes:\n"+ " **/*~\n"+ " **/#*#\n"+ @@ -85,11 +85,11 @@ public class DefaultExcludesTest extends BuildFileTest { " **/vssver.scc\n"+ " **/.svn\n"+ " **/.svn/**\n"+ - " **/.DS_Store\n"); + " **/.DS_Store"); } - + // adding something to the excludes' - public void test2() { + public void test2() { expectLog("test2", "Current Default Excludes:\n"+ " **/*~\n"+ " **/#*#\n"+ @@ -105,11 +105,11 @@ public class DefaultExcludesTest extends BuildFileTest { " **/.svn\n"+ " **/.svn/**\n"+ " **/.DS_Store\n"+ - " foo\n"); // foo added + " foo"); // foo added } // removing something from the defaults - public void test3() { + public void test3() { expectLog("test3", "Current Default Excludes:\n"+ " **/*~\n"+ " **/#*#\n"+ @@ -124,6 +124,6 @@ public class DefaultExcludesTest extends BuildFileTest { " **/vssver.scc\n"+ " **/.svn\n"+ " **/.svn/**\n"+ - " **/.DS_Store\n"); + " **/.DS_Store"); } } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/DemuxOutputTask.java b/src/testcases/org/apache/tools/ant/taskdefs/DemuxOutputTask.java index 604967c40..0e622329c 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/DemuxOutputTask.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/DemuxOutputTask.java @@ -71,41 +71,41 @@ public class DemuxOutputTask extends Task { private String randomErrValue; private boolean outputReceived = false; private boolean errorReceived = false; - + public void execute() { Random generator = new Random(); randomOutValue = "Output Value is " + generator.nextInt(); randomErrValue = "Error Value is " + generator.nextInt(); - + System.out.println(randomOutValue); System.err.println(randomErrValue); if (!outputReceived) { throw new BuildException("Did not receive output"); - } - + } + if (!errorReceived) { throw new BuildException("Did not receive error"); } } protected void handleOutput(String line) { + line = line.trim(); if (line.length() != 0 && !line.equals(randomOutValue)) { - String message = "Received = [" + line + "], expected = [" + String message = "Received = [" + line + "], expected = [" + randomOutValue + "]"; throw new BuildException(message); } outputReceived = true; } - + protected void handleErrorOutput(String line) { + line = line.trim(); if (line.length() != 0 && !line.equals(randomErrValue)) { - String message = "Received = [" + line + "], expected = [" + String message = "Received = [" + line + "], expected = [" + randomErrValue + "]"; throw new BuildException(message); } errorReceived = true; } - - }