1 package org.apache.maven.plugins.surefire.report;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URISyntaxException;
25 import java.net.URL;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28
29 import javax.xml.transform.stream.StreamSource;
30
31 import org.apache.maven.shared.utils.io.DirectoryScanner;
32 import org.fest.assertions.Assertions;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.xmlunit.validation.Languages;
36 import org.xmlunit.validation.ValidationProblem;
37 import org.xmlunit.validation.ValidationResult;
38 import org.xmlunit.validation.Validator;
39
40 public class SurefireSchemaValidationTest
41 {
42
43 @Test
44 public void validate_XMLs_against_schema()
45 throws Exception
46 {
47 File basedir = getProjectBasedir();
48
49 File xsd = getSchemaFile( basedir );
50 Assert.assertTrue( "XSD schema validation not found", xsd.exists() );
51
52
53 DirectoryScanner ds = new DirectoryScanner();
54 ds.setBasedir( basedir );
55 ds.setIncludes( "**/TEST-*.xml" );
56 ds.scan();
57
58 String[] xmlFiles = ds.getIncludedFiles();
59 Assertions.assertThat( xmlFiles ).describedAs( "No XML surefire reports found to validate" ).isNotEmpty();
60
61 Validator v = Validator.forLanguage( Languages.W3C_XML_SCHEMA_NS_URI );
62 v.setSchemaSource( new StreamSource( xsd ) );
63
64 for ( String xmlFile : xmlFiles )
65 {
66 ValidationResult vr = v.validateInstance( new StreamSource( new File( basedir, xmlFile ) ) );
67 StringBuilder msg = new StringBuilder();
68 if ( !vr.isValid() )
69 {
70 msg.append( xmlFile ).append( " has violations:" );
71 for ( ValidationProblem problem : vr.getProblems() )
72 {
73 msg.append( "\n" )
74 .append( " - " ).append( problem.getType() )
75 .append( " at row:" ).append( problem.getLine() )
76 .append( " col:" ).append( problem.getColumn() )
77 .append( ' ' ).append( problem.getMessage() );
78 }
79 }
80 Assert.assertTrue( Utils.toSystemNewLine( msg.toString() ), vr.isValid() );
81 }
82 }
83
84 private File getProjectBasedir()
85 throws URISyntaxException
86 {
87
88 URL basedirURL = SurefireSchemaValidationTest.class.getClassLoader().getResource( "." );
89 return new File( basedirURL.toURI() );
90 }
91
92 private File getSchemaFile( File basedir )
93 throws IOException
94 {
95
96 Path xsd = Paths.get( basedir.getAbsolutePath(), "..", "..", "..", "maven-surefire-plugin", "src", "site",
97 "resources", "xsd", "surefire-test-report-3.0.xsd" );
98 return xsd.toFile().getCanonicalFile();
99 }
100 }