1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.finder;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import junit.textui.TestRunner;
23  
24  import java.io.IOException;
25  import java.io.File;
26  
27  import java.util.HashMap;
28  
29  public class FileFinderTest extends TestCase {
30  
31      private HashMap options;
32      private FileFinder finder;
33      private String dirStr = "src" + File.separator + "test" + File.separator + "find-data" + File.separator;
34      private File dir = new File(dirStr);
35  
36      public FileFinderTest(String name) {
37          super(name);
38      }
39  
40      public void setUp() {
41          finder = new FileFinder();
42          options = new HashMap();
43          options.put(Finder.NOT+Finder.PATH, "*.svn*");
44          // uncomment for debugging
45  //        finder.addFindListener( new DebugListener() );
46      }
47  
48      //-----------------------------------------------------------------------
49      // To test: 
50      // find(File, Map)
51  
52      public void testFindName() {
53          options.put(Finder.NAME, "file");
54          File[] files = finder.find(new File(dir, "name"), options);
55          assertEquals(1, files.length);
56      }
57  
58      public void testFindIName() {
59          options.put(Finder.INAME, "FiLe");
60          File[] files = finder.find(new File(dir, "name"), options);
61          assertEquals(1, files.length);
62      }
63  
64      public void testFindPath() {
65          options.put(Finder.PATH, dirStr+"path" + File.separator + "dir" + File.separator + "file");
66          File[] files = finder.find(new File(dir, "path"), options);
67          assertEquals(1, files.length);
68      }
69  
70      public void testFindIPath() {
71          options.put(Finder.IPATH, dirStr+"PAth" + File.separator + "dIR" + File.separator + "fILe");
72          File[] files = finder.find(new File(dir, "path"), options);
73          assertEquals(1, files.length);
74      }
75  
76      public void testFindNotPath() {
77          options.put(Finder.NOT+Finder.PATH, dirStr+"*");
78          File[] files = finder.find(new File(dir, "path"), options);
79          assertEquals(0, files.length);
80      }
81  
82      public void testFindRegex() {
83          options.put(Finder.REGEX, escapePath(dirStr+"regex" + File.separator + "f.*"));
84          File[] files = finder.find(new File(dir, "regex"), options);
85          assertEquals(3, files.length);
86      }
87  
88      public void testFindIRegex() {
89          options.put(Finder.IREGEX, escapePath(dirStr+"REgeX" + File.separator + "F.*"));
90          File[] files = finder.find(new File(dir, "regex"), options);
91          assertEquals(3, files.length);
92      }
93  
94      public void testFindEmpty() {
95          options.put(Finder.EMPTY, "true");
96          File[] files = finder.find(new File(dir, "empty"), options);
97          assertEquals(1, files.length);
98      }
99  
100     /**
101      * N.B. dir size on vmbuild is 4k which caused this test to fail in that
102      *      environment (incl. nightly build) - so excluding directories.
103      *      TODO - currently just one 4k file - need non-4k files for proper test
104      */
105     public void testFindSize() {
106         options.put(Finder.FILE, null);
107         options.put(Finder.SIZE, "4k");
108         File[] files = finder.find(new File(dir, "size"), options);
109         assertEquals(1, files.length);
110     }
111 
112     // finds one in file and also one in dir as 
113     // CVS needs a file in dir for people to commonly have it checked out
114     public void testFindTypeFile() {
115         options.put(Finder.DIRECTORY, "f");
116         File[] files = finder.find(new File(dir, "type"), options);
117         assertEquals(2, files.length);
118     }
119 
120     public void testFindTypeD() {
121         options.put(Finder.FILE, "d");
122         File[] files = finder.find(new File(dir, "type"), options);
123         assertEquals(2, files.length);
124     }
125 
126     public void testCanWriteTrue() {
127         options.put(Finder.CAN_WRITE, "true");
128         File[] files = finder.find(new File(dir, "can_write"), options);
129         assertEquals(2, files.length);
130     }
131 
132     public void testCanReadTrue() {
133         options.put(Finder.CAN_READ, "true");
134         File[] files = finder.find(new File(dir, "can_read"), options);
135         assertEquals(2, files.length);
136     }
137 
138     public void testCanWriteFalse() {
139         options.put(Finder.CANNOT_WRITE, "false");
140         File[] files = finder.find(new File(dir, "can_write"), options);
141         assertEquals(0, files.length);
142     }
143 
144     public void testCanReadFalse() {
145         options.put(Finder.CANNOT_READ, "false");
146         File[] files = finder.find(new File(dir, "can_read"), options);
147         assertEquals(0, files.length);
148     }
149 
150     private static String escapePath(String text) {
151         String repl = "\\";
152         String with = "\\\\";
153 
154         StringBuffer buf = new StringBuffer(text.length());
155         int start = 0, end = 0;
156         while ((end = text.indexOf(repl, start)) != -1) {
157             buf.append(text.substring(start, end)).append(with);
158             start = end + repl.length();
159         }
160         buf.append(text.substring(start));
161         return buf.toString();
162     }
163 
164 }
165 
166 class DebugListener implements FindListener {
167     public void fileFound(FindEvent fe) {
168         System.out.println("EVENT: "+fe);
169     }
170     public void directoryStarted(FindEvent fe) {
171         System.out.println("EVENT: "+fe);
172     }
173     public void directoryFinished(FindEvent fe) {
174         System.out.println("EVENT: "+fe);
175     }
176 }