jhm         2003/09/23 14:07:00

  Modified:    src/etc/testcases/filters Tag: ANT_16_BRANCH concat.xml
               src/main/org/apache/tools/ant/filters Tag: ANT_16_BRANCH
                        ConcatFilter.java
               src/testcases/org/apache/tools/ant/filters Tag:
                        ANT_16_BRANCH ConcatFilterTest.java
  Log:
  Let ConcatFilterTest delete its files without exceptions.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.2.2.1   +1 -1      ant/src/etc/testcases/filters/concat.xml
  
  Index: concat.xml
  ===================================================================
  RCS file: /home/cvs/ant/src/etc/testcases/filters/concat.xml,v
  retrieving revision 1.2
  retrieving revision 1.2.2.1
  diff -u -r1.2 -r1.2.2.1
  --- concat.xml        19 Sep 2003 09:24:13 -0000      1.2
  +++ concat.xml        23 Sep 2003 21:07:00 -0000      1.2.2.1
  @@ -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>
  
  
  
  No                   revision
  No                   revision
  1.2.2.1   +21 -15    
ant/src/main/org/apache/tools/ant/filters/ConcatFilter.java
  
  Index: ConcatFilter.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/filters/ConcatFilter.java,v
  retrieving revision 1.2
  retrieving revision 1.2.2.1
  diff -u -r1.2 -r1.2.2.1
  --- ConcatFilter.java 19 Sep 2003 09:24:13 -0000      1.2
  +++ ConcatFilter.java 23 Sep 2003 21:07:00 -0000      1.2.2.1
  @@ -76,7 +76,7 @@
    * 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 @@
       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 @@
   
           // 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 @@
               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                   revision
  No                   revision
  1.2.2.1   +65 -70    
ant/src/testcases/org/apache/tools/ant/filters/ConcatFilterTest.java
  
  Index: ConcatFilterTest.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/testcases/org/apache/tools/ant/filters/ConcatFilterTest.java,v
  retrieving revision 1.2
  retrieving revision 1.2.2.1
  diff -u -r1.2 -r1.2.2.1
  --- ConcatFilterTest.java     19 Sep 2003 09:24:13 -0000      1.2
  +++ ConcatFilterTest.java     23 Sep 2003 21:07:00 -0000      1.2.2.1
  @@ -111,22 +111,7 @@
       }
   
       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 @@
           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 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 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 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 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 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));
  -    }
  -
  -    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));
  +    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 testConcatFilter() {
  +        doTest("testConcatFilter", FILE_PREPEND, FILE_APPEND);
  +    }
  +
  +    public void testConcatFilterBefore() {
  +        doTest("testConcatFilterPrepend", FILE_PREPEND_WITH, FILE_APPEND);
  +    }
  +
  +    public void testConcatFilterAfter() {
  +        doTest("testConcatFilterAppend", FILE_PREPEND, FILE_APPEND_WITH);
  +    }
  +
  +    public void testConcatFilterBeforeAfter() {
  +        doTest("testConcatFilterPrependAppend", FILE_PREPEND_WITH, 
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));
  +    }
  +
  +
  +    /**
  +     * 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;
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to