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