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