ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/testcases/org/apache/tools/ant/util FileUtilsTest.java
Date Fri, 02 Nov 2001 16:36:49 GMT
bodewig     01/11/02 08:36:49

  Modified:    src/main/org/apache/tools/ant/taskdefs FixCRLF.java
                        Javadoc.java
               src/main/org/apache/tools/ant/taskdefs/compilers
                        DefaultCompilerAdapter.java
               src/main/org/apache/tools/ant/taskdefs/optional Cab.java
                        ReplaceRegExp.java
               src/main/org/apache/tools/ant/util FileUtils.java
               src/testcases/org/apache/tools/ant/util FileUtilsTest.java
  Log:
  push createTempFile to FileUtils
  
  Revision  Changes    Path
  1.20      +7 -18     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
  
  Index: FixCRLF.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- FixCRLF.java	2001/10/28 21:26:29	1.19
  +++ FixCRLF.java	2001/11/02 16:36:49	1.20
  @@ -58,6 +58,7 @@
   import org.apache.tools.ant.DirectoryScanner;
   import org.apache.tools.ant.Project;
   import org.apache.tools.ant.types.EnumeratedAttribute;
  +import org.apache.tools.ant.util.FileUtils;
   
   import java.io.File;
   import java.io.BufferedReader;
  @@ -65,7 +66,6 @@
   import java.io.IOException;
   import java.io.BufferedWriter;
   import java.io.FileWriter;
  -import java.util.Random;
   import java.util.Enumeration;
   import java.util.NoSuchElementException;
   
  @@ -108,7 +108,7 @@
    *
    * @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
    * @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
  - * @version $Revision: 1.19 $ $Name:  $
  + * @version $Revision: 1.20 $ $Name:  $
    */
   
   public class FixCRLF extends MatchingTask {
  @@ -148,6 +148,8 @@
       private File srcDir;
       private File destDir = null;
   
  +    private FileUtils fileUtils = FileUtils.newFileUtils();
  +
       /**
        * Defaults the properties based on the system type.
        * <ul><li>Unix: eol="LF" tab="asis" eof="remove"
  @@ -356,19 +358,6 @@
       }
   
       /**
  -     * Creates a temporary file.
  -     */
  -    private File createTempFile() {
  -        String name = "fixcrlf" 
  -            + (new Random(System.currentTimeMillis())).nextLong();
  -        if (destDir == null) {
  -            return new File(srcDir, name);
  -        } else {
  -            return new File(destDir, name);
  -        }
  -    }
  -
  -    /**
        * Checks for the inequality of two files
        */
       private boolean filesEqual(File file1, File file2) {
  @@ -413,6 +402,7 @@
   
       private void processFile(String file) throws BuildException {
           File srcFile = new File(srcDir, file);
  +        File destD = destDir == null ? srcDir : destDir;
           File tmpFile = null;
           BufferedWriter outWriter;
           OneLiner.BufferLine line;
  @@ -423,7 +413,7 @@
           try {
               // Set up the output Writer
               try {
  -                tmpFile = createTempFile();
  +                tmpFile = fileUtils.createTempFile("fixcrlf", "", destD);
                   FileWriter writer = new FileWriter(tmpFile);
                   outWriter = new BufferedWriter(writer);
               } catch (IOException e) {
  @@ -554,8 +544,7 @@
                   throw new BuildException(e);
               } // end of try-catch
               
  -            File destFile = new File(destDir == null ? srcDir : destDir,
  -                                     file);
  +            File destFile = new File(destD, file);
   
               try {                                            
                   lines.close();
  
  
  
  1.65      +5 -10     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javadoc.java
  
  Index: Javadoc.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javadoc.java,v
  retrieving revision 1.64
  retrieving revision 1.65
  diff -u -r1.64 -r1.65
  --- Javadoc.java	2001/11/02 07:56:52	1.64
  +++ Javadoc.java	2001/11/02 16:36:49	1.65
  @@ -61,7 +61,6 @@
   import java.util.Vector;
   import java.util.Enumeration;
   import java.util.StringTokenizer;
  -import java.util.Random;
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.DirectoryScanner;
   import org.apache.tools.ant.Project;
  @@ -73,6 +72,7 @@
   import org.apache.tools.ant.types.EnumeratedAttribute;
   import org.apache.tools.ant.types.Commandline;
   import org.apache.tools.ant.types.FileSet;
  +import org.apache.tools.ant.util.FileUtils;
   
   /**
    * This task makes it easy to generate Javadoc documentation for a collection
  @@ -279,6 +279,8 @@
       private boolean useExternalFile = false;
       private File tmpList = null;
   
  +    private FileUtils fileUtils = FileUtils.newFileUtils();
  +
       /**
        * Work around command line length limit by using an external file
        * for the sourcefiles.
  @@ -924,7 +926,7 @@
                    */
                   if (useExternalFile) {
                       if (tmpList == null) {
  -                        tmpList = createTempFile();
  +                        tmpList = fileUtils.createTempFile("javadoc", "", null);
                           toExecute.createArgument().setValue("@" + tmpList.getAbsolutePath());
                       }
                       srcListWriter = new PrintWriter(new FileWriter(tmpList.getAbsolutePath(),

  @@ -1055,7 +1057,7 @@
           PrintWriter packageListWriter = null;
           try {
               if (useExternalFile) {
  -                tmpList = createTempFile();
  +                tmpList = fileUtils.createTempFile("javadoc", "", null);
                   toExecute.createArgument().setValue("@" + tmpList.getAbsolutePath());
                   packageListWriter = new PrintWriter(new FileWriter(tmpList));
               }
  @@ -1146,13 +1148,6 @@
       protected String expand(String content) {
           return ProjectHelper.replaceProperties(project, content, 
                                                  project.getProperties());
  -    }
  -
  -    /**
  -     * Creates a temporary file.
  -     */
  -    private File createTempFile() {
  -        return new File("javadoc" + (new Random(System.currentTimeMillis())).nextLong());
       }
   
       private String getJavadocExecutableName()
  
  
  
  1.14      +4 -2      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
  
  Index: DefaultCompilerAdapter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- DefaultCompilerAdapter.java	2001/11/02 15:18:47	1.13
  +++ DefaultCompilerAdapter.java	2001/11/02 16:36:49	1.14
  @@ -63,12 +63,12 @@
   import org.apache.tools.ant.types.Path;
   import org.apache.tools.ant.types.Commandline;
   import org.apache.tools.ant.types.FileSet;
  +import org.apache.tools.ant.util.FileUtils;
   
   import java.io.File;
   import java.io.PrintWriter;
   import java.io.FileWriter;
   import java.io.IOException;
  -import java.util.Random;
   
   /**
    * This is the default implementation for the CompilerAdapter interface.
  @@ -108,6 +108,8 @@
       protected static String lSep = System.getProperty("line.separator");
       protected Javac attributes;
   
  +    private FileUtils fileUtils = FileUtils.newFileUtils();
  +
       public void setJavac( Javac attributes ) {
           this.attributes = attributes;
           src = attributes.getSrcdir();
  @@ -362,7 +364,7 @@
               if (Commandline.toString(args).length() > 4096) {
                   PrintWriter out = null;
                   try {
  -                    tmpFile = new File("jikes"+(new Random(System.currentTimeMillis())).nextLong());
  +                    tmpFile = fileUtils.createTempFile("jikes", "", null);
                       out = new PrintWriter(new FileWriter(tmpFile));
                       for (int i = firstFileName; i < args.length; i++) {
                           out.println(args[i]);
  
  
  
  1.11      +5 -24     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/Cab.java
  
  Index: Cab.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/Cab.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Cab.java	2001/11/02 07:56:53	1.10
  +++ Cab.java	2001/11/02 16:36:49	1.11
  @@ -62,6 +62,7 @@
   import org.apache.tools.ant.taskdefs.condition.Os;
   import org.apache.tools.ant.types.FileSet;
   import org.apache.tools.ant.types.Commandline;
  +import org.apache.tools.ant.util.FileUtils;
   
   import java.io.File;
   import java.io.IOException;
  @@ -71,7 +72,6 @@
   import java.util.Enumeration;
   
   import java.util.Vector;
  -import java.util.Random;
   import java.text.DecimalFormat;
   
   /**
  @@ -93,6 +93,8 @@
   
       private static String myos;
   
  +    private FileUtils fileUtils = FileUtils.newFileUtils();
  +
       /**
        * This is the name/location of where to 
        * create the .cab file.
  @@ -213,27 +215,6 @@
           return command;
       }
   
  -    private static int counter = new Random().nextInt() % 100000;
  -    protected File createTempFile(String prefix, String suffix)
  -    {
  -        if (suffix == null)
  -        {
  -            suffix = ".tmp";
  -        }
  -
  -        String name = prefix +
  -            new DecimalFormat("#####").format(new Integer(counter++)) +
  -            suffix;
  -
  -        String tmpdir = System.getProperty("java.io.tmpdir");
  -
  -        // java.io.tmpdir is not present in 1.1
  -        if (tmpdir == null)
  -            return new File(name);
  -        else
  -            return new File(tmpdir, name);
  -    }
  -
       /**
        * Creates a list file.  This temporary file contains a list of all files
        * to be included in the cab, one file per line.
  @@ -241,7 +222,7 @@
       protected File createListFile(Vector files)
           throws IOException
       {
  -        File listFile = createTempFile("ant", null);
  +        File listFile = fileUtils.createTempFile("ant", "", null);
           
           PrintWriter writer = new PrintWriter(new FileOutputStream(listFile));
   
  @@ -342,7 +323,7 @@
                   exec.setDir(baseDir);
                   
                   if (!doVerbose) {
  -                    outFile = createTempFile("ant", null);
  +                    outFile = fileUtils.createTempFile("ant", "", null);
                       exec.setOutput(outFile);
                   }
                       
  
  
  
  1.3       +4 -8      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
  
  Index: ReplaceRegExp.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ReplaceRegExp.java	2001/11/02 14:04:56	1.2
  +++ ReplaceRegExp.java	2001/11/02 16:36:49	1.3
  @@ -61,6 +61,7 @@
   import org.apache.tools.ant.types.RegularExpression;
   import org.apache.tools.ant.types.Substitution;
   import org.apache.tools.ant.types.FileSet;
  +import org.apache.tools.ant.util.FileUtils;
   import java.io.BufferedReader;
   import java.io.BufferedWriter;
   import java.io.File;
  @@ -162,6 +163,8 @@
       private RegularExpression regex;
       private Substitution subs;
   
  +    private FileUtils fileUtils = FileUtils.newFileUtils();
  +
       /***
        * Default Constructor
        */
  @@ -260,7 +263,7 @@
           throws IOException
       {
           File parentDir = new File(new File(f.getAbsolutePath()).getParent());
  -        File temp = createTempFile(parentDir);
  +        File temp = fileUtils.createTempFile("replace", ".txt", parentDir);
   
           FileReader r = null;
           FileWriter w = null;
  @@ -418,13 +421,6 @@
                   }
               }
           }
  -    }
  -
  -    /**
  -     * Creates a temporary file.
  -     */
  -    private File createTempFile(File dir) {
  -        return new File(dir, "replace" + (new Random(System.currentTimeMillis())).nextLong()+".txt");
       }
   
   }
  
  
  
  1.8       +38 -1     jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FileUtils.java	2001/10/28 21:27:20	1.7
  +++ FileUtils.java	2001/11/02 16:36:49	1.8
  @@ -63,8 +63,10 @@
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
   import java.lang.reflect.Method;
  -import java.util.StringTokenizer;
  +import java.text.DecimalFormat;
  +import java.util.Random;
   import java.util.Stack;
  +import java.util.StringTokenizer;
   
   import org.apache.tools.ant.BuildException; 
   import org.apache.tools.ant.Project; 
  @@ -79,9 +81,12 @@
    * @author duncan@x180.com
    * @author <a href="mailto:conor@apache.org">Conor MacNeill</a>
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> 
  + *
  + * @version $Revision: 1.8 $
    */
    
   public class FileUtils {
  +    private static Random rand = new Random(System.currentTimeMillis());
       private static Object lockReflection = new Object();
       private static java.lang.reflect.Method setLastModified = null;
   
  @@ -466,6 +471,38 @@
               path = path.replace('/', '\\');
           }
           return new File(path);
  +    }
  +
  +    /**
  +     * Create a temporary file in a given directory.
  +     *
  +     * <p>The file denoted by the returned abstract pathname did not
  +     * exist before this method was invoked, any subsequent invocation
  +     * of this method will yield a different file name.</p>
  +     *
  +     * <p>This method is different to File.createTempFile of JDK 1.2
  +     * as it doesn't create the file itself and doesn't use platform
  +     * specific temporary directory when the parentDir attribute is
  +     * null.</p>
  +     *
  +     * @param parentDir Directory to create the temporary file in -
  +     * current working directory will be assumed if this parameter is
  +     * null.
  +     *
  +     * @since 1.8
  +     */
  +    public File createTempFile(String prefix, String suffix, File parentDir) {
  +
  +        File result = null;
  +        DecimalFormat fmt = new DecimalFormat("#####");
  +        synchronized (rand) {
  +            do {
  +                result = new File(parentDir, 
  +                                  prefix + fmt.format(rand.nextInt()) 
  +                                  + suffix);
  +            } while (result.exists());
  +        }
  +        return result;
       }
   }
   
  
  
  
  1.5       +25 -0     jakarta-ant/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java
  
  Index: FileUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FileUtilsTest.java	2001/10/01 13:32:36	1.4
  +++ FileUtilsTest.java	2001/11/02 16:36:49	1.5
  @@ -274,6 +274,31 @@
       }
   
       /**
  +     * Test createTempFile
  +     */
  +    public void testCreateTempFile() {
  +        File parent = new File((new File("/tmp")).getAbsolutePath());
  +        File tmp1 = fu.createTempFile("pre", ".suf", parent);
  +        assertTrue("new file", !tmp1.exists());
  +
  +        String name = tmp1.getName();
  +        assertTrue("starts with pre", name.startsWith("pre"));
  +        assertTrue("ends with .suf", name.endsWith(".suf"));
  +        assertEquals("is inside parent dir", 
  +                     parent.getAbsolutePath(),
  +                     tmp1.getParent());
  +
  +        File tmp2 = fu.createTempFile("pre", ".suf", parent);
  +        assertTrue("files are different", 
  +                   !tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath()));
  +
  +        // null parent dir
  +        File tmp3 = fu.createTempFile("pre", ".suf", null);
  +        assertEquals((new File(tmp3.getName())).getAbsolutePath(),
  +                     tmp3.getAbsolutePath());
  +    }
  +
  +    /**
        * adapt file separators to local conventions
        */
       private String localize(String path) {
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message