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;
20  
21  import org.apache.rat.analysis.util.HeaderMatcherMultiplexer;
22  import org.apache.rat.test.utils.Resources;
23  import org.apache.rat.walker.DirectoryWalker;
24  import org.junit.Test;
25  
26  import java.io.File;
27  import java.io.FilenameFilter;
28  import java.io.IOException;
29  import java.io.StringWriter;
30  import java.util.Arrays;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertNotNull;
34  import static org.junit.Assert.assertTrue;
35  
36  public class ReportTest {
37      private static final String NL = System.getProperty("line.separator");
38      private static final String PARAGRAPH = "*****************************************************";
39      private static final String FILE_PARAGRAPH = "=====================================================";
40  
41      private static final String HEADER =
42              NL +
43                      PARAGRAPH + NL +//
44                      "Summary" + NL +//
45                      "-------" + NL +//
46                      "Generated at: ";
47  
48      private static String getElementsReports(String pElementsPath) {
49          return
50                  NL + "Notes: 2" + NL +//
51                          "Binaries: 1" + NL +//
52                          "Archives: 1" + NL +//
53                          "Standards: 6" + NL +//
54                          "" + NL +//
55                          "Apache Licensed: 3" + NL +//
56                          "Generated Documents: 0" + NL +//
57                          "" + NL +//
58                          "JavaDocs are generated, thus a license header is optional." + NL +//
59                          "Generated files do not require license headers." + NL +//
60                          "" + NL +//
61                          "2 Unknown Licenses" + NL +//
62                          "" + NL +//
63                          PARAGRAPH + NL +//
64                          "" + NL +//
65                          "Files with unapproved licenses:" + NL +//
66                          "" + NL +//
67                          "  " + pElementsPath + "/Source.java" + NL +//
68                          "  " + pElementsPath + "/sub/Empty.txt" + NL +//
69                          "" + NL +//
70                          PARAGRAPH + NL +//
71                          "" + NL +//
72                          "Archives:" + NL +//
73                          "" + NL +//
74                          " + " + pElementsPath + "/dummy.jar" + NL +//
75                          " " + NL +//
76                          PARAGRAPH + NL +//
77                          "  Files with Apache License headers will be marked AL" + NL +//
78                          "  Binary files (which do not require any license headers) will be marked B" + NL +//
79                          "  Compressed archives will be marked A" + NL +//
80                          "  Notices, licenses etc. will be marked N" + NL +//
81                          "  MIT   " + pElementsPath + "/ILoggerFactory.java" + NL +//
82                          "  B     " + pElementsPath + "/Image.png" + NL +//
83                          "  N     " + pElementsPath + "/LICENSE" + NL +//
84                          "  N     " + pElementsPath + "/NOTICE" + NL +//
85                          " !????? " + pElementsPath + "/Source.java" + NL +//
86                          "  AL    " + pElementsPath + "/Text.txt" + NL +//
87                          "  AL    " + pElementsPath + "/Xml.xml" + NL +//
88                          "  AL    " + pElementsPath + "/buildr.rb" + NL +//
89                          "  A     " + pElementsPath + "/dummy.jar" + NL +//
90                          " !????? " + pElementsPath + "/sub/Empty.txt" + NL +//
91                          " " + NL +//
92                          PARAGRAPH + NL +//
93                          NL +//
94                          " Printing headers for text files without a valid license header..." + NL +//
95                          " " + NL +//
96                          FILE_PARAGRAPH + NL +//
97                          "== File: " + pElementsPath + "/Source.java" + NL +//
98                          FILE_PARAGRAPH + NL + //
99                          "package elements;" + NL +//
100                         "" + NL +//
101                         "/*" + NL +//
102                         " * This file does intentionally *NOT* contain an AL license header," + NL +//
103                         " * because it is used in the test suite." + NL +//
104                         " */" + NL +//
105                         "public class Source {" + NL +//
106                         "" + NL +//
107                         "}" + NL +//
108                         "" + NL +//
109                         FILE_PARAGRAPH + NL +//
110                         "== File: " + pElementsPath + "/sub/Empty.txt" + NL +//
111                         FILE_PARAGRAPH + NL +//
112                         NL;
113     }
114 
115     @Test
116     public void plainReportWithArchivesAndUnapprovedLicenses() throws Exception {
117         StringWriter out = new StringWriter();
118         HeaderMatcherMultiplexer matcherMultiplexer = new HeaderMatcherMultiplexer(Defaults.DEFAULT_MATCHERS);
119         final String elementsPath = Resources.getResourceDirectory("elements/Source.java");
120         final ReportConfiguration configuration = new ReportConfiguration();
121         configuration.setApproveDefaultLicenses(true);
122         configuration.setHeaderMatcher(matcherMultiplexer);
123         Report.report(out, new DirectoryWalker(new File(elementsPath)),
124                 Defaults.getPlainStyleSheet(), configuration);
125 
126         String result = out.getBuffer().toString();
127         assertTrue("'Generated at' is present in " + result,
128                 result.startsWith(HEADER));
129 
130         final int generatedAtLineEnd = result.indexOf(NL, HEADER.length());
131 
132         final String elementsReports = getElementsReports(elementsPath);
133         assertEquals("Report created was: " + result,
134                 elementsReports,
135                 result.substring(generatedAtLineEnd + NL.length()));
136     }
137 
138     @Test
139     public void parseExclusionsForCLIUsage() throws IOException {
140         final FilenameFilter filter = Report.parseExclusions(Arrays.asList("", " # foo/bar", "foo", "##", " ./foo/bar"));
141         assertNotNull(filter);
142     }
143 }