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.commons.io.IOUtils;
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.apache.rat.test.utils.Resources;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import java.io.BufferedReader;
30  import java.io.StringReader;
31  
32  import static org.junit.Assert.assertFalse;
33  import static org.junit.Assert.assertTrue;
34  
35  public class AppliedApacheSoftwareLicense20Test {
36  
37      private static final String HEADER
38              = "/*\n"
39              + " *  Copyright 2012-2013 FooBar.\n"
40              + " *\n"
41              + " *  Licensed under the Apache License, Version 2.0 (the \"License\");\n"
42              + " *  you may not use this file except in compliance with the License.\n"
43              + " *\n"
44              + " *  You may obtain a copy of the License at\n"
45              + " *       http://www.apache.org/licenses/LICENSE-2.0\n"
46              + " *\n"
47              + " *  Unless required by applicable law or agreed to in writing, software\n"
48              + " *  distributed under the License is distributed on an \"AS IS\" BASIS,\n"
49              + " *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
50              + " *  See the License for the specific language governing permissions and\n"
51              + " *  limitations under the License.\n"
52              + " */\n";
53  
54      private AppliedApacheSoftwareLicense20 license;
55  
56      private MockClaimReporter reporter;
57  
58      @Before
59      public void setUp() throws Exception {
60          license = new AppliedApacheSoftwareLicense20("FooBar");
61          reporter = new MockClaimReporter();
62      }
63  
64      @Test
65      public void match() throws Exception {
66          BufferedReader in = new BufferedReader(new StringReader(HEADER));
67          String line = in.readLine();
68          boolean result = false;
69          final Document subject = new MockLocation("subject");
70          while (line != null) {
71              result = license.match(subject, line);
72              line = in.readLine();
73          }
74          assertTrue("Applied AL2.0 license should be matched", result);
75          license.reset();
76          result = license.match(subject, "New line");
77          assertFalse("After reset, content should build up again", result);
78      }
79  
80      @Test
81      public void noMatch() throws Exception {
82          BufferedReader in = null;
83          try {
84              in = Resources.getBufferedResourceReader("elements/Source.java");
85              String line = in.readLine();
86              boolean result = false;
87              final Document subject = new MockLocation("subject");
88              while (line != null) {
89                  result = license.match(subject, line);
90                  line = in.readLine();
91              }
92              assertFalse("Applied AL2.0 license should not be matched", result);
93              license.reset();
94          } finally {
95              IOUtils.closeQuietly(in);
96          }
97      }
98  
99      @Test(timeout = 2000) // may need to be adjusted if many more files are added
100     public void goodFiles() throws Exception {
101         DirectoryScanner.testFilesInDir("appliedAL20/good", license, true);
102     }
103 
104     @Test(timeout = 2000) // may need to be adjusted if many more files are added
105     public void baddFiles() throws Exception {
106         DirectoryScanner.testFilesInDir("appliedAL20/bad", license, false);
107     }
108 
109 }