View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2;
18  
19  import org.junit.AfterClass;
20  import org.junit.Assert;
21  import org.junit.BeforeClass;
22  import org.junit.Test;
23  
24  /**
25   * Tests FileTypeSelector.
26   *
27   * @since 2.1
28   */
29  public class FileTypeSelectorTest {
30      private static FileObject BaseFolder;
31  
32      /**
33       * Creates a RAM FS.
34       *
35       * @throws Exception
36       */
37      @BeforeClass
38      public static void setUpClass() throws Exception {
39          BaseFolder = VFS.getManager().resolveFile("ram://" + FileTypeSelectorTest.class.getName());
40          BaseFolder.resolveFile("root1.html").createFile();
41          BaseFolder.resolveFile("root2.html").createFile();
42          BaseFolder.resolveFile("f1/a.html").createFile();
43          BaseFolder.resolveFile("f2/b.html").createFile();
44          BaseFolder.resolveFile("f3/c.html").createFile();
45          BaseFolder.resolveFile("f4/").createFolder();
46          BaseFolder.resolveFile("f5/").createFolder();
47          BaseFolder.resolveFile("f6/f7").createFolder();
48      }
49  
50      /**
51       * Deletes RAM FS files.
52       *
53       * @throws Exception
54       */
55      @AfterClass
56      public static void tearDownClass() throws Exception {
57          if (BaseFolder != null) {
58              BaseFolder.deleteAll();
59          }
60      }
61  
62      @Test
63      public void testFileOrFolders() throws Exception {
64          final FileSelector selector = new FileTypeSelector(FileType.FILE_OR_FOLDER);
65          final FileObject[] foList = BaseFolder.findFiles(selector);
66          // Why 0?
67          Assert.assertEquals(0, foList.length);
68      }
69  
70      @Test
71      public void testFiles() throws Exception {
72          final FileSelector selector = new FileTypeSelector(FileType.FILE);
73          final FileObject[] foList = BaseFolder.findFiles(selector);
74          Assert.assertEquals(5, foList.length);
75      }
76  
77      @Test
78      public void testFolders() throws Exception {
79          final FileSelector selector = new FileTypeSelector(FileType.FOLDER);
80          final FileObject[] foList = BaseFolder.findFiles(selector);
81          Assert.assertEquals(8, foList.length);
82      }
83  }