Path: blob/master/test/jdk/java/nio/file/PathMatcher/Basic.java
41153 views
/*1* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 6866397 807344525* @summary Unit test for java.nio.file.PathMatcher26*/2728import java.nio.file.*;29import java.util.regex.PatternSyntaxException;3031public class Basic {32static int failures;3334static void match(String name, String pattern, boolean expectedToMatch) {35System.out.format("%s -> %s", name, pattern);36Path file = Paths.get(name);37boolean matched = file.getFileSystem()38.getPathMatcher("glob:" + pattern).matches(file);39if (matched)40System.out.print(" (matched)");41else42System.out.print(" (no match)");43if (matched != expectedToMatch) {44System.out.println(" ==> UNEXPECTED RESULT!");45failures++;46} else {47System.out.println(" OKAY");48}49}5051static void assertMatch(String path, String pattern) {52match(path, pattern, true);53}5455static void assertNotMatch(String path, String pattern) {56match(path, pattern, false);57}5859static void assertBadPattern(String path, String pattern) {60System.out.format("Compile bad pattern %s\t", pattern);61try {62FileSystems.getDefault().getPathMatcher("glob:" + pattern);63System.out.println("Compiled ==> UNEXPECTED RESULT!");64failures++;65} catch (PatternSyntaxException e) {66System.out.println("Failed to compile ==> OKAY");67}68}6970static void assertRegExMatch(String path, String pattern) {71System.out.format("Test regex pattern: %s", pattern);72Path file = Paths.get(path);73boolean matched = file.getFileSystem()74.getPathMatcher("regex:" + pattern).matches(file);75if (matched) {76System.out.println(" OKAY");77} else {78System.out.println(" ==> UNEXPECTED RESULT!");79failures++;80}81}828384public static void main(String[] args) {85// basic86assertMatch("foo.html", "foo.html");87assertNotMatch("foo.html", "foo.htm");88assertNotMatch("foo.html", "bar.html");8990// match zero or more characters91assertMatch("foo.html", "f*");92assertMatch("foo.html", "*.html");93assertMatch("foo.html", "foo.html*");94assertMatch("foo.html", "*foo.html");95assertMatch("foo.html", "*foo.html*");96assertNotMatch("foo.html", "*.htm");97assertNotMatch("foo.html", "f.*");9899// match one character100assertMatch("foo.html", "?oo.html");101assertMatch("foo.html", "??o.html");102assertMatch("foo.html", "???.html");103assertMatch("foo.html", "???.htm?");104assertNotMatch("foo.html", "foo.???");105106// group of subpatterns107assertMatch("foo.html", "foo{.html,.class}");108assertMatch("foo.html", "foo.{class,html}");109assertNotMatch("foo.html", "foo{.htm,.class}");110111// bracket expressions112assertMatch("foo.html", "[f]oo.html");113assertMatch("foo.html", "[e-g]oo.html");114assertMatch("foo.html", "[abcde-g]oo.html");115assertMatch("foo.html", "[abcdefx-z]oo.html");116assertMatch("foo.html", "[!a]oo.html");117assertMatch("foo.html", "[!a-e]oo.html");118assertMatch("foo-bar", "foo[-a-z]bar"); // match dash119assertMatch("foo.html", "foo[!-]html"); // match !dash120121// groups of subpattern with bracket expressions122assertMatch("foo.html", "[f]oo.{[h]tml,class}");123assertMatch("foo.html", "foo.{[a-z]tml,class}");124assertMatch("foo.html", "foo.{[!a-e]tml,.class}");125126// assume special characters are allowed in file names127assertMatch("{foo}.html", "\\{foo*");128assertMatch("{foo}.html", "*\\}.html");129assertMatch("[foo].html", "\\[foo*");130assertMatch("[foo].html", "*\\].html");131132// errors133assertBadPattern("foo.html", "*[a--z]"); // bad range134assertBadPattern("foo.html", "*[a--]"); // bad range135assertBadPattern("foo.html", "*[a-z"); // missing ]136assertBadPattern("foo.html", "*{class,java"); // missing }137assertBadPattern("foo.html", "*.{class,{.java}}"); // nested group138assertBadPattern("foo.html", "*.html\\"); // nothing to escape139140// platform specific141if (System.getProperty("os.name").startsWith("Windows")) {142assertMatch("C:\\foo", "C:\\\\f*");143assertMatch("C:\\FOO", "c:\\\\f*");144assertMatch("C:\\foo\\bar\\gus", "C:\\\\**\\\\gus");145assertMatch("C:\\foo\\bar\\gus", "C:\\\\**");146} else {147assertMatch("/tmp/foo", "/tmp/*");148assertMatch("/tmp/foo/bar", "/tmp/**");149150// some special characters not allowed on Windows151assertMatch("myfile?", "myfile\\?");152assertMatch("one\\two", "one\\\\two");153assertMatch("one*two", "one\\*two");154}155156// regex syntax157assertRegExMatch("foo.html", ".*\\.html");158159if (System.getProperty("os.name").startsWith("Windows")) {160assertRegExMatch("foo012", "foo\\d+");161assertRegExMatch("fo o", "fo\\so");162assertRegExMatch("foo", "\\w+");163}164165// unknown syntax166try {167System.out.format("Test unknown syntax");168FileSystems.getDefault().getPathMatcher("grep:foo");169System.out.println(" ==> NOT EXPECTED TO COMPILE");170failures++;171} catch (UnsupportedOperationException e) {172System.out.println(" OKAY");173}174175// GLOB_SYNTAX case sensitivity of getPathMatcher: should not throw UOE176try {177FileSystems.getDefault().getPathMatcher("glob:java");178FileSystems.getDefault().getPathMatcher("Glob:java");179FileSystems.getDefault().getPathMatcher("GLOB:java");180System.out.println("Test GLOB_SYNTAX case sensitivity OKAY");181} catch (UnsupportedOperationException e) {182System.err.println("getPathMatcher GLOB_SYNTAX case sensitivity");183e.printStackTrace();184failures++;185}186187// REGEX_SYNTAX case sensitivity of getPathMatcher: should not throw UOE188try {189FileSystems.getDefault().getPathMatcher("regex:java");190FileSystems.getDefault().getPathMatcher("Regex:java");191FileSystems.getDefault().getPathMatcher("RegEx:java");192FileSystems.getDefault().getPathMatcher("REGEX:java");193System.out.println("Test REGEX_SYNTAX case sensitivity OKAY");194} catch (UnsupportedOperationException e) {195System.err.println("getPathMatcher REGEX_SYNTAX case sensitivity");196e.printStackTrace();197failures++;198}199200if (failures > 0)201throw new RuntimeException(failures +202" sub-test(s) failed - see log for details");203}204}205206207