Path: blob/master/test/jdk/java/nio/file/DirectoryStream/Basic.java
41153 views
/*1* Copyright (c) 2008, 2018, 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.getFileName());76}77};7879found = false;80try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {81for (Path entry: ds) {82if (entry.getFileName().equals(foo))83found = true;84}85if (!found)86throw new RuntimeException(String.format("Error: entry: %s was not found", foo));87}8889// check filtering: z* should not match any files90filter = new DirectoryStream.Filter<Path>() {91private PathMatcher matcher =92dir.getFileSystem().getPathMatcher("glob:z*");93public boolean accept(Path file) {94return matcher.matches(file.getFileName());95}96};97try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {98if (ds.iterator().hasNext())99throw new RuntimeException("no matching entries expected");100}101102// check that an IOException thrown by a filter is propagated103filter = new DirectoryStream.Filter<Path>() {104public boolean accept(Path file) throws IOException {105throw new java.util.zip.ZipException();106}107};108stream = newDirectoryStream(dir, filter);109try {110stream.iterator().hasNext();111throw new RuntimeException("DirectoryIteratorException expected");112} catch (DirectoryIteratorException x) {113IOException cause = x.getCause();114if (!(cause instanceof java.util.zip.ZipException))115throw new RuntimeException("Expected IOException not propagated");116} finally {117stream.close();118}119120// check that exception or error thrown by filter is not thrown121// by newDirectoryStream or iterator method.122stream = newDirectoryStream(dir, new DirectoryStream.Filter<Path>() {123public boolean accept(Path file) {124throw new RuntimeException("Should not be visible");125}126});127try {128stream.iterator();129} finally {130stream.close();131}132133// test NotDirectoryException134try {135newDirectoryStream(dir.resolve(foo));136throw new RuntimeException("NotDirectoryException not thrown");137} catch (NotDirectoryException x) {138}139140// test UnsupportedOperationException141stream = newDirectoryStream(dir);142Iterator<Path> i = stream.iterator();143i.next();144try {145i.remove();146throw new RuntimeException("UnsupportedOperationException expected");147} catch (UnsupportedOperationException uoe) {148}149150// test IllegalStateException151stream = newDirectoryStream(dir);152stream.iterator();153try {154// attempt to obtain second iterator155stream.iterator();156throw new RuntimeException("IllegalStateException not thrown as expected");157} catch (IllegalStateException x) {158}159stream.close();160161stream = newDirectoryStream(dir);162stream.close();163try {164// attempt to obtain iterator after stream is closed165stream.iterator();166throw new RuntimeException("IllegalStateException not thrown as expected");167} catch (IllegalStateException x) {168}169170// test that iterator reads to end of stream when closed171stream = newDirectoryStream(dir);172i = stream.iterator();173stream.close();174while (i.hasNext())175i.next();176177stream = newDirectoryStream(dir);178i = stream.iterator();179stream.close();180try {181for (;;) i.next();182} catch (NoSuchElementException expected) { }183}184185public static void main(String[] args) throws IOException {186Path dir = TestUtil.createTemporaryDirectory();187try {188doTest(dir);189} finally {190TestUtil.removeAll(dir);191}192}193}194195196