/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test * @bug 8006884 * @summary Unit test for java.nio.file.DirectoyStream * @library .. * @run testng Entries */ import java.nio.file.DirectoryStream; import java.nio.file.DirectoryIteratorException; import java.nio.file.NotDirectoryException; import java.nio.file.Files; import java.nio.file.Path; import java.io.IOException; import java.util.TreeSet; import java.util.Comparators; import java.util.stream.Stream; import java.util.Iterator; import org.testng.annotations.Test; import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; import static org.testng.Assert.*; @Test(groups = "unit") public class Entries { /** * Default test folder * testFolder - empty * - file * - dir - d1 * - f1 * - lnDir2 (../dir2) * - dir2 * - linkDir (./dir) * - linkFile(./file) */ static Path testFolder; static boolean supportsLinks; static Path[] level1; static Path[] level2; @BeforeClass void setupTestFolder() throws IOException { testFolder = TestUtil.createTemporaryDirectory(); supportsLinks = TestUtil.supportsLinks(testFolder); TreeSet set = new TreeSet<>(); // Level 1 Path empty = testFolder.resolve("empty"); Path file = testFolder.resolve("file"); Path dir = testFolder.resolve("dir"); Path dir2 = testFolder.resolve("dir2"); Files.createDirectory(empty); Files.createFile(file); Files.createDirectory(dir); Files.createDirectory(dir2); set.add(empty); set.add(file); set.add(dir); set.add(dir2); if (supportsLinks) { Path tmp = testFolder.resolve("linkDir"); Files.createSymbolicLink(tmp, dir); set.add(tmp); tmp = testFolder.resolve("linkFile"); Files.createSymbolicLink(tmp, file); set.add(tmp); } level1 = set.toArray(new Path[0]); set.clear(); // Level 2 Path tmp = dir.resolve("d1"); Files.createDirectory(tmp); set.add(tmp); tmp = dir.resolve("f1"); Files.createFile(tmp); set.add(tmp); if (supportsLinks) { tmp = dir.resolve("lnDir2"); Files.createSymbolicLink(tmp, dir2); set.add(tmp); } level2 = set.toArray(new Path[0]); } @AfterClass void cleanupTestFolder() throws IOException { TestUtil.removeAll(testFolder); } public void testEntriesWithoutFilter() throws IOException { try (DirectoryStream ds = Files.newDirectoryStream(testFolder)) { Path[] actual = ds.entries().sorted(Comparators.naturalOrder()).toArray(Path[]::new); assertEquals(actual, level1); } // link to a directory if (supportsLinks) { try (DirectoryStream ds = Files.newDirectoryStream(testFolder.resolve("linkDir"))) { Path[] actual = ds.entries().sorted(Comparators.naturalOrder()).toArray(Path[]::new); assertEquals(actual.length, level2.length); for (int i = 0; i < actual.length; i++) { assertEquals(actual[i].getFileName(), level2[i].getFileName()); } } } } public void testEntriesWithFilter() throws IOException { try (DirectoryStream ds = Files.newDirectoryStream(testFolder, "f*")) { Path[] actual = ds.entries().toArray(Path[]::new); assertEquals(actual.length, 1); assertEquals(actual[0], testFolder.resolve("file")); } try (DirectoryStream ds = Files.newDirectoryStream(testFolder, "z*")) { int count = ds.entries().mapToInt(p -> 1).reduce(0, Integer::sum); assertEquals(count, 0, "Expect empty stream."); } } public void testDirectoryIteratorException() throws IOException { // check that an IOException thrown by a filter is propagated DirectoryStream.Filter filter = new DirectoryStream.Filter() { public boolean accept(Path file) throws IOException { throw new java.util.zip.ZipException(); } }; try (DirectoryStream ds = Files.newDirectoryStream(testFolder, filter)) { Path[] actual = ds.entries().toArray(Path[]::new); throw new RuntimeException("DirectoryIteratorException expected"); } catch (DirectoryIteratorException x) { IOException cause = x.getCause(); if (!(cause instanceof java.util.zip.ZipException)) throw new RuntimeException("Expected IOException not propagated"); } } public void testEmptyFolder() throws IOException { try (DirectoryStream ds = Files.newDirectoryStream(testFolder.resolve("empty"))) { int count = ds.entries().mapToInt(p -> 1).reduce(0, Integer::sum); assertEquals(count, 0, "Expect empty stream."); } } public void testIllegalStateException() throws IOException { try (DirectoryStream ds = Files.newDirectoryStream(testFolder)) { Stream s = ds.entries(); try { ds.iterator(); fail("Expect IllegalStateException from iterator() call."); } catch (IllegalStateException ise1) {} } try (DirectoryStream ds = Files.newDirectoryStream(testFolder)) { Iterator it = ds.iterator(); try { ds.entries(); fail("Expect IllegalStateException from entries() call."); } catch (IllegalStateException ise2) {} } } public void testNotDirectoryException() throws IOException { try { Files.newDirectoryStream(testFolder.resolve("file")); throw new RuntimeException("NotDirectoryException not thrown"); } catch (NotDirectoryException x) { } } }