Browse Source

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
master
Jan Materne 22 years ago
parent
commit
8a19a4e64f
3 changed files with 75 additions and 74 deletions
  1. +1
    -1
      src/etc/testcases/filters/concat.xml
  2. +21
    -15
      src/main/org/apache/tools/ant/filters/ConcatFilter.java
  3. +53
    -58
      src/testcases/org/apache/tools/ant/filters/ConcatFilterTest.java

+ 1
- 1
src/etc/testcases/filters/concat.xml View File

@@ -93,7 +93,7 @@
<target name="testConcatFilterPrependAppend" depends="init">
<typedef name="concatfilter" classname="org.apache.tools.ant.filters.ConcatFilter"/>
<copy file="input/concatfilter.test"
tofile="result/concat.concatfilterPrependAppend.test">
tofile="result/concat.ConcatFilterPrependAppend.test">
<filterchain>
<concatfilter prepend="result/prepend.txt" append="result/append.txt"/>
</filterchain>


+ 21
- 15
src/main/org/apache/tools/ant/filters/ConcatFilter.java View File

@@ -76,7 +76,7 @@ import org.apache.tools.ant.types.Parameter;
* file.</p>
*
* @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() { }
}

}

+ 53
- 58
src/testcases/org/apache/tools/ant/filters/ConcatFilterTest.java View File

@@ -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 <sleep> before <delete> 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 <i>testHelloWorld</i>
* will search for a file <i>result/concat.HelloWorld.test</i>.
* @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 <i>null</i> 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;
}

}

Loading…
Cancel
Save