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