From fb2d2b20ba111990c473116f488661b5dd7c86c5 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 16 Jun 2010 09:36:08 +0000 Subject: [PATCH] use a more robust approach that doesn't rely on stream redirections git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@955166 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/PropertyFileCLITest.java | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java b/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java index 042aacd98..64e875324 100644 --- a/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java +++ b/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java @@ -19,40 +19,44 @@ package org.apache.tools.ant; import java.io.File; +import java.io.FileReader; import java.io.FileWriter; -import java.io.PrintStream; import junit.framework.TestCase; +import org.apache.tools.ant.util.FileUtils; public class PropertyFileCLITest extends TestCase { public void testPropertyResolution() throws Exception { - File props = File.createTempFile("propertyfilecli", ".properties"); - props.deleteOnExit(); - FileWriter fw = new FileWriter(props); - fw.write("w=world\nmessage=Hello, ${w}\n"); - fw.close(); - File build = File.createTempFile("propertyfilecli", ".xml"); - build.deleteOnExit(); - fw = new FileWriter(build); - fw.write("${message}"); - fw.close(); - PrintStream sysOut = System.out; - StringBuffer sb = new StringBuffer(); + FileUtils fu = FileUtils.getFileUtils(); + File props = fu.createTempFile("propertyfilecli", ".properties", + null, true, true); + File build = fu.createTempFile("propertyfilecli", ".xml", null, true, + true); + File log = fu.createTempFile("propertyfilecli", ".log", null, true, + true); + FileWriter fw = null; + FileReader fr = null; try { - PrintStream out = - new PrintStream(new BuildFileTest.AntOutputStream(sb)); - System.setOut(out); + fw = new FileWriter(props); + fw.write("w=world\nmessage=Hello, ${w}\n"); + fw.close(); + fw = new FileWriter(build); + fw.write("${message}"); + fw.close(); + fw = null; Main m = new NoExitMain(); m.startAnt(new String[] { "-propertyfile", props.getAbsolutePath(), - "-f", build.getAbsolutePath() + "-f", build.getAbsolutePath(), + "-l", log.getAbsolutePath() }, null, null); + String l = FileUtils.safeReadFully(fr = new FileReader(log)); + assertTrue("expected log to contain 'Hello, world' but was " + l, + l.indexOf("Hello, world") > -1); } finally { - System.setOut(sysOut); + FileUtils.close(fw); + FileUtils.close(fr); } - String log = sb.toString(); - assertTrue("expected log to contain 'Hello, world' but was " + log, - log.indexOf("Hello, world") > -1); } private static class NoExitMain extends Main {