test/java/nio/file/PathMatcher/Basic.java

Print this page
rev 11561 : 8073445: (fs) FileSystem.getPathMatcher(...) should check syntax component without regard to case
Summary: Change String equals() to equalsIgnoreCase() where needed.
Reviewed-by: alanb
   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!");


 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 }
   1 /*
   2  * Copyright (c) 2008, 2015, 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 8073445
  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!");


 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         // GLOB_SYNTAX case sensitivity of getPathMatcher: should not throw UOE
 177         Path foobar = Paths.get("/foo", "bar");
 178         try {
 179             PathMatcher pmGlob =
 180                 FileSystems.getDefault().getPathMatcher("glob:java");
 181             pmGlob = FileSystems.getDefault().getPathMatcher("Glob:java");
 182             pmGlob = FileSystems.getDefault().getPathMatcher("GLOB:java");
 183             // next three lines to avoid unused variable complaints about
 184             // 'pmGlob' and any possibility of things eventually being
 185             // optimized out
 186             if (pmGlob.matches(Paths.get("/foo", "bar"))) {
 187                 System.out.println("pmGlob matches foobar!");
 188             }
 189             System.out.println("Test GLOB_SYNTAX case sensitivity OKAY");
 190         } catch (UnsupportedOperationException e) {
 191             System.err.println("getPathMatcher GLOB_SYNTAX case sensitivity");
 192             e.printStackTrace();
 193             failures++;
 194         }
 195 
 196         // REGEX_SYNTAX case sensitivity of getPathMatcher: should not throw UOE
 197         try {
 198             PathMatcher pmRegex =
 199                 FileSystems.getDefault().getPathMatcher("regex:java");
 200             pmRegex = FileSystems.getDefault().getPathMatcher("Regex:java");
 201             pmRegex = FileSystems.getDefault().getPathMatcher("RegEx:java");
 202             pmRegex = FileSystems.getDefault().getPathMatcher("REGEX:java");
 203             // next three lines to avoid unused variable complaints about
 204             // 'pmRegex' and any possibility of things eventually being
 205             // optimized out
 206             if (pmRegex.matches(Paths.get("/foo", "bar"))) {
 207                 System.out.println("pmRegex matches foobar!");
 208             }
 209             System.out.println("Test REGEX_SYNTAX case sensitivity OKAY");
 210         } catch (UnsupportedOperationException e) {
 211             System.err.println("getPathMatcher REGEX_SYNTAX case sensitivity");
 212             e.printStackTrace();
 213             failures++;
 214         }
 215 
 216         if (failures > 0)
 217             throw new RuntimeException(failures +
 218                 " sub-test(s) failed - see log for details");
 219     }
 220 }