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  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  import static org.junit.jupiter.api.Assertions.fail;
27  
28  import java.io.BufferedReader;
29  import java.io.IOException;
30  import java.io.StringReader;
31  
32  import org.apache.commons.lang3.StringUtils;
33  import org.apache.rat.Defaults;
34  import org.apache.rat.analysis.IHeaderMatcher.State;
35  import org.apache.rat.analysis.matchers.FullTextMatcher;
36  import org.apache.rat.api.MetaData;
37  import org.apache.rat.license.ILicense;
38  import org.apache.rat.license.ILicenseFamily;
39  import org.apache.rat.license.LicenseSetFactory;
40  import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
41  import org.apache.rat.testhelpers.TestingLicense;
42  import org.junit.jupiter.api.BeforeEach;
43  import org.junit.jupiter.params.ParameterizedTest;
44  import org.junit.jupiter.params.provider.MethodSource;
45  
46  
47  /**
48   * Test to see if short form license information will be recognized correctly.
49   *
50   */
51  abstract public class AbstractLicenseTest {
52      private static int NAME = 0;
53      private static int TEXT = 1;
54  
55      private Defaults defaults;
56      protected MetaData data;
57  
58      
59      protected AbstractLicenseTest() {
60      }
61      
62  
63      @BeforeEach
64      public void setup() {
65          data = new MetaData();
66          defaults = Defaults.builder().build();
67      }
68  
69      protected ILicense extractCategory(String id) {
70          TestingLicense testingLicense = new TestingLicense();
71          testingLicense.setId(id);
72          ILicense result = LicenseSetFactory.search(testingLicense, defaults.getLicenses(LicenseFilter.all));
73          if (result == null) {
74              fail("No licenses for id: " + id);
75          }
76          return result;
77      }
78  
79      @ParameterizedTest
80      @MethodSource("parameterProvider")
81      public void testMatchProcessing(String id, String familyPattern, String name, String notes, String[][] targets) throws IOException {
82          ILicense license = extractCategory(id);
83          String family = ILicenseFamily.makeCategory(familyPattern);
84              try {
85                  for (String[] target : targets) {
86                      if (processText(license, target[TEXT])) {
87                          data.reportOnLicense(license);
88                          assertNotNull(data.get(MetaData.RAT_URL_HEADER_CATEGORY),"No URL HEADER CATEGORY");
89                          assertEquals(family,
90                                  data.get(MetaData.RAT_URL_HEADER_CATEGORY).getValue(), license.toString());
91                          assertNotNull(data.get(MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY), "No URL LICENSE FAMILY CATEGORY");
92                          assertEquals(family,
93                                  data.get(MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY).getValue(), license.toString());
94                          if (StringUtils.isNotBlank(notes)) {
95                              assertNotNull(data.get(MetaData.RAT_URL_HEADER_SAMPLE), "No URL HEADER SAMPLE");
96                              assertEquals(FullTextMatcher.prune(notes),
97                                      FullTextMatcher.prune(data.get(MetaData.RAT_URL_HEADER_SAMPLE).getValue()), license.toString());
98                          } else {
99                              assertNull(data.get(MetaData.RAT_URL_HEADER_SAMPLE), "URL HEADER SAMPLE was not null");
100                         }
101                         assertNotNull(data.get(MetaData.RAT_URL_LICENSE_FAMILY_NAME), "No URL LICENSE FAMILY NAME");
102                         assertEquals(name,
103                                 data.get(MetaData.RAT_URL_LICENSE_FAMILY_NAME).getValue(), license.toString());
104                         data.clear();
105                     } else {
106                         fail(license + " was not matched by " + target[NAME]);
107                     }
108                     license.reset();
109                 }
110             } finally {
111                 license.reset();
112             }
113 
114     }
115 
116     private boolean processText(ILicense license, String text) throws IOException {
117         try (BufferedReader in = new BufferedReader(new StringReader(text))) {
118             String line;
119             while (null != (line = in.readLine())) {
120                 if (license.matches(line) == State.t) {
121                     return true;
122                 }
123             }
124             return license.finalizeState().asBoolean();
125         }
126     }
127 
128     @ParameterizedTest
129     @MethodSource("parameterProvider")
130     public void testEmbeddedStrings(String id, String family, String name, String notes, String[][] targets) throws IOException {
131         String formats[] = { "%s", "now is not the time %s for copyright", "#%s", "##%s", "## %s", "##%s##", "## %s ##",
132                 "/*%s*/", "/* %s */" };
133 
134         ILicense license = extractCategory(id);
135             try {
136                 for (String[] target : targets) {
137                     for (String fmt : formats) {
138                         boolean found = processText(license, String.format(fmt, target[TEXT]));
139                         license.reset();
140                         assertTrue(found, ()->String.format("%s %s did not match pattern '%s' for target string %s", id,
141                                 name, fmt, target[NAME]));
142                     }
143                 }
144             } finally {
145                 license.reset();
146             }
147     }
148 }