Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/DirectoryStream/Basic.java
38828 views
/*1* Copyright (c) 2008, 2011, 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 683833325* @summary Unit test for java.nio.file.DirectoryStream26* @library ..27*/2829import java.nio.file.*;30import static java.nio.file.Files.*;31import java.util.*;32import java.io.IOException;3334public class Basic {35static boolean found;3637static void doTest(final Path dir) throws IOException {38DirectoryStream<Path> stream;3940// test that directory is empty41try (DirectoryStream<Path> ds = newDirectoryStream(dir)) {42if (ds.iterator().hasNext())43throw new RuntimeException("directory not empty");44}4546// create file in directory47final Path foo = Paths.get("foo");48createFile(dir.resolve(foo));4950// iterate over directory and check there is one entry51stream = newDirectoryStream(dir);52found = false;53try {54for (Path entry: stream) {55if (entry.getFileName().equals(foo)) {56if (found)57throw new RuntimeException("entry already found");58found = true;59} else {60throw new RuntimeException("entry " + entry.getFileName() +61" not expected");62}63}64} finally {65stream.close();66}67if (!found)68throw new RuntimeException("entry not found");6970// check filtering: f* should match foo71DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {72private PathMatcher matcher =73dir.getFileSystem().getPathMatcher("glob:f*");74public boolean accept(Path file) {75return matcher.matches(file);76}77};78try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {79for (Path entry: ds) {80if (!entry.getFileName().equals(foo))81throw new RuntimeException("entry not expected");82}83}8485// check filtering: z* should not match any files86filter = new DirectoryStream.Filter<Path>() {87private PathMatcher matcher =88dir.getFileSystem().getPathMatcher("glob:z*");89public boolean accept(Path file) {90return matcher.matches(file);91}92};93try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {94if (ds.iterator().hasNext())95throw new RuntimeException("no matching entries expected");96}9798// check that an IOException thrown by a filter is propagated99filter = new DirectoryStream.Filter<Path>() {100public boolean accept(Path file) throws IOException {101throw new java.util.zip.ZipException();102}103};104stream = newDirectoryStream(dir, filter);105try {106stream.iterator().hasNext();107throw new RuntimeException("DirectoryIteratorException expected");108} catch (DirectoryIteratorException x) {109IOException cause = x.getCause();110if (!(cause instanceof java.util.zip.ZipException))111throw new RuntimeException("Expected IOException not propagated");112} finally {113stream.close();114}115116// check that exception or error thrown by filter is not thrown117// by newDirectoryStream or iterator method.118stream = newDirectoryStream(dir, new DirectoryStream.Filter<Path>() {119public boolean accept(Path file) {120throw new RuntimeException("Should not be visible");121}122});123try {124stream.iterator();125} finally {126stream.close();127}128129// test NotDirectoryException130try {131newDirectoryStream(dir.resolve(foo));132throw new RuntimeException("NotDirectoryException not thrown");133} catch (NotDirectoryException x) {134}135136// test UnsupportedOperationException137stream = newDirectoryStream(dir);138Iterator<Path> i = stream.iterator();139i.next();140try {141i.remove();142throw new RuntimeException("UnsupportedOperationException expected");143} catch (UnsupportedOperationException uoe) {144}145146// test IllegalStateException147stream = newDirectoryStream(dir);148stream.iterator();149try {150// attempt to obtain second iterator151stream.iterator();152throw new RuntimeException("IllegalStateException not thrown as expected");153} catch (IllegalStateException x) {154}155stream.close();156157stream = newDirectoryStream(dir);158stream.close();159try {160// attempt to obtain iterator after stream is closed161stream.iterator();162throw new RuntimeException("IllegalStateException not thrown as expected");163} catch (IllegalStateException x) {164}165166// test that iterator reads to end of stream when closed167stream = newDirectoryStream(dir);168i = stream.iterator();169stream.close();170while (i.hasNext())171i.next();172173stream = newDirectoryStream(dir);174i = stream.iterator();175stream.close();176try {177for (;;) i.next();178} catch (NoSuchElementException expected) { }179}180181public static void main(String[] args) throws IOException {182Path dir = TestUtil.createTemporaryDirectory();183try {184doTest(dir);185} finally {186TestUtil.removeAll(dir);187}188}189}190191192