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.ArrayList;
24  import java.util.List;
25  import java.util.regex.Pattern;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.rat.walker.DirectoryWalker;
30  import org.apache.rat.analysis.DefaultAnalyserFactory;
31  import org.apache.rat.analysis.IHeaderMatcher;
32  import org.apache.rat.analysis.RatHeaderAnalysisException;
33  import org.apache.rat.api.Document;
34  import org.apache.rat.document.IDocumentAnalyser;
35  import org.apache.rat.report.RatReport;
36  import org.apache.rat.report.claim.impl.xml.SimpleXmlClaimReporter;
37  import org.apache.rat.report.claim.util.ClaimReporterMultiplexer;
38  import org.apache.rat.report.xml.writer.IXmlWriter;
39  import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
40  import org.apache.rat.test.utils.Resources;
41  
42  public class XmlReportTest extends TestCase {
43  
44      private static final Pattern IGNORE = Pattern.compile(".svn");
45      StringWriter out;
46      IXmlWriter writer;
47      RatReport report;
48      
49      protected void setUp() throws Exception {
50          super.setUp();
51          out = new StringWriter();
52          writer = new XmlWriter(out);
53          writer.startDocument();
54          final SimpleXmlClaimReporter reporter = new SimpleXmlClaimReporter(writer);
55          final IHeaderMatcher matcher = new IHeaderMatcher() {
56  
57              public boolean match(Document subject, String line) throws RatHeaderAnalysisException {
58                  return false;
59              }
60  
61              public void reset() {
62              }            
63          };
64          IDocumentAnalyser analyser = DefaultAnalyserFactory.createDefaultAnalyser(matcher);
65          final List reporters = new ArrayList();
66          reporters.add(reporter);
67          report = new ClaimReporterMultiplexer(analyser, reporters); 
68      }
69  
70      protected void tearDown() throws Exception {
71          super.tearDown();
72      }
73  
74      private void report(DirectoryWalker directory) throws Exception {
75          directory.run(report);
76      }
77      
78      public void testBaseReport() 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          assertEquals(
87                  "<?xml version='1.0'?>" +
88                  "<rat-report>" +
89                      "<resource name='" + elementsPath + "/Image.png'><type name='binary'/></resource>" +
90                      "<resource name='" + elementsPath + "/LICENSE'><type name='notice'/></resource>" +
91                      "<resource name='" + elementsPath + "/NOTICE'><type name='notice'/></resource>" +
92                      "<resource name='" + elementsPath + "/Source.java'><header-sample>package elements;\n" +
93  "\n" +
94  "/*\n" +
95  " * This file does intentionally *NOT* contain an ASL license header,\n" +
96  " * because it is used in the test suite.\n" +
97  " */\n" +
98  "public class Source {\n" +
99  "\n" +
100 "}\n" + "</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/></resource>" +
101                     "<resource name='" + elementsPath + "/Text.txt'><header-sample>/*\n" +
102 " * Licensed to the Apache Software Foundation (ASF) under one\n" +
103 " * or more contributor license agreements.  See the NOTICE file\n" +
104 " * distributed with this work for additional information\n" +
105 " * regarding copyright ownership.  The ASF licenses this file\n" +
106 " * to you under the Apache License, Version 2.0 (the \"License\");\n" +
107 " * you may not use this file except in compliance with the License.\n" +
108 " * You may obtain a copy of the License at\n" +
109 " *\n" +
110 " *    http://www.apache.org/licenses/LICENSE-2.0\n" +
111 " *\n" +
112 " * Unless required by applicable law or agreed to in writing,\n" +
113 " * software distributed under the License is distributed on an\n" +
114 " * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n" +
115 " * KIND, either express or implied.  See the License for the\n" +
116 " * specific language governing permissions and limitations\n" +
117 " * under the License.    \n" +
118 " */\n" +
119 "\n" +
120 "            \n" +
121 "</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/></resource>" +
122                     "<resource name='" + elementsPath + "/Xml.xml'><header-sample>&lt;?xml version='1.0'?&gt;\n" +
123 "&lt;!--\n" +
124 " Licensed to the Apache Software Foundation (ASF) under one   *\n" +
125 " or more contributor license agreements.  See the NOTICE file *\n" +
126 " distributed with this work for additional information        *\n" +
127 " regarding copyright ownership.  The ASF licenses this file   *\n" +
128 " to you under the Apache License, Version 2.0 (the            *\n" +
129 " \"License\"); you may not use this file except in compliance   *\n" +
130 " with the License.  You may obtain a copy of the License at   *\n" +
131 "                                                              *\n" +
132 "   http://www.apache.org/licenses/LICENSE-2.0                 *\n" +
133 "                                                              *\n" +
134 " Unless required by applicable law or agreed to in writing,   *\n" +
135 " software distributed under the License is distributed on an  *\n" +
136 " \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *\n" +
137 " KIND, either express or implied.  See the License for the    *\n" +
138 " specific language governing permissions and limitations      *\n" +
139 " under the License.                                           *\n" +
140 "--&gt;\n" +
141 "&lt;document/&gt;\n" +
142 "</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/></resource>" +
143                     "<resource name='" + elementsPath + "/dummy.jar'><type name='archive'/></resource>" +
144                     "<resource name='" + elementsPath + "/sub/Empty.txt'><header-sample>\n</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/></resource>" +
145                 "</rat-report>", output);
146         assertTrue("Is well formed", XmlUtils.isWellFormedXml(output));
147     }
148 
149 }