1 19 20 package org.netbeans.spi.project.support.ant; 21 22 import java.io.File ; 23 import java.util.Set ; 24 import java.util.SortedSet ; 25 import java.util.TreeSet ; 26 import org.netbeans.junit.NbTestCase; 27 28 32 public class PathMatcherTest extends NbTestCase { 33 34 public PathMatcherTest(String n) { 35 super(n); 36 } 37 38 private void assertMatches(String includes, String excludes, String 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 includes, String excludes, String 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 { 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 x) {} 63 } 64 65 public void testWildcards() throws Exception { 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 { 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 { 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 includes, String excludes, String files, String ... roots) throws Exception { 110 clearWorkDir(); 111 File d = getWorkDir(); 112 if (files != null) { 113 assert files.length() > 0; 114 for (String f : files.split(",")) { 115 File create = new File (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 <File > actual = new TreeSet <File >(m.findIncludedRoots()); 126 SortedSet <File > expected = new TreeSet <File >(); 127 for (String root : roots) { 128 expected.add(new File (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 setToS(Set <?> s) { 134 return s.isEmpty() ? "nil" : s.toString(); 135 } 136 137 public void testIncludedRoots() throws Exception { 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/"); 154 assertIncludedRoots("foo/bar", null, null); 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 ("nonexistent")).findIncludedRoots(); assertIncludedRoots("java/awt/ sun/awt/", null, "java/lang/Object.java,sun/awt/Mutex.java", "sun/awt/"); 160 } 161 162 } 163 | Popular Tags |