001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io.filefilter;
018:
019: import java.io.File;
020: import junit.framework.TestCase;
021:
022: public abstract class IOFileFilterAbstractTestCase extends TestCase {
023:
024: /**
025: * Constructs a new instance of
026: * <code>IOFileFilterAbstractTestCase</code>.
027: */
028: public IOFileFilterAbstractTestCase(final String name) {
029: super (name);
030: }
031:
032: public boolean assertFileFiltering(final int testNumber,
033: final IOFileFilter filter, final File file,
034: final boolean expected) throws Exception {
035: assertTrue("test " + testNumber + " Filter(File) "
036: + filter.getClass().getName() + " not " + expected
037: + " for " + file, (filter.accept(file) == expected));
038: return true; // return is irrelevant
039: }
040:
041: public boolean assertFilenameFiltering(final int testNumber,
042: final IOFileFilter filter, final File file,
043: final boolean expected) throws Exception {
044: // Assumes file has parent and is not passed as null
045: assertTrue("test " + testNumber + " Filter(File, String) "
046: + filter.getClass().getName() + " not " + expected
047: + " for " + file, (filter.accept(file.getParentFile(),
048: file.getName()) == expected));
049: return true; // return is irrelevant
050: }
051:
052: public void assertFiltering(final int testNumber,
053: final IOFileFilter filter, final File file,
054: final boolean expected) throws Exception {
055: // Note. This only tests the (File, String) version if the parent of
056: // the File passed in is not null
057: assertTrue("test " + testNumber + " Filter(File) "
058: + filter.getClass().getName() + " not " + expected
059: + " for " + file, (filter.accept(file) == expected));
060:
061: if (file != null && file.getParentFile() != null) {
062: assertTrue("test " + testNumber + " Filter(File, String) "
063: + filter.getClass().getName() + " not " + expected
064: + " for " + file, (filter.accept(file
065: .getParentFile(), file.getName()) == expected));
066: } else if (file == null) {
067: assertTrue("test " + testNumber + " Filter(File, String) "
068: + filter.getClass().getName() + " not " + expected
069: + " for null", filter.accept(file) == expected);
070: }
071: }
072:
073: public void assertTrueFiltersInvoked(final int testNumber,
074: final TesterTrueFileFilter[] filters,
075: final boolean[] invoked) {
076: for (int i = 1; i < filters.length; i++) {
077: assertEquals("test " + testNumber + " filter " + i
078: + " invoked", invoked[i - 1], filters[i]
079: .isInvoked());
080: }
081: }
082:
083: public void assertFalseFiltersInvoked(final int testNumber,
084: final TesterFalseFileFilter[] filters,
085: final boolean[] invoked) {
086: for (int i = 1; i < filters.length; i++) {
087: assertEquals("test " + testNumber + " filter " + i
088: + " invoked", invoked[i - 1], filters[i]
089: .isInvoked());
090: }
091: }
092:
093: public File determineWorkingDirectoryPath(final String key,
094: final String defaultPath) {
095: // Look for a system property to specify the working directory
096: String workingPathName = System.getProperty(key, defaultPath);
097: return new File(workingPathName);
098: }
099:
100: public void resetFalseFilters(TesterFalseFileFilter[] filters) {
101: for (int i = 0; i < filters.length; i++) {
102: if (filters[i] != null) {
103: filters[i].reset();
104: }
105: }
106: }
107:
108: public void resetTrueFilters(TesterTrueFileFilter[] filters) {
109: for (int i = 0; i < filters.length; i++) {
110: if (filters[i] != null) {
111: filters[i].reset();
112: }
113: }
114: }
115:
116: class TesterTrueFileFilter extends TrueFileFilter {
117:
118: private boolean invoked;
119:
120: public boolean accept(File file) {
121: setInvoked(true);
122: return super .accept(file);
123: }
124:
125: public boolean accept(File file, String str) {
126: setInvoked(true);
127: return super .accept(file, str);
128: }
129:
130: public boolean isInvoked() {
131: return this .invoked;
132: }
133:
134: public void setInvoked(boolean invoked) {
135: this .invoked = invoked;
136: }
137:
138: public void reset() {
139: setInvoked(false);
140: }
141: }
142:
143: class TesterFalseFileFilter extends FalseFileFilter {
144:
145: private boolean invoked;
146:
147: public boolean accept(File file) {
148: setInvoked(true);
149: return super .accept(file);
150: }
151:
152: public boolean accept(File file, String str) {
153: setInvoked(true);
154: return super .accept(file, str);
155: }
156:
157: public boolean isInvoked() {
158: return this .invoked;
159: }
160:
161: public void setInvoked(boolean invoked) {
162: this .invoked = invoked;
163: }
164:
165: public void reset() {
166: setInvoked(false);
167: }
168: }
169: }
|