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.BeforeClass;
26  import org.junit.Test;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertFalse;
33  import static org.junit.Assert.assertTrue;
34  
35  public class CDDL1LicenseTest {
36  
37      private static final String LICENSE_LINE =
38              " DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.\n\n"
39                      + "Copyright 2011-2013 Tirasa. All rights reserved.\n\n"
40                      + "The contents of this file are subject to the terms of the Common Development\n"
41                      + "and Distribution License(\"CDDL\") (the \"License\"). You may not use this file\n"
42                      + "except in compliance with the License.\n\n"
43                      + "You can obtain a copy of the License at https://oss.oracle.com/licenses/CDDL\n"
44                      + "See the License for the specific language governing permissions and limitations\n"
45                      + "under the License.";
46  
47      /**
48       * To ease testing provide a map with a given license version and the string to test for.
49       */
50      private static Map<IHeaderMatcher, String> licenseStringMap;
51  
52      private Document subject;
53  
54      @BeforeClass
55      public static void initLicensesUnderTest() {
56          licenseStringMap = new HashMap<IHeaderMatcher, String>();
57          licenseStringMap.put(new CDDL1License(), LICENSE_LINE);
58          assertEquals(1, licenseStringMap.entrySet().size());
59      }
60  
61      @Before
62      public final void initSubject() {
63          this.subject = new MockLocation("subject");
64      }
65  
66      @Test
67      public void testNegativeMatches() throws Exception {
68          for (Map.Entry<IHeaderMatcher, String> licenseUnderTest : licenseStringMap.entrySet()) {
69              assertFalse(licenseUnderTest.getKey().match(subject, "'Behold, Telemachus! (nor fear the sight,)"));
70          }
71      }
72  
73      @Test
74      public void testPositiveMatchInDocument() throws Exception {
75          for (Map.Entry<IHeaderMatcher, String> licenseUnderTest : licenseStringMap.entrySet()) {
76              assertTrue(licenseUnderTest.getKey().match(subject, "\t" + 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              assertTrue(licenseUnderTest.getKey().match(subject, " ## " + licenseUnderTest.getValue()));
85              assertTrue(licenseUnderTest.getKey().match(subject, " ## " + licenseUnderTest.getValue() + " ##"));
86          }
87      }
88  }