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.common.jimfs.Configuration; 23 import com.google.common.jimfs.Jimfs; 24 import com.google.errorprone.annotations.CanIgnoreReturnValue; 25 import java.io.IOException; 26 import java.nio.file.Files; 27 import java.nio.file.Path; 28 import junit.framework.TestCase; 29 30 /** 31 * Tests for {@link MoreFiles#fileTraverser()}. 32 * 33 * @author Jens Nyman 34 */ 35 36 public class MoreFilesFileTraverserTest extends TestCase { 37 38 private Path rootDir; 39 40 @Override setUp()41 public void setUp() throws IOException { 42 rootDir = Jimfs.newFileSystem(Configuration.unix()).getPath("/tmp"); 43 Files.createDirectory(rootDir); 44 } 45 46 @Override tearDown()47 public void tearDown() throws IOException { 48 rootDir.getFileSystem().close(); 49 } 50 testFileTraverser_emptyDirectory()51 public void testFileTraverser_emptyDirectory() throws Exception { 52 assertThat(MoreFiles.fileTraverser().breadthFirst(rootDir)).containsExactly(rootDir); 53 } 54 testFileTraverser_nonExistingFile()55 public void testFileTraverser_nonExistingFile() throws Exception { 56 Path file = rootDir.resolve("file-that-doesnt-exist"); 57 58 assertThat(MoreFiles.fileTraverser().breadthFirst(file)).containsExactly(file); 59 } 60 testFileTraverser_file()61 public void testFileTraverser_file() throws Exception { 62 Path file = newFile("some-file"); 63 64 assertThat(MoreFiles.fileTraverser().breadthFirst(file)).containsExactly(file); 65 } 66 testFileTraverser_singleFile()67 public void testFileTraverser_singleFile() throws Exception { 68 Path file = newFile("some-file"); 69 70 assertThat(MoreFiles.fileTraverser().breadthFirst(rootDir)).containsExactly(rootDir, file); 71 } 72 testFileTraverser_singleDirectory()73 public void testFileTraverser_singleDirectory() throws Exception { 74 Path file = newDir("some-dir"); 75 76 assertThat(MoreFiles.fileTraverser().breadthFirst(rootDir)).containsExactly(rootDir, file); 77 } 78 testFileTraverser_multipleFilesAndDirectories()79 public void testFileTraverser_multipleFilesAndDirectories() throws Exception { 80 Path fileA = newFile("file-a"); 81 Path fileB = newFile("file-b"); 82 Path dir1 = newDir("dir-1"); 83 Path dir2 = newDir("dir-2"); 84 85 assertThat(MoreFiles.fileTraverser().breadthFirst(rootDir)) 86 .containsExactly(rootDir, fileA, fileB, dir1, dir2); 87 } 88 testFileTraverser_multipleDirectoryLayers_breadthFirstStartsWithTopLayer()89 public void testFileTraverser_multipleDirectoryLayers_breadthFirstStartsWithTopLayer() 90 throws Exception { 91 Path fileA = newFile("file-a"); 92 Path dir1 = newDir("dir-1"); 93 newFile("dir-1/file-b"); 94 newFile("dir-1/dir-2"); 95 96 assertThat(Iterables.limit(MoreFiles.fileTraverser().breadthFirst(rootDir), 3)) 97 .containsExactly(rootDir, fileA, dir1); 98 } 99 testFileTraverser_multipleDirectoryLayers_traversalReturnsAll()100 public void testFileTraverser_multipleDirectoryLayers_traversalReturnsAll() throws Exception { 101 Path fileA = newFile("file-a"); 102 Path dir1 = newDir("dir-1"); 103 Path fileB = newFile("dir-1/file-b"); 104 Path dir2 = newFile("dir-1/dir-2"); 105 106 assertThat(MoreFiles.fileTraverser().breadthFirst(rootDir)) 107 .containsExactly(rootDir, fileA, fileB, dir1, dir2); 108 } 109 110 @CanIgnoreReturnValue newDir(String name)111 private Path newDir(String name) throws IOException { 112 Path dir = rootDir.resolve(name); 113 Files.createDirectory(dir); 114 return dir; 115 } 116 117 @CanIgnoreReturnValue newFile(String name)118 private Path newFile(String name) throws IOException { 119 Path file = rootDir.resolve(name); 120 MoreFiles.touch(file); 121 return file; 122 } 123 } 124