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 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          // looks for all xml surefire report in test resources
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          // get the root path of test-classes
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          // get the schema file placed in a different module
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 }