diff --git a/src/etc/testcases/taskdefs/recorder.xml b/src/etc/testcases/taskdefs/recorder.xml index 18829ff47..e508af7fd 100644 --- a/src/etc/testcases/taskdefs/recorder.xml +++ b/src/etc/testcases/taskdefs/recorder.xml @@ -8,26 +8,20 @@ - - - - - + - - + - @@ -38,7 +32,6 @@ - @@ -50,12 +43,11 @@ - - + diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 89fe5d4d3..4a426c45f 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -971,21 +971,31 @@ public class FileUtils { /** * Compares the contents of two files. * - *

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.

- * * @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 @@ public class FileUtils { return true; } + if (textfile) { + return textEquals(f1, f2); + } else { + return binaryEquals(f1, f2); + } + } + + /** + * Binary compares the contents of two files. + *

+ * 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. + *

+ * + * @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; @@ -1033,6 +1064,40 @@ public class FileUtils { } } + /** + * 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; + } finally { + close(in1); + close(in2); + } + } + /** * This was originally an emulation of {@link File#getParentFile} for JDK 1.1, * but it is now implemented using that method (Ant 1.7 onwards). diff --git a/src/testcases/org/apache/tools/ant/taskdefs/RecorderTest.java b/src/testcases/org/apache/tools/ant/taskdefs/RecorderTest.java index adf05e453..90f97715c 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/RecorderTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/RecorderTest.java @@ -27,7 +27,8 @@ import java.io.IOException; */ 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 class RecorderTest extends BuildFileTest { 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)); } }