git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277345 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -8,26 +8,20 @@ | |||||
| <target name="prepare"> | <target name="prepare"> | ||||
| <mkdir dir="${recdir}"/> | <mkdir dir="${recdir}"/> | ||||
| <copy toDir="${recdir}"> | |||||
| <fileset dir="${recin}"/> | |||||
| </copy> | |||||
| <fixcrlf srcdir="${recdir}"/> | |||||
| </target> | </target> | ||||
| <target name="noappend"> | <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" /> | <record name="${recdir}/rectest1.log" action="start" /> | ||||
| <echo message="some message1"/> | <echo message="some message1"/> | ||||
| <record name="${recdir}/rectest1.log" action="stop" /> | <record name="${recdir}/rectest1.log" action="stop" /> | ||||
| <fixcrlf srcdir="${recdir}" includes="*.log"/> | |||||
| </target> | </target> | ||||
| <target name="append"> | <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"/> | <record name="${recdir}/rectest2.log" append="true" action="start"/> | ||||
| <echo message="some message2"/> | <echo message="some message2"/> | ||||
| <record name="${recdir}/rectest2.log" action="stop"/> | <record name="${recdir}/rectest2.log" action="stop"/> | ||||
| <fixcrlf srcdir="${recdir}" includes="*.log"/> | |||||
| </target> | </target> | ||||
| <target name="restart"> | <target name="restart"> | ||||
| @@ -38,7 +32,6 @@ | |||||
| <record name="${recdir}/rectest3.log" action="start"/> | <record name="${recdir}/rectest3.log" action="start"/> | ||||
| <echo message="some message3"/> | <echo message="some message3"/> | ||||
| <record name="${recdir}/rectest3.log" action="stop"/> | <record name="${recdir}/rectest3.log" action="stop"/> | ||||
| <fixcrlf srcdir="${recdir}" includes="*.log"/> | |||||
| </target> | </target> | ||||
| <target name="deleterestart"> | <target name="deleterestart"> | ||||
| @@ -50,12 +43,11 @@ | |||||
| <record name="${recdir}/rectest4.log" action="start"/> | <record name="${recdir}/rectest4.log" action="start"/> | ||||
| <echo message="some message3"/> | <echo message="some message3"/> | ||||
| <record name="${recdir}/rectest4.log" action="stop"/> | <record name="${recdir}/rectest4.log" action="stop"/> | ||||
| <fixcrlf srcdir="${recdir}" includes="*.log"/> | |||||
| </target> | </target> | ||||
| <target name="cleanup"> | <target name="cleanup"> | ||||
| <delete dir="${recdir}"/> | |||||
| <delete dir="${recdir}"/> | |||||
| </target> | </target> | ||||
| </project> | </project> | ||||
| @@ -971,21 +971,31 @@ public class FileUtils { | |||||
| /** | /** | ||||
| * Compares the contents of two files. | * 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 f1 the file whose content is to be compared. | ||||
| * @param f2 the other 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. | * @return true if the content of the files is the same. | ||||
| * | * | ||||
| * @throws IOException if the files cannot be read. | * @throws IOException if the files cannot be read. | ||||
| * | |||||
| * @since 1.9 | |||||
| */ | */ | ||||
| public boolean contentEquals(File f1, File f2) throws IOException { | 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()) { | if (f1.exists() != f2.exists()) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| @@ -1005,6 +1015,27 @@ public class FileUtils { | |||||
| return true; | 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()) { | if (f1.length() != f2.length()) { | ||||
| // different size =>false | // different size =>false | ||||
| return 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, | * 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). | * but it is now implemented using that method (Ant 1.7 onwards). | ||||
| @@ -27,7 +27,8 @@ import java.io.IOException; | |||||
| */ | */ | ||||
| public class RecorderTest extends BuildFileTest { | 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 */ | /** Utilities used for file operations */ | ||||
| private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | ||||
| @@ -48,37 +49,37 @@ public class RecorderTest extends BuildFileTest { | |||||
| public void testNoAppend() throws IOException { | public void testNoAppend() throws IOException { | ||||
| executeTarget("noappend"); | executeTarget("noappend"); | ||||
| assertTrue(FILE_UTILS | assertTrue(FILE_UTILS | ||||
| .contentEquals(project.resolveFile(REC_DIR | |||||
| .contentEquals(project.resolveFile(REC_IN | |||||
| + "rectest1.result"), | + "rectest1.result"), | ||||
| project.resolveFile(REC_DIR | |||||
| + "rectest1.log"))); | |||||
| project.resolveFile(REC_DIR | |||||
| + "rectest1.log"), true)); | |||||
| } | } | ||||
| public void testAppend() throws IOException { | public void testAppend() throws IOException { | ||||
| executeTarget("append"); | executeTarget("append"); | ||||
| assertTrue(FILE_UTILS | assertTrue(FILE_UTILS | ||||
| .contentEquals(project.resolveFile(REC_DIR | |||||
| .contentEquals(project.resolveFile(REC_IN | |||||
| + "rectest2.result"), | + "rectest2.result"), | ||||
| project.resolveFile(REC_DIR | |||||
| + "rectest2.log"))); | |||||
| project.resolveFile(REC_DIR | |||||
| + "rectest2.log"), true)); | |||||
| } | } | ||||
| public void testRestart() throws IOException { | public void testRestart() throws IOException { | ||||
| executeTarget("restart"); | executeTarget("restart"); | ||||
| assertTrue(FILE_UTILS | assertTrue(FILE_UTILS | ||||
| .contentEquals(project.resolveFile(REC_DIR | |||||
| .contentEquals(project.resolveFile(REC_IN | |||||
| + "rectest3.result"), | + "rectest3.result"), | ||||
| project.resolveFile(REC_DIR | |||||
| + "rectest3.log"))); | |||||
| project.resolveFile(REC_DIR | |||||
| + "rectest3.log"), true)); | |||||
| } | } | ||||
| public void testDeleteRestart() throws IOException { | public void testDeleteRestart() throws IOException { | ||||
| executeTarget("deleterestart"); | executeTarget("deleterestart"); | ||||
| assertTrue(FILE_UTILS | assertTrue(FILE_UTILS | ||||
| .contentEquals(project.resolveFile(REC_DIR | |||||
| .contentEquals(project.resolveFile(REC_IN | |||||
| + "rectest4.result"), | + "rectest4.result"), | ||||
| project.resolveFile(REC_DIR | |||||
| + "rectest4.log"))); | |||||
| project.resolveFile(REC_DIR | |||||
| + "rectest4.log"), true)); | |||||
| } | } | ||||
| } | } | ||||