1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34
35
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
58
59 protected void setUp() throws Exception {
60 top.mkdirs();
61 }
62
63
64
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
108
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
127
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
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 }