KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > FileUtilsListFilesTestCase


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.io;
17
18 import java.io.File JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
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 /**
27  * Test cases for FileUtils.listFiles() methods.
28  */

29 public class FileUtilsListFilesTestCase extends FileBasedTestCase {
30
31     public FileUtilsListFilesTestCase(String JavaDoc name) {
32         super(name);
33     }
34     
35     private File JavaDoc getLocalTestDirectory() {
36         return new File JavaDoc(getTestDirectory(), "list-files");
37     }
38     
39     /**
40      * @see junit.framework.TestCase#setUp()
41      */

42     protected void setUp() throws Exception JavaDoc {
43         File JavaDoc dir = getLocalTestDirectory();
44         if (dir.exists()) {
45             FileUtils.deleteDirectory(dir);
46         }
47         dir.mkdirs();
48         File JavaDoc file = new File JavaDoc(dir, "dummy-build.xml");
49         FileUtils.touch(file);
50         file = new File JavaDoc(dir, "README");
51         FileUtils.touch(file);
52         
53         dir = new File JavaDoc(dir, "subdir1");
54         dir.mkdirs();
55         file = new File JavaDoc(dir, "dummy-build.xml");
56         FileUtils.touch(file);
57         file = new File JavaDoc(dir, "dummy-readme.txt");
58         FileUtils.touch(file);
59         
60         dir = new File JavaDoc(dir, "subsubdir1");
61         dir.mkdirs();
62         file = new File JavaDoc(dir, "dummy-file.txt");
63         FileUtils.touch(file);
64         file = new File JavaDoc(dir, "dummy-index.html");
65         FileUtils.touch(file);
66         
67         dir = dir.getParentFile();
68         dir = new File JavaDoc(dir, "CVS");
69         dir.mkdirs();
70         file = new File JavaDoc(dir, "Entries");
71         FileUtils.touch(file);
72         file = new File JavaDoc(dir, "Repository");
73         FileUtils.touch(file);
74     }
75
76     /**
77      * @see junit.framework.TestCase#tearDown()
78      */

79     protected void tearDown() throws Exception JavaDoc {
80         File JavaDoc dir = getLocalTestDirectory();
81         FileUtils.deleteDirectory(dir);
82     }
83     
84     private Collection JavaDoc filesToFilenames(Collection JavaDoc files) {
85         Collection JavaDoc filenames = new java.util.ArrayList JavaDoc(files.size());
86         Iterator JavaDoc i = files.iterator();
87         while (i.hasNext()) {
88             filenames.add(((File JavaDoc)i.next()).getName());
89         }
90         return filenames;
91     }
92     
93     public void testListFilesByExtension() throws Exception JavaDoc {
94         String JavaDoc[] extensions = {"xml", "txt"};
95         
96         Collection JavaDoc files = FileUtils.listFiles(getLocalTestDirectory(), extensions, false);
97         assertEquals(1, files.size());
98         Collection JavaDoc 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 JavaDoc {
119         Collection JavaDoc files;
120         Collection JavaDoc filenames;
121         IOFileFilter fileFilter;
122         IOFileFilter dirFilter;
123         
124         //First, find non-recursively
125
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         //Second, find recursively
133
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         //Do the same as above but now with the filter coming from FileFilterUtils
142
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         //Again with the CVS filter but now with a non-null parameter
151
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 JavaDoc e) {
164             //fine
165
}
166     }
167
168
169 }
170
Popular Tags