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