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.analysis.DefaultAnalyserFactory;
22  import org.apache.rat.analysis.IHeaderMatcher;
23  import org.apache.rat.analysis.RatHeaderAnalysisException;
24  import org.apache.rat.api.Document;
25  import org.apache.rat.document.IDocumentAnalyser;
26  import org.apache.rat.report.AbstractReport;
27  import org.apache.rat.report.RatReport;
28  import org.apache.rat.report.claim.impl.xml.SimpleXmlClaimReporter;
29  import org.apache.rat.report.claim.util.ClaimReporterMultiplexer;
30  import org.apache.rat.report.xml.writer.IXmlWriter;
31  import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
32  import org.apache.rat.test.utils.Resources;
33  import org.apache.rat.walker.DirectoryWalker;
34  import org.junit.Before;
35  import org.junit.Test;
36  
37  import java.io.File;
38  import java.io.StringWriter;
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.util.regex.Pattern;
42  
43  import static org.junit.Assert.assertTrue;
44  
45  public class XmlReportTest {
46  
47      private static final Pattern IGNORE = Pattern.compile(".svn");
48      private StringWriter out;
49      private IXmlWriter writer;
50      private RatReport report;
51      
52      @Before
53      public void setUp() throws Exception {
54          out = new StringWriter();
55          writer = new XmlWriter(out);
56          writer.startDocument();
57          final SimpleXmlClaimReporter reporter = new SimpleXmlClaimReporter(writer);
58          final IHeaderMatcher matcher = new IHeaderMatcher() {
59  
60              public boolean match(Document subject, String line) throws RatHeaderAnalysisException {
61                  return false;
62              }
63  
64              public void reset() {
65              }            
66          };
67          IDocumentAnalyser analyser = DefaultAnalyserFactory.createDefaultAnalyser(matcher);
68          final List<AbstractReport> reporters = new ArrayList<AbstractReport>();
69          reporters.add(reporter);
70          report = new ClaimReporterMultiplexer(analyser, reporters); 
71      }
72  
73      private void report(DirectoryWalker directory) throws Exception {
74          directory.run(report);
75      }
76      
77      @Test
78      public void baseReport() throws Exception {
79          final String elementsPath = Resources.getResourceDirectory("elements/Source.java");
80          DirectoryWalker directory = new DirectoryWalker(new File(elementsPath), IGNORE);
81          report.startReport();
82          report(directory);
83          report.endReport();
84          writer.closeDocument();
85          final String output = out.toString();
86          assertTrue("Preamble and document element are OK",
87                     output.startsWith("<?xml version='1.0'?>" +
88                  "<rat-report timestamp="));
89          assertTrue("Part after timestamp attribute is OK",
90                     output.endsWith(">" +
91                      "<resource name='" + elementsPath + "/Image.png'><type name='binary'/></resource>" +
92                      "<resource name='" + elementsPath + "/LICENSE'><type name='notice'/></resource>" +
93                      "<resource name='" + elementsPath + "/NOTICE'><type name='notice'/></resource>" +
94                      "<resource name='" + elementsPath + "/Source.java'><header-sample>package elements;\n" +
95  "\n" +
96  "/*\n" +
97  " * This file does intentionally *NOT* contain an AL license header,\n" +
98  " * because it is used in the test suite.\n" +
99  " */\n" +
100 "public class Source {\n" +
101 "\n" +
102 "}\n" + "</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/></resource>" +
103                     "<resource name='" + elementsPath + "/Text.txt'><header-sample>/*\n" +
104 " * Licensed to the Apache Software Foundation (ASF) under one\n" +
105 " * or more contributor license agreements.  See the NOTICE file\n" +
106 " * distributed with this work for additional information\n" +
107 " * regarding copyright ownership.  The ASF licenses this file\n" +
108 " * to you under the Apache License, Version 2.0 (the \"License\");\n" +
109 " * you may not use this file except in compliance with the License.\n" +
110 " * You may obtain a copy of the License at\n" +
111 " *\n" +
112 " *    http://www.apache.org/licenses/LICENSE-2.0\n" +
113 " *\n" +
114 " * Unless required by applicable law or agreed to in writing,\n" +
115 " * software distributed under the License is distributed on an\n" +
116 " * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n" +
117 " * KIND, either express or implied.  See the License for the\n" +
118 " * specific language governing permissions and limitations\n" +
119 " * under the License.    \n" +
120 " */\n" +
121 "\n" +
122 "            \n" +
123 "</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/></resource>" +
124                     "<resource name='" + elementsPath + "/Xml.xml'><header-sample>&lt;?xml version='1.0'?&gt;\n" +
125 "&lt;!--\n" +
126 " Licensed to the Apache Software Foundation (ASF) under one   *\n" +
127 " or more contributor license agreements.  See the NOTICE file *\n" +
128 " distributed with this work for additional information        *\n" +
129 " regarding copyright ownership.  The ASF licenses this file   *\n" +
130 " to you under the Apache License, Version 2.0 (the            *\n" +
131 " \"License\"); you may not use this file except in compliance   *\n" +
132 " with the License.  You may obtain a copy of the License at   *\n" +
133 "                                                              *\n" +
134 "   http://www.apache.org/licenses/LICENSE-2.0                 *\n" +
135 "                                                              *\n" +
136 " Unless required by applicable law or agreed to in writing,   *\n" +
137 " software distributed under the License is distributed on an  *\n" +
138 " \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *\n" +
139 " KIND, either express or implied.  See the License for the    *\n" +
140 " specific language governing permissions and limitations      *\n" +
141 " under the License.                                           *\n" +
142 "--&gt;\n" +
143 "&lt;document/&gt;\n" +
144 "</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/></resource>" +
145                     "<resource name='" + elementsPath + "/buildr.rb'><header-sample># Licensed to the Apache Software Foundation (ASF) under one or more\n" +
146 "# contributor license agreements.  See the NOTICE file distributed with this\n" +
147 "# work for additional information regarding copyright ownership.  The ASF\n" +
148 "# licenses this file to you under the Apache License, Version 2.0 (the\n" +
149 "# \"License\"); you may not use this file except in compliance with the License.\n" +
150 "# You may obtain a copy of the License at\n" +
151 "#\n" +
152 "#    http://www.apache.org/licenses/LICENSE-2.0\n" +
153 "#\n" +
154 "# Unless required by applicable law or agreed to in writing, software\n" +
155 "# distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n" +
156 "# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the\n" +
157 "# License for the specific language governing permissions and limitations under\n" +
158 "# the License.\n" +
159 "\n" +
160 "unless defined?(Buildr::VERSION)\n" +
161 "  require 'buildr/version'\n" +
162 "end\n" +
163 "\n" +
164 "require 'buildr/core'\n" +
165 "require 'buildr/packaging'\n" +
166 "require 'buildr/java'\n" +
167 "require 'buildr/ide'\n" +
168 "require 'buildr/shell'\n" +
169 "require 'buildr/run'\n" +
170 "\n" +
171 "# Methods defined in Buildr are both instance methods (e.g. when included in Project)\n" +
172 "# and class methods when invoked like Buildr.artifacts().\n" +
173 "module Buildr ; extend self ; end\n" +
174 "\n" +
175 "# The Buildfile object (self) has access to all the Buildr methods and constants.\n" +
176 "class &lt;&lt; self ; include Buildr ; end\n" +
177 "\n" +
178 "# All modules defined under Buildr::* can be referenced without Buildr:: prefix\n" +
179 "# unless a conflict exists (e.g.  Buildr::RSpec vs ::RSpec)\n" +
180 "class Object #:nodoc:\n" +
181 "  Buildr.constants.each do |name|\n" +
182 "    const = Buildr.const_get(name)\n" +
183 "    if const.is_a?(Module)\n" +
184 "      const_set name, const unless const_defined?(name)\n" +
185 "    end\n" +
186 "  end\n" +
187 "end\n" +
188 "\n" +
189                 "</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/></resource>" +
190                     "<resource name='" + elementsPath + "/dummy.jar'><type name='archive'/></resource>" +
191                     "<resource name='" + elementsPath + "/sub/Empty.txt'><header-sample>\n</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/></resource>" +
192                     "</rat-report>"));
193         assertTrue("Is well formed", XmlUtils.isWellFormedXml(output));
194     }
195 
196 }