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  import java.util.ArrayList;
21  import java.util.List;
22  
23  public abstract class ConditionalFileFilterAbstractTestCase
24    extends IOFileFilterAbstractTestCase {
25  
26    private static final String TEST_FILE_NAME_PREFIX = "TestFile";
27    private static final String TEST_FILE_TYPE = ".tst";
28  
29    protected TesterTrueFileFilter[] trueFilters;
30    protected TesterFalseFileFilter[] falseFilters;
31  
32    private File file;
33    private File workingPath;
34  
35    public ConditionalFileFilterAbstractTestCase(final String name) {
36      super(name);
37    }
38  
39    @Override
40    public void setUp() throws Exception {
41      this.workingPath = this.determineWorkingDirectoryPath(this.getWorkingPathNamePropertyKey(), this.getDefaultWorkingPath());
42      this.file = new File(this.workingPath, TEST_FILE_NAME_PREFIX + 1 + TEST_FILE_TYPE);
43      this.trueFilters = new TesterTrueFileFilter[4];
44      this.falseFilters = new TesterFalseFileFilter[4];
45      this.trueFilters[1] = new TesterTrueFileFilter();
46      this.trueFilters[2] = new TesterTrueFileFilter();
47      this.trueFilters[3] = new TesterTrueFileFilter();
48      this.falseFilters[1] = new TesterFalseFileFilter();
49      this.falseFilters[2] = new TesterFalseFileFilter();
50      this.falseFilters[3] = new TesterFalseFileFilter();
51    }
52  
53    public void testAdd() {
54      final List<TesterTrueFileFilter> filters = new ArrayList<TesterTrueFileFilter>();
55      final ConditionalFileFilter fileFilter = this.getConditionalFileFilter();
56      filters.add(new TesterTrueFileFilter());
57      filters.add(new TesterTrueFileFilter());
58      filters.add(new TesterTrueFileFilter());
59      filters.add(new TesterTrueFileFilter());
60      for(int i = 0; i < filters.size(); i++) {
61        assertEquals("file filters count: ", i, fileFilter.getFileFilters().size());
62        fileFilter.addFileFilter(filters.get(i));
63        assertEquals("file filters count: ", i+1, fileFilter.getFileFilters().size());
64      }
65      for (final IOFileFilter filter : fileFilter.getFileFilters()) {
66        assertTrue("found file filter", filters.contains(filter));
67      }
68      assertEquals("file filters count", filters.size(), fileFilter.getFileFilters().size());
69    }
70  
71    public void testRemove() {
72      final List<TesterTrueFileFilter> filters = new ArrayList<TesterTrueFileFilter>();
73      final ConditionalFileFilter fileFilter = this.getConditionalFileFilter();
74      filters.add(new TesterTrueFileFilter());
75      filters.add(new TesterTrueFileFilter());
76      filters.add(new TesterTrueFileFilter());
77      filters.add(new TesterTrueFileFilter());
78      for(int i = 0; i < filters.size(); i++) {
79        fileFilter.removeFileFilter(filters.get(i));
80        assertTrue("file filter removed", !fileFilter.getFileFilters().contains(filters.get(i)));
81      }
82      assertEquals("file filters count", 0, fileFilter.getFileFilters().size());
83    }
84  
85    public void testNoFilters() throws Exception {
86      final ConditionalFileFilter fileFilter = this.getConditionalFileFilter();
87      final File file = new File(this.workingPath, TEST_FILE_NAME_PREFIX + 1 + TEST_FILE_TYPE);
88      assertFileFiltering(1, (IOFileFilter) fileFilter, file, false);
89      assertFilenameFiltering(1, (IOFileFilter) fileFilter, file, false);
90    }
91  
92    public void testFilterBuiltUsingConstructor() throws Exception {
93      final List<List<IOFileFilter>> testFilters = this.getTestFilters();
94      final List<boolean []> testTrueResults = this.getTrueResults();
95      final List<boolean []> testFalseResults = this.getFalseResults();
96      final List<Boolean> testFileResults = this.getFileResults();
97      final List<Boolean> testFilenameResults = this.getFilenameResults();
98  
99      for(int i = 1; i < testFilters.size(); i++) {
100       final List<IOFileFilter> filters = testFilters.get(i);
101       final boolean[] trueResults = testTrueResults.get(i);
102       final boolean[] falseResults = testFalseResults.get(i);
103       final boolean fileResults = testFileResults.get(i).booleanValue();
104       final boolean filenameResults = testFilenameResults.get(i).booleanValue();
105 
106       // Test conditional AND filter created by passing filters to the constructor
107       final IOFileFilter filter = this.buildFilterUsingConstructor(filters);
108 
109       // Test as a file filter
110       this.resetTrueFilters(this.trueFilters);
111       this.resetFalseFilters(this.falseFilters);
112       this.assertFileFiltering(i, filter, this.file, fileResults);
113       this.assertTrueFiltersInvoked(i, trueFilters, trueResults);
114       this.assertFalseFiltersInvoked(i, falseFilters, falseResults);
115 
116       // Test as a filename filter
117       this.resetTrueFilters(this.trueFilters);
118       this.resetFalseFilters(this.falseFilters);
119       this.assertFilenameFiltering(i, filter, this.file, filenameResults);
120       this.assertTrueFiltersInvoked(i, trueFilters, trueResults);
121       this.assertFalseFiltersInvoked(i, falseFilters, falseResults);
122     }
123   }
124 
125   public void testFilterBuiltUsingAdd() throws Exception {
126     final List<List<IOFileFilter>> testFilters = this.getTestFilters();
127     final List<boolean[]> testTrueResults = this.getTrueResults();
128     final List<boolean[]> testFalseResults = this.getFalseResults();
129     final List<Boolean> testFileResults = this.getFileResults();
130     final List<Boolean> testFilenameResults = this.getFilenameResults();
131 
132     for(int i = 1; i < testFilters.size(); i++) {
133       final List<IOFileFilter> filters = testFilters.get(i);
134       final boolean[] trueResults = testTrueResults.get(i);
135       final boolean[] falseResults = testFalseResults.get(i);
136       final boolean fileResults = testFileResults.get(i).booleanValue();
137       final boolean filenameResults = testFilenameResults.get(i).booleanValue();
138 
139       // Test conditional AND filter created by passing filters to the constructor
140       final IOFileFilter filter = this.buildFilterUsingAdd(filters);
141 
142       // Test as a file filter
143       this.resetTrueFilters(this.trueFilters);
144       this.resetFalseFilters(this.falseFilters);
145       this.assertFileFiltering(i, filter, this.file, fileResults);
146       this.assertTrueFiltersInvoked(i, trueFilters, trueResults);
147       this.assertFalseFiltersInvoked(i, falseFilters, falseResults);
148 
149       // Test as a filename filter
150       this.resetTrueFilters(this.trueFilters);
151       this.resetFalseFilters(this.falseFilters);
152       this.assertFilenameFiltering(i, filter, this.file, filenameResults);
153       this.assertTrueFiltersInvoked(i, trueFilters, trueResults);
154       this.assertFalseFiltersInvoked(i, falseFilters, falseResults);
155     }
156   }
157 
158   protected abstract ConditionalFileFilter getConditionalFileFilter();
159   protected abstract IOFileFilter buildFilterUsingAdd(List<IOFileFilter> filters);
160   protected abstract IOFileFilter buildFilterUsingConstructor(List<IOFileFilter> filters);
161   protected abstract List<List<IOFileFilter>> getTestFilters();
162   protected abstract List<boolean[]> getTrueResults();
163   protected abstract List<boolean[]> getFalseResults();
164   protected abstract List<Boolean> getFileResults();
165   protected abstract List<Boolean> getFilenameResults();
166   protected abstract String getWorkingPathNamePropertyKey();
167   protected abstract String getDefaultWorkingPath();
168 }