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