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 java.util.Arrays;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import org.junit.AfterClass;
25  import org.junit.Assert;
26  import org.junit.BeforeClass;
27  import org.junit.Test;
28  
29  /**
30   * Tests FileExtensionSelector.
31   *
32   * @since 2.1
33   */
34  public class FileExtensionSelectorTest {
35      private static FileObject BaseFolder;
36  
37      private static final int FileCount = 9;
38  
39      private static final int ExtensionCount = 3;
40  
41      private static final int FilesPerExtensionCount = 3;
42  
43      /**
44       * Creates a RAM FS.
45       *
46       * @throws Exception
47       */
48      @BeforeClass
49      public static void setUpClass() throws Exception {
50          BaseFolder = VFS.getManager().resolveFile("ram://" + FileExtensionSelectorTest.class.getName());
51          BaseFolder.deleteAll();
52          BaseFolder.createFolder();
53          BaseFolder.resolveFile("a.htm").createFile();
54          BaseFolder.resolveFile("a.html").createFile();
55          BaseFolder.resolveFile("a.xhtml").createFile();
56          BaseFolder.resolveFile("b.htm").createFile();
57          BaseFolder.resolveFile("b.html").createFile();
58          BaseFolder.resolveFile("b.xhtml").createFile();
59          BaseFolder.resolveFile("c.htm").createFile();
60          BaseFolder.resolveFile("c.html").createFile();
61          BaseFolder.resolveFile("c.xhtml").createFile();
62      }
63  
64      /**
65       * Deletes RAM FS files.
66       *
67       * @throws Exception
68       */
69      @AfterClass
70      public static void tearDownClass() throws Exception {
71          if (BaseFolder != null) {
72              BaseFolder.deleteAll();
73          }
74      }
75  
76      /**
77       * Tests an empty selector.
78       *
79       * @throws Exception
80       */
81      @Test
82      public void testEmpty() throws Exception {
83          final FileSelector selector = new FileExtensionSelector();
84          final FileObject[] foList = BaseFolder.findFiles(selector);
85          Assert.assertEquals(0, foList.length);
86      }
87  
88      /**
89       * Tests many extensions at once.
90       *
91       * @throws Exception
92       */
93      @Test
94      public void testManyExtensions() throws Exception {
95          final FileObject[] foArray = BaseFolder.findFiles(Selectors.SELECT_FILES);
96          Assert.assertTrue(foArray.length > 0);
97          // gather file extensions.
98          final Set<String> extensionSet = new HashSet<>();
99          for (final FileObject fo : foArray) {
100             extensionSet.add(fo.getName().getExtension());
101         }
102         final String message = String.format("Extensions: %s; files: %s", extensionSet.toString(),
103                 Arrays.asList(foArray).toString());
104         Assert.assertFalse(message, extensionSet.isEmpty());
105         Assert.assertEquals(message, ExtensionCount, extensionSet.size());
106         // check all unique extensions
107         final FileSelector selector = new FileExtensionSelector(extensionSet);
108         final FileObject[] list = BaseFolder.findFiles(selector);
109         Assert.assertEquals(FileCount, list.length);
110     }
111 
112     /**
113      * Tests a null selector.
114      *
115      * @throws Exception
116      */
117     @Test
118     public void testNullCollection() throws Exception {
119         final FileSelector selector0 = new FileExtensionSelector((Collection<String>) null);
120         final FileObject[] foList = BaseFolder.findFiles(selector0);
121         Assert.assertEquals(0, foList.length);
122     }
123 
124     /**
125      * Tests a null selector.
126      *
127      * @throws Exception
128      */
129     @Test
130     public void testNullString() throws Exception {
131         final FileSelector selector0 = new FileExtensionSelector((String) null);
132         final FileObject[] foList = BaseFolder.findFiles(selector0);
133         Assert.assertEquals(0, foList.length);
134     }
135 
136     /**
137      * Tests a one extension selector.
138      *
139      * @throws Exception
140      */
141     @Test
142     public void testOneExtension() throws Exception {
143         final FileObject[] foArray = BaseFolder.findFiles(Selectors.SELECT_FILES);
144         Assert.assertTrue(foArray.length > 0);
145         // gather file extensions.
146         final Set<String> extensionSet = new HashSet<>();
147         for (final FileObject fo : foArray) {
148             extensionSet.add(fo.getName().getExtension());
149         }
150         final String message = String.format("Extensions: %s; files: %s", extensionSet.toString(),
151                 Arrays.asList(foArray).toString());
152         Assert.assertEquals(message, ExtensionCount, extensionSet.size());
153         // check each extension
154         for (final String extension : extensionSet) {
155             final FileSelector selector = new FileExtensionSelector(extension);
156             final FileObject[] list = BaseFolder.findFiles(selector);
157             Assert.assertEquals(FilesPerExtensionCount, list.length);
158         }
159         // check each file against itself
160         for (final FileObject fo : foArray) {
161             final FileSelector selector = new FileExtensionSelector(fo.getName().getExtension());
162             final FileObject[] list = BaseFolder.findFiles(selector);
163             Assert.assertEquals(FilesPerExtensionCount, list.length);
164         }
165     }
166 
167 }