View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.continuum.reports.surefire;
20  
21  import org.apache.maven.continuum.configuration.ConfigurationException;
22  import org.apache.maven.continuum.configuration.ConfigurationService;
23  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
24  import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
25  import org.codehaus.plexus.util.DirectoryScanner;
26  import org.springframework.stereotype.Service;
27  import org.xml.sax.SAXException;
28  
29  import javax.annotation.Resource;
30  import javax.xml.parsers.ParserConfigurationException;
31  import java.io.File;
32  import java.io.IOException;
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.LinkedList;
36  import java.util.List;
37  
38  /**
39   * @author <a href="mailto:olamy@apache.org">olamy</a>
40   * @version $Id: DefaultReportTestSuiteGenerator.java 729481 2008-12-26 11:13:12Z olamy $
41   * @since 12 nov. 07
42   */
43  @Service("reportTestSuiteGenerator")
44  public class DefaultReportTestSuiteGenerator
45      implements ReportTestSuiteGenerator, Initializable
46  {
47  
48      @Resource
49      private ConfigurationService configurationService;
50  
51      private List<String> defaultIncludes;
52  
53      private List<String> defaultexcludes;
54  
55      // -----------------------------
56      //  Plexus Lifecycle
57      // -----------------------------
58  
59      public void initialize()
60          throws InitializationException
61      {
62          defaultIncludes = new ArrayList<String>( 1 );
63          defaultIncludes.add( "*.xml" );
64          defaultexcludes = new ArrayList<String>( 1 );
65          defaultexcludes.add( "*.txt" );
66      }
67  
68      /**
69       * @see org.apache.maven.continuum.reports.surefire.ReportTestSuiteGenerator#generateReports(java.io.File, java.util.List, java.util.List)
70       */
71      public List<ReportTestSuite> generateReports( File directory, List<String> includes, List<String> excludes )
72          throws ReportTestSuiteGeneratorException
73      {
74          if ( directory == null )
75          {
76              return Collections.EMPTY_LIST;
77          }
78          if ( !directory.exists() )
79          {
80              return Collections.EMPTY_LIST;
81          }
82          List<ReportTestSuite> reportTestSuites = new LinkedList<ReportTestSuite>();
83          String[] includesArray;
84          if ( includes == null )
85          {
86              includesArray = new String[0];
87          }
88          else
89          {
90              includesArray = includes.toArray( new String[includes.size()] );
91          }
92          String[] excludesArray;
93          if ( excludes == null )
94          {
95              excludesArray = new String[0];
96          }
97          else
98          {
99              excludesArray = excludes.toArray( new String[excludes.size()] );
100         }
101         String[] xmlReportFiles = getIncludedFiles( directory, includesArray, excludesArray );
102 
103         if ( xmlReportFiles == null )
104         {
105             return Collections.EMPTY_LIST;
106         }
107         if ( xmlReportFiles.length == 0 )
108         {
109             return Collections.EMPTY_LIST;
110         }
111         for ( String currentReport : xmlReportFiles )
112         {
113             ReportTestSuite testSuite = new ReportTestSuite();
114 
115             try
116             {
117                 testSuite.parse( directory + File.separator + currentReport );
118             }
119             catch ( ParserConfigurationException e )
120             {
121                 throw new ReportTestSuiteGeneratorException( "Error setting up parser for Surefire XML report", e );
122             }
123             catch ( SAXException e )
124             {
125                 throw new ReportTestSuiteGeneratorException( "Error parsing Surefire XML report " + currentReport, e );
126             }
127             catch ( IOException e )
128             {
129                 throw new ReportTestSuiteGeneratorException( "Error reading Surefire XML report " + currentReport, e );
130             }
131 
132             reportTestSuites.add( testSuite );
133         }
134         return reportTestSuites;
135     }
136 
137     /**
138      * @see org.apache.maven.continuum.reports.surefire.ReportTestSuiteGenerator#generateReports(java.io.File)
139      */
140     public List<ReportTestSuite> generateReports( File directory )
141         throws ReportTestSuiteGeneratorException
142     {
143         return generateReports( directory, defaultIncludes, defaultexcludes );
144     }
145 
146     /**
147      * @see org.apache.maven.continuum.reports.surefire.ReportTestSuiteGenerator#generateReports(int, int)
148      */
149     public List<ReportTestSuite> generateReports( int buildId, int projectId )
150         throws ReportTestSuiteGeneratorException
151     {
152         try
153         {
154             File directory = configurationService.getTestReportsDirectory( buildId, projectId );
155             return generateReports( directory );
156         }
157         catch ( ConfigurationException e )
158         {
159             throw new ReportTestSuiteGeneratorException( e.getMessage(), e );
160         }
161     }
162 
163     /**
164      * @see org.apache.maven.continuum.reports.surefire.ReportTestSuiteGenerator#generateReportTestResult(int, int)
165      */
166     public ReportTestResult generateReportTestResult( int buildId, int projectId )
167         throws ReportTestSuiteGeneratorException
168     {
169         List<ReportTestSuite> reportTestSuites = generateReports( buildId, projectId );
170         ReportTestResult reportTestResult = new ReportTestResult();
171         for ( ReportTestSuite reportTestSuite : reportTestSuites )
172         {
173             reportTestResult.addReportTestSuite( reportTestSuite );
174         }
175         return reportTestResult;
176     }
177 
178     private String[] getIncludedFiles( File directory, String[] includes, String[] excludes )
179     {
180         DirectoryScanner scanner = new DirectoryScanner();
181 
182         scanner.setBasedir( directory );
183 
184         scanner.setIncludes( includes );
185 
186         scanner.setExcludes( excludes );
187 
188         scanner.scan();
189 
190         return scanner.getIncludedFiles();
191     }
192 
193 }