1 /* 2 * Copyright (C) 2017 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.io; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.collect.Iterables; 22 import com.google.errorprone.annotations.CanIgnoreReturnValue; 23 import java.io.File; 24 import java.io.IOException; 25 import java.nio.file.FileVisitResult; 26 import java.nio.file.Path; 27 import java.nio.file.SimpleFileVisitor; 28 import java.nio.file.attribute.BasicFileAttributes; 29 import junit.framework.TestCase; 30 31 /** 32 * Tests for {@link Files#fileTraverser()}. 33 * 34 * @author Jens Nyman 35 */ 36 37 public class FilesFileTraverserTest extends TestCase { 38 39 private File rootDir; 40 41 @Override setUp()42 public void setUp() throws IOException { 43 rootDir = Files.createTempDir(); 44 } 45 46 @Override tearDown()47 public void tearDown() throws IOException { 48 // delete rootDir and its contents 49 java.nio.file.Files.walkFileTree( 50 rootDir.toPath(), 51 new SimpleFileVisitor<Path>() { 52 @Override 53 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) 54 throws IOException { 55 java.nio.file.Files.deleteIfExists(file); 56 return FileVisitResult.CONTINUE; 57 } 58 59 @Override 60 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { 61 if (exc != null) { 62 return FileVisitResult.TERMINATE; 63 } 64 java.nio.file.Files.deleteIfExists(dir); 65 return FileVisitResult.CONTINUE; 66 } 67 }); 68 } 69 testFileTraverser_emptyDirectory()70 public void testFileTraverser_emptyDirectory() throws Exception { 71 assertThat(Files.fileTraverser().breadthFirst(rootDir)).containsExactly(rootDir); 72 } 73 testFileTraverser_nonExistingFile()74 public void testFileTraverser_nonExistingFile() throws Exception { 75 File file = new File(rootDir, "file-that-doesnt-exist"); 76 77 assertThat(Files.fileTraverser().breadthFirst(file)).containsExactly(file); 78 } 79 testFileTraverser_file()80 public void testFileTraverser_file() throws Exception { 81 File file = newFile("some-file"); 82 83 assertThat(Files.fileTraverser().breadthFirst(file)).containsExactly(file); 84 } 85 testFileTraverser_singleFile()86 public void testFileTraverser_singleFile() throws Exception { 87 File file = newFile("some-file"); 88 89 assertThat(Files.fileTraverser().breadthFirst(rootDir)).containsExactly(rootDir, file); 90 } 91 testFileTraverser_singleDirectory()92 public void testFileTraverser_singleDirectory() throws Exception { 93 File file = newDir("some-dir"); 94 95 assertThat(Files.fileTraverser().breadthFirst(rootDir)).containsExactly(rootDir, file); 96 } 97 testFileTraverser_multipleFilesAndDirectories()98 public void testFileTraverser_multipleFilesAndDirectories() throws Exception { 99 File fileA = newFile("file-a"); 100 File fileB = newFile("file-b"); 101 File dir1 = newDir("dir-1"); 102 File dir2 = newDir("dir-2"); 103 104 assertThat(Files.fileTraverser().breadthFirst(rootDir)) 105 .containsExactly(rootDir, fileA, fileB, dir1, dir2); 106 } 107 testFileTraverser_multipleDirectoryLayers_breadthFirstStartsWithTopLayer()108 public void testFileTraverser_multipleDirectoryLayers_breadthFirstStartsWithTopLayer() 109 throws Exception { 110 File fileA = newFile("file-a"); 111 File dir1 = newDir("dir-1"); 112 newFile("dir-1/file-b"); 113 newFile("dir-1/dir-2"); 114 115 assertThat(Iterables.limit(Files.fileTraverser().breadthFirst(rootDir), 3)) 116 .containsExactly(rootDir, fileA, dir1); 117 } 118 testFileTraverser_multipleDirectoryLayers_traversalReturnsAll()119 public void testFileTraverser_multipleDirectoryLayers_traversalReturnsAll() throws Exception { 120 File fileA = newFile("file-a"); 121 File dir1 = newDir("dir-1"); 122 File fileB = newFile("dir-1/file-b"); 123 File dir2 = newFile("dir-1/dir-2"); 124 125 assertThat(Files.fileTraverser().breadthFirst(rootDir)) 126 .containsExactly(rootDir, fileA, fileB, dir1, dir2); 127 } 128 129 @CanIgnoreReturnValue newDir(String name)130 private File newDir(String name) { 131 File file = new File(rootDir, name); 132 file.mkdir(); 133 return file; 134 } 135 136 @CanIgnoreReturnValue newFile(String name)137 private File newFile(String name) throws IOException { 138 File file = new File(rootDir, name); 139 file.createNewFile(); 140 return file; 141 } 142 } 143