Browse Source

Make log messages from System.out calls, remove some code in execute

that was redundant, remove some tabs, add testcase that checks that
files don't get overwritten if they'd be identical to the new version.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269474 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
f5f32f163a
2 changed files with 37 additions and 66 deletions
  1. +8
    -66
      src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
  2. +29
    -0
      src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java

+ 8
- 66
src/main/org/apache/tools/ant/taskdefs/FixCRLF.java View File

@@ -53,20 +53,16 @@
*/ */


package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.EnumeratedAttribute;


import java.io.*; import java.io.*;
import java.util.*; import java.util.*;


/** /**
* FixCRLF.java
*
* Based on FixCR.java
* by Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>.
*
* Task to convert text source files to local OS formatting conventions, as * Task to convert text source files to local OS formatting conventions, as
* well as repair text files damaged by misconfigured or misguided editors or * well as repair text files damaged by misconfigured or misguided editors or
* file transfer programs. * file transfer programs.
@@ -137,7 +133,6 @@ public class FixCRLF extends MatchingTask {
private StringBuffer linebuf = new StringBuffer(1024); private StringBuffer linebuf = new StringBuffer(1024);
private StringBuffer linebuf2 = new StringBuffer(1024); private StringBuffer linebuf2 = new StringBuffer(1024);
private int eol; private int eol;
private int addcr = UNDEF;
private String eolstr; private String eolstr;
private int ctrlz; private int ctrlz;
private int tabs; private int tabs;
@@ -317,7 +312,7 @@ public class FixCRLF extends MatchingTask {
*/ */
public void execute() throws BuildException { public void execute() throws BuildException {
// first off, make sure that we've got a srcdir and destdir // first off, make sure that we've got a srcdir and destdir
if (srcDir == null) { if (srcDir == null) {
throw new BuildException("srcdir attribute must be set!"); throw new BuildException("srcdir attribute must be set!");
} }
@@ -336,57 +331,6 @@ public class FixCRLF extends MatchingTask {
} }
} }


// Set up the correct EOL values
if (eol == UNDEF) {
if (addcr == UNDEF) {
// Neither eol not addcr has been defined
// go for the system defaults
if (System.getProperty("line.separator").equals("\r")) {
eol = CR;
} else if (System.getProperty("line.separator").equals("\n")) {
eol = LF;
} else {
eol = CRLF;
}
} // end of if (addcr == UNDEF)
else {
// addcr has been defined - translate to eol values
switch (addcr) {
case ADD:
eol = CRLF;
break;
case REMOVE:
eol = LF;
break;
case ASIS:
eol = ASIS;
break;

} // end of switch (addcr)
} // end of if (addcr == UNDEF)else
} // end of if (eol == UNDEF)

switch (eol) {
// set eolstr value unless ASIS
case CR:
eolstr = new String("\r");
break;
case LF:
eolstr = new String("\n");
break;
case CRLF:
eolstr = new String("\r\n");
break;
} // end of switch (eol)
// log options used // log options used
log("options:" + log("options:" +
" eol=" + " eol=" +
@@ -616,10 +560,9 @@ public class FixCRLF extends MatchingTask {


if (destFile.exists()) { if (destFile.exists()) {
// Compare the destination with the temp file // Compare the destination with the temp file
System.out.println("destFile exists");
log("destFile exists", Project.MSG_DEBUG);
if ( ! filesEqual(destFile, tmpFile)) { if ( ! filesEqual(destFile, tmpFile)) {
System.out.println("destFile exists: files not equal");
log(destFile + " is being written", Project.MSG_VERBOSE);
log(destFile + " is being written", Project.MSG_DEBUG);
if (!destFile.delete()) { if (!destFile.delete()) {
throw new BuildException("Unable to delete " throw new BuildException("Unable to delete "
+ destFile); + destFile);
@@ -633,17 +576,16 @@ public class FixCRLF extends MatchingTask {
} }


} else { // destination is equal to temp file } else { // destination is equal to temp file
System.out.println("destFile exists: files equal");
log(destFile + log(destFile +
" is not written, as the contents are identical", " is not written, as the contents are identical",
Project.MSG_VERBOSE);
Project.MSG_DEBUG);
if (!tmpFile.delete()) { if (!tmpFile.delete()) {
throw new BuildException("Unable to delete " throw new BuildException("Unable to delete "
+ destFile);
+ tmpFile);
} }
} }
} else { // destFile does not exist - write the temp file } else { // destFile does not exist - write the temp file
System.out.println("destFile does not exist");
log("destFile does not exist", Project.MSG_DEBUG);
if (!tmpFile.renameTo(destFile)) { if (!tmpFile.renameTo(destFile)) {
throw new BuildException( throw new BuildException(
"Failed to transform " + srcFile "Failed to transform " + srcFile


+ 29
- 0
src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java View File

@@ -130,6 +130,35 @@ public class FixCrLfTest extends TaskdefsTest {
new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk9.java")); new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk9.java"));
} }
public void testNoOverwrite() throws IOException {
executeTarget("test1");
File result =
new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk1.java");
long modTime = result.lastModified();

/*
* Sleep for some time to make sure a newer file would get a
* more recent timestamp according to the file system's
* granularity (should be > 2s to account for Windows FAT).
*/
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException ie) {
fail(ie.getMessage());
} // end of try-catch

/*
* make sure we get a new Project instance or the target won't get run
* a second time.
*/
configureProject("src/etc/testcases/taskdefs/fixcrlf/build.xml");

executeTarget("test1");
result =
new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk1.java");
assertEquals(modTime, result.lastModified());
}

public void assertEqualContent(File expect, File result) public void assertEqualContent(File expect, File result)
throws AssertionFailedError, IOException { throws AssertionFailedError, IOException {
if (!result.exists()) { if (!result.exists()) {


Loading…
Cancel
Save