001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026: import junit.textui.TestRunner;
027:
028: import org.apache.commons.io.FileUtils;
029: import org.apache.commons.io.testtools.FileBasedTestCase;
030:
031: /**
032: * Test cases for FileUtils.cleanDirectory() method.
033: *
034: * @version $Id: FileUtilsCleanDirectoryTestCase.java 518770 2007-03-15 22:02:46Z jochen $
035: * @author Chris Eldredge
036: */
037: public class FileUtilsCleanDirectoryTestCase extends FileBasedTestCase {
038: final File top = getLocalTestDirectory();
039:
040: public static void main(String[] args) {
041: TestRunner.run(suite());
042: }
043:
044: public static Test suite() {
045: return new TestSuite(FileUtilsCleanDirectoryTestCase.class);
046: }
047:
048: public FileUtilsCleanDirectoryTestCase(String name) {
049: super (name);
050: }
051:
052: private File getLocalTestDirectory() {
053: return new File(getTestDirectory(), "list-files");
054: }
055:
056: /**
057: * @see junit.framework.TestCase#setUp()
058: */
059: protected void setUp() throws Exception {
060: top.mkdirs();
061: }
062:
063: /**
064: * @see junit.framework.TestCase#tearDown()
065: */
066: protected void tearDown() throws Exception {
067: chmod(top, 775, true);
068: FileUtils.deleteDirectory(top);
069: }
070:
071: //-----------------------------------------------------------------------
072: public void testCleanEmpty() throws Exception {
073: assertEquals(0, top.list().length);
074:
075: FileUtils.cleanDirectory(top);
076:
077: assertEquals(0, top.list().length);
078: }
079:
080: public void testDeletesRegular() throws Exception {
081: FileUtils.touch(new File(top, "regular"));
082: FileUtils.touch(new File(top, ".hidden"));
083:
084: assertEquals(2, top.list().length);
085:
086: FileUtils.cleanDirectory(top);
087:
088: assertEquals(0, top.list().length);
089: }
090:
091: public void testDeletesNested() throws Exception {
092: final File nested = new File(top, "nested");
093:
094: assertTrue(nested.mkdirs());
095:
096: FileUtils.touch(new File(nested, "file"));
097:
098: assertEquals(1, top.list().length);
099:
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")
107: || !chmod(top, 0, false)) {
108: // test wont work if we can't restrict permissions on the
109: // directory, so skip it.
110: return;
111: }
112:
113: try {
114: FileUtils.cleanDirectory(top);
115: fail("expected IOException");
116: } catch (IOException e) {
117: assertEquals("Failed to list contents of "
118: + top.getAbsolutePath(), e.getMessage());
119: }
120: }
121:
122: public void testThrowsOnCannotDeleteFile() throws Exception {
123: final File file = new File(top, "restricted");
124: FileUtils.touch(file);
125:
126: if (System.getProperty("os.name").startsWith("Win")
127: || !chmod(top, 500, false)) {
128: // test wont work if we can't restrict permissions on the
129: // directory, so skip it.
130: return;
131: }
132:
133: try {
134: FileUtils.cleanDirectory(top);
135: fail("expected IOException");
136: } catch (IOException e) {
137: assertEquals("Unable to delete file: "
138: + file.getAbsolutePath(), e.getMessage());
139: }
140: }
141:
142: private boolean chmod(File file, int mode, boolean recurse)
143: throws IOException, InterruptedException {
144: // TODO: Refactor this to FileSystemUtils
145: List args = new ArrayList();
146: args.add("chmod");
147:
148: if (recurse) {
149: args.add("-R");
150: }
151:
152: args.add(Integer.toString(mode));
153: args.add(file.getAbsolutePath());
154:
155: Process proc;
156:
157: try {
158: proc = Runtime.getRuntime().exec(
159: (String[]) args.toArray(new String[args.size()]));
160: } catch (IOException e) {
161: return false;
162: }
163: int result = proc.waitFor();
164: return (result == 0);
165: }
166:
167: }
|