commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject cvs commit: jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test ProviderDeleteTests.java
Date Thu, 03 Jun 2004 17:13:24 GMT
imario      2004/06/03 10:13:24

  Modified:    vfs/src/java/org/apache/commons/vfs/provider
                        AbstractFileObject.java
               vfs/src/java/org/apache/commons/vfs FileObject.java
  Added:       vfs/src/test/org/apache/commons/vfs/test
                        ProviderDeleteTests.java
  Log:
  additional informations on delete:
  delete()  returns true if a delete operation happens
  delete(FileSelector) returns the number of deleted files
  + Testcase
  
  Revision  Changes    Path
  1.45      +24 -9     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java
  
  Index: AbstractFileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- AbstractFileObject.java	23 May 2004 18:34:33 -0000	1.44
  +++ AbstractFileObject.java	3 Jun 2004 17:13:24 -0000	1.45
  @@ -590,8 +590,10 @@
   
       /**
        * Deletes this file, once all its children have been deleted
  +     *
  +     * @return true if this file has been deleted
        */
  -    private void deleteSelf() throws FileSystemException
  +    private boolean deleteSelf() throws FileSystemException
       {
           if (!isWriteable())
           {
  @@ -601,7 +603,7 @@
           if (type == FileType.IMAGINARY)
           {
               // File does not exist
  -            return;
  +            return false;
           }
   
           try
  @@ -620,28 +622,36 @@
           {
               throw new FileSystemException("vfs.provider/delete.error", new Object[]{name},
exc);
           }
  +
  +        return true;
       }
   
       /**
        * Deletes this file.
        *
  +     * @return true if this object has been deleted
        * @todo This will not fail if this is a non-empty folder.
        */
  -    public void delete() throws FileSystemException
  +    public boolean delete() throws FileSystemException
       {
  -        delete(Selectors.SELECT_SELF);
  +        return delete(Selectors.SELECT_SELF) > 0;
       }
   
       /**
        * Deletes this file, and all children.
  +     *
  +     * @return the number of deleted files
        */
  -    public void delete(final FileSelector selector) throws FileSystemException
  +    public int delete(final FileSelector selector) throws FileSystemException
       {
           attach();
  +
  +        int nuofDeleted = 0;
  +
           if (type == FileType.IMAGINARY)
           {
               // File does not exist
  -            return;
  +            return nuofDeleted;
           }
   
           // Locate all the files to delete
  @@ -658,14 +668,19 @@
               // If the file is a folder, make sure all its children have been deleted
               if (file.type == FileType.FOLDER && file.getChildren().length != 0)
               {
  -                // TODO - fail??
  -                // Skip
  +                // Skip - as the selector forced us not to delete all files
                   continue;
               }
   
               // Delete the file
  -            file.deleteSelf();
  +            boolean deleted = file.deleteSelf();
  +            if (deleted)
  +            {
  +                nuofDeleted++;
  +            }
           }
  +
  +        return nuofDeleted;
       }
   
       /**
  
  
  
  1.26      +7 -5      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileObject.java
  
  Index: FileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileObject.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- FileObject.java	10 May 2004 20:09:45 -0000	1.25
  +++ FileObject.java	3 Jun 2004 17:13:24 -0000	1.26
  @@ -189,14 +189,15 @@
       FileObject[] findFiles(FileSelector selector) throws FileSystemException;
   
       /**
  -     * Deletes this file.  Does nothing if this file does not exist.  Does
  -     * not delete any descendents of this file, use {@link #delete(FileSelector)}
  -     * for that.
  +     * Deletes this file.  Does nothing if this file does not exist of if it is a
  +     * folder that has children.  Does not delete any descendents of this file,
  +     * use {@link #delete(FileSelector)} for that.
        *
  +     * @return true if this object has been deleted
        * @throws FileSystemException If this file is a non-empty folder, or if this file
is read-only,
        *                             or on error deleteing this file.
        */
  -    void delete() throws FileSystemException;
  +    boolean delete() throws FileSystemException;
   
       /**
        * Deletes all descendents of this file that match a selector.  Does
  @@ -206,10 +207,11 @@
        * exception, this file will potentially only be partially deleted.
        *
        * @param selector The selector to use to select which files to delete.
  +     * @return the number of deleted objects
        * @throws FileSystemException If this file or one of its descendents is read-only,
or on error
        *                             deleting this file or one of its descendents.
        */
  -    void delete(FileSelector selector) throws FileSystemException;
  +    int delete(FileSelector selector) throws FileSystemException;
   
       /**
        * Creates this folder, if it does not exist.  Also creates any ancestor
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/ProviderDeleteTests.java
  
  Index: ProviderDeleteTests.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.vfs.test;
  
  import org.apache.commons.vfs.Capability;
  import org.apache.commons.vfs.FileObject;
  import org.apache.commons.vfs.FileSelectInfo;
  import org.apache.commons.vfs.FileSelector;
  import org.apache.commons.vfs.FileType;
  import org.apache.commons.vfs.FileTypeSelector;
  import org.apache.commons.vfs.Selectors;
  
  /**
   * File system test that do some delete operations.
   *
   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
   */
  public class ProviderDeleteTests
      extends AbstractProviderTestCase
  {
      private class FileNameSelector implements FileSelector
      {
          final String basename;
  
          private FileNameSelector(String basename)
          {
              this.basename = basename;
          }
  
          public boolean includeFile(FileSelectInfo fileInfo) throws Exception
          {
              return this.basename.equals(fileInfo.getFile().getName().getBaseName());
          }
  
          public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception
          {
              return true;
          }
      }
  
      /**
       * Returns the capabilities required by the tests of this test case.
       */
      protected Capability[] getRequiredCaps()
      {
          return new Capability[]
          {
              Capability.CREATE,
              Capability.DELETE,
              Capability.GET_TYPE,
              Capability.LIST_CHILDREN,
          };
      }
  
      /**
       * Sets up a scratch folder for the test to use.
       */
      protected FileObject createScratchFolder() throws Exception
      {
          FileObject scratchFolder = getWriteFolder();
  
          // Make sure the test folder is empty
          scratchFolder.delete(Selectors.EXCLUDE_SELF);
          scratchFolder.createFolder();
  
          final FileObject dir1 = scratchFolder.resolveFile("dir1");
          dir1.createFolder();
          final FileObject dir1file1 = dir1.resolveFile("a.txt");
          dir1file1.createFile();
          final FileObject dir2 = scratchFolder.resolveFile("dir2");
          dir2.createFolder();
          final FileObject dir2file1 = dir2.resolveFile("b.txt");
          dir2file1.createFile();
  
          return scratchFolder;
      }
  
      /**
       * deletes the complete structure
       */
      public void testDeleteFiles() throws Exception
      {
          final FileObject scratchFolder = createScratchFolder();
  
          assertEquals(scratchFolder.delete(Selectors.EXCLUDE_SELF), 4);
      }
  
      /**
       * deletes a single file
       */
      public void testDeleteFile() throws Exception
      {
          final FileObject scratchFolder = createScratchFolder();
  
          final FileObject file = scratchFolder.resolveFile("dir1/a.txt");
  
          assertTrue(file.delete());
      }
  
      /**
       * Deletes a non existent file
       */
      public void testDeleteNonExistantFile() throws Exception
      {
          final FileObject scratchFolder = createScratchFolder();
  
          final FileObject file = scratchFolder.resolveFile("dir1/aa.txt");
  
          assertFalse(file.delete());
      }
  
      /**
       * deletes files
       */
      public void testDeleteAllFiles() throws Exception
      {
          final FileObject scratchFolder = createScratchFolder();
  
          assertEquals(scratchFolder.delete(new FileTypeSelector(FileType.FILE)), 2);
      }
  
      /**
       * deletes a.txt
       */
      public void testDeleteOneFiles() throws Exception
      {
          final FileObject scratchFolder = createScratchFolder();
  
          assertEquals(scratchFolder.delete(new FileNameSelector("a.txt")), 1);
      }
  }
  
  
  

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


Mime
View raw message