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         try {
 178             FileSystems.getDefault().getPathMatcher("glob:java");
 179             FileSystems.getDefault().getPathMatcher("Glob:java");
 180             FileSystems.getDefault().getPathMatcher("GLOB:java");
 181             System.out.println("Test GLOB_SYNTAX case sensitivity OKAY");
 182         } catch (UnsupportedOperationException e) {
 183             System.err.println("getPathMatcher GLOB_SYNTAX case sensitivity");
 184             e.printStackTrace();
 185             failures++;
 186         }
 187 
 188         // REGEX_SYNTAX case sensitivity of getPathMatcher: should not throw UOE
 189         try {
 190             FileSystems.getDefault().getPathMatcher("regex:java");
 191             FileSystems.getDefault().getPathMatcher("Regex:java");
 192             FileSystems.getDefault().getPathMatcher("RegEx:java");
 193             FileSystems.getDefault().getPathMatcher("REGEX:java");
 194             System.out.println("Test REGEX_SYNTAX case sensitivity OKAY");
 195         } catch (UnsupportedOperationException e) {
 196             System.err.println("getPathMatcher REGEX_SYNTAX case sensitivity");
 197             e.printStackTrace();
 198             failures++;
 199         }
 200 
 201         if (failures > 0)
 202             throw new RuntimeException(failures +
 203                 " sub-test(s) failed - see log for details");
 204     }
 205 }