View Javadoc
1   package org.apache.maven.plugin.failsafe.xmlsummary;
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 org.apache.maven.surefire.suite.RunResult;
23  
24  import javax.xml.bind.JAXBException;
25  import java.io.File;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.io.OutputStreamWriter;
29  import java.nio.charset.Charset;
30  
31  /**
32   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
33   * @since 2.20
34   */
35  public final class FailsafeSummaryXmlUtils
36  {
37      public static final Charset UTF8 = Charset.forName( "UTF-8" );
38  
39      private FailsafeSummaryXmlUtils()
40      {
41          throw new IllegalStateException( "No instantiable constructor." );
42      }
43  
44      public static RunResult toRunResult( File failsafeSummaryXml ) throws JAXBException
45      {
46          FailsafeSummary failsafeSummary = JAXB.unmarshal( failsafeSummaryXml, FailsafeSummary.class );
47  
48          return new RunResult( failsafeSummary.getCompleted(), failsafeSummary.getErrors(),
49                                      failsafeSummary.getFailures(), failsafeSummary.getSkipped(),
50                                      failsafeSummary.getFailureMessage(), failsafeSummary.isTimeout()
51          );
52      }
53  
54      public static void fromRunResultToFile( RunResult fromRunResult, File toFailsafeSummaryXml )
55              throws JAXBException, IOException
56      {
57          fromRunResultToFile( fromRunResult, toFailsafeSummaryXml, UTF8 );
58      }
59  
60      public static void fromRunResultToFile( RunResult fromRunResult, File toFailsafeSummaryXml, Charset encoding )
61              throws JAXBException, IOException
62      {
63          FailsafeSummary summary = new FailsafeSummary();
64          summary.setCompleted( fromRunResult.getCompletedCount() );
65          summary.setFailureMessage( fromRunResult.getFailure() );
66          summary.setErrors( fromRunResult.getErrors() );
67          summary.setFailures( fromRunResult.getFailures() );
68          summary.setSkipped( fromRunResult.getSkipped() );
69          summary.setTimeout( fromRunResult.isTimeout() );
70          summary.setResult( ErrorType.fromValue( fromRunResult.getFailsafeCode() ) );
71  
72          String unmarshalled = JAXB.marshal( summary, encoding );
73  
74          OutputStreamWriter os = new OutputStreamWriter( new FileOutputStream( toFailsafeSummaryXml ), encoding );
75          try
76          {
77              os.write( unmarshalled );
78              os.flush();
79          }
80          finally
81          {
82              os.close();
83          }
84      }
85  
86      public static void writeSummary( RunResult mergedSummary, File mergedSummaryFile, boolean inProgress,
87                                       Charset encoding )
88              throws IOException, JAXBException
89      {
90          if ( !mergedSummaryFile.getParentFile().isDirectory() )
91          {
92              //noinspection ResultOfMethodCallIgnored
93              mergedSummaryFile.getParentFile().mkdirs();
94          }
95  
96          if ( mergedSummaryFile.exists() && inProgress )
97          {
98              RunResult runResult = toRunResult( mergedSummaryFile );
99              mergedSummary = mergedSummary.aggregate( runResult );
100         }
101 
102         fromRunResultToFile( mergedSummary, mergedSummaryFile, encoding );
103     }
104 }