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.analysis.license;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  import static org.junit.jupiter.api.Assertions.fail;
24  
25  import java.io.BufferedReader;
26  import java.io.IOException;
27  import java.io.StringReader;
28  import java.util.Optional;
29  
30  import org.apache.rat.Defaults;
31  import org.apache.rat.analysis.HeaderCheckWorker;
32  import org.apache.rat.analysis.IHeaders;
33  import org.apache.rat.api.MetaData;
34  import org.apache.rat.license.ILicense;
35  import org.apache.rat.license.LicenseSetFactory;
36  import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.params.ParameterizedTest;
39  import org.junit.jupiter.params.provider.MethodSource;
40  
41  /**
42   * Test to see if short form license information will be recognized correctly.
43   *
44   */
45  abstract public class AbstractLicenseTest {
46      private static final int NAME = 0;
47      private static final int TEXT = 1;
48  
49      private Defaults defaults;
50      protected MetaData data;
51  
52      protected AbstractLicenseTest() {
53      }
54  
55      @BeforeEach
56      public void setup() {
57          data = new MetaData();
58          defaults = Defaults.builder().build();
59      }
60  
61      protected ILicense extractCategory(String famId, String id) {
62          Optional<ILicense> result = LicenseSetFactory.search(famId, id, defaults.getLicenseSetFactory().getLicenses(LicenseFilter.ALL));
63          if (!result.isPresent()) {
64              fail(String.format("No licenses for id: f:%s l:%s", famId, id));
65          }
66          return result.get();
67      }
68  
69      @ParameterizedTest
70      @MethodSource("parameterProvider")
71      public void testMatchProcessing(String id, String familyPattern, String name, String notes, String[][] targets)
72              throws IOException {
73          ILicense license = extractCategory(familyPattern, id);
74          try {
75              for (String[] target : targets) {
76                  if (processText(license, target[TEXT])) {
77                      data.reportOnLicense(license);
78                      assertEquals(1, data.licenses().count());
79                      assertEquals(license, data.licenses().findFirst().get());
80                  } else {
81                      fail(license + " was not matched by " + target[NAME]);
82                  }
83                  license.reset();
84              }
85          } finally {
86              license.reset();
87          }
88      }
89  
90      private boolean processText(ILicense license, String text) throws IOException {
91          try (BufferedReader in = new BufferedReader(new StringReader(text))) {
92              IHeaders headers = HeaderCheckWorker.readHeader(in,
93                      HeaderCheckWorker.DEFAULT_NUMBER_OF_RETAINED_HEADER_LINES);
94              return license.matches(headers);
95          }
96      }
97  
98      @ParameterizedTest
99      @MethodSource("parameterProvider")
100     public void testEmbeddedStrings(String id, String family, String name, String notes, String[][] targets)
101             throws IOException {
102         String[] formats = { "%s", "now is not the time %s for copyright", "#%s", "##%s", "## %s", "##%s##", "## %s ##",
103                 "/*%s*/", "/* %s */" };
104 
105         ILicense license = extractCategory(family, id);
106         try {
107             for (String[] target : targets) {
108                 for (String fmt : formats) {
109                     boolean found = processText(license, String.format(fmt, target[TEXT]));
110                     license.reset();
111                     assertTrue(found, () -> String.format("%s %s did not match pattern '%s' for target string %s", id,
112                             name, fmt, target[NAME]));
113                 }
114             }
115         } finally {
116             license.reset();
117         }
118     }
119 
120 }