View Javadoc
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.io.filefilter;
18  
19  import java.io.File;
20  
21  import junit.framework.TestCase;
22  
23  public abstract class IOFileFilterAbstractTestCase extends TestCase {
24  
25    /**
26     * Constructs a new instance of
27     * <code>IOFileFilterAbstractTestCase</code>.
28     */
29    public IOFileFilterAbstractTestCase(final String name) {
30      super(name);
31    }
32  
33    public boolean assertFileFiltering(final int testNumber, final IOFileFilter filter, final File file, final boolean expected)
34    throws Exception {
35      assertEquals(
36      "test " + testNumber + " Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file,
37      expected, filter.accept(file));
38      return true; // return is irrelevant
39    }
40  
41    public boolean assertFilenameFiltering(final int testNumber, final IOFileFilter filter, final File file, final boolean expected)
42    throws Exception {
43      // Assumes file has parent and is not passed as null
44        assertEquals(
45      "test " + testNumber + " Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file,
46      expected, filter.accept(file.getParentFile(), file.getName()));
47      return true; // return is irrelevant
48    }
49  
50    public void assertFiltering(final int testNumber, final IOFileFilter filter, final File file, final boolean expected)
51    throws Exception {
52      // Note. This only tests the (File, String) version if the parent of
53      //       the File passed in is not null
54        assertEquals(
55      "test " + testNumber + " Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file,
56      expected, filter.accept(file));
57  
58      if (file != null && file.getParentFile() != null) {
59          assertEquals(
60        "test " + testNumber + " Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file,
61        expected, filter.accept(file.getParentFile(), file.getName()));
62      }
63      else if (file == null) {
64          assertEquals(
65        "test " + testNumber + " Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null",
66        expected, filter.accept(file));
67      }
68    }
69  
70    public void assertTrueFiltersInvoked(final int testNumber, final TesterTrueFileFilter[] filters, final boolean[] invoked) {
71      for(int i = 1; i < filters.length; i++) {
72        assertEquals("test " + testNumber + " filter " + i + " invoked", invoked[i-1], filters[i].isInvoked());
73      }
74    }
75  
76    public void assertFalseFiltersInvoked(final int testNumber, final TesterFalseFileFilter[] filters, final boolean[] invoked) {
77      for(int i = 1; i < filters.length; i++) {
78        assertEquals("test " + testNumber + " filter " + i + " invoked", invoked[i-1], filters[i].isInvoked());
79      }
80    }
81  
82    public File determineWorkingDirectoryPath(final String key, final String defaultPath) {
83      // Look for a system property to specify the working directory
84      final String workingPathName = System.getProperty(key, defaultPath);
85      return new File(workingPathName);
86    }
87  
88    public void resetFalseFilters(final TesterFalseFileFilter[] filters) {
89      for (final TesterFalseFileFilter filter : filters) {
90        if(filter != null) {
91          filter.reset();
92        }
93      }
94    }
95  
96    public void resetTrueFilters(final TesterTrueFileFilter[] filters) {
97      for (final TesterTrueFileFilter filter : filters) {
98        if(filter != null) {
99          filter.reset();
100       }
101     }
102   }
103 
104   class TesterTrueFileFilter extends TrueFileFilter {
105 
106     private boolean invoked;
107 
108     @Override
109     public boolean accept(final File file) {
110       setInvoked(true);
111       return super.accept(file);
112     }
113 
114     @Override
115     public boolean accept(final File file, final String str) {
116       setInvoked(true);
117       return super.accept(file, str);
118     }
119 
120     public boolean isInvoked() {
121       return this.invoked;
122     }
123 
124     public void setInvoked(final boolean invoked) {
125       this.invoked = invoked;
126     }
127 
128     public void reset() {
129       setInvoked(false);
130     }
131   }
132 
133   class TesterFalseFileFilter extends FalseFileFilter {
134 
135     private boolean invoked;
136 
137     @Override
138     public boolean accept(final File file) {
139       setInvoked(true);
140       return super.accept(file);
141     }
142 
143     @Override
144     public boolean accept(final File file, final String str) {
145       setInvoked(true);
146       return super.accept(file, str);
147     }
148 
149     public boolean isInvoked() {
150       return this.invoked;
151     }
152 
153     public void setInvoked(final boolean invoked) {
154       this.invoked = invoked;
155     }
156 
157     public void reset() {
158       setInvoked(false);
159     }
160   }
161 }