Browse Source

Extended FileUtils with a comparison that ignores the kind of line break.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277345 13f79535-47bb-0310-9956-ffa450edef68
master
Jacobus Martinus Kruithof 20 years ago
parent
commit
f5d98f1e2c
3 changed files with 89 additions and 31 deletions
  1. +3
    -11
      src/etc/testcases/taskdefs/recorder.xml
  2. +72
    -7
      src/main/org/apache/tools/ant/util/FileUtils.java
  3. +14
    -13
      src/testcases/org/apache/tools/ant/taskdefs/RecorderTest.java

+ 3
- 11
src/etc/testcases/taskdefs/recorder.xml View File

@@ -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>

+ 72
- 7
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -971,21 +971,31 @@ public class FileUtils {
/**
* 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 @@ public class FileUtils {
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;
@@ -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).


+ 14
- 13
src/testcases/org/apache/tools/ant/taskdefs/RecorderTest.java View File

@@ -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));
}

}

Loading…
Cancel
Save