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 static org.assertj.core.api.Assertions.fail;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import static org.mockito.ArgumentMatchers.any;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.File;
30  import java.io.StringWriter;
31  import java.util.regex.Pattern;
32  
33  import org.apache.commons.io.filefilter.HiddenFileFilter;
34  import org.apache.rat.ConfigurationException;
35  import org.apache.rat.ReportConfiguration;
36  import org.apache.rat.analysis.IHeaderMatcher.State;
37  import org.apache.rat.api.MetaData;
38  import org.apache.rat.license.ILicense;
39  import org.apache.rat.license.ILicenseFamily;
40  import org.apache.rat.report.RatReport;
41  import org.apache.rat.report.claim.ClaimStatistic;
42  import org.apache.rat.report.xml.writer.IXmlWriter;
43  import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
44  import org.apache.rat.test.utils.Resources;
45  import org.apache.rat.testhelpers.TestingLicense;
46  import org.apache.rat.testhelpers.TestingMatcher;
47  import org.apache.rat.testhelpers.XmlUtils;
48  import org.apache.rat.utils.DefaultLog;
49  import org.apache.rat.walker.DirectoryWalker;
50  import org.junit.jupiter.api.BeforeEach;
51  import org.junit.jupiter.api.Test;
52  
53  public class XmlReportFactoryTest {
54  
55      private static final Pattern IGNORE_EMPTY = Pattern.compile(".svn|Empty.txt");
56      private ILicenseFamily family = ILicenseFamily.builder().setLicenseFamilyCategory("TEST")
57              .setLicenseFamilyName("Testing family").build();
58  
59      private StringWriter out;
60      private IXmlWriter writer;
61  
62      @BeforeEach
63      public void setUp() throws Exception {
64          out = new StringWriter();
65          writer = new XmlWriter(out);
66          writer.startDocument();
67      }
68  
69      private void report(DirectoryWalker directory, RatReport report) throws Exception {
70          directory.run(report);
71      }
72  
73      @Test
74      public void standardReport() throws Exception {
75          final String elementsPath = Resources.getResourceDirectory("elements/Source.java");
76  
77          
78          final TestingLicense testingLicense = new TestingLicense(new TestingMatcher(true), family);
79  
80          DirectoryWalker directory = new DirectoryWalker(new File(elementsPath), IGNORE_EMPTY, HiddenFileFilter.HIDDEN);
81          final ClaimStatistic statistic = new ClaimStatistic();
82          final ReportConfiguration configuration = new ReportConfiguration(DefaultLog.INSTANCE);
83          configuration.addLicense(testingLicense);
84          RatReport report = XmlReportFactory.createStandardReport(writer, statistic, configuration);
85          report.startReport();
86          report(directory, report);
87          report.endReport();
88          writer.closeDocument();
89          final String output = out.toString();
90          assertTrue(
91                  output.startsWith("<?xml version='1.0'?>" + "<rat-report timestamp="), "Preamble and document element are OK");
92  
93          assertTrue( XmlUtils.isWellFormedXml(output), "Is well formed");
94          assertEquals(Integer.valueOf(2),
95                  statistic.getDocumentCategoryMap().get(MetaData.RAT_DOCUMENT_CATEGORY_VALUE_BINARY), "Binary files");
96          assertEquals(Integer.valueOf(2),
97                  statistic.getDocumentCategoryMap().get(MetaData.RAT_DOCUMENT_CATEGORY_VALUE_NOTICE), "Notice files");
98          assertEquals(Integer.valueOf(6),
99                  statistic.getDocumentCategoryMap().get(MetaData.RAT_DOCUMENT_CATEGORY_VALUE_STANDARD), "Standard files");
100         assertEquals(Integer.valueOf(1),
101                 statistic.getDocumentCategoryMap().get(MetaData.RAT_DOCUMENT_CATEGORY_VALUE_ARCHIVE), "Archives");
102     }
103 
104     @Test
105     public void testNoLicense() throws Exception {
106 
107         final ILicense mockLicense = mock(ILicense.class);
108         when(mockLicense.matches(any())).thenReturn(State.t);
109         when(mockLicense.getLicenseFamily()).thenReturn(family);
110 
111         final ClaimStatistic statistic = new ClaimStatistic();
112         final ReportConfiguration configuration = new ReportConfiguration(DefaultLog.INSTANCE);
113         // configuration.addLicense(mockLicense);
114         try {
115             XmlReportFactory.createStandardReport(writer, statistic, configuration);
116             fail("Should have thrown exception");
117         } catch (ConfigurationException e) {
118             // expected;
119         }
120 
121     }
122 }