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.junit.Before;
25  import org.junit.Test;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertFalse;
32  import static org.junit.Assert.assertTrue;
33  
34  public class MITLicenseTest {
35      private Document subject;
36  
37      /**
38       * To ease testing provide a map with a given license version and the string to test for.
39       */
40      private static Map<IHeaderMatcher, String> licenseStringMap;
41  
42      /**
43       * If you replace this with BeforeClass and make this method static the build fails at line 71.
44       */
45      @Before
46      public void initLicensesUnderTest() {
47          licenseStringMap = new HashMap<IHeaderMatcher, String>();
48          licenseStringMap.put(new MITLicense(),
49                  MITLicense.FIRST_LICENSE_LINE
50                          + "\n" + MITLicense.MIDDLE_LICENSE_LINE
51                          + "\r\n * " + MITLicense.AS_IS_LICENSE_LINE);
52          assertEquals(1, licenseStringMap.entrySet().size());
53      }
54  
55  
56      @Before
57      public final void initReporter() {
58          this.subject = new MockLocation("subject");
59      }
60  
61      @Test
62      public void testNegativeMatches() throws Exception {
63          for (Map.Entry<IHeaderMatcher, String> licenseUnderTest : licenseStringMap.entrySet()) {
64              assertFalse(licenseUnderTest.getKey().match(subject, "'Behold, Telemachus! (nor fear the sight,)"));
65          }
66      }
67  
68      @Test
69      public void testPositiveMatchInDocument() throws Exception {
70          for (Map.Entry<IHeaderMatcher, String> licenseUnderTest : licenseStringMap.entrySet()) {
71              assertTrue(licenseUnderTest.getKey().match(subject, "\t" + licenseUnderTest.getValue()));
72              assertTrue(licenseUnderTest.getKey().match(subject, "     " + licenseUnderTest.getValue()));
73              assertTrue(licenseUnderTest.getKey().match(subject, licenseUnderTest.getValue()));
74              assertTrue(licenseUnderTest.getKey().match(subject, " * " + 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          }
82      }
83  
84  }