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.filter;
18  
19  import java.io.File;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.vfs2.FileSystemException;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  /**
28   * Test for {@link SuffixFileFilter}.
29   */
30  // CHECKSTYLE:OFF Test code
31  public class SuffixFileFilterTest extends BaseFilterTest {
32  
33      @Test
34      public void testAcceptList() throws FileSystemException {
35  
36          // PREPARE
37          final List<String> list = new ArrayList<>();
38          list.add(".txt");
39          list.add(".bin");
40          final SuffixFileFilter filter = new SuffixFileFilter(list);
41  
42          // TEST
43          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test1.txt"))));
44          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test2.bin"))));
45          Assert.assertFalse(filter.accept(createFileSelectInfo(new File("test2.BIN"))));
46          Assert.assertFalse(filter.accept(createFileSelectInfo(new File("test.xxx"))));
47  
48      }
49  
50      @Test
51      public void testAcceptListIOCaseInsensitive() throws FileSystemException {
52  
53          // PREPARE
54          final List<String> list = new ArrayList<>();
55          list.add(".txt");
56          list.add(".bin");
57          final SuffixFileFilter filter = new SuffixFileFilter(IOCase.INSENSITIVE, list);
58  
59          // TEST
60          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("TEST1.txt"))));
61          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test2.bin"))));
62          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test2.TXT"))));
63          Assert.assertFalse(filter.accept(createFileSelectInfo(new File("test.xxx"))));
64  
65      }
66  
67      @Test
68      public void testAcceptListIOCaseSensitive() throws FileSystemException {
69  
70          // PREPARE
71          final List<String> list = new ArrayList<>();
72          list.add(".txt");
73          list.add(".bin");
74          final SuffixFileFilter filter = new SuffixFileFilter(IOCase.SENSITIVE, list);
75  
76          // TEST
77          Assert.assertFalse(filter.accept(createFileSelectInfo(new File("test1.Txt"))));
78          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test2.txt"))));
79          Assert.assertFalse(filter.accept(createFileSelectInfo(new File("test2.BIN"))));
80          Assert.assertFalse(filter.accept(createFileSelectInfo(new File("test.xxx"))));
81  
82      }
83  
84      @Test
85      public void testAcceptString() throws FileSystemException {
86  
87          // PREPARE
88          final SuffixFileFilter filter = new SuffixFileFilter(".txt", ".xxx");
89  
90          // TEST
91          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test1.txt"))));
92          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test2.txt"))));
93          Assert.assertFalse(filter.accept(createFileSelectInfo(new File("test2.TXT"))));
94          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test.xxx"))));
95  
96      }
97  
98      @Test
99      public void testAcceptStringIOCaseInsensitive() throws FileSystemException {
100 
101         // PREPARE
102         final SuffixFileFilter filter = new SuffixFileFilter(IOCase.INSENSITIVE, ".txt", ".xxx");
103 
104         // TEST
105         Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test1.txt"))));
106         Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test2.txt"))));
107         Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test2.TXT"))));
108         Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test.xxx"))));
109 
110     }
111 
112     @Test
113     public void testAcceptStringIOCaseSensitive() throws FileSystemException {
114 
115         // PREPARE
116         final SuffixFileFilter filter = new SuffixFileFilter(IOCase.SENSITIVE, ".txt", ".xxx");
117 
118         // TEST
119         Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test1.txt"))));
120         Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test2.txt"))));
121         Assert.assertFalse(filter.accept(createFileSelectInfo(new File("test2.TXT"))));
122         Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test.xxx"))));
123 
124     }
125 
126 }
127 // CHECKSTYLE:ON