001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.project.support.ant;
043:
044: import java.io.File;
045: import java.util.Set;
046: import java.util.SortedSet;
047: import java.util.TreeSet;
048: import org.netbeans.junit.NbTestCase;
049:
050: /**
051: * Test of {@link PathMatcher}.
052: * @author Jesse Glick
053: */
054: public class PathMatcherTest extends NbTestCase {
055:
056: public PathMatcherTest(String n) {
057: super (n);
058: }
059:
060: private void assertMatches(String includes, String excludes,
061: String path) {
062: if (!new PathMatcher(includes, excludes, null).matches(path,
063: false)) {
064: fail("includes=" + includes + " excludes=" + excludes
065: + " should have matched " + path);
066: }
067: }
068:
069: private void assertDoesNotMatch(String includes, String excludes,
070: String path) {
071: if (new PathMatcher(includes, excludes, null).matches(path,
072: false)) {
073: fail("includes=" + includes + " excludes=" + excludes
074: + " should not have matched " + path);
075: }
076: }
077:
078: public void testPlainPaths() throws Exception {
079: assertMatches("foo/", null, "foo/");
080: assertDoesNotMatch("foo/", null, "foo");
081: assertMatches("foo/", null, "foo/bar");
082: assertMatches("foo/", null, "foo/bar/");
083: assertDoesNotMatch("foo", null, "foo/");
084: assertMatches("foo,bar", null, "foo");
085: assertDoesNotMatch("foo,bar", "foo", "foo");
086: assertDoesNotMatch("", null, "");
087: try {
088: new PathMatcher(null, null, null).matches(null, false);
089: fail();
090: } catch (Exception x) {
091: }
092: }
093:
094: public void testWildcards() throws Exception {
095: assertMatches("foo/**", null, "foo/");
096: assertDoesNotMatch("foo/**", null, "foo");
097: assertMatches("foo/**", null, "foo/bar");
098: assertMatches("foo/**", null, "foo/bar/");
099: assertMatches("foo/**/bar", null, "foo/bar");
100: assertMatches("**/foo", null, "foo");
101: assertMatches("foo*bar", null, "foobar");
102: assertMatches("foo*bar", null, "foo_bar");
103: assertDoesNotMatch("foo*bar", null, "foo/bar");
104: assertMatches("**/*.foo", null, "x/y/z.foo");
105: assertMatches("**/*.foo", null, "z.foo");
106: assertMatches("**", null, "");
107: assertMatches("**", null, "a");
108: assertMatches("**", null, "a/");
109: assertMatches("**", null, "a/b");
110: assertMatches("**", null, "a/b/");
111: // #98235
112: assertMatches("**", "**/b/*", "a/");
113: assertMatches("**", "**/b/*", "a/ClassA.java");
114: assertMatches("**", "**/b/*", "a/b/");
115: assertDoesNotMatch("**", "**/b/*", "a/b/ClassB.java");
116: assertMatches("**", "**/b/*", "a/b/c/");
117: assertMatches("**", "**/b/*", "a/b/c/ClassC.java");
118: }
119:
120: public void testOddChars() throws Exception {
121: assertMatches("foo$bar", null, "foo$bar");
122: assertMatches("foo.bar", null, "foo.bar");
123: assertDoesNotMatch("foo.bar", null, "foo_bar");
124: assertMatches("\u011E", null, "\u011E");
125: }
126:
127: public void testSeparators() throws Exception {
128: assertMatches("foo bar", null, "foo");
129: assertMatches("foo bar", null, "bar");
130: assertDoesNotMatch("foo bar", null, "foo bar");
131: assertMatches("foo*bar", null, "foo bar");
132: assertMatches(" foo bar ", null, "foo");
133: assertMatches(" foo bar ", null, "bar");
134: assertMatches(",,foo,bar,,", null, "foo");
135: assertMatches(",,foo,bar,,", null, "bar");
136: assertMatches(" foo , bar ", null, "foo");
137: assertMatches(" foo , bar ", null, "bar");
138: assertMatches("foo\\bar", null, "foo/bar");
139: assertDoesNotMatch("foo/bar", null, "foo\\bar");
140: assertMatches("foo\\", null, "foo/");
141: assertMatches("foo\\**", null, "foo/");
142: assertMatches("foo\\**\\bar", null, "foo/bar");
143: }
144:
145: private PathMatcher assertIncludedRoots(String includes,
146: String excludes, String files, String... roots)
147: throws Exception {
148: clearWorkDir();
149: File d = getWorkDir();
150: if (files != null) {
151: assert files.length() > 0;
152: for (String f : files.split(",")) {
153: File create = new File(d, f);
154: if (f.endsWith("/")) {
155: create.mkdirs();
156: } else {
157: create.getParentFile().mkdirs();
158: create.createNewFile();
159: }
160: }
161: }
162: PathMatcher m = new PathMatcher(includes, excludes, d);
163: SortedSet<File> actual = new TreeSet<File>(m
164: .findIncludedRoots());
165: SortedSet<File> expected = new TreeSet<File>();
166: for (String root : roots) {
167: expected.add(new File(d, root.replace('/',
168: File.separatorChar)));
169: }
170: assertEquals("includes=" + includes + " excludes=" + excludes
171: + " gave wrong roots with actual files " + files,
172: setToS(expected), setToS(actual));
173: return m;
174: }
175:
176: private String setToS(Set<?> s) {
177: return s.isEmpty() ? "nil" : s.toString();
178: }
179:
180: public void testIncludedRoots() throws Exception {
181: assertIncludedRoots("foo/,bar/", null, "foo/x,bar/x", "foo/",
182: "bar/");
183: assertIncludedRoots("foo/**,bar/**", null, "foo/x,bar/x",
184: "foo/", "bar/");
185: PathMatcher m = assertIncludedRoots("**/bar/", null,
186: "foo/bar/baz", "foo/bar/");
187: assertTrue(m.matches("", true));
188: assertFalse(m.matches("", false));
189: assertTrue(m.matches("foo/", true));
190: assertFalse(m.matches("foo/", false));
191: assertFalse(m.matches("foo/bar", true));
192: assertFalse(m.matches("foo/bar", false));
193: assertTrue(m.matches("foo/bar/", true));
194: assertTrue(m.matches("foo/bar/", false));
195: assertIncludedRoots("foo/,**/bar/", null, "foo/bar/baz", "foo/");
196: assertIncludedRoots("foo/bar/baz", null, "foo/bar/baz",
197: "foo/bar/");
198: assertIncludedRoots(null, null, null, "");
199: assertIncludedRoots("f,foo/", null, "f,foo/", "", "foo/");
200: assertIncludedRoots("foo/", "foo/", "foo/"/*, nothing*/);
201: assertIncludedRoots("foo/bar", null, null/*, nothing*/);
202: assertIncludedRoots("foo/bar", null, "foo/bar", "foo/");
203: assertIncludedRoots("foo/,bar/,baz", null, "foo/,bar/,baz",
204: "foo/", "bar/", "");
205: assertIncludedRoots("**", null, null, "");
206: new PathMatcher(null, null, new File("nonexistent"))
207: .findIncludedRoots(); // should not fail
208: assertIncludedRoots("java/awt/ sun/awt/", null,
209: "java/lang/Object.java,sun/awt/Mutex.java", "sun/awt/");
210: }
211:
212: }
|