1 /*
   2  * Copyright (c) 2013, 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 8006884
  26  * @summary Unit test for java.nio.file.DirectoyStream
  27  * @library ..
  28  * @run testng Entries
  29  */
  30 
  31 import java.nio.file.DirectoryStream;
  32 import java.nio.file.DirectoryIteratorException;
  33 import java.nio.file.NotDirectoryException;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.io.IOException;
  37 import java.util.TreeSet;
  38 import java.util.Comparators;
  39 import java.util.stream.Stream;
  40 import java.util.Iterator;
  41 import org.testng.annotations.Test;
  42 import org.testng.annotations.BeforeClass;
  43 import org.testng.annotations.AfterClass;
  44 import static org.testng.Assert.*;
  45 
  46 @Test(groups = "unit")
  47 public class Entries {
  48     /**
  49      * Default test folder
  50      * testFolder - empty
  51      *            - file
  52      *            - dir - d1
  53      *                  - f1
  54      *                  - lnDir2 (../dir2)
  55      *            - dir2
  56      *            - linkDir (./dir)
  57      *            - linkFile(./file)
  58      */
  59     static Path testFolder;
  60     static boolean supportsLinks;
  61     static Path[] level1;
  62     static Path[] level2;
  63 
  64     @BeforeClass
  65     void setupTestFolder() throws IOException {
  66         testFolder = TestUtil.createTemporaryDirectory();
  67         supportsLinks = TestUtil.supportsLinks(testFolder);
  68         TreeSet<Path> set = new TreeSet<>();
  69 
  70         // Level 1
  71         Path empty = testFolder.resolve("empty");
  72         Path file = testFolder.resolve("file");
  73         Path dir = testFolder.resolve("dir");
  74         Path dir2 = testFolder.resolve("dir2");
  75         Files.createDirectory(empty);
  76         Files.createFile(file);
  77         Files.createDirectory(dir);
  78         Files.createDirectory(dir2);
  79         set.add(empty);
  80         set.add(file);
  81         set.add(dir);
  82         set.add(dir2);
  83         if (supportsLinks) {
  84             Path tmp = testFolder.resolve("linkDir");
  85             Files.createSymbolicLink(tmp, dir);
  86             set.add(tmp);
  87             tmp = testFolder.resolve("linkFile");
  88             Files.createSymbolicLink(tmp, file);
  89             set.add(tmp);
  90         }
  91         level1 = set.toArray(new Path[0]);
  92 
  93         set.clear();
  94         // Level 2
  95         Path tmp = dir.resolve("d1");
  96         Files.createDirectory(tmp);
  97         set.add(tmp);
  98         tmp = dir.resolve("f1");
  99         Files.createFile(tmp);
 100         set.add(tmp);
 101         if (supportsLinks) {
 102             tmp = dir.resolve("lnDir2");
 103             Files.createSymbolicLink(tmp, dir2);
 104             set.add(tmp);
 105         }
 106         level2 = set.toArray(new Path[0]);
 107     }
 108 
 109     @AfterClass
 110     void cleanupTestFolder() throws IOException {
 111         TestUtil.removeAll(testFolder);
 112     }
 113 
 114     public void testEntriesWithoutFilter() throws IOException {
 115         try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder)) {
 116             Path[] actual = ds.entries().sorted(Comparators.naturalOrder()).toArray(Path[]::new);
 117             assertEquals(actual, level1);
 118         }
 119 
 120         // link to a directory
 121         if (supportsLinks) {
 122             try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder.resolve("linkDir"))) {
 123                 Path[] actual = ds.entries().sorted(Comparators.naturalOrder()).toArray(Path[]::new);
 124                 assertEquals(actual.length, level2.length);
 125                 for (int i = 0; i < actual.length; i++) {
 126                     assertEquals(actual[i].getFileName(), level2[i].getFileName());
 127                 }
 128             }
 129         }
 130     }
 131 
 132     public void testEntriesWithFilter() throws IOException {
 133         try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder, "f*")) {
 134             Path[] actual = ds.entries().toArray(Path[]::new);
 135             assertEquals(actual.length, 1);
 136             assertEquals(actual[0], testFolder.resolve("file"));
 137         }
 138 
 139         try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder, "z*")) {
 140             int count = ds.entries().mapToInt(p -> 1).reduce(0, Integer::sum);
 141             assertEquals(count, 0, "Expect empty stream.");
 142         }
 143     }
 144 
 145     public void testDirectoryIteratorException() throws IOException {
 146         // check that an IOException thrown by a filter is propagated
 147         DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
 148             public boolean accept(Path file) throws IOException {
 149                 throw new java.util.zip.ZipException();
 150             }
 151         };
 152 
 153         try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder, filter)) {
 154             Path[] actual = ds.entries().toArray(Path[]::new);
 155             throw new RuntimeException("DirectoryIteratorException expected");
 156         } catch (DirectoryIteratorException x) {
 157             IOException cause = x.getCause();
 158             if (!(cause instanceof java.util.zip.ZipException))
 159                 throw new RuntimeException("Expected IOException not propagated");
 160         }
 161     }
 162 
 163     public void testEmptyFolder() throws IOException {
 164         try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder.resolve("empty"))) {
 165             int count = ds.entries().mapToInt(p -> 1).reduce(0, Integer::sum);
 166             assertEquals(count, 0, "Expect empty stream.");
 167         }
 168     }
 169 
 170     public void testIllegalStateException() throws IOException {
 171         try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder)) {
 172             Stream<Path> s = ds.entries();
 173             try {
 174                 ds.iterator();
 175                 fail("Expect IllegalStateException from iterator() call.");
 176             } catch (IllegalStateException ise1) {}
 177         }
 178 
 179         try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder)) {
 180             Iterator<Path> it = ds.iterator();
 181             try {
 182                 ds.entries();
 183                 fail("Expect IllegalStateException from entries() call.");
 184             } catch (IllegalStateException ise2) {}
 185         }
 186     }
 187 
 188     public void testNotDirectoryException() throws IOException {
 189         try {
 190             Files.newDirectoryStream(testFolder.resolve("file"));
 191             throw new RuntimeException("NotDirectoryException not thrown");
 192         } catch (NotDirectoryException x) {
 193         }
 194     }
 195 }