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  import static org.mockito.ArgumentMatchers.any;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  import java.io.File;
29  import java.io.StringWriter;
30  
31  import org.apache.rat.ConfigurationException;
32  import org.apache.rat.Defaults;
33  import org.apache.rat.ReportConfiguration;
34  import org.apache.rat.api.Document;
35  import org.apache.rat.document.FileDocument;
36  import org.apache.rat.document.DocumentName;
37  import org.apache.rat.license.ILicense;
38  import org.apache.rat.license.ILicenseFamily;
39  import org.apache.rat.report.RatReport;
40  import org.apache.rat.report.claim.ClaimStatistic;
41  import org.apache.rat.report.xml.writer.IXmlWriter;
42  import org.apache.rat.report.xml.writer.XmlWriter;
43  import org.apache.rat.test.utils.Resources;
44  import org.apache.rat.testhelpers.TestingLicense;
45  import org.apache.rat.testhelpers.TestingMatcher;
46  import org.apache.rat.testhelpers.XmlUtils;
47  import org.apache.rat.walker.DirectoryWalker;
48  import org.junit.jupiter.api.BeforeEach;
49  import org.junit.jupiter.api.Test;
50  
51  public class XmlReportFactoryTest {
52  
53      private final ILicenseFamily family = ILicenseFamily.builder().setLicenseFamilyCategory("TEST")
54              .setLicenseFamilyName("Testing family").build();
55  
56      private StringWriter out;
57      private IXmlWriter writer;
58  
59      @BeforeEach
60      public void setUp() throws Exception {
61          out = new StringWriter();
62          writer = new XmlWriter(out);
63          writer.startDocument();
64      }
65  
66      private void report(DirectoryWalker directory, RatReport report) throws Exception {
67          directory.run(report);
68      }
69  
70      @Test
71      public void standardReport() throws Exception {
72          final File elementsDir = Resources.getExampleResource("exampleData");
73          final ReportConfiguration configuration = new ReportConfiguration();
74          final TestingLicense testingLicense = new TestingLicense("TEST", new TestingMatcher(true), family);
75          configuration.setFrom(Defaults.builder().build());
76          DocumentName documentName = DocumentName.builder(elementsDir).build();
77          DirectoryWalker directory = new DirectoryWalker(new FileDocument(documentName, elementsDir,
78                  configuration.getDocumentExcluder(documentName)));
79          final ClaimStatistic statistic = new ClaimStatistic();
80  
81          configuration.addLicense(testingLicense);
82          RatReport report = XmlReportFactory.createStandardReport(writer, statistic, configuration);
83          report.startReport();
84          report(directory, report);
85          report.endReport();
86          writer.closeDocument();
87          final String output = out.toString();
88          assertTrue(output.startsWith("<?xml version='1.0'?>" + "<rat-report timestamp="),
89                  "Preamble and document element are OK");
90  
91          assertTrue(XmlUtils.isWellFormedXml(output), "Is well formed");
92          assertEquals(2, statistic.getCounter(Document.Type.BINARY), "Binary files");
93          assertEquals(2, statistic.getCounter(Document.Type.NOTICE), "Notice files");
94          assertEquals(8, statistic.getCounter(Document.Type.STANDARD), "Standard files");
95          assertEquals(1, statistic.getCounter(Document.Type.ARCHIVE), "Archives");
96          assertEquals(2, statistic.getCounter(Document.Type.IGNORED), "Ignored documents");
97      }
98  
99      @Test
100     public void testNoLicense()  {
101         final ILicense mockLicense = mock(ILicense.class);
102         when(mockLicense.matches(any())).thenReturn(true);
103         when(mockLicense.getLicenseFamily()).thenReturn(family);
104 
105         final ClaimStatistic statistic = new ClaimStatistic();
106         final ReportConfiguration configuration = new ReportConfiguration();
107         // configuration.addLicense(mockLicense);
108         try {
109             XmlReportFactory.createStandardReport(writer, statistic, configuration);
110             fail("Should have thrown exception");
111         } catch (ConfigurationException e) {
112             // expected;
113         }
114     }
115 }