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.report.xml;
20  
21  import org.apache.rat.ReportConfiguration;
22  import org.apache.rat.analysis.MockLicenseMatcher;
23  import org.apache.rat.api.MetaData;
24  import org.apache.rat.report.RatReport;
25  import org.apache.rat.report.claim.ClaimStatistic;
26  import org.apache.rat.report.xml.writer.IXmlWriter;
27  import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
28  import org.apache.rat.test.utils.Resources;
29  import org.apache.rat.walker.DirectoryWalker;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  import java.io.File;
34  import java.io.StringWriter;
35  import java.util.regex.Pattern;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.assertTrue;
39  
40  public class XmlReportFactoryTest {
41  
42      private static final Pattern IGNORE_EMPTY = Pattern.compile(".svn|Empty.txt");
43      
44      private StringWriter out;
45      private IXmlWriter writer;
46      
47      @Before
48      public void setUp() throws Exception {
49          out = new StringWriter();
50          writer = new XmlWriter(out);
51          writer.startDocument();
52      }
53  
54      private void report(DirectoryWalker directory, RatReport report) throws Exception {
55          directory.run(report);
56      }
57      
58      @Test
59      public void standardReport() throws Exception {
60          final String elementsPath = Resources.getResourceDirectory("elements/Source.java");
61          final MockLicenseMatcher mockLicenseMatcher = new MockLicenseMatcher();
62          DirectoryWalker directory = new DirectoryWalker(new File(elementsPath), IGNORE_EMPTY);
63          final ClaimStatistic statistic = new ClaimStatistic();
64          final ReportConfiguration configuration = new ReportConfiguration();
65          configuration.setHeaderMatcher(mockLicenseMatcher);
66          RatReport report = XmlReportFactory.createStandardReport(writer, statistic, configuration);
67          report.startReport();
68          report(directory, report);
69          report.endReport();
70          writer.closeDocument();
71          final String output = out.toString();
72          assertTrue("Preamble and document element are OK",
73                     output.startsWith("<?xml version='1.0'?>" +
74                  "<rat-report timestamp="));
75          assertTrue("Part after timestamp attribute is OK",
76                     output.endsWith(">" +
77                  "<resource name='" + elementsPath + "/ILoggerFactory.java'><type name='standard'/></resource>" +
78                  "<resource name='" + elementsPath + "/Image.png'><type name='binary'/></resource>" +
79                  "<resource name='" + elementsPath + "/LICENSE'><type name='notice'/></resource>" +
80                  "<resource name='" + elementsPath + "/NOTICE'><type name='notice'/></resource>" +
81                  "<resource name='" + elementsPath + "/Source.java'><type name='standard'/>" +
82                  "</resource>" +
83                  "<resource name='" + elementsPath + "/Text.txt'><type name='standard'/>" +
84                  "</resource>" +
85                  "<resource name='" + elementsPath + "/Xml.xml'><type name='standard'/>" +
86                  "</resource>" +
87                  "<resource name='" + elementsPath + "/buildr.rb'><type name='standard'/>" +
88                  "</resource>" +
89                  "<resource name='" + elementsPath + "/dummy.jar'><type name='archive'/></resource>" +
90                                     "</rat-report>"));
91          assertTrue("Is well formed", XmlUtils.isWellFormedXml(output));
92          assertEquals("Binary files", Integer.valueOf(1), statistic.getDocumentCategoryMap().get(MetaData.RAT_DOCUMENT_CATEGORY_VALUE_BINARY));
93          assertEquals("Notice files", Integer.valueOf(2), statistic.getDocumentCategoryMap().get(MetaData.RAT_DOCUMENT_CATEGORY_VALUE_NOTICE));
94          assertEquals("Standard files", Integer.valueOf(5), statistic.getDocumentCategoryMap().get(MetaData.RAT_DOCUMENT_CATEGORY_VALUE_STANDARD));
95          assertEquals("Archives", Integer.valueOf(1), statistic.getDocumentCategoryMap().get(MetaData.RAT_DOCUMENT_CATEGORY_VALUE_ARCHIVE));
96      }
97  }