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.util.Collection;
021: import java.util.Iterator;
022:
023: import org.apache.commons.io.filefilter.FileFilterUtils;
024: import org.apache.commons.io.filefilter.IOFileFilter;
025: import org.apache.commons.io.testtools.FileBasedTestCase;
026:
027: /**
028: * Test cases for FileUtils.listFiles() methods.
029: */
030: public class FileUtilsListFilesTestCase extends FileBasedTestCase {
031:
032: public FileUtilsListFilesTestCase(String name) {
033: super (name);
034: }
035:
036: private File getLocalTestDirectory() {
037: return new File(getTestDirectory(), "list-files");
038: }
039:
040: /**
041: * @see junit.framework.TestCase#setUp()
042: */
043: protected void setUp() throws Exception {
044: File dir = getLocalTestDirectory();
045: if (dir.exists()) {
046: FileUtils.deleteDirectory(dir);
047: }
048: dir.mkdirs();
049: File file = new File(dir, "dummy-build.xml");
050: FileUtils.touch(file);
051: file = new File(dir, "README");
052: FileUtils.touch(file);
053:
054: dir = new File(dir, "subdir1");
055: dir.mkdirs();
056: file = new File(dir, "dummy-build.xml");
057: FileUtils.touch(file);
058: file = new File(dir, "dummy-readme.txt");
059: FileUtils.touch(file);
060:
061: dir = new File(dir, "subsubdir1");
062: dir.mkdirs();
063: file = new File(dir, "dummy-file.txt");
064: FileUtils.touch(file);
065: file = new File(dir, "dummy-index.html");
066: FileUtils.touch(file);
067:
068: dir = dir.getParentFile();
069: dir = new File(dir, "CVS");
070: dir.mkdirs();
071: file = new File(dir, "Entries");
072: FileUtils.touch(file);
073: file = new File(dir, "Repository");
074: FileUtils.touch(file);
075: }
076:
077: /**
078: * @see junit.framework.TestCase#tearDown()
079: */
080: protected void tearDown() throws Exception {
081: File dir = getLocalTestDirectory();
082: FileUtils.deleteDirectory(dir);
083: }
084:
085: private Collection filesToFilenames(Collection files) {
086: Collection filenames = new java.util.ArrayList(files.size());
087: Iterator i = files.iterator();
088: while (i.hasNext()) {
089: filenames.add(((File) i.next()).getName());
090: }
091: return filenames;
092: }
093:
094: public void testListFilesByExtension() throws Exception {
095: String[] extensions = { "xml", "txt" };
096:
097: Collection files = FileUtils.listFiles(getLocalTestDirectory(),
098: extensions, false);
099: assertEquals(1, files.size());
100: Collection filenames = filesToFilenames(files);
101: assertTrue(filenames.contains("dummy-build.xml"));
102: assertFalse(filenames.contains("README"));
103: assertFalse(filenames.contains("dummy-file.txt"));
104:
105: files = FileUtils.listFiles(getLocalTestDirectory(),
106: extensions, true);
107: filenames = filesToFilenames(files);
108: assertEquals(4, filenames.size());
109: assertTrue(filenames.contains("dummy-file.txt"));
110: assertFalse(filenames.contains("dummy-index.html"));
111:
112: files = FileUtils.listFiles(getLocalTestDirectory(), null,
113: false);
114: assertEquals(2, files.size());
115: filenames = filesToFilenames(files);
116: assertTrue(filenames.contains("dummy-build.xml"));
117: assertTrue(filenames.contains("README"));
118: assertFalse(filenames.contains("dummy-file.txt"));
119:
120: }
121:
122: public void testListFiles() throws Exception {
123: Collection files;
124: Collection filenames;
125: IOFileFilter fileFilter;
126: IOFileFilter dirFilter;
127:
128: //First, find non-recursively
129: fileFilter = FileFilterUtils.trueFileFilter();
130: files = FileUtils.listFiles(getLocalTestDirectory(),
131: fileFilter, null);
132: filenames = filesToFilenames(files);
133: assertTrue("'dummy-build.xml' is missing", filenames
134: .contains("dummy-build.xml"));
135: assertFalse("'dummy-index.html' shouldn't be found", filenames
136: .contains("dummy-index.html"));
137: assertFalse("'Entries' shouldn't be found", filenames
138: .contains("Entries"));
139:
140: //Second, find recursively
141: fileFilter = FileFilterUtils.trueFileFilter();
142: dirFilter = FileFilterUtils.notFileFilter(FileFilterUtils
143: .nameFileFilter("CVS"));
144: files = FileUtils.listFiles(getLocalTestDirectory(),
145: fileFilter, dirFilter);
146: filenames = filesToFilenames(files);
147: assertTrue("'dummy-build.xml' is missing", filenames
148: .contains("dummy-build.xml"));
149: assertTrue("'dummy-index.html' is missing", filenames
150: .contains("dummy-index.html"));
151: assertFalse("'Entries' shouldn't be found", filenames
152: .contains("Entries"));
153:
154: //Do the same as above but now with the filter coming from FileFilterUtils
155: fileFilter = FileFilterUtils.trueFileFilter();
156: dirFilter = FileFilterUtils.makeCVSAware(null);
157: files = FileUtils.listFiles(getLocalTestDirectory(),
158: fileFilter, dirFilter);
159: filenames = filesToFilenames(files);
160: assertTrue("'dummy-build.xml' is missing", filenames
161: .contains("dummy-build.xml"));
162: assertTrue("'dummy-index.html' is missing", filenames
163: .contains("dummy-index.html"));
164: assertFalse("'Entries' shouldn't be found", filenames
165: .contains("Entries"));
166:
167: //Again with the CVS filter but now with a non-null parameter
168: fileFilter = FileFilterUtils.trueFileFilter();
169: dirFilter = FileFilterUtils.prefixFileFilter("sub");
170: dirFilter = FileFilterUtils.makeCVSAware(dirFilter);
171: files = FileUtils.listFiles(getLocalTestDirectory(),
172: fileFilter, dirFilter);
173: filenames = filesToFilenames(files);
174: assertTrue("'dummy-build.xml' is missing", filenames
175: .contains("dummy-build.xml"));
176: assertTrue("'dummy-index.html' is missing", filenames
177: .contains("dummy-index.html"));
178: assertFalse("'Entries' shouldn't be found", filenames
179: .contains("Entries"));
180:
181: try {
182: FileUtils.listFiles(getLocalTestDirectory(), null, null);
183: fail("Expected error about null parameter");
184: } catch (Exception e) {
185: //fine
186: }
187: }
188:
189: }
|