1/*
2 * Copyright (C) 2012 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
17package com.google.common.io;
18
19import com.google.common.collect.ImmutableSet;
20
21import junit.framework.TestCase;
22
23import java.io.File;
24import java.io.IOException;
25
26/**
27 * Tests for {@link Files#fileTreeViewer}.
28 *
29 * @author Colin Decker
30 */
31
32public class FileTreeTraverserTest extends TestCase {
33
34  private File dir;
35
36  @Override
37  public void setUp() throws IOException {
38    dir = Files.createTempDir();
39  }
40
41  @Override
42  public void tearDown() throws IOException {
43    File[] files = dir.listFiles();
44    if (files == null) {
45      return;
46    }
47
48    // we aren't creating any files in subdirs
49    for (File file : files) {
50      file.delete();
51    }
52
53    dir.delete();
54  }
55
56  public void testFileTreeViewer_emptyDir() throws IOException {
57    assertDirChildren();
58  }
59
60  public void testFileTreeViewer_singleFile() throws IOException {
61    File file = newFile("test");
62    assertDirChildren(file);
63  }
64
65  public void testFileTreeViewer_singleDir() throws IOException {
66    File file = newDir("test");
67    assertDirChildren(file);
68  }
69
70  public void testFileTreeViewer_multipleFiles() throws IOException {
71    File a = newFile("a");
72    File b = newDir("b");
73    File c = newFile("c");
74    File d = newDir("d");
75    assertDirChildren(a, b, c, d);
76  }
77
78  private File newDir(String name) throws IOException {
79    File file = new File(dir, name);
80    file.mkdir();
81    return file;
82  }
83
84  private File newFile(String name) throws IOException {
85    File file = new File(dir, name);
86    file.createNewFile();
87    return file;
88  }
89
90  private void assertDirChildren(File... files) {
91    assertEquals(ImmutableSet.copyOf(files),
92        ImmutableSet.copyOf(Files.fileTreeTraverser().children(dir)));
93  }
94}
95