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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Arrays;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.junit.jupiter.api.AfterAll;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests FileExtensionSelector.
33   */
34  public class PatternFileSelectorTest {
35  
36      private static FileObject baseFolder;
37  
38      /**
39       * 9 files and 1 directory = 10
40       */
41      private static final int ENTRY_COUNT = 10;
42  
43      private static final int EXTENSION_COUNT = 3;
44  
45      private static final int FILES_PER_EXTENSION_COUNT = 3;
46  
47      static FileObject getBaseFolder() {
48          return baseFolder;
49      }
50  
51      /**
52       * Creates a RAM FS.
53       *
54       * @throws Exception
55       */
56      @BeforeAll
57      public static void setUpClass() throws Exception {
58          baseFolder = VFS.getManager().resolveFile("ram://" + PatternFileSelectorTest.class.getName());
59          baseFolder.deleteAll();
60          baseFolder.createFolder();
61          baseFolder.resolveFile("aa.htm").createFile();
62          baseFolder.resolveFile("aa.html").createFile();
63          baseFolder.resolveFile("aa.xhtml").createFile();
64          baseFolder.resolveFile("b.htm").createFile();
65          baseFolder.resolveFile("b.html").createFile();
66          baseFolder.resolveFile("b.xhtml").createFile();
67          baseFolder.resolveFile("c.htm").createFile();
68          baseFolder.resolveFile("c.html").createFile();
69          baseFolder.resolveFile("c.xhtml").createFile();
70      }
71  
72      /**
73       * Deletes RAM FS files.
74       *
75       * @throws Exception
76       */
77      @AfterAll
78      public static void tearDownClass() throws Exception {
79          if (baseFolder != null) {
80              baseFolder.deleteAll();
81          }
82      }
83  
84      /**
85       * Tests a one extension selector.
86       *
87       * @throws Exception
88       */
89      @Test
90      public void testFileExtensions() throws Exception {
91          final FileObject[] foArray = baseFolder.findFiles(Selectors.SELECT_FILES);
92          assertTrue(foArray.length > 0);
93          final String regExPrefix = ".*\\.";
94          // gather file extensions.
95          final Set<String> extensionSet = new HashSet<>();
96          for (final FileObject fo : foArray) {
97              extensionSet.add(regExPrefix + fo.getName().getExtension());
98          }
99          final String message = String.format("Extensions: %s; files: %s", extensionSet.toString(),
100                 Arrays.asList(foArray).toString());
101         assertEquals(EXTENSION_COUNT, extensionSet.size(), message);
102         // check each extension
103         for (final String extension : extensionSet) {
104             final FileSelector selector = new PatternFileSelector(extension);
105             final FileObject[] list = baseFolder.findFiles(selector);
106             assertEquals(FILES_PER_EXTENSION_COUNT, list.length);
107         }
108         // check each file against itself
109         for (final FileObject fo : foArray) {
110             final FileSelector selector = new PatternFileSelector(regExPrefix + fo.getName().getExtension());
111             final FileObject[] list = baseFolder.findFiles(selector);
112             assertEquals(FILES_PER_EXTENSION_COUNT, list.length);
113         }
114     }
115 
116     /**
117      * Tests matching all.
118      *
119      * @throws Exception
120      */
121     @Test
122     public void testMatchAll() throws Exception {
123         final FileObject[] list = baseFolder.findFiles(new PatternFileSelector(".*"));
124         assertEquals(ENTRY_COUNT, list.length);
125     }
126 
127     /**
128      * Tests matching partial file names.
129      *
130      * @throws Exception
131      */
132     @Test
133     public void testMatchPartial() throws Exception {
134         final FileObject[] list = baseFolder.findFiles(new PatternFileSelector(".*a.htm"));
135         assertEquals(1, list.length);
136         assertEquals("aa.htm", list[0].getName().getBaseName());
137     }
138 
139     /**
140      * Tests matching partial file names with delimiter.
141      *
142      * @throws Exception
143      */
144     @Test
145     public void testMatchPartialDelimited() throws Exception {
146         final FileObject[] list = baseFolder.findFiles(new PatternFileSelector("^.*\\/b.htm$"));
147         assertEquals(1, list.length);
148         assertEquals("b.htm", list[0].getName().getBaseName());
149     }
150 
151     /**
152      * Tests a null selector.
153      */
154     @Test
155     public void testNullString() {
156         // Yep, this will blow up.
157         assertThrows(NullPointerException.class, () -> new PatternFileSelector((String) null));
158     }
159 
160 }