KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > ant > PathMatcherTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.project.support.ant;
21
22 import java.io.File JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.SortedSet JavaDoc;
25 import java.util.TreeSet JavaDoc;
26 import org.netbeans.junit.NbTestCase;
27
28 /**
29  * Test of {@link PathMatcher}.
30  * @author Jesse Glick
31  */

32 public class PathMatcherTest extends NbTestCase {
33
34     public PathMatcherTest(String JavaDoc n) {
35         super(n);
36     }
37
38     private void assertMatches(String JavaDoc includes, String JavaDoc excludes, String JavaDoc path) {
39         if (!new PathMatcher(includes, excludes, null).matches(path, false)) {
40             fail("includes=" + includes + " excludes=" + excludes + " should have matched " + path);
41         }
42     }
43
44     private void assertDoesNotMatch(String JavaDoc includes, String JavaDoc excludes, String JavaDoc path) {
45         if (new PathMatcher(includes, excludes, null).matches(path, false)) {
46             fail("includes=" + includes + " excludes=" + excludes + " should not have matched " + path);
47         }
48     }
49
50     public void testPlainPaths() throws Exception JavaDoc {
51         assertMatches("foo/", null, "foo/");
52         assertDoesNotMatch("foo/", null, "foo");
53         assertMatches("foo/", null, "foo/bar");
54         assertMatches("foo/", null, "foo/bar/");
55         assertDoesNotMatch("foo", null, "foo/");
56         assertMatches("foo,bar", null, "foo");
57         assertDoesNotMatch("foo,bar", "foo", "foo");
58         assertDoesNotMatch("", null, "");
59         try {
60             new PathMatcher(null, null, null).matches(null, false);
61             fail();
62         } catch (Exception JavaDoc x) {}
63     }
64
65     public void testWildcards() throws Exception JavaDoc {
66         assertMatches("foo/**", null, "foo/");
67         assertDoesNotMatch("foo/**", null, "foo");
68         assertMatches("foo/**", null, "foo/bar");
69         assertMatches("foo/**", null, "foo/bar/");
70         assertMatches("foo/**/bar", null, "foo/bar");
71         assertMatches("**/foo", null, "foo");
72         assertMatches("foo*bar", null, "foobar");
73         assertMatches("foo*bar", null, "foo_bar");
74         assertDoesNotMatch("foo*bar", null, "foo/bar");
75         assertMatches("**/*.foo", null, "x/y/z.foo");
76         assertMatches("**/*.foo", null, "z.foo");
77         assertMatches("**", null, "");
78         assertMatches("**", null, "a");
79         assertMatches("**", null, "a/");
80         assertMatches("**", null, "a/b");
81         assertMatches("**", null, "a/b/");
82     }
83
84     public void testOddChars() throws Exception JavaDoc {
85         assertMatches("foo$bar", null, "foo$bar");
86         assertMatches("foo.bar", null, "foo.bar");
87         assertDoesNotMatch("foo.bar", null, "foo_bar");
88         assertMatches("\u011E", null, "\u011E");
89     }
90
91     public void testSeparators() throws Exception JavaDoc {
92         assertMatches("foo bar", null, "foo");
93         assertMatches("foo bar", null, "bar");
94         assertDoesNotMatch("foo bar", null, "foo bar");
95         assertMatches("foo*bar", null, "foo bar");
96         assertMatches(" foo bar ", null, "foo");
97         assertMatches(" foo bar ", null, "bar");
98         assertMatches(",,foo,bar,,", null, "foo");
99         assertMatches(",,foo,bar,,", null, "bar");
100         assertMatches(" foo , bar ", null, "foo");
101         assertMatches(" foo , bar ", null, "bar");
102         assertMatches("foo\\bar", null, "foo/bar");
103         assertDoesNotMatch("foo/bar", null, "foo\\bar");
104         assertMatches("foo\\", null, "foo/");
105         assertMatches("foo\\**", null, "foo/");
106         assertMatches("foo\\**\\bar", null, "foo/bar");
107     }
108
109     private PathMatcher assertIncludedRoots(String JavaDoc includes, String JavaDoc excludes, String JavaDoc files, String JavaDoc... roots) throws Exception JavaDoc {
110         clearWorkDir();
111         File JavaDoc d = getWorkDir();
112         if (files != null) {
113             assert files.length() > 0;
114             for (String JavaDoc f : files.split(",")) {
115                 File JavaDoc create = new File JavaDoc(d, f);
116                 if (f.endsWith("/")) {
117                     create.mkdirs();
118                 } else {
119                     create.getParentFile().mkdirs();
120                     create.createNewFile();
121                 }
122             }
123         }
124         PathMatcher m = new PathMatcher(includes, excludes, d);
125         SortedSet JavaDoc<File JavaDoc> actual = new TreeSet JavaDoc<File JavaDoc>(m.findIncludedRoots());
126         SortedSet JavaDoc<File JavaDoc> expected = new TreeSet JavaDoc<File JavaDoc>();
127         for (String JavaDoc root : roots) {
128             expected.add(new File JavaDoc(d, root.replace('/', File.separatorChar)));
129         }
130         assertEquals("includes=" + includes + " excludes=" + excludes + " gave wrong roots with actual files " + files, setToS(expected), setToS(actual));
131         return m;
132     }
133     private String JavaDoc setToS(Set JavaDoc<?> s) {
134         return s.isEmpty() ? "nil" : s.toString();
135     }
136
137     public void testIncludedRoots() throws Exception JavaDoc {
138         assertIncludedRoots("foo/,bar/", null, "foo/x,bar/x", "foo/", "bar/");
139         assertIncludedRoots("foo/**,bar/**", null, "foo/x,bar/x", "foo/", "bar/");
140         PathMatcher m = assertIncludedRoots("**/bar/", null, "foo/bar/baz", "foo/bar/");
141         assertTrue(m.matches("", true));
142         assertFalse(m.matches("", false));
143         assertTrue(m.matches("foo/", true));
144         assertFalse(m.matches("foo/", false));
145         assertFalse(m.matches("foo/bar", true));
146         assertFalse(m.matches("foo/bar", false));
147         assertTrue(m.matches("foo/bar/", true));
148         assertTrue(m.matches("foo/bar/", false));
149         assertIncludedRoots("foo/,**/bar/", null, "foo/bar/baz", "foo/");
150         assertIncludedRoots("foo/bar/baz", null, "foo/bar/baz", "foo/bar/");
151         assertIncludedRoots(null, null, null, "");
152         assertIncludedRoots("f,foo/", null, "f,foo/", "", "foo/");
153         assertIncludedRoots("foo/", "foo/", "foo/"/*, nothing*/);
154         assertIncludedRoots("foo/bar", null, null/*, nothing*/);
155         assertIncludedRoots("foo/bar", null, "foo/bar", "foo/");
156         assertIncludedRoots("foo/,bar/,baz", null, "foo/,bar/,baz", "foo/", "bar/", "");
157         assertIncludedRoots("**", null, null, "");
158         new PathMatcher(null, null, new File JavaDoc("nonexistent")).findIncludedRoots(); // should not fail
159
assertIncludedRoots("java/awt/ sun/awt/", null, "java/lang/Object.java,sun/awt/Mutex.java", "sun/awt/");
160     }
161
162 }
163
Popular Tags