jkf         2005/01/11 13:56:45

  Modified:    src/main/org/apache/tools/ant/util FileUtils.java
               src/etc/testcases/taskdefs recorder.xml
               src/testcases/org/apache/tools/ant/taskdefs
                        RecorderTest.java
  Log:
  Extended FileUtils with a comparison that ignores the kind of line break.
  
  Revision  Changes    Path
  1.83      +73 -8     ant/src/main/org/apache/tools/ant/util/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/util/FileUtils.java,v
  retrieving revision 1.82
  retrieving revision 1.83
  diff -u -r1.82 -r1.83
  --- FileUtils.java    7 Jan 2005 00:14:06 -0000       1.82
  +++ FileUtils.java    11 Jan 2005 21:56:45 -0000      1.83
  @@ -971,21 +971,31 @@
       /**
        * Compares the contents of two files.
        *
  -     * <p>simple but sub-optimal comparision algorithm.  written for
  -     * working rather than fast. Better would be a block read into
  -     * buffers followed by long comparisions apart from the final 1-7
  -     * bytes.</p>
  -     *
        * @param f1 the file whose content is to be compared.
        * @param f2 the other file whose content is to be compared.
        *
        * @return true if the content of the files is the same.
        *
        * @throws IOException if the files cannot be read.
  -     *
  -     * @since 1.9
        */
       public boolean contentEquals(File f1, File f2) throws IOException {
  +        return contentEquals(f1, f2, false);
  +    }
  +
  +    /**
  +     * Compares the contents of two files.
  +     *
  +     * @param f1 the file whose content is to be compared.
  +     * @param f2 the other file whose content is to be compared.
  +     * @param textfile true if the file is to be treated as a text file and
  +     *        differences in kind of line break are to be ignored.
  +     *
  +     * @return true if the content of the files is the same.
  +     *
  +     * @throws IOException if the files cannot be read.
  +     * @since ant 1.7
  +     */
  +    public boolean contentEquals(File f1, File f2, boolean textfile) throws 
