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