commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sand...@apache.org
Subject cvs commit: jakarta-commons-sandbox/io/src/test/org/apache/commons/io test.txt IOTestCase.java
Date Mon, 28 Jan 2002 04:44:49 GMT
sanders     02/01/27 20:44:49

  Modified:    io       build.xml
               io/src/java/org/apache/commons/io FileUtils.java
               io/src/test/org/apache/commons/io IOTestCase.java
  Added:       io/src/test/org/apache/commons/io test.txt
  Log:
  Updated build file.
  Sniped from alexandria's FileUtils.
  Added FileUtils test case.
  
  Revision  Changes    Path
  1.2       +2 -2      jakarta-commons-sandbox/io/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/io/build.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- build.xml	25 Jan 2002 19:06:14 -0000	1.1
  +++ build.xml	28 Jan 2002 04:44:49 -0000	1.2
  @@ -3,7 +3,7 @@
   
   <!--
           "IO" component of the Jakarta Commons Subproject
  -        $Id: build.xml,v 1.1 2002/01/25 19:06:14 sanders Exp $
  +        $Id: build.xml,v 1.2 2002/01/28 04:44:49 sanders Exp $
   -->
   
   
  @@ -233,7 +233,7 @@
       <echo message="Running IO tests ..."/>
       <java classname="${test.runner}" fork="yes"
           failonerror="${test.failonerror}">
  -      <arg value="org.apache.commons.io.TestIO"/>
  +      <arg value="org.apache.commons.io.IOTestCase"/>
         <classpath refid="test.classpath"/>
       </java>
     </target>
  
  
  
  1.2       +107 -3    jakarta-commons-sandbox/io/src/java/org/apache/commons/io/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/io/src/java/org/apache/commons/io/FileUtils.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FileUtils.java	26 Jan 2002 02:47:42 -0000	1.1
  +++ FileUtils.java	28 Jan 2002 04:44:49 -0000	1.2
  @@ -57,16 +57,19 @@
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
  +import java.util.Vector;
   
   /**
    * Common {@link java.io.File} manipulation routines.
    *
    * Taken from the commons-utils repo.
  + * Also code from alexandria's FileUtils.
    *
  + * @author <a href="mailto:burton@relativity.yi.org">Kevin A. Burton</A>
    * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
    * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
    * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph.Reck</a>
  - * @version $Id: FileUtils.java,v 1.1 2002/01/26 02:47:42 sanders Exp $
  + * @version $Id: FileUtils.java,v 1.2 2002/01/28 04:44:49 sanders Exp $
    */
   public class FileUtils
   {
  @@ -173,7 +176,7 @@
   
       /**
        * Returns the extension portion of a file specification string.
  -     * This everything after the last dot '.' in the filename (including
  +     * This everything after the last dot '.' in the filename (NOT including
        * the dot).
        */
       public static String extension(String filename)
  @@ -182,7 +185,7 @@
   
           if (lastDot >= 0)
           {
  -          return filename.substring(lastDot);
  +          return filename.substring(lastDot + 1);
           }
           else
           {
  @@ -297,4 +300,105 @@
       {
           return new File(fileName);
       }
  +
  +    /**
  +     * Given a directory and an array of extensions... return an array of
  +     * compliant files.
  +     *
  +     * TODO Should an ignore list be passed in?
  +     * TODO Should a recurse flag be passed in?
  +     *
  +     * The given extensions should be like "java" and not like ".java"
  +    */
  +    public static String[] getFilesFromExtension(String directory, String[] extensions)
{
  +
  +        Vector files = new Vector();
  +
  +        java.io.File currentDir = new java.io.File(directory);
  +
  +        String[] unknownFiles = currentDir.list();
  +
  +        if (unknownFiles == null) {
  +            return new String[0];
  +        }
  +
  +        for (int i = 0;i < unknownFiles.length;++i) {
  +            String currentFileName = directory + System.getProperty("file.separator") +
unknownFiles[i];
  +            java.io.File currentFile = new java.io.File(currentFileName);
  +
  +            if (currentFile.isDirectory()) {
  +
  +
  +                //ignore all CVS directories...
  +                if ( currentFile.getName().equals("CVS") ) {
  +                    continue;
  +                }
  +
  +
  +                //ok... transverse into this directory and get all the files... then combine
  +                //them with the current list.
  +
  +                String[] fetchFiles = getFilesFromExtension(currentFileName, extensions);
  +                files = blendFilesToVector( files, fetchFiles);
  +
  +            } else {
  +                //ok... add the file
  +
  +                String add = currentFile.getAbsolutePath();
  +                if ( isValidFile( add, extensions ) ) {
  +                    files.addElement( add );
  +
  +                }
  +
  +            }
  +        }
  +
  +        //ok... move the Vector into the files list...
  +
  +        String[] foundFiles = new String[files.size()];
  +        files.copyInto(foundFiles);
  +
  +        return foundFiles;
  +
  +    }
  +
  +
  +    /**
  +     * Private hepler method for getFilesFromExtension()
  +    */
  +    private static Vector blendFilesToVector(Vector v, String[] files) {
  +
  +        for (int i = 0; i < files.length; ++i) {
  +            v.addElement(files[i]);
  +        }
  +
  +        return v;
  +    }
  +
  +    /**
  +     * Checks to see if a file is of a particular type(s).
  +     * Note that if the file does not have an extension, an empty string
  +     * (&quot;&quot;) is matched for.
  +     *
  +    */
  +    private static boolean isValidFile(String file, String[] extensions) {
  +
  +
  +        String extension = FileUtils.extension(file);
  +        if (extension == null) {
  +            extension = "";
  +        }
  +
  +        //ok.. now that we have the "extension" go through the current know
  +        //excepted extensions and determine if this one is OK.
  +
  +        for (int i = 0; i < extensions.length; ++i) {
  +            if (extensions[i].equals(extension))
  +                return true;
  +        }
  +
  +        return false;
  +
  +    }
  +
   }
  
  
  
  1.2       +28 -7     jakarta-commons-sandbox/io/src/test/org/apache/commons/io/IOTestCase.java
  
  Index: IOTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/io/src/test/org/apache/commons/io/IOTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- IOTestCase.java	26 Jan 2002 02:47:42 -0000	1.1
  +++ IOTestCase.java	28 Jan 2002 04:44:49 -0000	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/io/src/test/org/apache/commons/io/IOTestCase.java,v
1.1 2002/01/26 02:47:42 sanders Exp $
  - * $Revision: 1.1 $
  - * $Date: 2002/01/26 02:47:42 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/io/src/test/org/apache/commons/io/IOTestCase.java,v
1.2 2002/01/28 04:44:49 sanders Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/01/28 04:44:49 $
    *
    * ====================================================================
    *
  @@ -78,7 +78,7 @@
    * </p>
    *
    * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   
   public class IOTestCase extends TestCase {
  @@ -115,10 +115,31 @@
   
   
       /**
  -     *  tests the string and int arrays of TestBean
  +     *
        */
  -    public void testSomething() {
  -
  +    public void testFileUtils() {
  +        String filename = "src/test/org/apache/commons/io/test.txt";
  +        String filename2 = "src/test/org/apache/commons/io/test2.txt";
  +        assertTrue("test.txt extension == \"txt\"", FileUtils.extension(filename).equals("txt"));
  +        assertTrue("Test file exists", FileUtils.fileExists(filename));
  +        assertTrue("Second test file does not exist", !FileUtils.fileExists(filename2));
  +        try {
  +            FileUtils.fileWrite(filename2, filename);
  +            assertTrue("Second file was written", FileUtils.fileExists(filename2));
  +            String file2contents = FileUtils.fileRead(filename2);
  +            assertTrue("Second file's contents correct", FileUtils.fileRead(filename2).equals(file2contents));
  +            FileUtils.fileDelete(filename2);
  +            assertTrue("Second test file does not exist", !FileUtils.fileExists(filename2));
  +        } catch (Exception e) {
  +            fail("Error reading or writing second test file: " + filename);
  +        }
  +
  +        try {
  +            String contents = FileUtils.fileRead(filename);
  +            assertTrue("FileUtils.fileRead()", contents.equals("This is a test"));
  +        } catch (Exception e) {
  +            fail("Error loading file: " + filename);
  +        }
       }
   
   
  
  
  
  1.1                  jakarta-commons-sandbox/io/src/test/org/apache/commons/io/test.txt
  
  Index: test.txt
  ===================================================================
  This is a test
  
  

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


Mime
View raw message