test/jdk/java/nio/file/DirectoryStream/Basic.java
changeset 53031 0873841d1669
parent 47216 71c04702a3d5
equal deleted inserted replaced
53030:5274fb04cad9 53031:0873841d1669
     1 /*
     1 /*
     2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    71         // check filtering: f* should match foo
    71         // check filtering: f* should match foo
    72         DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
    72         DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
    73             private PathMatcher matcher =
    73             private PathMatcher matcher =
    74                 dir.getFileSystem().getPathMatcher("glob:f*");
    74                 dir.getFileSystem().getPathMatcher("glob:f*");
    75             public boolean accept(Path file) {
    75             public boolean accept(Path file) {
    76                 return matcher.matches(file);
    76                 return matcher.matches(file.getFileName());
    77             }
    77             }
    78         };
    78         };
       
    79 
       
    80         found = false;
    79         try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
    81         try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
    80             for (Path entry: ds) {
    82             for (Path entry: ds) {
    81                 if (!entry.getFileName().equals(foo))
    83                 if (entry.getFileName().equals(foo))
    82                     throw new RuntimeException("entry not expected");
    84                    found = true;
    83             }
    85             }
       
    86             if (!found)
       
    87                 throw new RuntimeException(String.format("Error: entry: %s was not found", foo));
    84         }
    88         }
    85 
    89 
    86         // check filtering: z* should not match any files
    90         // check filtering: z* should not match any files
    87         filter = new DirectoryStream.Filter<Path>() {
    91         filter = new DirectoryStream.Filter<Path>() {
    88             private PathMatcher matcher =
    92             private PathMatcher matcher =
    89                 dir.getFileSystem().getPathMatcher("glob:z*");
    93                 dir.getFileSystem().getPathMatcher("glob:z*");
    90             public boolean accept(Path file) {
    94             public boolean accept(Path file) {
    91                 return matcher.matches(file);
    95                 return matcher.matches(file.getFileName());
    92             }
    96             }
    93         };
    97         };
    94         try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
    98         try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
    95             if (ds.iterator().hasNext())
    99             if (ds.iterator().hasNext())
    96                 throw new RuntimeException("no matching entries expected");
   100                 throw new RuntimeException("no matching entries expected");