IOException {
           if (f1.exists() != f2.exists()) {
               return false;
           }
  @@ -1005,6 +1015,27 @@
               return true;
           }
   
  +        if (textfile) {
  +            return textEquals(f1, f2);
  +        } else {
  +            return binaryEquals(f1, f2);
  +        }
  +    }
  +
  +    /**
  +     * Binary compares the contents of two files.
  +     * <p>
  +     * simple but sub-optimal comparision algorithm. written for working
  +     * rather than fast. Better would be a block read into buffers followed
  +     * by long comparisions apart from the final 1-7 bytes.
  +     * </p>
  +     *
  +     * @param f1 the file whose content is to be compared.
  +     * @param f2 the other file whose content is to be compared.
  +     * @return true if the content of the files is the same.
  +     * @throws IOException if the files cannot be read.
  +     */
  +    private boolean binaryEquals(File f1, File f2) throws IOException {
           if (f1.length() != f2.length()) {
               // different size =>false
               return false;
  @@ -1024,6 +1055,40 @@
                   expectedByte = in1.read();
               }
               if (in2.read() != -1) {
  +                return false;
  +            }
  +            return true;
  +        } finally {
  +            close(in1);
  +            close(in2);
  +        }
  +    }
  +
  +    /**
  +     * Text compares the contents of two files.
  +     *
  +     * Ignores different kinds of line endings.
  +     *
  +     * @param f1 the file whose content is to be compared.
  +     * @param f2 the other file whose content is to be compared.
  +     * @return true if the content of the files is the same.
  +     * @throws IOException if the files cannot be read.
  +     */
  +    private boolean textEquals(File f1, File f2) throws IOException {
  +        BufferedReader in1 = null;
  +        BufferedReader in2 = null;
  +        try {
  +            in1 = new BufferedReader(new FileReader(f1));
  +            in2 = new BufferedReader(new FileReader(f2));
  +
  +            String expected = in1.readLine();
  +            while (expected != null) {
  +                if (!expected.equals(in2.readLine())) {
  +                    return false;
  +                }
  +                expected = in1.readLine();
  +            }
  +            if (in2.readLine() != null) {
                   return false;
               }
               return true;
  
  
  
  1.3       +3 -11     ant/src/etc/testcases/taskdefs/recorder.xml
  
  Index: recorder.xml
  ===================================================================
  RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/recorder.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- recorder.xml      1 Dec 2004 07:45:11 -0000       1.2
  +++ recorder.xml      11 Jan 2005 21:56:45 -0000      1.3
  @@ -8,26 +8,20 @@
   
       <target name="prepare">
         <mkdir dir="${recdir}"/>
  -      <copy toDir="${recdir}">
  -        <fileset dir="${recin}"/>
  -      </copy>
  -      <fixcrlf srcdir="${recdir}"/>
       </target>
   
       <target name="noappend">
  -        <copy file="${recdir}/rectest2.result" 
tofile="${recdir}/rectest1.log"/>
  +        <copy file="${recin}/rectest2.result" 
tofile="${recdir}/rectest1.log"/>
           <record name="${recdir}/rectest1.log" action="start" />
           <echo message="some message1"/>
           <record name="${recdir}/rectest1.log" action="stop" />
  -        <fixcrlf srcdir="${recdir}" includes="*.log"/>
       </target>
   
       <target name="append">
  -        <copy file="${recdir}/rectest1.result" 
tofile="${recdir}/rectest2.log"/>
  +        <copy file="${recin}/rectest1.result" 
tofile="${recdir}/rectest2.log"/>
           <record name="${recdir}/rectest2.log" append="true" action="start"/>
           <echo message="some message2"/>
           <record name="${recdir}/rectest2.log" action="stop"/>
  -        <fixcrlf srcdir="${recdir}" includes="*.log"/>
       </target>
   
       <target name="restart">
  @@ -38,7 +32,6 @@
           <record name="${recdir}/rectest3.log" action="start"/>
           <echo message="some message3"/>
           <record name="${recdir}/rectest3.log" action="stop"/>
  -        <fixcrlf srcdir="${recdir}" includes="*.log"/>
       </target>
   
       <target name="deleterestart">
  @@ -50,12 +43,11 @@
           <record name="${recdir}/rectest4.log" action="start"/>
           <echo message="some message3"/>
           <record name="${recdir}/rectest4.log" action="stop"/>
  -        <fixcrlf srcdir="${recdir}" includes="*.log"/>
       </target>
   
   
       <target name="cleanup">
  -        <delete dir="${recdir}"/>
  +       <delete dir="${recdir}"/>
       </target>
   
   </project>
  
  
  
  1.4       +15 -14    
ant/src/testcases/org/apache/tools/ant/taskdefs/RecorderTest.java
  
  Index: RecorderTest.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/RecorderTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RecorderTest.java 6 Jan 2005 12:05:04 -0000       1.3
  +++ RecorderTest.java 11 Jan 2005 21:56:45 -0000      1.4
  @@ -27,7 +27,8 @@
    */
   public class RecorderTest extends BuildFileTest {
   
  -    private static final String REC_DIR = "recorder-out";
  +    private static final String REC_IN = "recorder/";
  +    private static final String REC_DIR = "recorder-out/";
   
       /** Utilities used for file operations */
       private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  @@ -48,37 +49,37 @@
       public void testNoAppend() throws IOException {
           executeTarget("noappend");
           assertTrue(FILE_UTILS
  -                   .contentEquals(project.resolveFile(REC_DIR 
  +                   .contentEquals(project.resolveFile(REC_IN
                                                         + "rectest1.result"),
  -                                  project.resolveFile(REC_DIR 
  -                                                      + "rectest1.log")));
  +                                  project.resolveFile(REC_DIR
  +                                                      + "rectest1.log"), 
true));
       }
   
       public void testAppend() throws IOException {
           executeTarget("append");
           assertTrue(FILE_UTILS
  -                   .contentEquals(project.resolveFile(REC_DIR 
  +                   .contentEquals(project.resolveFile(REC_IN
                                                         + "rectest2.result"),
  -                                  project.resolveFile(REC_DIR 
  -                                                      + "rectest2.log")));
  +                                  project.resolveFile(REC_DIR
  +                                                      + "rectest2.log"), 
true));
       }
   
       public void testRestart() throws IOException {
           executeTarget("restart");
           assertTrue(FILE_UTILS
  -                   .contentEquals(project.resolveFile(REC_DIR 
  +                   .contentEquals(project.resolveFile(REC_IN
                                                         + "rectest3.result"),
  -                                  project.resolveFile(REC_DIR 
  -                                                      + "rectest3.log")));
  +                                  project.resolveFile(REC_DIR
  +                                                      + "rectest3.log"), 
true));
       }
   
       public void testDeleteRestart() throws IOException {
           executeTarget("deleterestart");
           assertTrue(FILE_UTILS
  -                   .contentEquals(project.resolveFile(REC_DIR 
  +                   .contentEquals(project.resolveFile(REC_IN
                                                         + "rectest4.result"),
  -                                  project.resolveFile(REC_DIR 
  -                                                      + "rectest4.log")));
  +                                  project.resolveFile(REC_DIR
  +                                                      + "rectest4.log"), 
true));
       }
   
   }
  
  
  

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

Reply via email to