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 java.io.File;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import java.util.stream.Collectors;
28  import org.apache.rat.ReportConfiguration;
29  import org.apache.rat.api.Document;
30  import org.apache.rat.api.RatException;
31  import org.apache.rat.config.exclusion.StandardCollection;
32  import org.apache.rat.document.FileDocument;
33  import org.apache.rat.document.DocumentName;
34  import org.apache.rat.report.RatReport;
35  import org.junit.jupiter.api.BeforeAll;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  import org.junit.jupiter.api.io.TempDir;
39  
40  import static org.assertj.core.api.Assertions.assertThat;
41  
42  public class DirectoryWalkerTest {
43  
44      private ReportConfiguration reportConfiguration;
45  
46      @TempDir
47      private static File tempDir;
48  
49      private static void fileWriter(File dir, String name, String contents) throws IOException {
50          try (FileWriter writer = new FileWriter(new File(dir, name))) {
51              writer.write(contents);
52              writer.flush();
53          }
54      }
55  
56      @BeforeEach
57      public void beforeEach() {
58          reportConfiguration = new ReportConfiguration();
59      }
60  
61      public Document toWalk() {
62          DocumentName documentName = DocumentName.builder(tempDir).build();
63          return new FileDocument(documentName, tempDir, reportConfiguration.getDocumentExcluder(documentName));
64      }
65  
66      @BeforeAll
67      public static void setUp() throws Exception {
68  
69          /*
70          Create a directory structure like this:
71  
72              regular
73                  regularFile
74                  .hiddenFile
75              .hidden
76                  regularFile
77                  .hiddenFile
78           */
79          File regular = new File(tempDir, "regular");
80          regular.mkdir();
81          fileWriter(regular, "regularFile", "regular file");
82          fileWriter(regular, ".hiddenFile", "hidden file");
83  
84          File hidden = new File(tempDir, ".hidden");
85          hidden.mkdir();
86          fileWriter(hidden, "regularFile", "regular file");
87          fileWriter(hidden, ".hiddenFile", "hidden file");
88      }
89      
90      @Test
91      public void noFiltersTest() throws RatException {
92          DirectoryWalker walker = new DirectoryWalker(toWalk());
93          List<Document> scanned = new ArrayList<>();
94          walker.run(new TestRatReport(scanned));
95          String[] expected = {"/regular/regularFile", "/regular/.hiddenFile", "/.hidden/regularFile", "/.hidden/.hiddenFile"};
96          List<String> actual = scanned.stream().filter(d -> !d.isIgnored()).map(d -> d.getName().localized("/")).collect(Collectors.toList());
97          assertThat(actual).size().isEqualTo(4);
98          for (String ex : expected) {
99              assertThat(actual).as(()-> String.format("Missing %s", ex)).contains(ex);
100         }
101     }
102 
103     @Test
104     public void noHiddenFileFiltersTest() throws RatException {
105         reportConfiguration.addExcludedCollection(StandardCollection.HIDDEN_FILE);
106         DirectoryWalker walker = new DirectoryWalker(toWalk());
107         List<Document> scanned = new ArrayList<>();
108         walker.run(new TestRatReport(scanned));
109         List<String> actual = scanned.stream().filter(d -> !d.isIgnored()).map(d -> d.getName().localized("/")).collect(Collectors.toList());
110         String[] expected = {"/regular/regularFile", "/.hidden/regularFile"};
111         assertThat(actual.size()).isEqualTo(2);
112         for (String ex : expected) {
113             assertThat(actual).as(()-> String.format("Missing %s", ex)).contains(ex);
114         }
115 
116         actual = scanned.stream().filter(Document::isIgnored).map(d -> d.getName().localized("/")).collect(Collectors.toList());
117         expected = new String[] {"/regular/.hiddenFile", "/.hidden/.hiddenFile"};
118         assertThat(actual.size()).isEqualTo(2);
119         for (String ex : expected) {
120             assertThat(actual).as(()-> String.format("Missing ignored %s", ex)).contains(ex);
121         }
122     }
123 
124     @Test
125     public void noHiddenDirectoryFiltersTest() throws RatException {
126         reportConfiguration.addExcludedCollection(StandardCollection.HIDDEN_DIR);
127         DirectoryWalker walker = new DirectoryWalker(toWalk());
128         List<Document> scanned = new ArrayList<>();
129         walker.run(new TestRatReport(scanned));
130         List<String> actual = scanned.stream().filter(d -> !d.isIgnored()).map(d -> d.getName().localized("/")).collect(Collectors.toList());
131         String[] expected = {"/regular/regularFile", "/regular/.hiddenFile"};
132         assertThat(actual.size()).isEqualTo(2);
133         for (String ex : expected) {
134             assertThat(actual).as(()-> String.format("Missing %s", ex)).contains(ex);
135         }
136 
137         List<Document> excluded = scanned.stream().filter(Document::isIgnored).collect(Collectors.toList());
138         assertThat(excluded.size()).isEqualTo(1);
139         Document d = excluded.get(0);
140         assertThat(d.getName().localized("/")).isEqualTo("/.hidden");
141         assertThat(d.isIgnored()).isTrue();
142     }
143 
144     @Test
145     public void noHiddenDirectoryAndNoHiddenFileFiltersTest() throws RatException {
146         reportConfiguration.addExcludedCollection(StandardCollection.HIDDEN_DIR);
147         reportConfiguration.addExcludedCollection(StandardCollection.HIDDEN_FILE);
148         DirectoryWalker walker = new DirectoryWalker(toWalk());
149         List<Document> scanned = new ArrayList<>();
150         walker.run(new TestRatReport(scanned));
151         List<String> actual = scanned.stream().filter(d -> !d.isIgnored()).map(d -> d.getName().localized("/")).collect(Collectors.toList());
152         String[] expected = {"/regular/regularFile"};
153         assertThat(actual.size()).isEqualTo(1);
154         for (String ex : expected) {
155             assertThat(actual).as(()-> String.format("Missing %s", ex)).contains(ex);
156         }
157     }
158 
159     static class TestRatReport implements RatReport {
160 
161         private final List<Document> scanned;
162 
163         public TestRatReport(List<Document> scanned) {
164             this.scanned = scanned;
165         }
166 
167         @Override
168         public void startReport() {
169             // no-op
170         }
171 
172         @Override
173         public void report(Document document) {
174             scanned.add(document);
175         }
176 
177         @Override
178         public void endReport() {
179             // no-op
180         }
181     }
182 }