1 /*
   2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4313887 6838333
  26  * @summary Unit test for java.nio.file.DirectoryStream
  27  * @library ..
  28  */
  29 
  30 import java.nio.file.*;
  31 import static java.nio.file.Files.*;
  32 import java.util.*;
  33 import java.io.IOException;
  34 
  35 public class Basic {
  36     static boolean found;
  37 
  38     static void doTest(final Path dir) throws IOException {
  39         DirectoryStream<Path> stream;
  40 
  41         // test that directory is empty
  42         try (DirectoryStream<Path> ds = newDirectoryStream(dir)) {
  43             if (ds.iterator().hasNext())
  44                 throw new RuntimeException("directory not empty");
  45         }
  46 
  47         // create file in directory
  48         final Path foo = Paths.get("foo");
  49         createFile(dir.resolve(foo));
  50 
  51         // iterate over directory and check there is one entry
  52         stream = newDirectoryStream(dir);
  53         found = false;
  54         try {
  55             for (Path entry: stream) {
  56                 if (entry.getFileName().equals(foo)) {
  57                     if (found)
  58                         throw new RuntimeException("entry already found");
  59                     found = true;
  60                 } else {
  61                     throw new RuntimeException("entry " + entry.getFileName() +
  62                         " not expected");
  63                 }
  64             }
  65         } finally {
  66             stream.close();
  67         }
  68         if (!found)
  69             throw new RuntimeException("entry not found");
  70 
  71         // check filtering: f* should match foo
  72         DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
  73             private PathMatcher matcher =
  74                 dir.getFileSystem().getPathMatcher("glob:f*");
  75             public boolean accept(Path file) {
  76                 return matcher.matches(file);
  77             }
  78         };
  79         try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
  80             for (Path entry: ds) {
  81                 if (!entry.getFileName().equals(foo))
  82                     throw new RuntimeException("entry not expected");
  83             }
  84         }
  85 
  86         // check filtering: z* should not match any files
  87         filter = new DirectoryStream.Filter<Path>() {
  88             private PathMatcher matcher =
  89                 dir.getFileSystem().getPathMatcher("glob:z*");
  90             public boolean accept(Path file) {
  91                 return matcher.matches(file);
  92             }
  93         };
  94         try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
  95             if (ds.iterator().hasNext())
  96                 throw new RuntimeException("no matching entries expected");
  97         }
  98 
  99         // check that an IOException thrown by a filter is propagated
 100         filter = new DirectoryStream.Filter<Path>() {
 101             public boolean accept(Path file) throws IOException {
 102                 throw new java.util.zip.ZipException();
 103             }
 104         };
 105         stream = newDirectoryStream(dir, filter);
 106         try {
 107             stream.iterator().hasNext();
 108             throw new RuntimeException("DirectoryIteratorException expected");
 109         } catch (DirectoryIteratorException x) {
 110             IOException cause = x.getCause();
 111             if (!(cause instanceof java.util.zip.ZipException))
 112                 throw new RuntimeException("Expected IOException not propagated");
 113         } finally {
 114             stream.close();
 115         }
 116 
 117         // check that exception or error thrown by filter is not thrown
 118         // by newDirectoryStream or iterator method.
 119         stream = newDirectoryStream(dir, new DirectoryStream.Filter<Path>() {
 120             public boolean accept(Path file) {
 121                 throw new RuntimeException("Should not be visible");
 122             }
 123         });
 124         try {
 125             stream.iterator();
 126         } finally {
 127             stream.close();
 128         }
 129 
 130         // test NotDirectoryException
 131         try {
 132             newDirectoryStream(dir.resolve(foo));
 133             throw new RuntimeException("NotDirectoryException not thrown");
 134         } catch (NotDirectoryException x) {
 135         }
 136 
 137         // test UnsupportedOperationException
 138         stream = newDirectoryStream(dir);
 139         Iterator<Path> i = stream.iterator();
 140         i.next();
 141         try {
 142             i.remove();
 143             throw new RuntimeException("UnsupportedOperationException expected");
 144         } catch (UnsupportedOperationException uoe) {
 145         }
 146 
 147         // test IllegalStateException
 148         stream = newDirectoryStream(dir);
 149         stream.iterator();
 150         try {
 151             // attempt to obtain second iterator
 152             stream.iterator();
 153             throw new RuntimeException("IllegalStateException not thrown as expected");
 154         } catch (IllegalStateException x) {
 155         }
 156         stream.close();
 157 
 158         stream = newDirectoryStream(dir);
 159         stream.close();
 160         try {
 161             // attempt to obtain iterator after stream is closed
 162             stream.iterator();
 163             throw new RuntimeException("IllegalStateException not thrown as expected");
 164         } catch (IllegalStateException x) {
 165         }
 166 
 167         // test that iterator reads to end of stream when closed
 168         stream = newDirectoryStream(dir);
 169         i = stream.iterator();
 170         stream.close();
 171         while (i.hasNext())
 172             i.next();
 173 
 174         stream = newDirectoryStream(dir);
 175         i = stream.iterator();
 176         stream.close();
 177         try {
 178             for (;;) i.next();
 179         } catch (NoSuchElementException expected) { }
 180     }
 181 
 182     public static void main(String[] args) throws IOException {
 183         Path dir = TestUtil.createTemporaryDirectory();
 184         try {
 185             doTest(dir);
 186         } finally {
 187             TestUtil.removeAll(dir);
 188         }
 189     }
 190 }