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  
20  package org.apache.rat.analysis;
21  
22  import java.io.StringReader;
23  
24  import java.util.Collections;
25  import java.util.stream.Collectors;
26  import org.apache.rat.Defaults;
27  import org.apache.rat.api.Document;
28  import org.apache.rat.configuration.builders.AnyBuilder;
29  import org.apache.rat.license.ILicenseFamily;
30  import org.apache.rat.testhelpers.TestingDocument;
31  import org.apache.rat.license.ILicense;
32  import org.apache.rat.testhelpers.TestingLicense;
33  import org.apache.rat.testhelpers.TestingMatcher;
34  import org.assertj.core.util.Lists;
35  import org.junit.jupiter.api.Test;
36  
37  import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
38  
39  public class HeaderCheckWorkerTest {
40  
41      @Test
42      public void emptyInputIsUnknownTest() throws RatHeaderAnalysisException {
43          final Document subject = new TestingDocument("subject");
44          subject.getMetaData().setApprovalPredicate(Defaults.builder().build().getLicenseSetFactory().getApprovedLicensePredicate());
45          ILicense matcher = new TestingLicense("test", "test");
46          HeaderCheckWorker worker = new HeaderCheckWorker(new TestingMatcher(), new StringReader(""), Lists.list(matcher), subject);
47          worker.read();
48          assertThat(subject.getMetaData().unapprovedLicenses().count()).isEqualTo(1);
49          assertThat(subject.getMetaData().unapprovedLicenses().collect(Collectors.toList()).get(0).getLicenseFamily()).isEqualTo(ILicenseFamily.UNKNOWN);
50      }
51  
52      @Test
53      public void generatedFileDetectionTest() throws Exception {
54          final Document subject = new TestingDocument(new StringReader("Generated from configure.ac by autoheader"), "subject");
55          IHeaderMatcher matcher = new AnyBuilder().setResource("/org/apache/rat/generation-keywords.txt").build();
56          HeaderCheckWorker worker = new HeaderCheckWorker(matcher, subject.reader(), Collections.emptyList(), subject);
57          worker.read();
58          assertThat(subject.getMetaData().getDocumentType()).isEqualTo(Document.Type.IGNORED);
59      }
60  }