1 /* 2 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 4313887 6866397 26 * @summary Unit test for java.nio.file.PathMatcher 27 */ 28 29 import java.nio.file.*; 30 import java.util.regex.PatternSyntaxException; 31 32 public class Basic { 33 static int failures; 34 35 static void match(String name, String pattern, boolean expectedToMatch) { 36 System.out.format("%s -> %s", name, pattern); 37 Path file = Paths.get(name); 38 boolean matched = file.getFileSystem() 39 .getPathMatcher("glob:" + pattern).matches(file); 40 if (matched) 41 System.out.print(" (matched)"); 42 else 43 System.out.print(" (no match)"); 44 if (matched != expectedToMatch) { 45 System.out.println(" ==> UNEXPECTED RESULT!"); 46 failures++; 47 } else { 48 System.out.println(" OKAY"); 49 } 50 } 51 52 static void assertMatch(String path, String pattern) { 53 match(path, pattern, true); 54 } 55 56 static void assertNotMatch(String path, String pattern) { 57 match(path, pattern, false); 58 } 59 60 static void assertBadPattern(String path, String pattern) { 61 System.out.format("Compile bad pattern %s\t", pattern); 62 try { 63 FileSystems.getDefault().getPathMatcher("glob:" + pattern); 64 System.out.println("Compiled ==> UNEXPECTED RESULT!"); 65 failures++; 66 } catch (PatternSyntaxException e) { 67 System.out.println("Failed to compile ==> OKAY"); 68 } 69 } 70 71 static void assertRegExMatch(String path, String pattern) { 72 System.out.format("Test regex pattern: %s", pattern); 73 Path file = Paths.get(path); 74 boolean matched = file.getFileSystem() 75 .getPathMatcher("regex:" + pattern).matches(file); 76 if (matched) { 77 System.out.println(" OKAY"); 78 } else { 79 System.out.println(" ==> UNEXPECTED RESULT!"); 80 failures++; 81 } 82 } 83 84 85 public static void main(String[] args) { 86 // basic 87 assertMatch("foo.html", "foo.html"); 88 assertNotMatch("foo.html", "foo.htm"); 89 assertNotMatch("foo.html", "bar.html"); 90 91 // match zero or more characters 92 assertMatch("foo.html", "f*"); 93 assertMatch("foo.html", "*.html"); 94 assertMatch("foo.html", "foo.html*"); 95 assertMatch("foo.html", "*foo.html"); 96 assertMatch("foo.html", "*foo.html*"); 97 assertNotMatch("foo.html", "*.htm"); 98 assertNotMatch("foo.html", "f.*"); 99 100 // match one character 101 assertMatch("foo.html", "?oo.html"); 102 assertMatch("foo.html", "??o.html"); 103 assertMatch("foo.html", "???.html"); 104 assertMatch("foo.html", "???.htm?"); 105 assertNotMatch("foo.html", "foo.???"); 106 107 // group of subpatterns 108 assertMatch("foo.html", "foo{.html,.class}"); 109 assertMatch("foo.html", "foo.{class,html}"); 110 assertNotMatch("foo.html", "foo{.htm,.class}"); 111 112 // bracket expressions 113 assertMatch("foo.html", "[f]oo.html"); 114 assertMatch("foo.html", "[e-g]oo.html"); 115 assertMatch("foo.html", "[abcde-g]oo.html"); 116 assertMatch("foo.html", "[abcdefx-z]oo.html"); 117 assertMatch("foo.html", "[!a]oo.html"); 118 assertMatch("foo.html", "[!a-e]oo.html"); 119 assertMatch("foo-bar", "foo[-a-z]bar"); // match dash 120 assertMatch("foo.html", "foo[!-]html"); // match !dash 121 122 // groups of subpattern with bracket expressions 123 assertMatch("foo.html", "[f]oo.{[h]tml,class}"); 124 assertMatch("foo.html", "foo.{[a-z]tml,class}"); 125 assertMatch("foo.html", "foo.{[!a-e]tml,.class}"); 126 127 // assume special characters are allowed in file names 128 assertMatch("{foo}.html", "\\{foo*"); 129 assertMatch("{foo}.html", "*\\}.html"); 130 assertMatch("[foo].html", "\\[foo*"); 131 assertMatch("[foo].html", "*\\].html"); 132 133 // errors 134 assertBadPattern("foo.html", "*[a--z]"); // bad range 135 assertBadPattern("foo.html", "*[a--]"); // bad range 136 assertBadPattern("foo.html", "*[a-z"); // missing ] 137 assertBadPattern("foo.html", "*{class,java"); // missing } 138 assertBadPattern("foo.html", "*.{class,{.java}}"); // nested group 139 assertBadPattern("foo.html", "*.html\\"); // nothing to escape 140 141 // platform specific 142 if (System.getProperty("os.name").startsWith("Windows")) { 143 assertMatch("C:\\foo", "C:\\\\f*"); 144 assertMatch("C:\\FOO", "c:\\\\f*"); 145 assertMatch("C:\\foo\\bar\\gus", "C:\\\\**\\\\gus"); 146 assertMatch("C:\\foo\\bar\\gus", "C:\\\\**"); 147 } else { 148 assertMatch("/tmp/foo", "/tmp/*"); 149 assertMatch("/tmp/foo/bar", "/tmp/**"); 150 151 // some special characters not allowed on Windows 152 assertMatch("myfile?", "myfile\\?"); 153 assertMatch("one\\two", "one\\\\two"); 154 assertMatch("one*two", "one\\*two"); 155 } 156 157 // regex syntax 158 assertRegExMatch("foo.html", ".*\\.html"); 159 160 if (System.getProperty("os.name").startsWith("Windows")) { 161 assertRegExMatch("foo012", "foo\\d+"); 162 assertRegExMatch("fo o", "fo\\so"); 163 assertRegExMatch("foo", "\\w+"); 164 } 165 166 // unknown syntax 167 try { 168 System.out.format("Test unknown syntax"); 169 FileSystems.getDefault().getPathMatcher("grep:foo"); 170 System.out.println(" ==> NOT EXPECTED TO COMPILE"); 171 failures++; 172 } catch (UnsupportedOperationException e) { 173 System.out.println(" OKAY"); 174 } 175 176 if (failures > 0) 177 throw new RuntimeException(failures + 178 " sub-test(s) failed - see log for details"); 179 } 180 }