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.generation;
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.File;
31  import java.io.FileReader;
32  
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  
36  public class JavaDocLicenseNotRequiredTest {
37  
38      private MockClaimReporter reporter;
39      private JavaDocLicenseNotRequired license;
40  
41      @Before
42      public void setUp() throws Exception {
43          license = new JavaDocLicenseNotRequired();
44          reporter = new MockClaimReporter();
45      }
46  
47      @Test
48      public void matchIndexDoc() throws Exception {
49          boolean result = readAndMatch("index.html");
50          assertTrue("Is a javadoc", result);
51      }
52  
53      @Test
54      public void matchClassDoc() throws Exception {
55          boolean result = readAndMatch("ArchiveElement.html");
56          assertTrue("Is a javadoc", result);
57      }
58  
59      @Test
60      public void matchNonJavaDoc() throws Exception {
61          boolean result = readAndMatch("notjavadoc.html");
62          assertFalse("Not javadocs and so should return null", result);
63      }
64  
65      boolean readAndMatch(String name) throws Exception {
66          File file = Resources.getResourceFile("javadocs/" + name);
67          boolean result = false;
68          BufferedReader in = null;
69          try {
70              in = new BufferedReader(new FileReader(file));
71              String line = in.readLine();
72              final Document subject = new MockLocation("subject");
73              while (line != null && !result) {
74                  result = license.match(subject, line);
75                  line = in.readLine();
76              }
77          } finally {
78                  IOUtils.closeQuietly(in);
79          }
80              return result;
81          }
82      }