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 org.apache.rat.analysis.IHeaderMatcher;
22  import org.apache.rat.api.Document;
23  import org.apache.rat.document.MockLocation;
24  import org.apache.rat.report.claim.impl.xml.MockClaimReporter;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import static org.junit.Assert.assertFalse;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   * Tests GPL license occurrences within comments and other characters.
36   * Works for GPL1 to GPL3.
37   */
38  public class GPL123LicenseTest {
39      MockClaimReporter reporter;
40      Document subject;
41  
42      /**
43       * To ease testing provide a map with a given license version and the string to test for.
44       */
45      private static Map<IHeaderMatcher, String> licenseStringMap;
46  
47      /**
48       * If you replace this with BeforeClass and make this method static the build fails at line 67.
49       */
50      @Before
51      public void initLicensesUnderTest() {
52          licenseStringMap = new HashMap<IHeaderMatcher, String>();
53          licenseStringMap.put(new GPL1License(), GPL1License.FIRST_LICENSE_LINE);
54          licenseStringMap.put(new GPL2License(), GPL2License.FIRST_LICENSE_LINE);
55          licenseStringMap.put(new GPL3License(), GPL3License.FIRST_LICENSE_LINE);
56      }
57  
58      @Before
59      public final void initReporter() {
60          this.reporter = new MockClaimReporter();
61          this.subject = new MockLocation("subject");
62      }
63  
64      @Test
65      public void testNegativeMatches() throws Exception {
66          for (Map.Entry<IHeaderMatcher, String> licenseUnderTest : licenseStringMap.entrySet()) {
67              assertFalse(licenseUnderTest.getKey().match(subject, "'Behold, Telemachus! (nor fear the sight,)"));
68          }
69      }
70  
71      @Test
72      public void testPositiveMatchInDocument() throws Exception {
73          for (Map.Entry<IHeaderMatcher, String> licenseUnderTest : licenseStringMap.entrySet()) {
74              assertTrue(licenseUnderTest.getKey().match(subject, "\t" + licenseUnderTest.getValue()));
75              assertTrue(licenseUnderTest.getKey().match(subject, "     " + licenseUnderTest.getValue()));
76              assertTrue(licenseUnderTest.getKey().match(subject, licenseUnderTest.getValue()));
77              assertTrue(licenseUnderTest.getKey().match(subject, " * " + licenseUnderTest.getValue()));
78              assertTrue(licenseUnderTest.getKey().match(subject, " // " + licenseUnderTest.getValue()));
79              assertTrue(licenseUnderTest.getKey().match(subject, " /* " + licenseUnderTest.getValue()));
80              assertTrue(licenseUnderTest.getKey().match(subject, " /** " + licenseUnderTest.getValue()));
81              assertTrue(licenseUnderTest.getKey().match(subject, "    " + licenseUnderTest.getValue()));
82              assertTrue(licenseUnderTest.getKey().match(subject, " ## " + licenseUnderTest.getValue()));
83              assertTrue(licenseUnderTest.getKey().match(subject, " ## " + licenseUnderTest.getValue() + " ##"));
84          }
85      }
86  
87  }