From 8a19a4e64fcf0eccf49faa383aecbcf974f4a69e Mon Sep 17 00:00:00 2001
From: Jan Materne
Date: Tue, 23 Sep 2003 21:21:01 +0000
Subject: [PATCH] Let ConcatFilterTest delete its files without exceptions.
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275321 13f79535-47bb-0310-9956-ffa450edef68
---
src/etc/testcases/filters/concat.xml | 2 +-
.../tools/ant/filters/ConcatFilter.java | 36 +++---
.../tools/ant/filters/ConcatFilterTest.java | 111 +++++++++---------
3 files changed, 75 insertions(+), 74 deletions(-)
diff --git a/src/etc/testcases/filters/concat.xml b/src/etc/testcases/filters/concat.xml
index 4fe195166..276aceea9 100644
--- a/src/etc/testcases/filters/concat.xml
+++ b/src/etc/testcases/filters/concat.xml
@@ -93,7 +93,7 @@
+ tofile="result/concat.ConcatFilterPrependAppend.test">
diff --git a/src/main/org/apache/tools/ant/filters/ConcatFilter.java b/src/main/org/apache/tools/ant/filters/ConcatFilter.java
index 87d9809c0..529ff4fcd 100644
--- a/src/main/org/apache/tools/ant/filters/ConcatFilter.java
+++ b/src/main/org/apache/tools/ant/filters/ConcatFilter.java
@@ -76,7 +76,7 @@ import org.apache.tools.ant.types.Parameter;
* file.
*
* @since 1.6
- * @version 2003-09-17
+ * @version 2003-09-23
* @author Jan Matèrne
*/
public final class ConcatFilter extends BaseParamFilterReader
@@ -89,10 +89,10 @@ public final class ConcatFilter extends BaseParamFilterReader
private File append;
/** Reader for prepend-file. */
- private Reader prependReader = new EmptyReader();
+ private Reader prependReader = null;
/** Reader for append-file. */
- private Reader appendReader = new EmptyReader();
+ private Reader appendReader = null;
/**
* Constructor for "dummy" instances.
@@ -136,12 +136,28 @@ public final class ConcatFilter extends BaseParamFilterReader
// The readers return -1 if they end. So simply read the "prepend"
// after that the "content" and at the end the "append" file.
- ch = prependReader.read();
+ if (prependReader != null) {
+ ch = prependReader.read();
+ if (ch == -1) {
+ // I am the only one so I have to close the reader
+ prependReader.close();
+ prependReader = null;
+ }
+ }
if (ch == -1) {
ch = super.read();
}
if (ch == -1) {
- ch = appendReader.read();
+ // don´t call super.close() because that reader is used
+ // on other places ...
+ if (appendReader != null) {
+ ch = appendReader.read();
+ if (ch == -1) {
+ // I am the only one so I have to close the reader
+ appendReader.close();
+ appendReader = null;
+ }
+ }
}
return ch;
@@ -233,14 +249,4 @@ public final class ConcatFilter extends BaseParamFilterReader
appendReader = new BufferedReader(new FileReader(append));
}
}
-
- /**
- * Reader which is always at the end of file.
- * Used for easier algorithm (polymorphism instead if-cascades).
- */
- private class EmptyReader extends Reader {
- public int read(char[] ch, int i1, int i2) { return -1; }
- public void close() { }
- }
-
}
\ No newline at end of file
diff --git a/src/testcases/org/apache/tools/ant/filters/ConcatFilterTest.java b/src/testcases/org/apache/tools/ant/filters/ConcatFilterTest.java
index c97b68a27..8befa4dcb 100644
--- a/src/testcases/org/apache/tools/ant/filters/ConcatFilterTest.java
+++ b/src/testcases/org/apache/tools/ant/filters/ConcatFilterTest.java
@@ -111,22 +111,7 @@ public class ConcatFilterTest extends BuildFileTest {
}
public void tearDown() {
- // I dont know why - but on my machine I always get a
- // "Unable to delete file ...result\append.txt" (or prepend.txt)
- // from Delete.removeDir(Delete.java:612).
- // Win2000, JDK 1.4.1_02
- // A before doesn´t work. From 10ms to 3000ms.
- // I modified the taskdefs.Delete.DELETE_RETRY_SLEEP_MILLIS
- // from 10 up to 2000 ms, but no success.
- // So I give up - and hope for a suggestion from another one.
- // But this shouldn´t let the testcases fail, so I do the cleanup
- // inside a try-block
- // Jan
- try {
- executeTarget("cleanup");
- } catch (Exception e) {
- e.printStackTrace();
- }
+ executeTarget("cleanup");
}
public void testFilterReaderNoArgs() throws IOException {
@@ -136,60 +121,70 @@ public class ConcatFilterTest extends BuildFileTest {
assertTrue("testFilterReaderNoArgs: Result not like expected", fu.contentEquals(expected, result));
}
- public void testFilterReaderBefore() throws IOException {
- executeTarget("testFilterReaderPrepend");
- File resultFile = getProject().resolveFile("result/concat.filterReaderPrepend.test");
- String resultContent = fu.readFully(new java.io.FileReader(resultFile));
- assertTrue("First 5 lines differs.", resultContent.startsWith(FILE_PREPEND_WITH));
- assertTrue("Last 5 lines differs.", resultContent.endsWith(FILE_APPEND));
+ public void testFilterReaderBefore() {
+ doTest("testFilterReaderPrepend", FILE_PREPEND_WITH, FILE_APPEND);
+ }
+
+ public void testFilterReaderAfter() {
+ doTest("testFilterReaderAppend", FILE_PREPEND, FILE_APPEND_WITH);
+ }
+
+ public void testFilterReaderBeforeAfter() {
+ doTest("testFilterReaderPrependAppend", FILE_PREPEND_WITH, FILE_APPEND_WITH);
}
- public void testFilterReaderAfter() throws IOException {
- executeTarget("testFilterReaderAppend");
- File resultFile = getProject().resolveFile("result/concat.filterReaderAppend.test");
- String resultContent = fu.readFully(new java.io.FileReader(resultFile));
- assertTrue("First 5 lines differs.", resultContent.startsWith(FILE_PREPEND));
- assertTrue("Last 5 lines differs.", resultContent.endsWith(FILE_APPEND_WITH));
+ public void testConcatFilter() {
+ doTest("testConcatFilter", FILE_PREPEND, FILE_APPEND);
}
- public void testFilterReaderBeforeAfter() throws IOException {
- executeTarget("testFilterReaderPrependAppend");
- File resultFile = getProject().resolveFile("result/concat.filterReaderPrependAppend.test");
- String resultContent = fu.readFully(new java.io.FileReader(resultFile));
- assertTrue("First 5 lines differs.", resultContent.startsWith(FILE_PREPEND_WITH));
- assertTrue("Last 5 lines differs.", resultContent.endsWith(FILE_APPEND_WITH));
+ public void testConcatFilterBefore() {
+ doTest("testConcatFilterPrepend", FILE_PREPEND_WITH, FILE_APPEND);
}
- public void testConcatFilter() throws IOException {
- executeTarget("testConcatFilter");
- File resultFile = getProject().resolveFile("result/concat.concatfilter.test");
- String resultContent = fu.readFully(new java.io.FileReader(resultFile));
- assertTrue("First 5 lines differs.", resultContent.startsWith(FILE_PREPEND));
- assertTrue("Last 5 lines differs.", resultContent.endsWith(FILE_APPEND));
+ public void testConcatFilterAfter() {
+ doTest("testConcatFilterAppend", FILE_PREPEND, FILE_APPEND_WITH);
}
- public void testConcatFilterBefore() throws IOException {
- executeTarget("testConcatFilterPrepend");
- File resultFile = getProject().resolveFile("result/concat.concatfilterPrepend.test");
- String resultContent = fu.readFully(new java.io.FileReader(resultFile));
- assertTrue("First 5 lines differs.", resultContent.startsWith(FILE_PREPEND_WITH));
- assertTrue("Last 5 lines differs.", resultContent.endsWith(FILE_APPEND));
+ public void testConcatFilterBeforeAfter() {
+ doTest("testConcatFilterPrependAppend", FILE_PREPEND_WITH, FILE_APPEND_WITH);
}
- public void testConcatFilterAfter() throws IOException {
- executeTarget("testConcatFilterAppend");
- File resultFile = getProject().resolveFile("result/concat.concatfilterAppend.test");
- String resultContent = fu.readFully(new java.io.FileReader(resultFile));
- assertTrue("First 5 lines differs.", resultContent.startsWith(FILE_PREPEND));
- assertTrue("Last 5 lines differs.", resultContent.endsWith(FILE_APPEND_WITH));
+
+ /**
+ * Executes a target and checks the beginning and the ending of a file.
+ * The filename depends on the target name: target name testHelloWorld
+ * will search for a file result/concat.HelloWorld.test.
+ * @param target The target to invoke
+ * @param expectedStart The string which should be at the beginning of the file
+ * @param expectedEnd The string which should be at the end of the file
+ */
+ protected void doTest(String target, String expectedStart, String expectedEnd) {
+ executeTarget(target);
+ String resultContent = read("result/concat." + target.substring(4) + ".test");
+ assertTrue("First 5 lines differs.", resultContent.startsWith(expectedStart));
+ assertTrue("Last 5 lines differs.", resultContent.endsWith(expectedEnd));
}
- public void testConcatFilterBeforeAfter() throws IOException {
- executeTarget("testConcatFilterPrependAppend");
- File resultFile = getProject().resolveFile("result/concat.concatfilterPrependAppend.test");
- String resultContent = fu.readFully(new java.io.FileReader(resultFile));
- assertTrue("First 5 lines differs.", resultContent.startsWith(FILE_PREPEND_WITH));
- assertTrue("Last 5 lines differs.", resultContent.endsWith(FILE_APPEND_WITH));
+
+ /**
+ * Wrapper for FileUtils.readFully().
+ * Additionally it resolves the filename according the the projects basedir
+ * and closes the used reader.
+ * @param filename The name of the file to read
+ * @return the content of the file or null if something goes wrong
+ */
+ protected String read(String filename) {
+ String content = null;
+ try {
+ File file = getProject().resolveFile(filename);
+ java.io.FileReader rdr = new java.io.FileReader(file);
+ content = fu.readFully(rdr);
+ rdr.close();
+ rdr = null;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return content;
}
}
\ No newline at end of file