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.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.BufferedWriter;
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.io.File;
27  import java.io.OutputStreamWriter;
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.List;
31  import java.util.regex.Pattern;
32  
33  import javax.xml.xpath.XPath;
34  import javax.xml.xpath.XPathFactory;
35  
36  import org.apache.commons.io.filefilter.HiddenFileFilter;
37  import org.apache.rat.analysis.DefaultAnalyserFactory;
38  import org.apache.rat.analysis.IHeaderMatcher;
39  import org.apache.rat.analysis.matchers.CopyrightMatcher;
40  import org.apache.rat.analysis.matchers.OrMatcher;
41  import org.apache.rat.analysis.matchers.SimpleTextMatcher;
42  import org.apache.rat.document.IDocumentAnalyser;
43  import org.apache.rat.license.ILicense;
44  import org.apache.rat.report.AbstractReport;
45  import org.apache.rat.report.RatReport;
46  import org.apache.rat.report.claim.impl.xml.SimpleXmlClaimReporter;
47  import org.apache.rat.report.claim.util.ClaimReporterMultiplexer;
48  import org.apache.rat.report.xml.writer.IXmlWriter;
49  import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
50  import org.apache.rat.test.utils.Resources;
51  import org.apache.rat.testhelpers.TestingLicense;
52  import org.apache.rat.testhelpers.XmlUtils;
53  import org.apache.rat.utils.DefaultLog;
54  import org.apache.rat.walker.DirectoryWalker;
55  import org.junit.jupiter.api.BeforeEach;
56  import org.junit.jupiter.api.Test;
57  import org.w3c.dom.Document;
58  
59  public class XmlReportTest {
60  
61      private static final Pattern IGNORE = Pattern.compile(".svn");
62      private ByteArrayOutputStream out;
63      private IXmlWriter writer;
64      private RatReport report;
65  
66      @BeforeEach
67      public void setUp() throws Exception {
68          out = new ByteArrayOutputStream();
69          writer = new XmlWriter(new BufferedWriter(new OutputStreamWriter(out)));
70          writer.startDocument();
71          final SimpleXmlClaimReporter reporter = new SimpleXmlClaimReporter(writer);
72          final IHeaderMatcher asf1Matcher = new SimpleTextMatcher("http://www.apache.org/licenses/LICENSE-2.0");
73          final IHeaderMatcher asf2Matcher = new SimpleTextMatcher("https://www.apache.org/licenses/LICENSE-2.0.txt");
74          final IHeaderMatcher asfMatcher = new OrMatcher(Arrays.asList(asf1Matcher, asf2Matcher));
75          final ILicense asfLic = new TestingLicense("ASF", asfMatcher);
76  
77          final IHeaderMatcher qosMatcher = new CopyrightMatcher("2004", "2011", "QOS.ch");
78          final ILicense qosLic = new TestingLicense("QOS", qosMatcher);
79  
80          IDocumentAnalyser analyser = DefaultAnalyserFactory.createDefaultAnalyser(DefaultLog.INSTANCE,Arrays.asList(asfLic, qosLic));
81          final List<AbstractReport> reporters = new ArrayList<>();
82          reporters.add(reporter);
83          report = new ClaimReporterMultiplexer(analyser, reporters);
84      }
85  
86      private void report(DirectoryWalker directory) throws Exception {
87          directory.run(report);
88      }
89  
90      @Test
91      public void baseReport() throws Exception {
92          final String elementsPath = Resources.getResourceDirectory("elements/Source.java");
93          DirectoryWalker directory = new DirectoryWalker(new File(elementsPath), IGNORE, HiddenFileFilter.HIDDEN);
94          report.startReport();
95          report(directory);
96          report.endReport();
97          writer.closeDocument();
98          final String output = out.toString();
99          assertTrue(output.startsWith("<?xml version='1.0'?>" + "<rat-report timestamp="), "Preamble and document element are OK");
100         assertTrue(XmlUtils.isWellFormedXml(output),"Is well formed");
101 
102         XPath xPath = XPathFactory.newInstance().newXPath();
103         Document doc = XmlUtils.toDom(new ByteArrayInputStream(out.toByteArray()));
104 
105         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/ILoggerFactory.java", "QOS", null, "standard");
106         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/Image.png", null, null, "binary");
107         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/LICENSE", null, null, "notice");
108         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/NOTICE", null, null, "notice");
109         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/Source.java", "?????", null, "standard");
110         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/Text.txt", "ASF", null, "standard");
111         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/TextHttps.txt", "ASF", null, "standard");
112         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/Xml.xml", "ASF", null, "standard");
113         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/buildr.rb", "ASF", null, "standard");
114         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/dummy.jar", null, null, "archive");
115         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/plain.json", null, null, "binary");
116         XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/sub/Empty.txt", "?????", null, "standard");
117     }
118 
119 }