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 java.io.File;
22  import java.io.StringWriter;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.rat.walker.DirectoryWalker;
27  import org.apache.rat.analysis.util.HeaderMatcherMultiplexer;
28  import org.apache.rat.test.utils.Resources;
29  
30  public class ReportTest extends TestCase {
31  	private static String getElementsReports(String pElementsPath) {
32  		return
33              "\n" + 
34              "*****************************************************\n" + 
35              "Summary\n" + 
36              "-------\n" + 
37              "Notes: 2\n" + 
38              "Binaries: 1\n" + 
39              "Archives: 1\n" + 
40              "Standards: 4\n" + 
41              "\n" + 
42              "Apache Licensed: 2\n" + 
43              "Generated Documents: 0\n" + 
44              "\n" + 
45              "JavaDocs are generated and so license header is optional\n" + 
46              "Generated files do not required license headers\n" + 
47              "\n" + 
48              "2 Unknown Licenses\n" + 
49              "\n" + 
50              "*******************************\n" + 
51              "\n" + 
52              "Unapproved licenses:\n" + 
53              "\n" +
54              "  " + pElementsPath + "/Source.java\n" +
55              "  " + pElementsPath + "/sub/Empty.txt\n" +
56              "\n" +
57              "*******************************\n" + 
58              "\n" + 
59              "Archives:\n" + 
60              "\n" + 
61              " + " + pElementsPath + "/dummy.jar\n" + 
62              " \n" + 
63              "*****************************************************\n" + 
64              "  Files with Apache License headers will be marked AL\n" + 
65              "  Binary files (which do not require AL headers) will be marked B\n" + 
66              "  Compressed archives will be marked A\n" + 
67              "  Notices, licenses etc will be marked N\n" + 
68              "  B     " + pElementsPath + "/Image.png\n" + 
69              "  N     " + pElementsPath + "/LICENSE\n" + 
70              "  N     " + pElementsPath + "/NOTICE\n" + 
71              " !????? " + pElementsPath + "/Source.java\n" + 
72              "  AL    " + pElementsPath + "/Text.txt\n" + 
73              "  AL    " + pElementsPath + "/Xml.xml\n" + 
74              "  A     " + pElementsPath + "/dummy.jar\n" + 
75              " !????? " + pElementsPath + "/sub/Empty.txt\n" + 
76              " \n" + 
77              " *****************************************************\n" + 
78              " Printing headers for files without AL header...\n" + 
79              " \n" + 
80              " \n" + 
81              " =======================================================================\n" + 
82              " ==" + pElementsPath + "/Source.java\n" + 
83              " =======================================================================\n" + 
84              "package elements;\n" + 
85              "\n" +
86              "/*\n" +
87              " * This file does intentionally *NOT* contain an ASL license header,\n" +
88              " * because it is used in the test suite.\n" +
89              " */\n" +
90              "public class Source {\n" + 
91              "\n" + 
92              "}\n" + 
93              "\n" + 
94              " =======================================================================\n" + 
95              " ==" + pElementsPath + "/sub/Empty.txt\n" + 
96              " =======================================================================\n" + 
97              "\n";
98  	}
99      
100     protected void setUp() throws Exception {
101         super.setUp();
102     }
103 
104     protected void tearDown() throws Exception {
105         super.tearDown();
106     }
107 
108     public void testPlainReport() 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 elementsReports = getElementsReports(elementsPath);
118         assertEquals("Report created",
119                      elementsReports.replaceAll("\n",
120                                                  System.getProperty("line.separator")),
121                      result);
122     }
123 }