1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  import junit.textui.TestRunner;
27  
28  import org.apache.commons.io.FileUtils;
29  import org.apache.commons.io.testtools.FileBasedTestCase;
30  
31  /**
32   * Test cases for FileUtils.cleanDirectory() method.
33   *
34   * @version $Id: FileUtilsCleanDirectoryTestCase.java 518770 2007-03-15 22:02:46Z jochen $
35   * @author Chris Eldredge
36   */
37  public class FileUtilsCleanDirectoryTestCase extends FileBasedTestCase {
38      final File top = getLocalTestDirectory();
39  
40      public static void main(String[] args) {
41          TestRunner.run(suite());
42      }
43  
44      public static Test suite() {
45          return new TestSuite(FileUtilsCleanDirectoryTestCase.class);
46      }
47  
48      public FileUtilsCleanDirectoryTestCase(String name) {
49          super(name);
50      }
51  
52      private File getLocalTestDirectory() {
53          return new File(getTestDirectory(), "list-files");
54      }
55  
56      /**
57       * @see junit.framework.TestCase#setUp()
58       */
59      protected void setUp() throws Exception {
60          top.mkdirs();
61      }
62  
63      /**
64       * @see junit.framework.TestCase#tearDown()
65       */
66      protected void tearDown() throws Exception {
67          chmod(top, 775, true);
68          FileUtils.deleteDirectory(top);
69      }
70  
71      //-----------------------------------------------------------------------
72      public void testCleanEmpty() throws Exception {
73          assertEquals(0, top.list().length);
74  
75          FileUtils.cleanDirectory(top);
76  
77          assertEquals(0, top.list().length);
78      }
79  
80      public void testDeletesRegular() throws Exception {
81          FileUtils.touch(new File(top, "regular"));
82          FileUtils.touch(new File(top, ".hidden"));
83  
84          assertEquals(2, top.list().length);
85  
86          FileUtils.cleanDirectory(top);
87  
88          assertEquals(0, top.list().length);
89      }
90  
91      public void testDeletesNested() throws Exception {
92          final File nested = new File(top, "nested");
93  
94          assertTrue(nested.mkdirs());
95  
96          FileUtils.touch(new File(nested, "file"));
97  
98          assertEquals(1, top.list().length);
99  
100         FileUtils.cleanDirectory(top);
101 
102         assertEquals(0, top.list().length);
103     }
104 
105     public void testThrowsOnNullList() throws Exception {
106         if (System.getProperty("os.name").startsWith("Win")  ||  !chmod(top, 0, false)) {
107             // test wont work if we can't restrict permissions on the
108             // directory, so skip it.
109             return;
110         }
111 
112         try {
113             FileUtils.cleanDirectory(top);
114             fail("expected IOException");
115         } catch (IOException e) {
116             assertEquals("Failed to list contents of " +
117                     top.getAbsolutePath(), e.getMessage());
118         }
119     }
120 
121     public void testThrowsOnCannotDeleteFile() throws Exception {
122         final File file = new File(top, "restricted");
123         FileUtils.touch(file);
124 
125         if (System.getProperty("os.name").startsWith("Win")  ||  !chmod(top, 500, false)) {
126             // test wont work if we can't restrict permissions on the
127             // directory, so skip it.
128             return;
129         }
130 
131         try {
132             FileUtils.cleanDirectory(top);
133             fail("expected IOException");
134         } catch (IOException e) {
135             assertEquals("Unable to delete file: " +
136                     file.getAbsolutePath(), e.getMessage());
137         }
138     }
139 
140     private boolean chmod(File file, int mode, boolean recurse)
141             throws IOException, InterruptedException {
142         // TODO: Refactor this to FileSystemUtils
143         List args = new ArrayList();
144         args.add("chmod");
145 
146         if (recurse) {
147             args.add("-R");
148         }
149 
150         args.add(Integer.toString(mode));
151         args.add(file.getAbsolutePath());
152 
153         Process proc;
154 
155         try {
156             proc = Runtime.getRuntime().exec(
157                     (String[]) args.toArray(new String[args.size()]));
158         } catch (IOException e) {
159             return false;
160         }
161         int result = proc.waitFor();
162         return (result == 0);
163     }
164 
165 }