1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary <rdar://problem/2226785> EXT: delete() fails to delete directory
  27  * @summary com.apple.junit.java.io.File
  28  */
  29 
  30 import junit.framework.*;
  31 import java.io.File;
  32 
  33 public class R2226785File_delete extends TestCase {
  34     static final boolean DEBUG = false;
  35 
  36     public static Test suite() {
  37         return new TestSuite(R2226785File_delete.class);
  38     }
  39 
  40     public static void main( String[] args ) {
  41         junit.textui.TestRunner.run( suite() );
  42     }
  43 
  44     private String STARTINGPOINT = System.getProperty("java.io.tmpdir");
  45 
  46     public void MakeFolder( File inFolder ) throws Exception {
  47         File dot = new File(STARTINGPOINT);
  48         assertTrue( "ERROR: the directory " + dot + " is not writeable!", dot.canWrite() );
  49         assertTrue( "ERROR: inFolder.mkdirs() " + inFolder , inFolder.mkdirs() );
  50     }
  51 
  52     public boolean TestDelete( String absolutePath ) {
  53         File f = new File( absolutePath );
  54 
  55         if( f.exists() ) {
  56             if(!f.delete()) {
  57                 if (DEBUG) System.out.println("Unable to delete (" + absolutePath +").");
  58                 if (f.isDirectory() && (f.list().length > 0)) {
  59                     if (DEBUG) System.out.println("Ah ha! It's a non-empty directory. So we shouldn't be able to delete it!");
  60                 }
  61                 return false;
  62             }
  63         } else {
  64             if (DEBUG) System.out.println("ERROR: (" + absolutePath + ") does not appear to exist!");
  65             return false;
  66         }
  67         return true;
  68     }
  69 
  70     public void testCreateDelete() throws Exception {
  71         File testFolder1 = new File(STARTINGPOINT, "testFolder1");
  72 
  73         assertFalse("Please delete previously created test files and folders.\nTheir existence indicates a previous failure.", testFolder1.exists() );
  74         MakeFolder( testFolder1 );
  75 
  76 
  77         File testFolder2 = new File( testFolder1, "testFolder2" );
  78         if (DEBUG) System.out.println( "Making " + testFolder2.toString());
  79 
  80         MakeFolder( testFolder2 );
  81         assertTrue( "Both folders successfully created.", testFolder1.exists() && testFolder2.exists());
  82 
  83         if (DEBUG) System.out.println( "Deleting a folder which contains a sub-folder: " + testFolder1);
  84 
  85         assertFalse( "ERROR: File.delete() deleted a non-empty folder.",  TestDelete( testFolder1.getAbsolutePath() ));
  86 
  87         if (DEBUG) System.out.println( "Deleting " + testFolder2);
  88 
  89         assertTrue("ERROR: File.delete() failed.", TestDelete( testFolder2.getAbsolutePath() ));
  90         assertFalse( "ERROR: (" + testFolder2 + ") exists after being deleted.", testFolder2.exists() );
  91 
  92         if (DEBUG) System.out.println( "Deleting " + testFolder1);
  93 
  94         assertTrue("ERROR: File.delete() failed.", TestDelete( testFolder1.getAbsolutePath() ) );
  95         assertFalse("ERROR: (" + testFolder1 + ") exists after being deleted.", testFolder1.exists() );
  96     }
  97 }
  98