1 16 package org.apache.commons.io; 17 18 import java.io.File ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 22 import org.apache.commons.io.filefilter.FileFilterUtils; 23 import org.apache.commons.io.filefilter.IOFileFilter; 24 import org.apache.commons.io.testtools.FileBasedTestCase; 25 26 29 public class FileUtilsListFilesTestCase extends FileBasedTestCase { 30 31 public FileUtilsListFilesTestCase(String name) { 32 super(name); 33 } 34 35 private File getLocalTestDirectory() { 36 return new File (getTestDirectory(), "list-files"); 37 } 38 39 42 protected void setUp() throws Exception { 43 File dir = getLocalTestDirectory(); 44 if (dir.exists()) { 45 FileUtils.deleteDirectory(dir); 46 } 47 dir.mkdirs(); 48 File file = new File (dir, "dummy-build.xml"); 49 FileUtils.touch(file); 50 file = new File (dir, "README"); 51 FileUtils.touch(file); 52 53 dir = new File (dir, "subdir1"); 54 dir.mkdirs(); 55 file = new File (dir, "dummy-build.xml"); 56 FileUtils.touch(file); 57 file = new File (dir, "dummy-readme.txt"); 58 FileUtils.touch(file); 59 60 dir = new File (dir, "subsubdir1"); 61 dir.mkdirs(); 62 file = new File (dir, "dummy-file.txt"); 63 FileUtils.touch(file); 64 file = new File (dir, "dummy-index.html"); 65 FileUtils.touch(file); 66 67 dir = dir.getParentFile(); 68 dir = new File (dir, "CVS"); 69 dir.mkdirs(); 70 file = new File (dir, "Entries"); 71 FileUtils.touch(file); 72 file = new File (dir, "Repository"); 73 FileUtils.touch(file); 74 } 75 76 79 protected void tearDown() throws Exception { 80 File dir = getLocalTestDirectory(); 81 FileUtils.deleteDirectory(dir); 82 } 83 84 private Collection filesToFilenames(Collection files) { 85 Collection filenames = new java.util.ArrayList (files.size()); 86 Iterator i = files.iterator(); 87 while (i.hasNext()) { 88 filenames.add(((File )i.next()).getName()); 89 } 90 return filenames; 91 } 92 93 public void testListFilesByExtension() throws Exception { 94 String [] extensions = {"xml", "txt"}; 95 96 Collection files = FileUtils.listFiles(getLocalTestDirectory(), extensions, false); 97 assertEquals(1, files.size()); 98 Collection filenames = filesToFilenames(files); 99 assertTrue(filenames.contains("dummy-build.xml")); 100 assertFalse(filenames.contains("README")); 101 assertFalse(filenames.contains("dummy-file.txt")); 102 103 files = FileUtils.listFiles(getLocalTestDirectory(), extensions, true); 104 filenames = filesToFilenames(files); 105 assertEquals(4, filenames.size()); 106 assertTrue(filenames.contains("dummy-file.txt")); 107 assertFalse(filenames.contains("dummy-index.html")); 108 109 files = FileUtils.listFiles(getLocalTestDirectory(), null, false); 110 assertEquals(2, files.size()); 111 filenames = filesToFilenames(files); 112 assertTrue(filenames.contains("dummy-build.xml")); 113 assertTrue(filenames.contains("README")); 114 assertFalse(filenames.contains("dummy-file.txt")); 115 116 } 117 118 public void testListFiles() throws Exception { 119 Collection files; 120 Collection filenames; 121 IOFileFilter fileFilter; 122 IOFileFilter dirFilter; 123 124 fileFilter = FileFilterUtils.trueFileFilter(); 126 files = FileUtils.listFiles(getLocalTestDirectory(), fileFilter, null); 127 filenames = filesToFilenames(files); 128 assertTrue("'dummy-build.xml' is missing", filenames.contains("dummy-build.xml")); 129 assertFalse("'dummy-index.html' shouldn't be found", filenames.contains("dummy-index.html")); 130 assertFalse("'Entries' shouldn't be found", filenames.contains("Entries")); 131 132 fileFilter = FileFilterUtils.trueFileFilter(); 134 dirFilter = FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter("CVS")); 135 files = FileUtils.listFiles(getLocalTestDirectory(), fileFilter, dirFilter); 136 filenames = filesToFilenames(files); 137 assertTrue("'dummy-build.xml' is missing", filenames.contains("dummy-build.xml")); 138 assertTrue("'dummy-index.html' is missing", filenames.contains("dummy-index.html")); 139 assertFalse("'Entries' shouldn't be found", filenames.contains("Entries")); 140 141 fileFilter = FileFilterUtils.trueFileFilter(); 143 dirFilter = FileFilterUtils.makeCVSAware(null); 144 files = FileUtils.listFiles(getLocalTestDirectory(), fileFilter, dirFilter); 145 filenames = filesToFilenames(files); 146 assertTrue("'dummy-build.xml' is missing", filenames.contains("dummy-build.xml")); 147 assertTrue("'dummy-index.html' is missing", filenames.contains("dummy-index.html")); 148 assertFalse("'Entries' shouldn't be found", filenames.contains("Entries")); 149 150 fileFilter = FileFilterUtils.trueFileFilter(); 152 dirFilter = FileFilterUtils.prefixFileFilter("sub"); 153 dirFilter = FileFilterUtils.makeCVSAware(dirFilter); 154 files = FileUtils.listFiles(getLocalTestDirectory(), fileFilter, dirFilter); 155 filenames = filesToFilenames(files); 156 assertTrue("'dummy-build.xml' is missing", filenames.contains("dummy-build.xml")); 157 assertTrue("'dummy-index.html' is missing", filenames.contains("dummy-index.html")); 158 assertFalse("'Entries' shouldn't be found", filenames.contains("Entries")); 159 160 try { 161 FileUtils.listFiles(getLocalTestDirectory(), null, null); 162 fail("Expected error about null parameter"); 163 } catch (Exception e) { 164 } 166 } 167 168 169 } 170 | Popular Tags |