View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat.walker;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.File;
24  import java.io.FileWriter;
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.commons.io.filefilter.FalseFileFilter;
30  import org.apache.rat.api.Document;
31  import org.apache.rat.api.RatException;
32  import org.apache.rat.report.RatReport;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.io.TempDir;
35  
36  public class DirectoryWalkerTest {
37      
38      @TempDir
39  	private File toWalk;
40      
41      @Test
42      public void walk() throws IOException, RatException {
43          File regular = new File(toWalk, "regular");
44          regular.mkdir();
45          File regularFile = new File(regular, "test");
46          try (FileWriter writer = new FileWriter(regularFile)) {
47              writer.write("test");
48              writer.flush();
49          }
50  
51          File hidden = new File(toWalk, ".hidden");
52          hidden.mkdir();
53          File hiddenFile = new File(hidden, "test");
54  
55          try (FileWriter writer = new FileWriter(hiddenFile)) {
56              writer.write("test");
57              writer.flush();
58          }
59  
60          DirectoryWalker walker = new DirectoryWalker(toWalk, NameBasedHiddenFileFilter.HIDDEN);
61          List<String> scanned = new ArrayList<>();
62          walker.run(new TestRatReport(scanned));
63  
64          assertEquals(1, scanned.size());
65  
66          walker = new DirectoryWalker(toWalk, FalseFileFilter.FALSE);
67          scanned = new ArrayList<>();
68          walker.run(new TestRatReport(scanned));
69  
70          assertEquals(2, scanned.size());
71      }
72  
73      class TestRatReport implements RatReport {
74  
75          private List<String> scanned;
76  
77          public TestRatReport(List<String> scanned) {
78              this.scanned = scanned;
79          }
80  
81          @Override
82          public void startReport() {
83              // no-op
84          }
85  
86          @Override
87          public void report(Document document) throws RatException {
88              scanned.add(document.getName());
89          }
90  
91          @Override
92          public void endReport() {
93              // no-op
94          }
95  
96      }
97  
98  }