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