6866397: (file) PathMatcher with regex syntax doesn't match as expected (win)
authorsherman
Fri, 14 Aug 2009 11:23:01 -0700
changeset 3622 9676ac8a9bf1
parent 3621 526f2b12a0db
child 3623 4e71b4e83158
6866397: (file) PathMatcher with regex syntax doesn't match as expected (win) Summary: Use unicode_case_insensitive for windows path matcher for now. Reviewed-by: alanb
jdk/src/windows/classes/sun/nio/fs/WindowsFileSystem.java
jdk/test/java/nio/file/PathMatcher/Basic.java
--- a/jdk/src/windows/classes/sun/nio/fs/WindowsFileSystem.java	Thu Aug 13 15:12:32 2009 -0700
+++ b/jdk/src/windows/classes/sun/nio/fs/WindowsFileSystem.java	Fri Aug 14 11:23:01 2009 -0700
@@ -283,25 +283,15 @@
             }
         }
 
-        // match in uppercase
-        StringBuilder sb = new StringBuilder(expr.length());
-        for (int i=0; i<expr.length(); i++) {
-            sb.append(Character.toUpperCase(expr.charAt(i)));
-        }
-        expr = sb.toString();
+        // match in unicode_case_insensitive
+        final Pattern pattern = Pattern.compile(expr,
+            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
 
         // return matcher
-        final Pattern pattern = Pattern.compile(expr);
         return new PathMatcher() {
             @Override
             public boolean matches(Path path) {
-                // match in uppercase
-                String s = path.toString();
-                StringBuilder sb = new StringBuilder(s.length());
-                for (int i=0; i<s.length(); i++) {
-                    sb.append( Character.toUpperCase(s.charAt(i)) );
-                }
-                return pattern.matcher(sb).matches();
+                return pattern.matcher(path.toString()).matches();
             }
         };
     }
--- a/jdk/test/java/nio/file/PathMatcher/Basic.java	Thu Aug 13 15:12:32 2009 -0700
+++ b/jdk/test/java/nio/file/PathMatcher/Basic.java	Fri Aug 14 11:23:01 2009 -0700
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 4313887
+ * @bug 4313887 6866397
  * @summary Unit test for java.nio.file.PathMatcher
  */
 
@@ -68,6 +68,20 @@
         }
     }
 
+    static void assertRegExMatch(String path, String pattern) {
+        System.out.format("Test regex pattern: %s", pattern);
+        Path file = Paths.get(path);
+        boolean matched =  file.getFileSystem()
+                               .getPathMatcher("regex:" + pattern).matches(file);
+        if (matched) {
+            System.out.println(" OKAY");
+        } else {
+            System.out.println(" ==> UNEXPECTED RESULT!");
+            failures++;
+        }
+    }
+
+
     public static void main(String[] args) {
         // basic
         assertMatch("foo.html", "foo.html");
@@ -140,21 +154,13 @@
             assertMatch("one*two", "one\\*two");
         }
 
-
+        // regex syntax
+        assertRegExMatch("foo.html", ".*\\.html");
 
-        // regex syntax
-        {
-            String pattern = ".*\\.html";
-            System.out.format("Test regex pattern: %s", pattern);
-            Path file = Paths.get("foo.html");
-            boolean matched =  file.getFileSystem()
-                .getPathMatcher("regex:" + pattern).matches(file);
-            if (matched) {
-                System.out.println(" OKAY");
-            } else {
-                System.out.println(" ==> UNEXPECTED RESULT!");
-                failures++;
-            }
+        if (System.getProperty("os.name").startsWith("Windows")) {
+            assertRegExMatch("foo012", "foo\\d+");
+            assertRegExMatch("fo o", "fo\\so");
+            assertRegExMatch("foo", "\\w+");
         }
 
         // unknown syntax