jdk/test/java/nio/file/PathMatcher/Basic.java
changeset 2057 3acf8e5e2ca0
child 3622 9676ac8a9bf1
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4313887
       
    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     public static void main(String[] args) {
       
    72         // basic
       
    73         assertMatch("foo.html", "foo.html");
       
    74         assertNotMatch("foo.html", "foo.htm");
       
    75         assertNotMatch("foo.html", "bar.html");
       
    76 
       
    77         // match zero or more characters
       
    78         assertMatch("foo.html", "f*");
       
    79         assertMatch("foo.html", "*.html");
       
    80         assertMatch("foo.html", "foo.html*");
       
    81         assertMatch("foo.html", "*foo.html");
       
    82         assertMatch("foo.html", "*foo.html*");
       
    83         assertNotMatch("foo.html", "*.htm");
       
    84         assertNotMatch("foo.html", "f.*");
       
    85 
       
    86         // match one character
       
    87         assertMatch("foo.html", "?oo.html");
       
    88         assertMatch("foo.html", "??o.html");
       
    89         assertMatch("foo.html", "???.html");
       
    90         assertMatch("foo.html", "???.htm?");
       
    91         assertNotMatch("foo.html", "foo.???");
       
    92 
       
    93         // group of subpatterns
       
    94         assertMatch("foo.html", "foo{.html,.class}");
       
    95         assertMatch("foo.html", "foo.{class,html}");
       
    96         assertNotMatch("foo.html", "foo{.htm,.class}");
       
    97 
       
    98         // bracket expressions
       
    99         assertMatch("foo.html", "[f]oo.html");
       
   100         assertMatch("foo.html", "[e-g]oo.html");
       
   101         assertMatch("foo.html", "[abcde-g]oo.html");
       
   102         assertMatch("foo.html", "[abcdefx-z]oo.html");
       
   103         assertMatch("foo.html", "[!a]oo.html");
       
   104         assertMatch("foo.html", "[!a-e]oo.html");
       
   105         assertMatch("foo-bar", "foo[-a-z]bar");     // match dash
       
   106         assertMatch("foo.html", "foo[!-]html");     // match !dash
       
   107 
       
   108         // groups of subpattern with bracket expressions
       
   109         assertMatch("foo.html", "[f]oo.{[h]tml,class}");
       
   110         assertMatch("foo.html", "foo.{[a-z]tml,class}");
       
   111         assertMatch("foo.html", "foo.{[!a-e]tml,.class}");
       
   112 
       
   113         // assume special characters are allowed in file names
       
   114         assertMatch("{foo}.html", "\\{foo*");
       
   115         assertMatch("{foo}.html", "*\\}.html");
       
   116         assertMatch("[foo].html", "\\[foo*");
       
   117         assertMatch("[foo].html", "*\\].html");
       
   118 
       
   119         // errors
       
   120         assertBadPattern("foo.html", "*[a--z]");            // bad range
       
   121         assertBadPattern("foo.html", "*[a--]");             // bad range
       
   122         assertBadPattern("foo.html", "*[a-z");              // missing ]
       
   123         assertBadPattern("foo.html", "*{class,java");       // missing }
       
   124         assertBadPattern("foo.html", "*.{class,{.java}}");  // nested group
       
   125         assertBadPattern("foo.html", "*.html\\");           // nothing to escape
       
   126 
       
   127         // platform specific
       
   128         if (System.getProperty("os.name").startsWith("Windows")) {
       
   129             assertMatch("C:\\foo", "C:\\\\f*");
       
   130             assertMatch("C:\\FOO", "c:\\\\f*");
       
   131             assertMatch("C:\\foo\\bar\\gus", "C:\\\\**\\\\gus");
       
   132             assertMatch("C:\\foo\\bar\\gus", "C:\\\\**");
       
   133         } else {
       
   134             assertMatch("/tmp/foo", "/tmp/*");
       
   135             assertMatch("/tmp/foo/bar", "/tmp/**");
       
   136 
       
   137             // some special characters not allowed on Windows
       
   138             assertMatch("myfile?", "myfile\\?");
       
   139             assertMatch("one\\two", "one\\\\two");
       
   140             assertMatch("one*two", "one\\*two");
       
   141         }
       
   142 
       
   143 
       
   144 
       
   145         // regex syntax
       
   146         {
       
   147             String pattern = ".*\\.html";
       
   148             System.out.format("Test regex pattern: %s", pattern);
       
   149             Path file = Paths.get("foo.html");
       
   150             boolean matched =  file.getFileSystem()
       
   151                 .getPathMatcher("regex:" + pattern).matches(file);
       
   152             if (matched) {
       
   153                 System.out.println(" OKAY");
       
   154             } else {
       
   155                 System.out.println(" ==> UNEXPECTED RESULT!");
       
   156                 failures++;
       
   157             }
       
   158         }
       
   159 
       
   160         // unknown syntax
       
   161         try {
       
   162             System.out.format("Test unknown syntax");
       
   163             FileSystems.getDefault().getPathMatcher("grep:foo");
       
   164             System.out.println(" ==> NOT EXPECTED TO COMPILE");
       
   165             failures++;
       
   166         } catch (UnsupportedOperationException e) {
       
   167             System.out.println(" OKAY");
       
   168         }
       
   169 
       
   170         if (failures > 0)
       
   171             throw new RuntimeException(failures +
       
   172                 " sub-test(s) failed - see log for details");
       
   173     }
       
   174 }