View Javadoc
1   package org.apache.maven.plugins.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // looks for all xml surefire report in test resources
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          // 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 )
93          throws IOException
94      {
95          // get the schema file placed in a different module
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 }