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