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;
018:
019: import java.io.File;
020:
021: import junit.framework.TestCase;
022:
023: public class FilenameUtilsWildcardTestCase extends TestCase {
024:
025: private static final boolean WINDOWS = (File.separatorChar == '\\');
026:
027: public FilenameUtilsWildcardTestCase(String name) {
028: super (name);
029: }
030:
031: //-----------------------------------------------------------------------
032: // Testing:
033: // FilenameUtils.wildcardMatch(String,String)
034:
035: public void testMatch() {
036: assertEquals(false, FilenameUtils.wildcardMatch(null, "Foo"));
037: assertEquals(false, FilenameUtils.wildcardMatch("Foo", null));
038: assertEquals(true, FilenameUtils.wildcardMatch(null, null));
039: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo"));
040: assertEquals(true, FilenameUtils.wildcardMatch("", ""));
041: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Fo*"));
042: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Fo?"));
043: assertEquals(true, FilenameUtils.wildcardMatch(
044: "Foo Bar and Catflap", "Fo*"));
045: assertEquals(true, FilenameUtils.wildcardMatch("New Bookmarks",
046: "N?w ?o?k??r?s"));
047: assertEquals(false, FilenameUtils.wildcardMatch("Foo", "Bar"));
048: assertEquals(true, FilenameUtils.wildcardMatch("Foo Bar Foo",
049: "F*o Bar*"));
050: assertEquals(true, FilenameUtils.wildcardMatch(
051: "Adobe Acrobat Installer", "Ad*er"));
052: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "*Foo"));
053: assertEquals(true, FilenameUtils
054: .wildcardMatch("BarFoo", "*Foo"));
055: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo*"));
056: assertEquals(true, FilenameUtils
057: .wildcardMatch("FooBar", "Foo*"));
058: assertEquals(false, FilenameUtils.wildcardMatch("FOO", "*Foo"));
059: assertEquals(false, FilenameUtils.wildcardMatch("BARFOO",
060: "*Foo"));
061: assertEquals(false, FilenameUtils.wildcardMatch("FOO", "Foo*"));
062: assertEquals(false, FilenameUtils.wildcardMatch("FOOBAR",
063: "Foo*"));
064: }
065:
066: public void testMatchOnSystem() {
067: assertEquals(false, FilenameUtils.wildcardMatchOnSystem(null,
068: "Foo"));
069: assertEquals(false, FilenameUtils.wildcardMatchOnSystem("Foo",
070: null));
071: assertEquals(true, FilenameUtils.wildcardMatchOnSystem(null,
072: null));
073: assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo",
074: "Foo"));
075: assertEquals(true, FilenameUtils.wildcardMatchOnSystem("", ""));
076: assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo",
077: "Fo*"));
078: assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo",
079: "Fo?"));
080: assertEquals(true, FilenameUtils.wildcardMatchOnSystem(
081: "Foo Bar and Catflap", "Fo*"));
082: assertEquals(true, FilenameUtils.wildcardMatchOnSystem(
083: "New Bookmarks", "N?w ?o?k??r?s"));
084: assertEquals(false, FilenameUtils.wildcardMatchOnSystem("Foo",
085: "Bar"));
086: assertEquals(true, FilenameUtils.wildcardMatchOnSystem(
087: "Foo Bar Foo", "F*o Bar*"));
088: assertEquals(true, FilenameUtils.wildcardMatchOnSystem(
089: "Adobe Acrobat Installer", "Ad*er"));
090: assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo",
091: "*Foo"));
092: assertEquals(true, FilenameUtils.wildcardMatchOnSystem(
093: "BarFoo", "*Foo"));
094: assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo",
095: "Foo*"));
096: assertEquals(true, FilenameUtils.wildcardMatchOnSystem(
097: "FooBar", "Foo*"));
098: assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem(
099: "FOO", "*Foo"));
100: assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem(
101: "BARFOO", "*Foo"));
102: assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem(
103: "FOO", "Foo*"));
104: assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem(
105: "FOOBAR", "Foo*"));
106: }
107:
108: public void testMatchCaseSpecified() {
109: assertEquals(false, FilenameUtils.wildcardMatch(null, "Foo",
110: IOCase.SENSITIVE));
111: assertEquals(false, FilenameUtils.wildcardMatch("Foo", null,
112: IOCase.SENSITIVE));
113: assertEquals(true, FilenameUtils.wildcardMatch(null, null,
114: IOCase.SENSITIVE));
115: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo",
116: IOCase.SENSITIVE));
117: assertEquals(true, FilenameUtils.wildcardMatch("", "",
118: IOCase.SENSITIVE));
119: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Fo*",
120: IOCase.SENSITIVE));
121: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Fo?",
122: IOCase.SENSITIVE));
123: assertEquals(true, FilenameUtils.wildcardMatch(
124: "Foo Bar and Catflap", "Fo*", IOCase.SENSITIVE));
125: assertEquals(true, FilenameUtils.wildcardMatch("New Bookmarks",
126: "N?w ?o?k??r?s", IOCase.SENSITIVE));
127: assertEquals(false, FilenameUtils.wildcardMatch("Foo", "Bar",
128: IOCase.SENSITIVE));
129: assertEquals(true, FilenameUtils.wildcardMatch("Foo Bar Foo",
130: "F*o Bar*", IOCase.SENSITIVE));
131: assertEquals(true, FilenameUtils.wildcardMatch(
132: "Adobe Acrobat Installer", "Ad*er", IOCase.SENSITIVE));
133: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "*Foo",
134: IOCase.SENSITIVE));
135: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo*",
136: IOCase.SENSITIVE));
137: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "*Foo",
138: IOCase.SENSITIVE));
139: assertEquals(true, FilenameUtils.wildcardMatch("BarFoo",
140: "*Foo", IOCase.SENSITIVE));
141: assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo*",
142: IOCase.SENSITIVE));
143: assertEquals(true, FilenameUtils.wildcardMatch("FooBar",
144: "Foo*", IOCase.SENSITIVE));
145:
146: assertEquals(false, FilenameUtils.wildcardMatch("FOO", "*Foo",
147: IOCase.SENSITIVE));
148: assertEquals(false, FilenameUtils.wildcardMatch("BARFOO",
149: "*Foo", IOCase.SENSITIVE));
150: assertEquals(false, FilenameUtils.wildcardMatch("FOO", "Foo*",
151: IOCase.SENSITIVE));
152: assertEquals(false, FilenameUtils.wildcardMatch("FOOBAR",
153: "Foo*", IOCase.SENSITIVE));
154: assertEquals(true, FilenameUtils.wildcardMatch("FOO", "*Foo",
155: IOCase.INSENSITIVE));
156: assertEquals(true, FilenameUtils.wildcardMatch("BARFOO",
157: "*Foo", IOCase.INSENSITIVE));
158: assertEquals(true, FilenameUtils.wildcardMatch("FOO", "Foo*",
159: IOCase.INSENSITIVE));
160: assertEquals(true, FilenameUtils.wildcardMatch("FOOBAR",
161: "Foo*", IOCase.INSENSITIVE));
162: assertEquals(WINDOWS, FilenameUtils.wildcardMatch("FOO",
163: "*Foo", IOCase.SYSTEM));
164: assertEquals(WINDOWS, FilenameUtils.wildcardMatch("BARFOO",
165: "*Foo", IOCase.SYSTEM));
166: assertEquals(WINDOWS, FilenameUtils.wildcardMatch("FOO",
167: "Foo*", IOCase.SYSTEM));
168: assertEquals(WINDOWS, FilenameUtils.wildcardMatch("FOOBAR",
169: "Foo*", IOCase.SYSTEM));
170: }
171:
172: public void testSplitOnTokens() {
173: assertArrayEquals(new String[] { "Ad", "*", "er" },
174: FilenameUtils.splitOnTokens("Ad*er"));
175: assertArrayEquals(new String[] { "Ad", "?", "er" },
176: FilenameUtils.splitOnTokens("Ad?er"));
177: assertArrayEquals(new String[] { "Test", "*", "?", "One" },
178: FilenameUtils.splitOnTokens("Test*?One"));
179: assertArrayEquals(new String[] { "*" }, FilenameUtils
180: .splitOnTokens("****"));
181: assertArrayEquals(new String[] { "*", "?", "?", "*" },
182: FilenameUtils.splitOnTokens("*??*"));
183: assertArrayEquals(new String[] { "*", "?", "?", "*" },
184: FilenameUtils.splitOnTokens("*??*"));
185: assertArrayEquals(new String[] { "h", "?", "?", "*" },
186: FilenameUtils.splitOnTokens("h??*"));
187: assertArrayEquals(new String[] { "" }, FilenameUtils
188: .splitOnTokens(""));
189: }
190:
191: private void assertArrayEquals(Object[] a1, Object[] a2) {
192: assertEquals(a1.length, a2.length);
193: for (int i = 0; i < a1.length; i++) {
194: assertEquals(a1[i], a2[i]);
195: }
196: }
197:
198: private void assertMatch(String text, String wildcard,
199: boolean expected) {
200: assertEquals(text + " " + wildcard, expected, FilenameUtils
201: .wildcardMatch(text, wildcard));
202: }
203:
204: // A separate set of tests, added to this batch
205: public void testMatch2() {
206: assertMatch("log.txt", "log.txt", true);
207: assertMatch("log.txt1", "log.txt", false);
208:
209: assertMatch("log.txt", "log.txt*", true);
210: assertMatch("log.txt", "log.txt*1", false);
211: assertMatch("log.txt", "*log.txt*", true);
212:
213: assertMatch("log.txt", "*.txt", true);
214: assertMatch("txt.log", "*.txt", false);
215: assertMatch("config.ini", "*.ini", true);
216:
217: assertMatch("config.txt.bak", "con*.txt", false);
218:
219: assertMatch("log.txt9", "*.txt?", true);
220: assertMatch("log.txt", "*.txt?", false);
221:
222: assertMatch("progtestcase.java~5~", "*test*.java~*~", true);
223: assertMatch("progtestcase.java;5~", "*test*.java~*~", false);
224: assertMatch("progtestcase.java~5", "*test*.java~*~", false);
225:
226: assertMatch("log.txt", "log.*", true);
227:
228: assertMatch("log.txt", "log?*", true);
229:
230: assertMatch("log.txt12", "log.txt??", true);
231:
232: assertMatch("log.log", "log**log", true);
233: assertMatch("log.log", "log**", true);
234: assertMatch("log.log", "log.**", true);
235: assertMatch("log.log", "**.log", true);
236: assertMatch("log.log", "**log", true);
237:
238: assertMatch("log.log", "log*log", true);
239: assertMatch("log.log", "log*", true);
240: assertMatch("log.log", "log.*", true);
241: assertMatch("log.log", "*.log", true);
242: assertMatch("log.log", "*log", true);
243:
244: assertMatch("log.log", "*log?", false);
245: assertMatch("log.log", "*log?*", true);
246: assertMatch("log.log.abc", "*log?abc", true);
247: assertMatch("log.log.abc.log.abc", "*log?abc", true);
248: assertMatch("log.log.abc.log.abc.d", "*log?abc?d", true);
249: }
250:
251: }
|