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.maven.plugins.surefire.report;
20  
21  import javax.xml.transform.stream.StreamSource;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.net.URISyntaxException;
26  import java.net.URL;
27  import java.nio.file.Path;
28  import java.nio.file.Paths;
29  
30  import junit.framework.TestCase;
31  import org.apache.maven.shared.utils.io.DirectoryScanner;
32  import org.junit.Assert;
33  import org.xmlunit.validation.Languages;
34  import org.xmlunit.validation.ValidationProblem;
35  import org.xmlunit.validation.ValidationResult;
36  import org.xmlunit.validation.Validator;
37  
38  import static org.assertj.core.api.Assertions.assertThat;
39  
40  /**
41   *
42   */
43  public class SurefireSchemaValidationTest extends TestCase {
44      @SuppressWarnings("checkstyle:methodname")
45      public void testValidate_XMLs_against_schema() throws Exception {
46          File basedir = getProjectBasedir();
47  
48          File xsd = getSchemaFile(basedir);
49          Assert.assertTrue("XSD schema validation not found", xsd.exists());
50  
51          // looks for all xml surefire report in test resources
52          DirectoryScanner ds = new DirectoryScanner();
53          ds.setBasedir(basedir);
54          ds.setIncludes("**/TEST-*.xml");
55          ds.scan();
56  
57          String[] xmlFiles = ds.getIncludedFiles();
58          assertThat(xmlFiles)
59                  .describedAs("No XML surefire reports found to validate")
60                  .isNotEmpty();
61  
62          Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
63          v.setSchemaSource(new StreamSource(xsd));
64  
65          for (String xmlFile : xmlFiles) {
66              ValidationResult vr = v.validateInstance(new StreamSource(new File(basedir, xmlFile)));
67              StringBuilder msg = new StringBuilder();
68              if (!vr.isValid()) {
69                  msg.append(xmlFile).append(" has violations:");
70                  for (ValidationProblem problem : vr.getProblems()) {
71                      msg.append("\n") //
72                              .append(" - ")
73                              .append(problem.getType()) //
74                              .append(" at row:")
75                              .append(problem.getLine()) //
76                              .append(" col:")
77                              .append(problem.getColumn()) //
78                              .append(' ')
79                              .append(problem.getMessage());
80                  }
81              }
82              Assert.assertTrue(Utils.toSystemNewLine(msg.toString()), vr.isValid());
83          }
84      }
85  
86      private File getProjectBasedir() throws URISyntaxException {
87          // get the root path of test-classes
88          URL basedirURL = SurefireSchemaValidationTest.class.getClassLoader().getResource(".");
89          return new File(basedirURL.toURI());
90      }
91  
92      private File getSchemaFile(File basedir) throws IOException {
93          // get the schema file placed in a different module
94          Path xsd = Paths.get(
95                  basedir.getAbsolutePath(),
96                  "..",
97                  "..",
98                  "..",
99                  "maven-surefire-plugin",
100                 "src",
101                 "site",
102                 "resources",
103                 "xsd",
104                 "surefire-test-report.xsd");
105         return xsd.toFile().getCanonicalFile();
106     }
107 }