1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
88 URL basedirURL = SurefireSchemaValidationTest.class.getClassLoader().getResource(".");
89 return new File(basedirURL.toURI());
90 }
91
92 private File getSchemaFile(File basedir) throws IOException {
93
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 }