--- /dev/null 2013-07-08 08:32:16.866272774 -0700 +++ new/test/java/nio/Files/FilesLambdaTest.java 2013-07-10 14:11:57.475135373 -0700 @@ -0,0 +1,336 @@ +/* + * Copyright (c) 2012, 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 + * @summary function test for Files.list()/find()/walk()/lines() + * @library /sqeutil + * @(#) FilesLambdaTest.java + * @author Tristan Yan + * @run testng/othervm FilesLambdaTest + */ + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.NotDirectoryException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import static org.testng.Assert.*; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class FilesLambdaTest { + private static final Random rand = new Random(System.nanoTime()); + + private static final String TEST_SRC = System.getProperty("test.src"); + + private static final String ROOT_NAME = "FilesLambdaTest" + System.nanoTime(); + + private static final String LINES_TEST_FILE = "lines" + System.nanoTime(); + + private static final int MAX_FILES_NUMBER = 1 << 6; + + private static final Charset UTF8 = Charset.forName("UTF-8"); + + private final static int MIN_LEN = 1 << 2; + + private final static int MAX_LEN = 1 << 8; + + private final static int LINES_NUM = 1 << 8; + + private static final String[][] folders = { + {"A01"}, + {"A01", "AA02"}, + {"A01", "AB02"}, + {"A01", "AC02"}, + {"B01"}, + {"B01", "BA02"}, + {"B01", "BA02", "BAA03"}, + {"B01", "BA02", "BAA03", "BAAA04", "BAAAA05", "BAAAAA06", "BAAAAAA07", + "BAAAAAAA08", "BAAAAAAAA09", "BAAAAAAAAA10", "BAAAAAAAAAA11"}, + {"C01"}, + {"C01", "CA02"}, + {"C01", "CD02"}, + {"D01", "DA02", "DAA03", "DAAA04", "DAAAA05", "DAAAAA06", "DAAAAAA07"}, + {"E01"}, + {"E01", "EA02"}, + {"E01", "EB02", "EBB03"}, + {"E01", "EC02", "ECC03", "ECCC04"}, + {"E01", "ED02", "EDD03", "EDDD04", "EDDDD05"}, + {"E01", "EE02", "EEE03", "EEEE04", "EEEEE05", "EEEEEE06"}, + {"E01", "EF02", "EFF03", "EFFF04", "EFFFF05", "EFFFFF06"}, + {"E01", "EG02", "EGG03", "EGGG04", "EGGGG05", "EGGGGG06", "EGGGGGG07"}, + {"E01", "EH02", "EHH03", "EHHH04", "EHHHH05", "EHHHHH06", "EHHHHHH07", + "EHHHHHHH08"}, + {"E01", "EI02", "EII03", "EIII04", "EIIII05", "EIIIII06", "EIIIIII07", + "EIIIIIII08", "EIIIIIIII09"}, + {"E01", "EJ02", "EJJ03", "EJJJ04", "EJJJJ05", "EJJJJJ06", "EJJJJJJ07", + "EJJJJJJJ08", "EJJJJJJJJ09", "EJJJJJJJJJ10"}, + {"E01", "EK02", "EKK03", "EKKK04", "EKKKK05", "EKKKKK06", "EKKKKKK07", + "EKKKKKKK08", "EKKKKKKKK09", "EKKKKKKKKK10", "EJJJJJJJJJJ11"} + }; + + private Path root; + + private Path testFile; + + private Path notExistPath; + + final Consumer writeReverseWithLink = path -> { + int filesCount = rand.nextInt(MAX_FILES_NUMBER); + for(int count = 0; count < filesCount; count++) { + String fileName = String.valueOf(rand.nextLong()); + Path file = path.resolve(fileName); + String linkName = String.valueOf(rand.nextLong()); + Path link = path.resolve(linkName); + try (BufferedWriter writer + = Files.newBufferedWriter(Files.createFile(file), UTF8)) { + writer.write(new StringBuilder(fileName).reverse().toString(), + 0, fileName.length()); + Files.createSymbolicLink(link, file); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + }; + + @BeforeClass + public void filesSetUp() throws IOException { + List paths = new ArrayList<>(); + root = Paths.get(TEST_SRC, ROOT_NAME); + paths.add(Files.createDirectory(root)); + for(int i = 0; i < folders.length; i++ ) { + String[] toBeCreated = folders[i]; + Path folder = Paths.get(ROOT_NAME, toBeCreated); + paths.add(Files.createDirectories(folder)); + } + paths.forEach(writeReverseWithLink); + + testFile = Paths.get(LINES_TEST_FILE); + try (BufferedWriter writer + = Files.newBufferedWriter(Files.createFile(testFile), UTF8)) { + for(int i = 0; i< LINES_NUM; i++) { + String line = StringUtilities.randomString(MAX_LEN, MIN_LEN); + writer.write(line, 0, line.length()); + } + } catch (IOException ex) { + throw new RuntimeException(ex); + } + + String NOT_EXIST = "NOT_EXIST"; + notExistPath = Paths.get(TEST_SRC, NOT_EXIST); + } + + @AfterClass + public void filesTearDown() throws IOException { + if(root != null) { + Files.walkFileTree(root, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) + throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) + throws IOException{ + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + }); + } + if(testFile != null) { + Files.delete(testFile); + } + } + + @Test + public void testFilesList() throws IOException { + checkFilesList(root); + } + + @Test + public void testWalk() throws IOException{ + String[] dir = folders[rand.nextInt(folders.length)]; + Path walkFolder = Paths.get(ROOT_NAME, dir); + final int maxDepth = rand.nextInt(11); + List expectedFullFileList = new ArrayList<>(); + List expectedMaxDepthFileList = new ArrayList<>(); + + List expectedFullSymList = new ArrayList<>(); + List expectedMaxDepthSymList = new ArrayList<>(); + + List expectedFullDirList = new ArrayList<>(); + List expectedMaxDepthDirList = new ArrayList<>(); + + Files.walkFileTree(walkFolder, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) + throws IOException { + if (walkFolder.relativize(file).getNameCount() <= maxDepth) { + expectedMaxDepthFileList.add(file); + if(Files.isSymbolicLink(file)) { + expectedMaxDepthSymList.add(file); + } + } + expectedFullFileList.add(file); + if(Files.isSymbolicLink(file)) { + expectedFullSymList.add(file); + } + return FileVisitResult.CONTINUE; + } + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) + throws IOException{ + if (walkFolder.relativize(dir).getNameCount() <= maxDepth + || walkFolder.equals(dir)) { + expectedMaxDepthDirList.add(dir); + } + expectedFullDirList.add(dir); + return FileVisitResult.CONTINUE; + } + }); + Collections.sort(expectedFullFileList); + Collections.sort(expectedMaxDepthFileList); + Collections.sort(expectedFullSymList); + Collections.sort(expectedMaxDepthSymList); + Collections.sort(expectedFullDirList); + Collections.sort(expectedMaxDepthDirList); + + assertEquals(expectedMaxDepthFileList, + Files.walk(walkFolder, maxDepth).filter(p -> !Files.isDirectory(p)). + sorted().collect(Collectors.toList())); + assertEquals(expectedMaxDepthSymList, + Files.walk(walkFolder, maxDepth).filter(p -> Files.isSymbolicLink(p)). + sorted().collect(Collectors.toList())); + assertEquals(expectedMaxDepthDirList, + Files.walk(walkFolder, maxDepth).filter(p -> Files.isDirectory(p)). + sorted().collect(Collectors.toList())); + assertEquals(expectedFullFileList, + Files.walk(walkFolder).filter(p -> !Files.isDirectory(p)). + sorted().collect(Collectors.toList())); + assertEquals(expectedFullSymList, + Files.walk(walkFolder).filter(p -> Files.isSymbolicLink(p)). + sorted().collect(Collectors.toList())); + assertEquals(expectedFullDirList, + Files.walk(walkFolder).filter(p -> Files.isDirectory(p)). + sorted().collect(Collectors.toList())); + } + + @Test + public void testFind() throws IOException{ + String[] dir = folders[rand.nextInt(folders.length)]; + Path walkFolder = Paths.get(ROOT_NAME, dir); + String walkFoderName = dir[dir.length - 1]; + + int walkFolderDepth = Integer.parseInt(walkFoderName.substring(walkFoderName.length() - 2)); + int maxDepth = rand.nextInt(11); + Stream stream + = Files.find(walkFolder, maxDepth, (p,bfa) -> Files.isSymbolicLink(p)); + stream.forEach(p -> { + assertTrue(Files.isSymbolicLink(p)); + assertFalse(Files.isDirectory(p)); + assertFalse(Files.isRegularFile(p, LinkOption.NOFOLLOW_LINKS)); + String parentName = p.getParent().toFile().getName(); + int depth = Integer.parseInt(parentName.substring(parentName.length() - 2)); + assertTrue(depth - walkFolderDepth < maxDepth); + }); + + assertEquals(Files.find(walkFolder, maxDepth, (p,bfa) -> Files.isSymbolicLink(p)).toArray(), + Files.find(walkFolder, maxDepth, (p,bfa) -> bfa.isSymbolicLink()).toArray()); + } + + @Test + public void testLines() throws IOException{ + Stream stream = Files.lines(testFile, UTF8); + List lines = Files.readAllLines(testFile, UTF8); + assertEquals(lines, stream.collect(Collectors.toList())); + } + + @Test(expectedExceptions = IOException.class) + public void testListNoReadAccess() throws IOException{ + Files.list(notExistPath); + } + + @Test(expectedExceptions = NotDirectoryException.class) + public void testListNotDirectory() throws IOException{ + Files.list(testFile); + } + + @Test(expectedExceptions = IOException.class) + public void testFindNoExist() throws IOException{ + Files.find(notExistPath, Integer.MAX_VALUE, (p, bfa) -> true); + } + + @Test(expectedExceptions = IOException.class) + public void testLinesNoExist() throws IOException{ + Files.lines(notExistPath, UTF8); + } + + @Test(expectedExceptions = IOException.class) + public void testWalkNoExist() throws IOException{ + Files.walk(notExistPath); + } + + @Test(expectedExceptions = IOException.class) + public void testWalkNoExistWithDepth() throws IOException{ + Files.walk(notExistPath, Integer.MAX_VALUE); + } + + public void checkFilesList(Path checkPath){ + try { + assert(Files.isDirectory(checkPath)); + Files.list(checkPath).filter(p -> Files.isDirectory(p)).forEach(p -> checkFilesList(p)); + assertEquals(Files.list(checkPath).filter(p -> Files.isRegularFile(p, LinkOption.NOFOLLOW_LINKS)).count(), + Files.list(checkPath).filter(p -> Files.isSymbolicLink(p)).count()); + assertTrue(Files.list(checkPath).filter(p -> Files.isRegularFile(p, LinkOption.NOFOLLOW_LINKS)).allMatch(file -> contentRevered(file))); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + private boolean contentRevered(Path path) { + try (BufferedReader reader = Files.newBufferedReader(path, UTF8)){ + String fileName = path.getName(path.getNameCount() -1).toString(); + String reversed = new StringBuilder(reader.readLine()).reverse().toString(); + return (fileName.equals(reversed)); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } +} \ No newline at end of file