001: /*
002: * @(#)ExtensionFilenameFilterUTest.java
003: *
004: * Copyright (C) 2001,,2003 2002 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.io.v1;
028:
029: import java.io.File;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: /**
036: *
037: *
038: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
039: * @since 0.9.1d Alpha (sometime in 2001)
040: * @version $Date: 2003/02/10 22:52:45 $
041: */
042: public class ExtensionFilenameFilterUTest extends TestCase {
043: //-------------------------------------------------------------------------
044: // Global constructs that only depend upon the class name
045: private static final Class THIS_CLASS = ExtensionFilenameFilterUTest.class;
046:
047: public ExtensionFilenameFilterUTest(String name) {
048: super (name);
049: }
050:
051: //-------------------------------------------------------------------------
052: // Test methods
053:
054: public void testInstantiate() {
055: createFilter();
056: createFilter("");
057: createFilter("a");
058: createFilter(".a");
059: createFilter(".A");
060: createFilter(true);
061: createFilter(false);
062: createFilter(new String[] { "" });
063: createFilter(new String[] { "B", "A" });
064: createFilter("", true);
065: createFilter("", false);
066: createFilter("A", true);
067: createFilter("A", false);
068: createFilter(new String[] { "A", "a" }, true);
069: createFilter(new String[] { "A", "a" }, false);
070: }
071:
072: public void testInstantiateBad1() {
073: try {
074: createFilter((String) null);
075: fail("did not throw an IllegalArgumentException.");
076: } catch (IllegalArgumentException iae) {
077: }
078: }
079:
080: public void testInstantiateBad2() {
081: try {
082: createFilter((String[]) null);
083: fail("did not throw an IllegalArgumentException.");
084: } catch (IllegalArgumentException iae) {
085: }
086: }
087:
088: public void testInstantiateBad3() {
089: try {
090: createFilter(new String[] { null });
091: fail("did not throw an IllegalArgumentException.");
092: } catch (IllegalArgumentException iae) {
093: }
094: }
095:
096: public void testInstantiateBad4() {
097: try {
098: createFilter(new String[] { "", null });
099: fail("did not throw an IllegalArgumentException.");
100: } catch (IllegalArgumentException iae) {
101: }
102: }
103:
104: public void testInstantiateBad5() {
105: try {
106: createFilter(new String[] { "a", null });
107: fail("did not throw an IllegalArgumentException.");
108: } catch (IllegalArgumentException iae) {
109: }
110: }
111:
112: public void testInstantiateBad6() {
113: try {
114: createFilter((String) null, true);
115: fail("did not throw an IllegalArgumentException.");
116: } catch (IllegalArgumentException iae) {
117: }
118: }
119:
120: public void testInstantiateBad7() {
121: try {
122: createFilter((String[]) null, false);
123: fail("did not throw an IllegalArgumentException.");
124: } catch (IllegalArgumentException iae) {
125: }
126: }
127:
128: public void testDefaultDirectorySetting() {
129: assertTrue("Must allow directories by default.", createFilter()
130: .allowsDirectories());
131: }
132:
133: public void testNullAccept1() {
134: try {
135: createFilter("a").accept(null, null);
136: fail("Did not throw IllegalArgumentException.");
137: } catch (IllegalArgumentException e) {
138: }
139: }
140:
141: public void testNullAccept2() {
142: try {
143: createFilter("a").accept(new File("b"), null);
144: fail("Did not throw IllegalArgumentException.");
145: } catch (IllegalArgumentException e) {
146: }
147: }
148:
149: public void testEmptyAccept() {
150: noMatchFile(createFilter(), new File("a"));
151: }
152:
153: public void testAcceptCaseSensitive() {
154: matchFile(createFilter("A", false), new File("2/A"));
155: matchFile(createFilter("A", false), new File("2/.A"));
156: matchFile(createFilter("A", false), new File("2/asdfA"));
157: matchFile(createFilter("A", false), new File("2/asdf.A"));
158: matchFile(createFilter(".a", false), new File("2/b.a"));
159: matchFile(createFilter(".a", false), new File("2/.a"));
160: matchFile(createFilter(".a", false), new File("2/asdf.a"));
161: matchFile(createFilter(".a", false), new File("2/asdf.a.a"));
162: matchFile(createFilter(".abc", false), new File("2/asdf.a.abc"));
163:
164: noMatchFile(createFilter(".a", false), new File(""));
165: noMatchFile(createFilter(".a", false), new File("4/asdf"));
166: noMatchFile(createFilter(".a", false), new File("4/asdf.A"));
167: noMatchFile(createFilter(".a", false), new File("4/.A"));
168: noMatchFile(createFilter(".a", false), new File("4/.a.A"));
169: noMatchFile(createFilter(".a", false), new File("4/asdf.a.A"));
170: noMatchFile(createFilter(".a", false), new File("4/asdf.a."));
171: noMatchFile(createFilter(".ABC", false), new File("4/asdf.a.A"));
172: noMatchFile(createFilter(".ABC", false),
173: new File("4/asdf.a.AB"));
174: noMatchFile(createFilter(".ABC", false),
175: new File("4/asdf.a.AC"));
176: }
177:
178: public void testAcceptCaseInsensitive() {
179: matchFile(createFilter("A"), new File("A"));
180: matchFile(createFilter("A"), new File("asdfA"));
181: matchFile(createFilter("A"), new File("asdf.A"));
182: matchFile(createFilter(".a"), new File("b.a"));
183: matchFile(createFilter(".a"), new File(".a"));
184: matchFile(createFilter(".a"), new File("asdf.a"));
185: matchFile(createFilter(".a"), new File("asdf.a.a"));
186: matchFile(createFilter(".a"), new File("asdf.A"));
187: matchFile(createFilter(".a"), new File(".A"));
188: matchFile(createFilter(".a"), new File(".a.A"));
189: matchFile(createFilter(".a"), new File("asdf.a.A"));
190:
191: matchFile(createFilter("A"), new File("B", "A"));
192: matchFile(createFilter("A"), new File("B", "asdfA"));
193: matchFile(createFilter("A"), new File("B", "asdf.A"));
194: matchFile(createFilter(".a"), new File("a.b", "b.a"));
195: matchFile(createFilter(".a"), new File("a.b", ".a"));
196: matchFile(createFilter(".a"), new File("a.b", "asdf.a"));
197: matchFile(createFilter(".a"), new File("a.b", "asdf.a.a"));
198: matchFile(createFilter(".a"), new File("a.b", "asdf.A"));
199: matchFile(createFilter(".a"), new File("a.b", ".A"));
200: matchFile(createFilter(".a"), new File("a.b", ".a.A"));
201: matchFile(createFilter(".a"), new File("a.b", "asdf.a.A"));
202:
203: matchFile(createFilter("A", true), new File("A"));
204: matchFile(createFilter("A", true), new File("asdfA"));
205: matchFile(createFilter("A", true), new File("asdf.A"));
206: matchFile(createFilter(".a", true), new File("b.a"));
207: matchFile(createFilter(".a", true), new File(".a"));
208: matchFile(createFilter(".a", true), new File("asdf.a"));
209: matchFile(createFilter(".a", true), new File("asdf.a.a"));
210: matchFile(createFilter(".a", true), new File("asdf.A"));
211: matchFile(createFilter(".a", true), new File(".A"));
212: matchFile(createFilter(".a", true), new File(".a.A"));
213: matchFile(createFilter(".a", true), new File("asdf.a.A"));
214:
215: noMatchFile(createFilter(".a"), new File(""));
216: noMatchFile(createFilter(".a"), new File("asdf"));
217: noMatchFile(createFilter(".a"), new File("asdf.a."));
218: noMatchFile(createFilter(".a"), new File("asdf.aa"));
219: noMatchFile(createFilter(".ABC"), new File("asdf.a.A"));
220: noMatchFile(createFilter(".ABC"), new File("asdf.a.AB"));
221: noMatchFile(createFilter(".ABC"), new File("asdf.a.AC"));
222: noMatchFile(createFilter(".ABC"), new File("asdf.a.ACB"));
223: noMatchFile(createFilter(".ABC"), new File("asdf.a.ABCD"));
224: noMatchFile(createFilter(".a", true), new File(""));
225: noMatchFile(createFilter(".a", true), new File("asdf"));
226: noMatchFile(createFilter(".a", true), new File("asdf.a."));
227: noMatchFile(createFilter(".ABC", true), new File("asdf.a.A"));
228: noMatchFile(createFilter(".ABC", true), new File("asdf.a.AB"));
229: noMatchFile(createFilter(".ABC", true), new File("asdf.a.AC"));
230: noMatchFile(createFilter(".ABC", true), new File("asdf.a.ACB"));
231: noMatchFile(createFilter(".ABC", true), new File("asdf.a.ABCD"));
232:
233: noMatchFile(createFilter(".a"), new File("a.a", "asdf"));
234: noMatchFile(createFilter(".a"), new File("a.a", "asdf.a."));
235: noMatchFile(createFilter(".ABC"), new File("a.a", "asdf.a.A"));
236: noMatchFile(createFilter(".ABC"), new File("a.a", "asdf.a.AB"));
237: noMatchFile(createFilter(".ABC"), new File("a.a", "asdf.a.AC"));
238: noMatchFile(createFilter(".a", true), new File(""));
239: noMatchFile(createFilter(".a", true), new File("a.a", "asdf"));
240: noMatchFile(createFilter(".a", true),
241: new File("a.a", "asdf.a."));
242: noMatchFile(createFilter(".ABC", true), new File("a.ABC",
243: "asdf.a.A"));
244: noMatchFile(createFilter(".ABC", true), new File("a.ABC",
245: "asdf.a.AB"));
246: noMatchFile(createFilter(".ABC", true), new File("a.ABC",
247: "asdf.a.AC"));
248: }
249:
250: public void testAcceptDirectories() {
251: matchFile(createFilter("A"), new File("."));
252: matchFile(createFilter("A", false), new File("."));
253: matchFile(createFilter("A", true), new File("."));
254: }
255:
256: public void testDontAcceptDirectories() {
257: ExtensionFilenameFilter eff = createFilter("A");
258: eff.allowsDirectories(false);
259: noMatchFile(eff, new File("."));
260:
261: eff = createFilter("A", true);
262: eff.allowsDirectories(false);
263: noMatchFile(eff, new File("."));
264:
265: eff = createFilter("A", false);
266: eff.allowsDirectories(false);
267: noMatchFile(eff, new File("."));
268: }
269:
270: protected void matchFile(ExtensionFilenameFilter eff, File f) {
271: File parent = getParentFile(f);
272: String name = f.getName();
273: assertTrue("Filter must accept parent '" + parent
274: + "' and name '" + name + "'.", eff
275: .accept(parent, name));
276: }
277:
278: protected void noMatchFile(ExtensionFilenameFilter eff, File f) {
279: File parent = getParentFile(f);
280: String name = f.getName();
281: assertTrue("Filter must not accept parent '" + parent
282: + "' and name '" + name + "'.", !eff.accept(parent,
283: name));
284: }
285:
286: protected ExtensionFilenameFilter createFilter() {
287: return new ExtensionFilenameFilter();
288: }
289:
290: protected ExtensionFilenameFilter createFilter(String s) {
291: return new ExtensionFilenameFilter(s);
292: }
293:
294: protected ExtensionFilenameFilter createFilter(String s[]) {
295: return new ExtensionFilenameFilter(s);
296: }
297:
298: protected ExtensionFilenameFilter createFilter(boolean b) {
299: return new ExtensionFilenameFilter(b);
300: }
301:
302: protected ExtensionFilenameFilter createFilter(String s, boolean b) {
303: return new ExtensionFilenameFilter(s, b);
304: }
305:
306: protected ExtensionFilenameFilter createFilter(String s[], boolean b) {
307: return new ExtensionFilenameFilter(s, b);
308: }
309:
310: protected File getParentFile(File f) {
311: String pname = f.getParent();
312: if (pname == null) {
313: // no parent
314: return null;
315: }
316: return new File(pname);
317: }
318:
319: //-------------------------------------------------------------------------
320: // Global static methods that don't change.
321:
322: public static Test suite() {
323: TestSuite suite = new TestSuite(THIS_CLASS);
324:
325: return suite;
326: }
327:
328: public static void main(String[] args) {
329: String[] name = { THIS_CLASS.getName() };
330:
331: // junit.textui.TestRunner.main( name );
332: // junit.swingui.TestRunner.main( name );
333:
334: junit.textui.TestRunner.main(name);
335: }
336:
337: protected void setUp() throws Exception {
338: super .setUp();
339:
340: // set ourself up
341: }
342:
343: protected void tearDown() throws Exception {
344: // tear ourself down
345:
346: super.tearDown();
347: }
348:
349: }
|